1 00:00:00,430 --> 00:00:03,680 We've just finished testing our editText with Espresso, and 2 00:00:03,680 --> 00:00:05,940 now it's time that we move on to testing our spinner. 3 00:00:08,450 --> 00:00:10,402 Let's start in the Arrange section, 4 00:00:10,402 --> 00:00:13,819 by copying over the given color variable from MainActivity test. 5 00:00:26,518 --> 00:00:31,394 To find the right spinner item to click we'll be using the with text a few so 6 00:00:31,394 --> 00:00:35,498 let's create a new string variable named spinner item text. 7 00:00:39,755 --> 00:00:42,900 And set it equal to the string Green. 8 00:00:46,490 --> 00:00:49,478 In the act section, we need to start by clicking on our spinner. 9 00:00:49,478 --> 00:00:56,828 Let's type onView(withId( R.id.colorspinner), 10 00:00:56,828 --> 00:01:00,728 and then, .perform, and for 11 00:01:00,728 --> 00:01:05,832 the view action, let's pass in click. 12 00:01:09,045 --> 00:01:11,583 Now that we can see all the items in the spinner, 13 00:01:11,583 --> 00:01:13,880 we need to click on the one that says Green. 14 00:01:15,020 --> 00:01:18,548 Lets use control or command D to duplicate this line and 15 00:01:18,548 --> 00:01:23,082 then replace the withId view matcher with the withText view matcher. 16 00:01:27,837 --> 00:01:32,310 And instead of passing in an ID, let's pass in our spinnerItemText variable. 17 00:01:33,870 --> 00:01:36,115 At this point if we run the test again. 18 00:01:45,848 --> 00:01:50,400 We can see that we're successfully changing the background color to green. 19 00:01:50,400 --> 00:01:51,760 Nice. 20 00:01:51,760 --> 00:01:55,320 But before we move on to asserting, we need to talk about an edge case. 21 00:01:56,360 --> 00:02:01,770 What if we had a spinner with 100 items and we wanted to click on the 98th item. 22 00:02:03,020 --> 00:02:06,700 Well, the spinner only shows so many items at a time. 23 00:02:07,810 --> 00:02:12,080 So if we try to click on the 98th item, it's not going to be there. 24 00:02:12,080 --> 00:02:12,800 And we'll get an error. 25 00:02:14,280 --> 00:02:15,210 To fix this, 26 00:02:15,210 --> 00:02:20,170 Espresso gives us the onData method, which we use to replace the onView method. 27 00:02:21,940 --> 00:02:25,450 OnView only searches through the view hierarchy, but 28 00:02:25,450 --> 00:02:27,170 onData searches through everything. 29 00:02:28,210 --> 00:02:31,840 So if you want to click on that 98th item, onData is the way to go. 30 00:02:32,980 --> 00:02:37,296 Let's refactor our spinnerItem click to use onData instead. 31 00:02:41,746 --> 00:02:45,528 Then let's copy in a few other imports we'll be needing from the teachers' 32 00:02:45,528 --> 00:02:46,247 notes below. 33 00:02:47,987 --> 00:02:50,236 And paste them up here in the import section. 34 00:02:53,192 --> 00:02:58,340 Next we need to provide an object matcher to match the object we're trying to click. 35 00:02:59,700 --> 00:03:02,400 So get rid of what's inside our onData method now. 36 00:03:05,050 --> 00:03:08,630 Because instead of a view matcher, we need to be providing an object matcher. 37 00:03:09,950 --> 00:03:11,640 So inside the parentheses. 38 00:03:11,640 --> 00:03:13,620 Let's first use the all of matcher. 39 00:03:17,780 --> 00:03:23,397 The all of matcher takes in other matchers as parameters and if all 40 00:03:23,397 --> 00:03:30,358 of those other matchers report a match the all of matcher will report a match too. 41 00:03:30,358 --> 00:03:35,303 For the first parameter of all of method, 42 00:03:35,303 --> 00:03:41,812 lets pass on (is(instance0f(String.class). 43 00:03:41,812 --> 00:03:43,705 We don't want to try and 44 00:03:43,705 --> 00:03:49,385 match our spinner item text to something that isn't a string, then for 45 00:03:49,385 --> 00:03:54,891 the second matcher parameter lets pass in is(spinner item text). 46 00:03:56,770 --> 00:04:00,240 Now it will search through the data in our app looking for 47 00:04:00,240 --> 00:04:04,950 a string equal to our spinner item text and 48 00:04:04,950 --> 00:04:09,850 when it finds it, it will find the associated view and perform a click. 49 00:04:11,420 --> 00:04:15,140 Let's run our test again to make sure we're still changing the background color. 50 00:04:24,133 --> 00:04:24,950 Perfect. 51 00:04:25,990 --> 00:04:28,670 Moving onto the assert section we need to assert 52 00:04:28,670 --> 00:04:33,120 that the background color of the app is equal to the given color. 53 00:04:33,120 --> 00:04:36,924 Let's grab the linear layout by using the withIdViewMatcher. 54 00:04:40,694 --> 00:04:47,587 OnView (withId of (R.Id.LinearLayout). 55 00:04:49,628 --> 00:04:55,526 Then since were asserting we need to add the check method, and now we just need 56 00:04:55,526 --> 00:05:01,360 to matcher to check that the background of our view matches the given color. 57 00:05:02,990 --> 00:05:05,613 Unfortunately there isn't a matcher for this. 58 00:05:05,613 --> 00:05:08,366 So we'll just have to make our own. 59 00:05:08,366 --> 00:05:14,713 Let's add a line above this one and declare a new bounded matcher variable. 60 00:05:14,713 --> 00:05:23,119 Named background color matcher. 61 00:05:23,119 --> 00:05:29,620 And let's set it equal to new bounded matcher. 62 00:05:29,620 --> 00:05:34,920 Bounded matchers give us a way to match only on objects of a given class. 63 00:05:34,920 --> 00:05:38,620 But autocomplete didn't fill this part in for us. 64 00:05:38,620 --> 00:05:44,365 So first, in the matchesSafely method, let's replace object item with 65 00:05:44,365 --> 00:05:51,670 (LinearLayout linearLayout). 66 00:05:51,670 --> 00:05:57,460 Then between BoundedMatcher and the parentheses, let's add angle brackets and 67 00:05:57,460 --> 00:06:02,130 inside lets type view on the linear layout. 68 00:06:05,830 --> 00:06:06,560 And import view. 69 00:06:08,230 --> 00:06:12,871 This tells our matcher to only be looking at views and 70 00:06:12,871 --> 00:06:16,254 then to only match on linear layouts. 71 00:06:16,254 --> 00:06:18,258 To finish off the errors, 72 00:06:18,258 --> 00:06:23,460 we just need to provide the class we're looking for as a parameter. 73 00:06:23,460 --> 00:06:29,914 Linearlayout.class and add a semicolon. 74 00:06:29,914 --> 00:06:32,446 Finally we just need to return whether or 75 00:06:32,446 --> 00:06:35,503 not we have a match in the matchesSafely method. 76 00:06:35,503 --> 00:06:37,044 To check if we have a match, 77 00:06:37,044 --> 00:06:41,610 let's start by copying over the actual color variable from MainActivityTest. 78 00:06:46,380 --> 00:06:48,602 And pasting it into our matchesSafely method. 79 00:06:53,800 --> 00:06:58,165 Then let's delete Activity to leave our linear layout parameter, 80 00:07:01,935 --> 00:07:10,230 And let's finish up by returning the outcome of givenColor == actualColor. 81 00:07:10,230 --> 00:07:13,890 The describeTo method is what we'll see in an error message 82 00:07:13,890 --> 00:07:15,640 if something doesn't match. 83 00:07:15,640 --> 00:07:19,068 And while we don't have to fill it in, it's quick and it doesn't hurt. 84 00:07:19,068 --> 00:07:25,261 Let's type description.appendText and 85 00:07:25,261 --> 00:07:29,733 then for the text let's pass in 86 00:07:29,733 --> 00:07:35,065 background color should equal: and 87 00:07:35,065 --> 00:07:39,198 then outside of the quotes. 88 00:07:39,198 --> 00:07:42,515 Plus given color. 89 00:07:42,515 --> 00:07:46,680 Semi colon. 90 00:07:46,680 --> 00:07:47,330 All right. 91 00:07:47,330 --> 00:07:52,030 We finally finished our custom matcher all that's left is to use it. 92 00:07:52,030 --> 00:07:53,810 Inside the check method. 93 00:07:53,810 --> 00:07:55,638 Let's use the matches view assertion. 94 00:07:55,638 --> 00:08:03,427 And pass in our background color matcher. 95 00:08:03,427 --> 00:08:06,616 Then let's use Alt+enter to fix this warning. 96 00:08:06,616 --> 00:08:11,496 And it looks like Android Studio just wanted us to specify the types 97 00:08:11,496 --> 00:08:12,900 over here as well. 98 00:08:14,810 --> 00:08:17,160 Okay moment of truth. 99 00:08:17,160 --> 00:08:19,400 Let's run the test and see what happens. 100 00:08:30,710 --> 00:08:32,020 All right. 101 00:08:32,020 --> 00:08:33,556 You're really getting good at this stuff. 102 00:08:33,556 --> 00:08:36,300 Now, let's comment out the act lines. 103 00:08:40,180 --> 00:08:43,791 And if we run the test again we should be able to see our description. 104 00:08:54,169 --> 00:08:55,780 There it is. 105 00:08:55,780 --> 00:09:00,060 Background color should equal about negative seventeen million. 106 00:09:01,420 --> 00:09:02,998 AKA color dark green. 107 00:09:02,998 --> 00:09:06,460 Now let's un-comment the act section and 108 00:09:06,460 --> 00:09:08,960 then let's conquer our final foe and the next video.