| /* |
| * 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.Espresso.onIdle; |
| import static androidx.test.espresso.action.ViewActions.click; |
| import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist; |
| 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.withContentDescription; |
| import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| import static androidx.test.espresso.matcher.ViewMatchers.withParent; |
| 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.OrientationUtils.setLandscapeOrientation; |
| import static com.android.providers.media.photopicker.espresso.OrientationUtils.setPortraitOrientation; |
| import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.longClickItem; |
| |
| 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 android.graphics.drawable.ColorDrawable; |
| import android.graphics.drawable.Drawable; |
| import android.view.View; |
| import android.widget.Button; |
| |
| import androidx.appcompat.widget.Toolbar; |
| import androidx.test.espresso.Espresso; |
| import androidx.test.espresso.IdlingRegistry; |
| import androidx.test.ext.junit.rules.ActivityScenarioRule; |
| import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; |
| |
| import com.android.providers.media.R; |
| |
| import org.junit.Rule; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| |
| @RunWith(AndroidJUnit4ClassRunner.class) |
| public class PreviewSingleSelectTest extends PhotoPickerBaseTest { |
| |
| @Rule |
| public ActivityScenarioRule<PhotoPickerTestActivity> mRule |
| = new ActivityScenarioRule<>(PhotoPickerBaseTest.getSingleSelectionIntent()); |
| |
| @Test |
| public void testPreview_singleSelect_image() { |
| onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed())); |
| |
| final BottomSheetIdlingResource bottomSheetIdlingResource = |
| BottomSheetIdlingResource.register(mRule); |
| |
| try { |
| 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); |
| }); |
| |
| // Navigate to preview |
| longClickItem(PICKER_TAB_RECYCLERVIEW_ID, IMAGE_1_POSITION, ICON_THUMBNAIL_ID); |
| |
| registerIdlingResourceAndWaitForIdle(); |
| |
| // No dragBar in preview |
| bottomSheetIdlingResource.setExpectedState(STATE_EXPANDED); |
| onView(withId(DRAG_BAR_ID)).check(matches(not(isDisplayed()))); |
| // No privacy text in preview |
| onView(withId(PRIVACY_TEXT_ID)).check(matches(not(isDisplayed()))); |
| mRule.getScenario().onActivity(activity -> { |
| assertBottomSheetState(activity, STATE_EXPANDED); |
| }); |
| |
| // Verify image is previewed |
| assertSingleSelectCommonLayoutMatches(); |
| onView(withId(R.id.preview_imageView)).check(matches(isDisplayed())); |
| // Verify no special format icon is previewed |
| onView(withId(PREVIEW_MOTION_PHOTO_ID)).check(doesNotExist()); |
| onView(withId(PREVIEW_GIF_ID)).check(doesNotExist()); |
| |
| // Navigate back to Photo grid |
| onView(withContentDescription("Navigate up")).perform(click()); |
| |
| onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed())); |
| |
| bottomSheetIdlingResource.setExpectedState(STATE_COLLAPSED); |
| // Shows dragBar and privacy text after we are back to Photos tab |
| onView(withId(DRAG_BAR_ID)).check(matches(isDisplayed())); |
| onView(withId(PRIVACY_TEXT_ID)).check(matches(isDisplayed())); |
| mRule.getScenario().onActivity(activity -> { |
| assertBottomSheetState(activity, STATE_COLLAPSED); |
| }); |
| } finally { |
| IdlingRegistry.getInstance().unregister(bottomSheetIdlingResource); |
| } |
| } |
| |
| @Test |
| public void testPreview_singleSelect_video() { |
| onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed())); |
| |
| // Navigate to preview |
| longClickItem(PICKER_TAB_RECYCLERVIEW_ID, VIDEO_POSITION, ICON_THUMBNAIL_ID); |
| |
| registerIdlingResourceAndWaitForIdle(); |
| |
| // Verify video player is displayed |
| assertSingleSelectCommonLayoutMatches(); |
| onView(withId(R.id.preview_player_view)).check(matches(isDisplayed())); |
| // Verify no special format icon is previewed |
| onView(withId(PREVIEW_MOTION_PHOTO_ID)).check(doesNotExist()); |
| onView(withId(PREVIEW_GIF_ID)).check(doesNotExist()); |
| } |
| |
| @Test |
| public void testPreview_singleSelect_fromAlbumsPhoto() { |
| // Navigate to Albums tab |
| onView(allOf(withText(PICKER_ALBUMS_STRING_ID), withParent(withId(CHIP_CONTAINER_ID)))) |
| .perform(click()); |
| |
| 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()); |
| |
| // Verify that toolbar has the title as category name Camera |
| onView(allOf(withText(cameraStringId), withParent(withId(R.id.toolbar)))) |
| .check(matches(isDisplayed())); |
| |
| // Navigate to preview |
| longClickItem(PICKER_TAB_RECYCLERVIEW_ID, /* position */ 1, ICON_THUMBNAIL_ID); |
| |
| registerIdlingResourceAndWaitForIdle(); |
| |
| // Verify image is previewed |
| assertSingleSelectCommonLayoutMatches(); |
| onView(withId(R.id.preview_imageView)).check(matches(isDisplayed())); |
| |
| // Navigate back to Camera album |
| onView(withContentDescription("Navigate up")).perform(click()); |
| |
| // Verify that toolbar has the title as category name Camera |
| onView(allOf(withText(cameraStringId), withParent(withId(R.id.toolbar)))) |
| .check(matches(isDisplayed())); |
| } |
| |
| @Test |
| public void testPreview_noScrimLayerAndHasSolidColorInPortrait() { |
| setPortraitOrientation(mRule); |
| |
| onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed())); |
| // Navigate to preview |
| longClickItem(PICKER_TAB_RECYCLERVIEW_ID, IMAGE_1_POSITION, ICON_THUMBNAIL_ID); |
| |
| registerIdlingResourceAndWaitForIdle(); |
| |
| onView(withId(R.id.preview_top_scrim)).check(matches(not(isDisplayed()))); |
| onView(withId(R.id.preview_bottom_scrim)).check(matches(not(isDisplayed()))); |
| |
| mRule.getScenario().onActivity(activity -> { |
| assertBackgroundColorOnToolbarAndBottomBar(activity, R.color.preview_scrim_solid_color); |
| }); |
| } |
| |
| @Test |
| public void testPreview_showScrimLayerInLandscape() { |
| setLandscapeOrientation(mRule); |
| |
| onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed())); |
| |
| // Navigate to preview |
| longClickItem(PICKER_TAB_RECYCLERVIEW_ID, IMAGE_1_POSITION, ICON_THUMBNAIL_ID); |
| |
| registerIdlingResourceAndWaitForIdle(); |
| |
| onView(withId(R.id.preview_top_scrim)).check(matches(isDisplayed())); |
| onView(withId(R.id.preview_bottom_scrim)).check(matches(isDisplayed())); |
| |
| mRule.getScenario().onActivity(activity -> { |
| assertBackgroundColorOnToolbarAndBottomBar(activity, android.R.color.transparent); |
| }); |
| } |
| |
| @Test |
| public void testPreview_addButtonWidth() { |
| onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed())); |
| // Navigate to preview |
| longClickItem(PICKER_TAB_RECYCLERVIEW_ID, IMAGE_1_POSITION, ICON_THUMBNAIL_ID); |
| |
| registerIdlingResourceAndWaitForIdle(); |
| // Check that Add button is visible |
| onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed())); |
| onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.add))); |
| |
| setPortraitOrientation(mRule); |
| mRule.getScenario().onActivity(activity -> { |
| final Button addOrSelectButton |
| = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID); |
| final int expectedAddOrSelectButtonWidth = activity.getResources() |
| .getDimensionPixelOffset(DIMEN_PREVIEW_ADD_OR_SELECT_WIDTH); |
| // Check that button width in portrait mode is = R.dimen.preview_add_or_select_width |
| assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth); |
| }); |
| |
| setLandscapeOrientation(mRule); |
| mRule.getScenario().onActivity(activity -> { |
| final Button addOrSelectButton |
| = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID); |
| final int expectedAddOrSelectButtonWidth = activity.getResources() |
| .getDimensionPixelOffset(DIMEN_PREVIEW_ADD_OR_SELECT_WIDTH); |
| // Check that button width in landscape mode is == R.dimen.preview_add_or_select_width |
| assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth); |
| }); |
| } |
| |
| private void registerIdlingResourceAndWaitForIdle() { |
| mRule.getScenario().onActivity((activity -> IdlingRegistry.getInstance().register( |
| new ViewPager2IdlingResource(activity.findViewById(R.id.preview_viewPager))))); |
| onIdle(); |
| } |
| |
| private void assertBackgroundColorOnToolbarAndBottomBar(Activity activity, int colorResId) { |
| final Toolbar toolbar = activity.findViewById(R.id.toolbar); |
| final Drawable toolbarDrawable = toolbar.getBackground(); |
| |
| assertThat(toolbarDrawable).isInstanceOf(ColorDrawable.class); |
| |
| final int expectedColor = activity.getColor(colorResId); |
| assertThat(((ColorDrawable) toolbarDrawable).getColor()).isEqualTo(expectedColor); |
| |
| final View bottomBar = activity.findViewById(R.id.preview_bottom_bar); |
| final Drawable bottomBarDrawable = bottomBar.getBackground(); |
| |
| assertThat(bottomBarDrawable).isInstanceOf(ColorDrawable.class); |
| assertThat(((ColorDrawable) bottomBarDrawable).getColor()).isEqualTo(expectedColor); |
| } |
| |
| private void assertSingleSelectCommonLayoutMatches() { |
| onView(withId(R.id.preview_viewPager)).check(matches(isDisplayed())); |
| onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed())); |
| // Verify that the text in Add button |
| onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.add))); |
| |
| onView(withId(R.id.preview_select_check_button)).check(matches(not(isDisplayed()))); |
| onView(withId(R.id.preview_add_button)).check(matches(not(isDisplayed()))); |
| } |
| } |