1 00:00:04,689 --> 00:00:07,240 Alright, so in this video, I want to talk 2 00:00:07,240 --> 00:00:10,059 a bit about tests. Now we've tested the 3 00:00:10,059 --> 00:00:12,129 app and the various scenarios, in 4 00:00:12,129 --> 00:00:13,630 previous videos in this section of the 5 00:00:13,630 --> 00:00:16,270 course, but there are a few more tests we 6 00:00:16,270 --> 00:00:19,090 should run. It's a good idea to create a 7 00:00:19,090 --> 00:00:21,609 test script, to make sure that you cover 8 00:00:21,609 --> 00:00:23,800 all the possible situations. Now the 9 00:00:23,800 --> 00:00:25,269 script itself doesn't have to be very 10 00:00:25,269 --> 00:00:26,170 complicated, 11 00:00:26,170 --> 00:00:28,060 but it should include the starting 12 00:00:28,060 --> 00:00:30,220 condition as well as the expected 13 00:00:30,220 --> 00:00:32,949 outcome. Now for testing that we've got 14 00:00:32,949 --> 00:00:34,480 the permissions worked correctly, our 15 00:00:34,480 --> 00:00:36,309 script might look something like the 16 00:00:36,309 --> 00:00:38,649 following; so as you can see, we've got 17 00:00:38,649 --> 00:00:40,809 various tests here to start. I'm going to 18 00:00:40,809 --> 00:00:41,920 go through just the first one - we've got 19 00:00:41,920 --> 00:00:44,469 Starting Condition, App not installed, and 20 00:00:44,469 --> 00:00:45,820 we've got various actions there. You can 21 00:00:45,820 --> 00:00:47,350 see we can run the app, and then to the 22 00:00:47,350 --> 00:00:48,850 right-hand side we've got the expected 23 00:00:48,850 --> 00:00:52,149 outcome. For that first test, the expected 24 00:00:52,149 --> 00:00:53,620 outcome is the Permissions dialogue 25 00:00:53,620 --> 00:00:55,059 should appear when the app's not 26 00:00:55,059 --> 00:00:56,890 installed, and so on, going through the 27 00:00:56,890 --> 00:00:59,079 list there. So we've got, basically, quite a 28 00:00:59,079 --> 00:01:01,629 few tests there. And this script, at the 29 00:01:01,629 --> 00:01:04,330 moment, doesn't test Allowing access when 30 00:01:04,330 --> 00:01:06,340 the app is first run, so that should 31 00:01:06,340 --> 00:01:08,920 probably also be included. In addition, it 32 00:01:08,920 --> 00:01:11,229 doesn't include testing on an Android 33 00:01:11,229 --> 00:01:14,500 version prior to API 23. So I'm going to 34 00:01:14,500 --> 00:01:16,179 give you a challenge here, and the 35 00:01:16,179 --> 00:01:18,520 challenge is to add these two tests to 36 00:01:18,520 --> 00:01:20,590 the script, then run them to make sure 37 00:01:20,590 --> 00:01:21,610 that everything works. 38 00:01:21,610 --> 00:01:23,590 Now when I say script, I'm just talking 39 00:01:23,590 --> 00:01:26,979 about a set of scripts that you would 40 00:01:26,979 --> 00:01:28,539 write down on a piece of paper, so 41 00:01:28,539 --> 00:01:30,640 basically, just jot down what you think 42 00:01:30,640 --> 00:01:33,160 the extra two tests should be here. And 43 00:01:33,160 --> 00:01:34,899 in addition to that, there's also another 44 00:01:34,899 --> 00:01:37,360 test that we haven't included, so have a 45 00:01:37,360 --> 00:01:39,190 think about that as well, what that is, 46 00:01:39,190 --> 00:01:41,140 and add that to the script - in other 47 00:01:41,140 --> 00:01:42,910 words, jot that down as well. So have a 48 00:01:42,910 --> 00:01:44,860 think about those tests, pause the 49 00:01:44,860 --> 00:01:46,989 video now, and I'll see you when you get 50 00:01:46,989 --> 00:01:51,190 back. Pause the video now. Okay, so welcome 51 00:01:51,190 --> 00:01:54,310 back. So let's have a talk now, about what 52 00:01:54,310 --> 00:01:56,080 we missed out in that previous set 53 00:01:56,080 --> 00:01:59,890 of scripts. So firstly, the items you can 54 00:01:59,890 --> 00:02:01,750 see in red - they're the additional tests. 55 00:02:01,750 --> 00:02:03,340 So you want to make sure that the app 56 00:02:03,340 --> 00:02:06,039 still behaves correctly, after the user 57 00:02:06,039 --> 00:02:08,440 revokes permissions that are previously 58 00:02:08,440 --> 00:02:10,899 being granted. And obviously there, the 59 00:02:10,899 --> 00:02:12,640 other tests there that we talked about, 60 00:02:12,640 --> 00:02:14,260 was where the app is not installed at the 61 00:02:14,260 --> 00:02:16,450 top. We run the app; Permissions dialogue 62 00:02:16,450 --> 00:02:18,030 should appear, and so on. And then also, 63 00:02:18,030 --> 00:02:20,430 the final script or the final test 64 00:02:20,430 --> 00:02:22,100 there, was if the app is not installed, 65 00:02:22,100 --> 00:02:25,680 running a version of Android prior to 66 00:02:25,680 --> 00:02:28,140 API 23, and the various steps there to 67 00:02:28,140 --> 00:02:30,390 make sure that's tested as well. Al 68 00:02:30,390 --> 00:02:32,310 right, so that's good - we've done that. 69 00:02:32,310 --> 00:02:34,050 So let's actually swing back to Android 70 00:02:34,050 --> 00:02:36,600 Studio, and have a final look at the code 71 00:02:36,600 --> 00:02:38,580 before we leave permissions and get back 72 00:02:38,580 --> 00:02:41,940 to data bases. So the first thing that 73 00:02:41,940 --> 00:02:44,010 I'd now change, now that we've 74 00:02:44,010 --> 00:02:46,410 removed the READ_CONTACTS_GRANTED field, 75 00:02:46,410 --> 00:02:48,720 is in the onCreate method. So let's just 76 00:02:48,720 --> 00:02:52,380 go up to that - you can see that here. Now 77 00:02:52,380 --> 00:02:54,780 the condition there has an if and an 78 00:02:54,780 --> 00:02:57,150 else part - I'm talking about this code 79 00:02:57,150 --> 00:03:00,090 here, on screen. But the only thing we're 80 00:03:00,090 --> 00:03:01,769 doing if the condition's true now, is 81 00:03:01,769 --> 00:03:04,230 logging. Now obviously, previously, we're using 82 00:03:04,230 --> 00:03:06,750 that code to assign the value of true to 83 00:03:06,750 --> 00:03:08,430 our READ_GRANTED_FIELD, but we've 84 00:03:08,430 --> 00:03:10,920 commented that out. Now it is useful to 85 00:03:10,920 --> 00:03:12,959 lock the fact that we're requesting 86 00:03:12,959 --> 00:03:15,209 permission, but it's not really necessary 87 00:03:15,209 --> 00:03:16,650 to log that the permission was already 88 00:03:16,650 --> 00:03:18,900 granted. We'd know if the requesting 89 00:03:18,900 --> 00:03:21,060 permission entry doesn't appear in the 90 00:03:21,060 --> 00:03:23,400 logcat. So consequently, we can simplify 91 00:03:23,400 --> 00:03:25,590 this bit of code. So what I'm going to do 92 00:03:25,590 --> 00:03:28,170 is comment out the existing code, and put 93 00:03:28,170 --> 00:03:30,630 a simplified version above that. And 94 00:03:30,630 --> 00:03:32,040 probably, the quickest way to do that is 95 00:03:32,040 --> 00:03:37,230 just to take a copy of that code. I'm 96 00:03:37,230 --> 00:03:39,690 going to comment that out, and I'm gonna come 97 00:03:39,690 --> 00:03:41,400 down here and paste it in again. And we're 98 00:03:41,400 --> 00:03:42,780 just going to change this round a little 99 00:03:42,780 --> 00:03:45,000 bit. So at the moment, firstly, we've got a 100 00:03:45,000 --> 00:03:47,310 test there for; if hasReadContact 101 00:03:47,310 --> 00:03:48,570 Permission is equal to PERMISSION 102 00:03:48,570 --> 00:03:48,989 _GRANTED, 103 00:03:48,989 --> 00:03:50,850 but we've already decided that we don't 104 00:03:50,850 --> 00:03:52,650 really want to execute this code. So I'm 105 00:03:52,650 --> 00:03:54,329 going to delete those two lines of code, 106 00:03:54,329 --> 00:03:57,600 as well as the else, and then I'm going 107 00:03:57,600 --> 00:03:59,670 to pop up over here, to where it's got 108 00:03:59,670 --> 00:04:03,480 equal to, and set that to not equal to. So 109 00:04:03,480 --> 00:04:05,100 essentially now, we're only going to be 110 00:04:05,100 --> 00:04:06,660 logging the fact that we're requesting 111 00:04:06,660 --> 00:04:08,489 permission, and then actually going 112 00:04:08,489 --> 00:04:10,739 through the process of using the Compat 113 00:04:10,739 --> 00:04:12,540 version of requestPermissions to 114 00:04:12,540 --> 00:04:14,940 actually request the permission. And that's 115 00:04:14,940 --> 00:04:16,350 obviously simplified that and made that a 116 00:04:16,350 --> 00:04:19,350 lot easier to maintain now. So that's 117 00:04:19,350 --> 00:04:20,010 step number one. 118 00:04:20,010 --> 00:04:22,350 The next thing, though, is in the on 119 00:04:22,350 --> 00:04:24,870 RequestPermissionsResult method. Now if 120 00:04:24,870 --> 00:04:25,830 we come down there and have a look at 121 00:04:25,830 --> 00:04:28,680 that, we made those changes in a previous 122 00:04:28,680 --> 00:04:30,720 video. Now that we've done that, really, 123 00:04:30,720 --> 00:04:31,950 the only thing going on here 124 00:04:31,950 --> 00:04:34,140 in this function, is that we're logging - 125 00:04:34,140 --> 00:04:35,610 we're not doing anything else, other than 126 00:04:35,610 --> 00:04:37,680 logging. Now this particular callback 127 00:04:37,680 --> 00:04:40,080 function can be very useful, and we've 128 00:04:40,080 --> 00:04:42,390 used it earlier to disable the Floating 129 00:04:42,390 --> 00:04:43,950 Action Button, if we didn't have access 130 00:04:43,950 --> 00:04:47,010 to the contacts. So you still want to use 131 00:04:47,010 --> 00:04:49,500 it sometimes, but here specifically, we 132 00:04:49,500 --> 00:04:51,630 don't need to use it. As you create more 133 00:04:51,630 --> 00:04:53,820 complex apps that might have secondary 134 00:04:53,820 --> 00:04:56,460 functionality, you may well find uses for 135 00:04:56,460 --> 00:04:57,870 the onRequestPermissionsResult 136 00:04:57,870 --> 00:05:00,120 function. And now you do know how to use 137 00:05:00,120 --> 00:05:02,250 it, if you do actually need that. One 138 00:05:02,250 --> 00:05:03,840 thing you may want to do in here, is 139 00:05:03,840 --> 00:05:05,340 perhaps, provide a more descriptive 140 00:05:05,340 --> 00:05:08,250 dialogue explaining why the app needed a 141 00:05:08,250 --> 00:05:09,690 permission that's been denied, and 142 00:05:09,690 --> 00:05:11,850 explaining what the app won't be able to 143 00:05:11,850 --> 00:05:14,070 do, without access - in other words, without 144 00:05:14,070 --> 00:05:16,620 that permission being enabled. Often 145 00:05:16,620 --> 00:05:18,420 though, we were not really interested in 146 00:05:18,420 --> 00:05:20,100 knowing if permission was granted or not. 147 00:05:20,100 --> 00:05:22,230 You write the code to cope with both 148 00:05:22,230 --> 00:05:24,450 conditions, so there's really no point 149 00:05:24,450 --> 00:05:26,850 creating the onRequestPermissions 150 00:05:26,850 --> 00:05:29,220 Result function, just for the sake of it. 151 00:05:29,220 --> 00:05:30,960 So for that reason, I'm going to comment 152 00:05:30,960 --> 00:05:39,450 out the entire function, like so. Al 153 00:05:39,450 --> 00:05:40,380 right, so that's the end of the 154 00:05:40,380 --> 00:05:42,050 discussion on the Permissions model 155 00:05:42,050 --> 00:05:44,370 introduced in Android marshmallow, and 156 00:05:44,370 --> 00:05:46,320 we've covered quite a bit here. Now 157 00:05:46,320 --> 00:05:47,670 hopefully, when you read through the 158 00:05:47,670 --> 00:05:49,430 documentation, and I'll just go back to 159 00:05:49,430 --> 00:05:52,380 Chrome - so basically, this link. So 160 00:05:52,380 --> 00:05:54,030 hopefully, when you go through this now, 161 00:05:54,030 --> 00:05:55,530 it'll make a lot more sense now you've 162 00:05:55,530 --> 00:05:58,080 seen it in action. The new system is very 163 00:05:58,080 --> 00:06:00,570 flexible, at least for the users, and 164 00:06:00,570 --> 00:06:02,220 doesn't really create a lot of extra 165 00:06:02,220 --> 00:06:04,920 work for us, as developers. Most of the 166 00:06:04,920 --> 00:06:06,600 time, you'll probably just copy the code 167 00:06:06,600 --> 00:06:07,920 from this app, or from the Google 168 00:06:07,920 --> 00:06:10,500 documentation, and use slightly modified 169 00:06:10,500 --> 00:06:12,420 versions of that. Now if you do want to 170 00:06:12,420 --> 00:06:14,940 use something, or do something, a bit more 171 00:06:14,940 --> 00:06:17,250 complex, you should now have all you need 172 00:06:17,250 --> 00:06:19,800 to be able to do so, at least from a 173 00:06:19,800 --> 00:06:21,690 permissions point of view. I haven't 174 00:06:21,690 --> 00:06:23,670 covered how to show dialogues yet, but 175 00:06:23,670 --> 00:06:25,710 we'll be doing that in a later video. For 176 00:06:25,710 --> 00:06:28,920 now though, on with databases. So far in 177 00:06:28,920 --> 00:06:30,420 this section, we've looked at databases, 178 00:06:30,420 --> 00:06:33,210 and then we saw how to perform basic 179 00:06:33,210 --> 00:06:35,820 database operations using the Sequel 180 00:06:35,820 --> 00:06:38,280 language and SQLite. We then 181 00:06:38,280 --> 00:06:41,130 experimented with ContentProviders, and 182 00:06:41,130 --> 00:06:43,350 we saw how they're used to provide shared 183 00:06:43,350 --> 00:06:45,270 access to a data source. 184 00:06:45,270 --> 00:06:47,310 And we also used Cursors to loop through 185 00:06:47,310 --> 00:06:50,190 the data from a data source. So with that 186 00:06:50,190 --> 00:06:52,259 done, in the next section, we're now going 187 00:06:52,259 --> 00:06:54,419 to create a full app that uses a 188 00:06:54,419 --> 00:06:56,490 SQLite database. We're going to access the 189 00:06:56,490 --> 00:06:59,340 data via a ContentProvider, so we'll be 190 00:06:59,340 --> 00:07:01,319 seeing how to create and register our 191 00:07:01,319 --> 00:07:03,840 own ContentProvider, to allow the data 192 00:07:03,840 --> 00:07:05,970 to be shared. So let's end the video here 193 00:07:05,970 --> 00:07:07,650 and move on to the next section, and get 194 00:07:07,650 --> 00:07:09,949 started.