1
+ /*
2
+ * Copyright 2016 Andrey Tolpeev
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com .github .vase4kin .teamcityapp .favorites .view ;
18
+
19
+ import android .content .Intent ;
20
+ import android .support .test .InstrumentationRegistry ;
21
+ import android .support .test .runner .AndroidJUnit4 ;
22
+
23
+ import com .github .vase4kin .teamcityapp .R ;
24
+ import com .github .vase4kin .teamcityapp .TeamCityApplication ;
25
+ import com .github .vase4kin .teamcityapp .api .TeamCityService ;
26
+ import com .github .vase4kin .teamcityapp .dagger .components .AppComponent ;
27
+ import com .github .vase4kin .teamcityapp .dagger .components .RestApiComponent ;
28
+ import com .github .vase4kin .teamcityapp .dagger .modules .AppModule ;
29
+ import com .github .vase4kin .teamcityapp .dagger .modules .FakeTeamCityServiceImpl ;
30
+ import com .github .vase4kin .teamcityapp .dagger .modules .Mocks ;
31
+ import com .github .vase4kin .teamcityapp .dagger .modules .RestApiModule ;
32
+ import com .github .vase4kin .teamcityapp .helper .CustomIntentsTestRule ;
33
+ import com .github .vase4kin .teamcityapp .helper .TestUtils ;
34
+ import com .github .vase4kin .teamcityapp .navigation .api .BuildType ;
35
+ import com .github .vase4kin .teamcityapp .root .view .RootProjectsActivity ;
36
+ import com .github .vase4kin .teamcityapp .storage .SharedUserStorage ;
37
+
38
+ import org .junit .Before ;
39
+ import org .junit .BeforeClass ;
40
+ import org .junit .Rule ;
41
+ import org .junit .Test ;
42
+ import org .junit .runner .RunWith ;
43
+ import org .mockito .Spy ;
44
+
45
+ import it .cosenonjaviste .daggermock .DaggerMockRule ;
46
+ import rx .Observable ;
47
+
48
+ import static android .content .Intent .FLAG_ACTIVITY_SINGLE_TOP ;
49
+ import static android .support .test .espresso .Espresso .onView ;
50
+ import static android .support .test .espresso .action .ViewActions .click ;
51
+ import static android .support .test .espresso .assertion .ViewAssertions .matches ;
52
+ import static android .support .test .espresso .intent .Intents .intended ;
53
+ import static android .support .test .espresso .intent .matcher .IntentMatchers .hasComponent ;
54
+ import static android .support .test .espresso .intent .matcher .IntentMatchers .hasFlags ;
55
+ import static android .support .test .espresso .matcher .ViewMatchers .isDisplayed ;
56
+ import static android .support .test .espresso .matcher .ViewMatchers .withId ;
57
+ import static android .support .test .espresso .matcher .ViewMatchers .withText ;
58
+ import static com .github .vase4kin .teamcityapp .helper .RecyclerViewMatcher .withRecyclerView ;
59
+ import static com .github .vase4kin .teamcityapp .helper .TestUtils .hasItemsCount ;
60
+ import static com .github .vase4kin .teamcityapp .helper .TestUtils .matchToolbarTitle ;
61
+ import static org .hamcrest .core .AllOf .allOf ;
62
+ import static org .mockito .Matchers .anyString ;
63
+ import static org .mockito .Mockito .when ;
64
+
65
+ @ RunWith (AndroidJUnit4 .class )
66
+ public class FavoritesActivityTest {
67
+
68
+ @ Rule
69
+ public DaggerMockRule <AppComponent > daggerRule = new DaggerMockRule <>(AppComponent .class , new AppModule ((TeamCityApplication ) InstrumentationRegistry .getInstrumentation ().getTargetContext ().getApplicationContext ()))
70
+ .set (new DaggerMockRule .ComponentSetter <AppComponent >() {
71
+ @ Override
72
+ public void setComponent (AppComponent appComponent ) {
73
+ TeamCityApplication app = (TeamCityApplication ) InstrumentationRegistry .getInstrumentation ().getTargetContext ().getApplicationContext ();
74
+ app .setAppInjector (appComponent );
75
+ }
76
+ });
77
+
78
+ @ Rule
79
+ public DaggerMockRule <RestApiComponent > restComponentDaggerRule = new DaggerMockRule <>(RestApiComponent .class , new RestApiModule (Mocks .URL ))
80
+ .addComponentDependency (AppComponent .class , new AppModule ((TeamCityApplication ) InstrumentationRegistry .getInstrumentation ().getTargetContext ().getApplicationContext ()))
81
+ .set (new DaggerMockRule .ComponentSetter <RestApiComponent >() {
82
+ @ Override
83
+ public void setComponent (RestApiComponent restApiComponent ) {
84
+ TeamCityApplication app = (TeamCityApplication ) InstrumentationRegistry .getInstrumentation ().getTargetContext ().getApplicationContext ();
85
+ app .setRestApiInjector (restApiComponent );
86
+ }
87
+ });
88
+
89
+ @ Rule
90
+ public CustomIntentsTestRule <FavoritesActivity > activityRule = new CustomIntentsTestRule <>(FavoritesActivity .class );
91
+
92
+ @ Spy
93
+ private TeamCityService teamCityService = new FakeTeamCityServiceImpl ();
94
+
95
+ @ BeforeClass
96
+ public static void disableOnboarding () {
97
+ TestUtils .disableOnboarding ();
98
+ }
99
+
100
+ @ Before
101
+ public void setUp () {
102
+ SharedUserStorage storage = getStorage ();
103
+ storage .clearAll ();
104
+ storage .saveGuestUserAccountAndSetItAsActive (Mocks .URL , false );
105
+ }
106
+
107
+ @ Test
108
+ public void testUserCanSeeFavoritesListIfSmthBadHappensInFavoritesLoading () {
109
+ // prepare mocks
110
+ when (teamCityService .buildType (anyString ())).thenReturn (Observable .<BuildType >error (new RuntimeException ("smth bad happend!" )));
111
+ getStorage ().addBuildTypeToFavorites ("id" );
112
+ getStorage ().addBuildTypeToFavorites ("id2" );
113
+ getStorage ().addBuildTypeToFavorites ("id3" );
114
+
115
+ // launch activity
116
+ activityRule .launchActivity (null );
117
+
118
+ // Check empty list
119
+ onView (withId (android .R .id .empty )).check (matches (isDisplayed ())).check (matches (withText (R .string .empty_list_message_favorites )));
120
+ }
121
+
122
+ @ Test
123
+ public void testUserCanSeeLoadEmptyListMessageIfFavoritesIsEmpty () {
124
+ // launch activity
125
+ activityRule .launchActivity (null );
126
+
127
+ // Checking toolbar title
128
+ matchToolbarTitle ("Favorites (0)" );
129
+
130
+ // Check the list is empty
131
+ onView (withId (android .R .id .empty )).check (matches (isDisplayed ())).check (matches (withText (R .string .empty_list_message_favorites )));
132
+ }
133
+
134
+ @ Test
135
+ public void testUserCanClickOnFabAndSeeProjects () {
136
+ // launch activity
137
+ activityRule .launchActivity (null );
138
+
139
+ // click on fab
140
+ onView (withId (R .id .floating_action_button )).perform (click ());
141
+
142
+ // check snack bar text
143
+ onView (withText (R .string .text_info_add )).check (matches (isDisplayed ()));
144
+
145
+ // click on snack bar action
146
+ onView (withText (R .string .text_info_add_action )).perform (click ());
147
+
148
+ // check activity is opened
149
+ intended (allOf (
150
+ hasComponent (RootProjectsActivity .class .getName ()),
151
+ hasFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP | Intent .FLAG_ACTIVITY_NEW_TASK
152
+ | FLAG_ACTIVITY_SINGLE_TOP )));
153
+
154
+ }
155
+
156
+ @ Test
157
+ public void testUserCanAddBuildTypeToFavorites () {
158
+ //prepare mocks
159
+ when (teamCityService .buildType (Mocks .buildTypeMock ().getId ())).thenReturn (Observable .just (Mocks .buildTypeMock ()));
160
+
161
+ // launch activity
162
+ activityRule .launchActivity (null );
163
+
164
+ // click on fab
165
+ onView (withId (R .id .floating_action_button )).perform (click ());
166
+
167
+ // click on snack bar action
168
+ onView (withText (R .string .text_info_add_action )).perform (click ());
169
+
170
+ // Open build type
171
+ onView (withText ("build type" ))
172
+ .perform (click ());
173
+
174
+ // Click on add to favorites
175
+ onView (withId (R .id .add_to_favorites )).perform (click ());
176
+
177
+ // check snack bar text
178
+ onView (withText (R .string .text_add_to_favorites )).check (matches (isDisplayed ()));
179
+
180
+ // click on snack bar action
181
+ onView (withText (R .string .text_view_favorites )).perform (click ());
182
+
183
+ // Checking toolbar title
184
+ matchToolbarTitle ("Favorites (1)" );
185
+
186
+ // List has item with header
187
+ onView (withId (R .id .favorites_recycler_view )).check (hasItemsCount (2 ));
188
+ // Checking header 1
189
+ onView (withRecyclerView (R .id .favorites_recycler_view ).atPositionOnView (0 , R .id .section_text ))
190
+ .check (matches (withText ("Secret project" )));
191
+ // Checking adapter item 1
192
+ onView (withRecyclerView (R .id .favorites_recycler_view ).atPositionOnView (1 , R .id .itemTitle ))
193
+ .check (matches (withText ("build type" )));
194
+ }
195
+
196
+ private SharedUserStorage getStorage () {
197
+ TeamCityApplication app = (TeamCityApplication ) InstrumentationRegistry .getInstrumentation ().getTargetContext ().getApplicationContext ();
198
+ return app .getAppInjector ().sharedUserStorage ();
199
+ }
200
+ }
0 commit comments