summaryrefslogtreecommitdiff
path: root/tests/src/com/android/providers/media/photopicker/espresso/PhotoPickerActivityTest.java
blob: 8919187cf18faeff1edfcbfdfb8d9cec1eedf838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.media.photopicker.espresso;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isNotSelected;
import static androidx.test.espresso.matcher.ViewMatchers.isSelected;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import static com.android.providers.media.photopicker.espresso.BottomSheetTestUtils.assertBottomSheetState;
import static com.android.providers.media.photopicker.espresso.CustomSwipeAction.customSwipeDownPartialScreen;
import static com.android.providers.media.photopicker.espresso.CustomSwipeAction.swipeLeftAndWait;
import static com.android.providers.media.photopicker.espresso.CustomSwipeAction.swipeRightAndWait;
import static com.android.providers.media.photopicker.espresso.OrientationUtils.setLandscapeOrientation;
import static com.android.providers.media.photopicker.espresso.OrientationUtils.setPortraitOrientation;
import static com.android.providers.media.photopicker.espresso.RecyclerViewMatcher.withRecyclerView;

import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED;
import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED;
import static com.google.common.truth.Truth.assertThat;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.not;

import android.app.Activity;

import androidx.test.InstrumentationRegistry;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.viewpager2.widget.ViewPager2;

import com.android.providers.media.R;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4ClassRunner.class)
public class PhotoPickerActivityTest extends PhotoPickerBaseTest {

    private static final int TAB_VIEW_PAGER_ID = R.id.picker_tab_viewpager;

    @Rule
    public ActivityScenarioRule<PhotoPickerTestActivity> mRule
            = new ActivityScenarioRule<>(PhotoPickerBaseTest.getSingleSelectionIntent());

    /**
     * Simple test to check we are able to launch PhotoPickerActivity
     */
    @Test
    public void testActivityLayout_Simple() {
        onView(withId(R.id.toolbar)).check(matches(isDisplayed()));
        onView(withId(R.id.fragment_container)).check(matches(isDisplayed()));
        onView(withId(DRAG_BAR_ID)).check(matches(isDisplayed()));
        onView(withId(PRIVACY_TEXT_ID)).check(matches(isDisplayed()));
        // Partial screen does not show profile button
        onView(withId(R.id.profile_button)).check(matches(not(isDisplayed())));
        onView(withId(android.R.id.empty)).check(matches(not(isDisplayed())));

        final String cancelString =
                InstrumentationRegistry.getTargetContext().getResources().getString(
                        android.R.string.cancel);
        onView(withContentDescription(cancelString)).perform(click());
        assertThat(mRule.getScenario().getResult().getResultCode()).isEqualTo(
                Activity.RESULT_CANCELED);
    }

    @Test
    public void testDoesNotShowProfileButton_partialScreen() {
        assertProfileButtonNotShown();
    }

    @Test
    @Ignore("Enable after b/222013536 is fixed")
    public void testDoesNotShowProfileButton_fullScreen() {
        // Bottomsheet assertions are different for landscape mode
        setPortraitOrientation(mRule);

        // Partial screen does not show profile button
        onView(withId(R.id.profile_button)).check(matches(not(isDisplayed())));

        BottomSheetTestUtils.swipeUp(mRule);

        assertProfileButtonNotShown();
    }

    @Test
    @Ignore("Enable after b/222013536 is fixed")
    public void testBottomSheetState() {
        // Bottom sheet assertions are different for landscape mode
        setPortraitOrientation(mRule);

        // Register bottom sheet idling resource so that we don't read bottom sheet state when
        // in between changing states
        final BottomSheetIdlingResource bottomSheetIdlingResource =
                BottomSheetIdlingResource.register(mRule);

        try {
            // Single select PhotoPicker is launched in partial screen mode
            bottomSheetIdlingResource.setExpectedState(STATE_COLLAPSED);
            onView(withId(DRAG_BAR_ID)).check(matches(isDisplayed()));
            onView(withId(PRIVACY_TEXT_ID)).check(matches(isDisplayed()));
            mRule.getScenario().onActivity(activity -> {
                assertBottomSheetState(activity, STATE_COLLAPSED);
            });

            // Swipe up and check that the PhotoPicker is in full screen mode
            bottomSheetIdlingResource.setExpectedState(STATE_EXPANDED);
            onView(withId(PRIVACY_TEXT_ID)).perform(ViewActions.swipeUp());
            mRule.getScenario().onActivity(activity -> {
                assertBottomSheetState(activity, STATE_EXPANDED);
            });

            // Swipe down and check that the PhotoPicker is in partial screen mode
            bottomSheetIdlingResource.setExpectedState(STATE_COLLAPSED);
            onView(withId(PRIVACY_TEXT_ID)).perform(ViewActions.swipeDown());
            mRule.getScenario().onActivity(activity -> {
                assertBottomSheetState(activity, STATE_COLLAPSED);
            });

            // Swiping down on drag bar is not strong enough as closing the bottomsheet requires a
            // stronger downward swipe using espresso.
            // Simply swiping down on R.id.bottom_sheet throws an error from espresso, as the view
            // is only 60% visible, but downward swipe is only successful on an element which is 90%
            // visible.
            onView(withId(R.id.bottom_sheet)).perform(customSwipeDownPartialScreen());
        } finally {
            IdlingRegistry.getInstance().unregister(bottomSheetIdlingResource);
        }
        assertThat(mRule.getScenario().getResult().getResultCode()).isEqualTo(
                Activity.RESULT_CANCELED);
    }

    @Test
    @Ignore("Enable after b/222013536 is fixed")
    public void testBottomSheetStateInLandscapeMode() {
        // Bottom sheet assertions are different for landscape mode
        setLandscapeOrientation(mRule);

        // Register bottom sheet idling resource so that we don't read bottom sheet state when
        // in between changing states
        final BottomSheetIdlingResource bottomSheetIdlingResource =
                BottomSheetIdlingResource.register(mRule);

        try {
            // Single select PhotoPicker is launched in full screen mode in Landscape orientation
            mRule.getScenario().onActivity(activity -> {
                assertBottomSheetState(activity, STATE_EXPANDED);
            });

            // Swiping down on drag bar / privacy text is not strong enough as closing the
            // bottomsheet requires a stronger downward swipe using espresso.
            onView(withId(R.id.bottom_sheet)).perform(ViewActions.swipeDown());
        } finally {
            IdlingRegistry.getInstance().unregister(bottomSheetIdlingResource);
        }
        assertThat(mRule.getScenario().getResult().getResultCode()).isEqualTo(
                Activity.RESULT_CANCELED);
    }

    @Test
    public void testToolbarLayout() {
        onView(withId(R.id.toolbar)).check(matches(isDisplayed()));

        onView(withId(TAB_LAYOUT_ID)).check(matches(isDisplayed()));

        mRule.getScenario().onActivity(activity -> {
            final ViewPager2 viewPager2 = activity.findViewById(TAB_VIEW_PAGER_ID);
            assertThat(viewPager2.getAdapter().getItemCount()).isEqualTo(2);
        });

        onView(allOf(withText(PICKER_PHOTOS_STRING_ID),
                isDescendantOfA(withId(TAB_LAYOUT_ID)))).check(matches(isDisplayed()));
        onView(allOf(withText(PICKER_ALBUMS_STRING_ID),
                isDescendantOfA(withId(TAB_LAYOUT_ID)))).check(matches(isDisplayed()));

        // TODO(b/200513333): Check close icon
    }

    @Test
    public void testTabNavigation() {
        onView(withId(TAB_LAYOUT_ID)).check(matches(isDisplayed()));

        // On clicking albums tab item, we should see albums tab
        onView(allOf(withText(PICKER_ALBUMS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .perform(click());
        onView(allOf(withText(PICKER_ALBUMS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .check(matches(isSelected()));
        onView(allOf(withText(PICKER_PHOTOS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .check(matches(isNotSelected()));
        // Verify Camera album is shown, we are in albums tab
        onView(allOf(withText(R.string.picker_category_camera),
                isDescendantOfA(withId(PICKER_TAB_RECYCLERVIEW_ID)))).check(matches(isDisplayed()));


        // On clicking photos tab item, we should see photos tab
        onView(allOf(withText(PICKER_PHOTOS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .perform(click());
        onView(allOf(withText(PICKER_PHOTOS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .check(matches(isSelected()));
        onView(allOf(withText(PICKER_ALBUMS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .check(matches(isNotSelected()));
        // Verify first item is recent header, we are in photos tab
        onView(withRecyclerView(PICKER_TAB_RECYCLERVIEW_ID)
                .atPositionOnView(0, R.id.date_header_title))
                .check(matches(withText(R.string.recent)));
    }

    @Test
    @Ignore("Enable after b/222013536 is fixed")
    public void testTabSwiping() throws Exception {
        onView(withId(TAB_LAYOUT_ID)).check(matches(isDisplayed()));

        // If we want to swipe the viewPager2 of tabContainerFragment in Espresso tests, at least 90
        // percent of the view's area is displayed to the user. Swipe up the bottom Sheet to make
        // sure it is in full Screen mode.
        // Register bottom sheet idling resource so that we don't read bottom sheet state when
        // in between changing states
        final BottomSheetIdlingResource bottomSheetIdlingResource =
                BottomSheetIdlingResource.register(mRule);

        try {

            // When accessibility is enabled, we always launch the photo picker in full screen mode.
            // Accessibility is enabled in Espresso test, so we can't check the COLLAPSED state.
//            // Single select PhotoPicker is launched in partial screen mode
//            bottomSheetIdlingResource.setExpectedState(STATE_COLLAPSED);
//            mRule.getScenario().onActivity(activity -> {
//                assertBottomSheetState(activity, STATE_COLLAPSED);
//            });

            // Swipe up and check that the PhotoPicker is in full screen mode.
//            onView(withId(PRIVACY_TEXT_ID)).check(matches(isDisplayed()));
//            onView(withId(PRIVACY_TEXT_ID)).perform(ViewActions.swipeUp());
            bottomSheetIdlingResource.setExpectedState(STATE_EXPANDED);
            mRule.getScenario().onActivity(activity -> {
                assertBottomSheetState(activity, STATE_EXPANDED);
            });
        } finally {
            IdlingRegistry.getInstance().unregister(bottomSheetIdlingResource);
        }

        try (ViewPager2IdlingResource idlingResource
                     = ViewPager2IdlingResource.register(mRule, TAB_VIEW_PAGER_ID)) {
            // Swipe left, we should see albums tab
            swipeLeftAndWait(TAB_VIEW_PAGER_ID);

            onView(allOf(withText(PICKER_ALBUMS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                    .check(matches(isSelected()));
            onView(allOf(withText(PICKER_PHOTOS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                    .check(matches(isNotSelected()));
            // Verify Camera album is shown, we are in albums tab
            onView(allOf(withText(R.string.picker_category_camera),
                    isDescendantOfA(withId(PICKER_TAB_RECYCLERVIEW_ID)))).check(
                    matches(isDisplayed()));

            // Swipe right, we should see photos tab
            swipeRightAndWait(TAB_VIEW_PAGER_ID);

            onView(allOf(withText(PICKER_PHOTOS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                    .check(matches(isSelected()));
            onView(allOf(withText(PICKER_ALBUMS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                    .check(matches(isNotSelected()));
            // Verify first item is recent header, we are in photos tab
            onView(withRecyclerView(PICKER_TAB_RECYCLERVIEW_ID)
                    .atPositionOnView(0, R.id.date_header_title))
                    .check(matches(withText(R.string.recent)));
        }
    }

    private void assertProfileButtonNotShown() {
        // Partial screen does not show profile button
        onView(withId(R.id.profile_button)).check(matches(not(isDisplayed())));

        // Navigate to Albums tab
        onView(allOf(withText(PICKER_ALBUMS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .perform(click());
        onView(withId(R.id.profile_button)).check(matches(not(isDisplayed())));

        final int cameraStringId = R.string.picker_category_camera;
        // Navigate to photos in Camera album
        onView(allOf(withText(cameraStringId),
                isDescendantOfA(withId(PICKER_TAB_RECYCLERVIEW_ID)))).perform(click());
        onView(withId(R.id.profile_button)).check(matches(not(isDisplayed())));

        // Click back button
        onView(withContentDescription("Navigate up")).perform(click());

        // on clicking back button we are back to Album grid
        onView(allOf(withText(PICKER_ALBUMS_STRING_ID), isDescendantOfA(withId(TAB_LAYOUT_ID))))
                .check(matches(isSelected()));
        onView(withId(R.id.profile_button)).check(matches(not(isDisplayed())));
    }
}