• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 package com.android.wallpaper.picker;
17 
18 import static androidx.test.espresso.Espresso.onView;
19 import static androidx.test.espresso.action.ViewActions.click;
20 import static androidx.test.espresso.action.ViewActions.pressBack;
21 import static androidx.test.espresso.assertion.ViewAssertions.matches;
22 import static androidx.test.espresso.matcher.RootMatchers.isDialog;
23 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
24 import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
25 import static androidx.test.espresso.matcher.ViewMatchers.withId;
26 import static androidx.test.espresso.matcher.ViewMatchers.withText;
27 
28 import static org.hamcrest.Matchers.not;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotEquals;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertNull;
33 import static org.junit.Assert.assertTrue;
34 
35 import android.content.Context;
36 import android.content.Intent;
37 import android.content.pm.ActivityInfo;
38 import android.graphics.Bitmap;
39 import android.graphics.Point;
40 import android.graphics.Rect;
41 import android.widget.TextView;
42 
43 import androidx.test.espresso.intent.Intents;
44 import androidx.test.ext.junit.runners.AndroidJUnit4;
45 import androidx.test.filters.MediumTest;
46 import androidx.test.platform.app.InstrumentationRegistry;
47 import androidx.test.rule.ActivityTestRule;
48 
49 import com.android.wallpaper.R;
50 import com.android.wallpaper.model.WallpaperInfo;
51 import com.android.wallpaper.module.InjectorProvider;
52 import com.android.wallpaper.module.WallpaperChangedNotifier;
53 import com.android.wallpaper.module.WallpaperPersister;
54 import com.android.wallpaper.module.logging.TestUserEventLogger;
55 import com.android.wallpaper.testing.TestAsset;
56 import com.android.wallpaper.testing.TestExploreIntentChecker;
57 import com.android.wallpaper.testing.TestInjector;
58 import com.android.wallpaper.testing.TestLiveWallpaperInfo;
59 import com.android.wallpaper.testing.TestStaticWallpaperInfo;
60 import com.android.wallpaper.testing.TestWallpaperPersister;
61 import com.android.wallpaper.testing.TestWallpaperStatusChecker;
62 import com.android.wallpaper.util.ScreenSizeCalculator;
63 import com.android.wallpaper.util.WallpaperCropUtils;
64 
65 import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
66 
67 import dagger.hilt.android.testing.HiltAndroidRule;
68 import dagger.hilt.android.testing.HiltAndroidTest;
69 
70 import org.junit.After;
71 import org.junit.Before;
72 import org.junit.Ignore;
73 import org.junit.Rule;
74 import org.junit.Test;
75 import org.junit.rules.RuleChain;
76 import org.junit.runner.RunWith;
77 
78 import java.util.ArrayList;
79 import java.util.List;
80 
81 import javax.inject.Inject;
82 
83 /**
84  * Tests for {@link PreviewActivity}.
85  */
86 @HiltAndroidTest
87 @RunWith(AndroidJUnit4.class)
88 @MediumTest
89 public class PreviewActivityTest {
90     private static final float FLOAT_ERROR_MARGIN = 0.001f;
91     private static final String ACTION_URL = "http://google.com";
92 
93     private TestStaticWallpaperInfo mTestStaticWallpaper;
94     private TestLiveWallpaperInfo mTestLiveWallpaper;
95     private TestWallpaperPersister mWallpaperPersister;
96     private TestUserEventLogger mEventLogger;
97     private TestExploreIntentChecker mExploreIntentChecker;
98     private TestWallpaperStatusChecker mWallpaperStatusChecker;
99 
100     private final HiltAndroidRule mHiltRule = new HiltAndroidRule(this);
101     private final ActivityTestRule<PreviewActivity> mActivityRule =
102             new ActivityTestRule<>(PreviewActivity.class, false, false);
103     @Rule
104     public RuleChain rules = RuleChain.outerRule(mHiltRule).around(mActivityRule);
105 
106     @Inject TestInjector mInjector;
107 
108     @Before
setUp()109     public void setUp() {
110         mHiltRule.inject();
111         InjectorProvider.setInjector(mInjector);
112 
113         Intents.init();
114 
115         List<String> attributions = new ArrayList<>();
116         attributions.add("Title");
117         attributions.add("Subtitle 1");
118         attributions.add("Subtitle 2");
119         mTestStaticWallpaper = new TestStaticWallpaperInfo(TestStaticWallpaperInfo.COLOR_DEFAULT);
120         mTestStaticWallpaper.setAttributions(attributions);
121         mTestStaticWallpaper.setCollectionId("collectionStatic");
122         mTestStaticWallpaper.setWallpaperId("wallpaperStatic");
123         mTestStaticWallpaper.setActionUrl(ACTION_URL);
124 
125         mTestLiveWallpaper = new TestLiveWallpaperInfo(TestStaticWallpaperInfo.COLOR_DEFAULT);
126         mTestLiveWallpaper.setAttributions(attributions);
127         mTestLiveWallpaper.setCollectionId("collectionLive");
128         mTestLiveWallpaper.setWallpaperId("wallpaperLive");
129         mTestLiveWallpaper.setActionUrl(ACTION_URL);
130 
131         Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
132         mWallpaperPersister = (TestWallpaperPersister) mInjector.getWallpaperPersister(context);
133         mEventLogger = (TestUserEventLogger) mInjector.getUserEventLogger();
134         mExploreIntentChecker = (TestExploreIntentChecker)
135                 mInjector.getExploreIntentChecker(context);
136         mWallpaperStatusChecker = (TestWallpaperStatusChecker)
137                 mInjector.getWallpaperStatusChecker(context);
138     }
139 
140     @After
tearDown()141     public void tearDown() {
142         Intents.release();
143         mActivityRule.finishActivity();
144     }
145 
launchActivityIntentWithWallpaper(WallpaperInfo wallpaperInfo)146     private void launchActivityIntentWithWallpaper(WallpaperInfo wallpaperInfo) {
147         Intent intent = PreviewActivity.newIntent(
148                 InstrumentationRegistry.getInstrumentation().getTargetContext(),
149                 wallpaperInfo, /* viewAsHome= */ false, /* isAssetIdPresent= */ true);
150 
151         mActivityRule.launchActivity(intent);
152     }
153 
finishSettingWallpaperThenDo(Runnable runnable)154     private void finishSettingWallpaperThenDo(Runnable runnable) {
155         final WallpaperChangedNotifier wallpaperChangedNotifier =
156                 WallpaperChangedNotifier.getInstance();
157         WallpaperChangedNotifier.Listener listener = new WallpaperChangedNotifier.Listener() {
158             @Override
159             public void onWallpaperChanged() {
160                 wallpaperChangedNotifier.unregisterListener(this);
161                 runnable.run();
162             }
163         };
164         wallpaperChangedNotifier.registerListener(listener);
165 
166         try {
167             mActivityRule.runOnUiThread(() -> mWallpaperPersister.finishSettingWallpaper());
168         } catch (Throwable throwable) {
169             throwable.printStackTrace();
170         }
171     }
172 
getFullResImageView(PreviewActivity activity)173     private SubsamplingScaleImageView getFullResImageView(PreviewActivity activity) {
174         PreviewFragment fragment =
175                 (PreviewFragment) activity.getSupportFragmentManager().findFragmentById(
176                         R.id.fragment_container);
177         if (fragment instanceof ImagePreviewFragment) {
178             return ((ImagePreviewFragment) fragment).getFullResImageView();
179         } else {
180             return null;
181         }
182     }
183 
184     @Test
testRendersWallpaperDrawableFromIntent()185     public void testRendersWallpaperDrawableFromIntent() {
186         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
187         assertTrue(getFullResImageView(mActivityRule.getActivity()).hasImage());
188     }
189 
190     @Test
testClickSetWallpaper_Success_HomeScreen()191     public void testClickSetWallpaper_Success_HomeScreen() {
192         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
193         assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
194 
195         onView(withId(R.id.button_set_wallpaper)).perform(click());
196 
197         // Destination dialog is shown; click "Home screen".
198         onView(withText(R.string.set_wallpaper_home_screen_destination)).perform(click());
199 
200         assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
201 
202         finishSettingWallpaperThenDo(() -> {
203             // Mock system wallpaper bitmap should be equal to the mock WallpaperInfo's bitmap.
204             Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
205             Bitmap srcBitmap = ((TestAsset) mTestStaticWallpaper.getAsset(context)).getBitmap();
206             assertTrue(srcBitmap.sameAs(mWallpaperPersister.getCurrentHomeWallpaper()));
207 
208             // The wallpaper should have been set on the home screen.
209             assertEquals(WallpaperPersister.DEST_HOME_SCREEN,
210                     mWallpaperPersister.getLastDestination());
211             assertEquals(1, mEventLogger.getNumWallpaperSetEvents());
212 
213             assertEquals(1, mEventLogger.getNumWallpaperSetResultEvents());
214         });
215     }
216 
217     @Test
testClickSetWallpaper_Success_LockScreen()218     public void testClickSetWallpaper_Success_LockScreen() {
219         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
220         assertNull(mWallpaperPersister.getCurrentLockWallpaper());
221 
222         onView(withId(R.id.button_set_wallpaper)).perform(click());
223 
224         // Destination dialog is shown; click "Lock screen."
225         onView(withText(R.string.set_wallpaper_lock_screen_destination)).perform(click());
226 
227         assertNull(mWallpaperPersister.getCurrentLockWallpaper());
228 
229         finishSettingWallpaperThenDo(() -> {
230             // Mock system wallpaper bitmap should be equal to the mock WallpaperInfo's bitmap.
231             Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
232             Bitmap srcBitmap = ((TestAsset) mTestStaticWallpaper.getAsset(context)).getBitmap();
233             assertTrue(srcBitmap.sameAs(mWallpaperPersister.getCurrentLockWallpaper()));
234 
235             // The wallpaper should have been set on the lock screen.
236             assertEquals(WallpaperPersister.DEST_LOCK_SCREEN,
237                     mWallpaperPersister.getLastDestination());
238             assertEquals(1, mEventLogger.getNumWallpaperSetEvents());
239 
240             assertEquals(1, mEventLogger.getNumWallpaperSetResultEvents());
241         });
242     }
243 
244     @Test
testClickSetWallpaper_Success_BothHomeAndLockScreen()245     public void testClickSetWallpaper_Success_BothHomeAndLockScreen() {
246         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
247         assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
248         assertNull(mWallpaperPersister.getCurrentLockWallpaper());
249 
250         onView(withId(R.id.button_set_wallpaper)).perform(click());
251 
252         // Destination dialog is shown; click "Both."
253         onView(withText(R.string.set_wallpaper_both_destination)).perform(click());
254 
255         assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
256         assertNull(mWallpaperPersister.getCurrentLockWallpaper());
257 
258         finishSettingWallpaperThenDo(() -> {
259             // Mock system wallpaper bitmap should be equal to the mock WallpaperInfo's bitmap.
260             Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
261             Bitmap srcBitmap = ((TestAsset) mTestStaticWallpaper.getAsset(context)).getBitmap();
262             assertTrue(srcBitmap.sameAs(mWallpaperPersister.getCurrentHomeWallpaper()));
263             assertTrue(srcBitmap.sameAs(mWallpaperPersister.getCurrentLockWallpaper()));
264 
265             // The wallpaper should have been set on both the home and the lock screen.
266             assertEquals(WallpaperPersister.DEST_BOTH, mWallpaperPersister.getLastDestination());
267             assertEquals(1, mEventLogger.getNumWallpaperSetEvents());
268 
269             assertEquals(1, mEventLogger.getNumWallpaperSetResultEvents());
270         });
271     }
272 
273     @Test
testClickSetWallpaper_Fails_HomeScreen_ShowsErrorDialog()274     public void testClickSetWallpaper_Fails_HomeScreen_ShowsErrorDialog() {
275         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
276         assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
277 
278         mWallpaperPersister.setFailNextCall(true);
279 
280         onView(withId(R.id.button_set_wallpaper)).perform(click());
281 
282         // Destination dialog is shown; click "Home screen."
283         onView(withText(R.string.set_wallpaper_home_screen_destination)).perform(click());
284 
285         finishSettingWallpaperThenDo(() -> {
286             assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
287             onView(withText(R.string.set_wallpaper_error_message)).check(matches(isDisplayed()));
288 
289             assertEquals(1, mEventLogger.getNumWallpaperSetResultEvents());
290 
291             // Set next call to succeed and current wallpaper bitmap should not be null and
292             // equals to the mock wallpaper bitmap after clicking "try again".
293             mWallpaperPersister.setFailNextCall(false);
294 
295             onView(withText(R.string.try_again)).perform(click());
296             finishSettingWallpaperThenDo(() -> {
297                 assertNotNull(mWallpaperPersister.getCurrentHomeWallpaper());
298                 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
299                 Bitmap srcBitmap = ((TestAsset) mTestStaticWallpaper.getAsset(context)).getBitmap();
300                 assertTrue(srcBitmap.sameAs(mWallpaperPersister.getCurrentHomeWallpaper()));
301 
302                 // The wallpaper should have been set on the home screen.
303                 assertEquals(WallpaperPersister.DEST_HOME_SCREEN,
304                         mWallpaperPersister.getLastDestination());
305             });
306         });
307     }
308 
309     @Test
testClickSetWallpaper_Fails_LockScreen_ShowsErrorDialog()310     public void testClickSetWallpaper_Fails_LockScreen_ShowsErrorDialog() {
311         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
312         assertNull(mWallpaperPersister.getCurrentLockWallpaper());
313 
314         mWallpaperPersister.setFailNextCall(true);
315 
316         onView(withId(R.id.button_set_wallpaper)).perform(click());
317 
318         // Destination dialog is shown; click "Lock screen."
319         onView(withText(R.string.set_wallpaper_lock_screen_destination)).perform(click());
320 
321         finishSettingWallpaperThenDo(() -> {
322             assertNull(mWallpaperPersister.getCurrentLockWallpaper());
323 
324             onView(withText(R.string.set_wallpaper_error_message)).check(
325                     matches(isDisplayed()));
326 
327             assertEquals(1, mEventLogger.getNumWallpaperSetResultEvents());
328 
329             // Set next call to succeed and current wallpaper bitmap should not be
330             // null and equals to the mock wallpaper bitmap after clicking "try again".
331             mWallpaperPersister.setFailNextCall(false);
332 
333             onView(withText(R.string.try_again)).perform(click());
334 
335             finishSettingWallpaperThenDo(() -> {
336                 Bitmap newBitmap = mWallpaperPersister.getCurrentLockWallpaper();
337                 assertNotNull(newBitmap);
338                 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
339                 Bitmap srcBitmap = ((TestAsset) mTestStaticWallpaper.getAsset(context)).getBitmap();
340                 assertTrue(srcBitmap.sameAs(newBitmap));
341 
342                 // The wallpaper should have been set on the lock screen.
343                 assertEquals(WallpaperPersister.DEST_LOCK_SCREEN,
344                         mWallpaperPersister.getLastDestination());
345             });
346         });
347     }
348 
349     @Test
testClickSetWallpaper_Fails_BothHomeAndLock_ShowsErrorDialog()350     public void testClickSetWallpaper_Fails_BothHomeAndLock_ShowsErrorDialog() {
351         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
352         assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
353         assertNull(mWallpaperPersister.getCurrentLockWallpaper());
354 
355         mWallpaperPersister.setFailNextCall(true);
356 
357         onView(withId(R.id.button_set_wallpaper)).perform(click());
358 
359         // Destination dialog is shown; click "Both."
360         onView(withText(R.string.set_wallpaper_both_destination)).perform(click());
361 
362         finishSettingWallpaperThenDo(() -> {
363             assertNull(mWallpaperPersister.getCurrentHomeWallpaper());
364             assertNull(mWallpaperPersister.getCurrentLockWallpaper());
365             onView(withText(R.string.set_wallpaper_error_message)).check(matches(isDisplayed()));
366 
367             assertEquals(1, mEventLogger.getNumWallpaperSetResultEvents());
368 
369             // Set next call to succeed and current wallpaper bitmap should not be null and
370             // equals to the mock wallpaper bitmap after clicking "try again".
371             mWallpaperPersister.setFailNextCall(false);
372 
373             onView(withText(R.string.try_again)).perform(click());
374             finishSettingWallpaperThenDo(() -> {
375                 assertNotNull(mWallpaperPersister.getCurrentHomeWallpaper());
376                 assertNotNull(mWallpaperPersister.getCurrentLockWallpaper());
377                 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
378                 Bitmap srcBitmap = ((TestAsset) mTestStaticWallpaper.getAsset(context)).getBitmap();
379                 assertTrue(srcBitmap.sameAs(mWallpaperPersister.getCurrentHomeWallpaper()));
380                 assertTrue(srcBitmap.sameAs(mWallpaperPersister.getCurrentLockWallpaper()));
381 
382                 // The wallpaper should have been set on both the home screen and the lock screen.
383                 assertEquals(WallpaperPersister.DEST_BOTH,
384                         mWallpaperPersister.getLastDestination());
385             });
386         });
387     }
388 
389     @Test
390     @Ignore("b/248538709")
testClickSetWallpaper_CropsAndScalesWallpaper()391     public void testClickSetWallpaper_CropsAndScalesWallpaper() {
392         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
393         // Scale should not have a meaningful value before clicking "set wallpaper".
394         assertTrue(mWallpaperPersister.getScale() < 0);
395 
396         onView(withId(R.id.button_set_wallpaper)).perform(click());
397 
398         // Destination dialog is shown; click "Home screen".
399         onView(withText(R.string.set_wallpaper_home_screen_destination)).perform(click());
400 
401         // WallpaperPersister's scale should match the ScaleImageView's scale.
402         SubsamplingScaleImageView imageView = getFullResImageView(mActivityRule.getActivity());
403         assertNotNull("R.id.full_res_image not found", imageView);
404 
405         float zoom = imageView.getScale();
406         assertEquals(mWallpaperPersister.getScale(), zoom, FLOAT_ERROR_MARGIN);
407 
408         Point screenSize = ScreenSizeCalculator.getInstance().getScreenSize(
409                 mActivityRule.getActivity().getWindowManager().getDefaultDisplay());
410         int maxDim = Math.max(screenSize.x, screenSize.y);
411         Rect cropRect = mWallpaperPersister.getCropRect();
412 
413         // Crop rect should be greater or equal than screen size in both directions.
414         assertTrue(cropRect.width() >= maxDim);
415         assertTrue(cropRect.height() >= maxDim);
416     }
417 
418     @Test
testClickSetWallpaper_FailsCroppingAndScalingWallpaper_ShowsErrorDialog()419     public void testClickSetWallpaper_FailsCroppingAndScalingWallpaper_ShowsErrorDialog() {
420         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
421         mWallpaperPersister.setFailNextCall(true);
422         onView(withId(R.id.button_set_wallpaper)).perform(click());
423         // Destination dialog is shown; click "Home screen".
424         onView(withText(R.string.set_wallpaper_home_screen_destination)).perform(click());
425 
426         finishSettingWallpaperThenDo(() ->
427                 onView(withText(R.string.set_wallpaper_error_message)).check(matches(isDisplayed()))
428         );
429     }
430 
431     /**
432      * Tests that tapping Set Wallpaper shows the destination dialog (i.e., choosing
433      * between Home screen, Lock screen, or Both).
434      */
435     @Test
testClickSetWallpaper_ShowsDestinationDialog()436     public void testClickSetWallpaper_ShowsDestinationDialog() {
437         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
438         onView(withId(R.id.button_set_wallpaper)).perform(click());
439         onView(withText(R.string.set_wallpaper_dialog_message)).check(matches(isDisplayed()));
440     }
441 
442     @Test
testDestinationOptions_multiEngine_setLive_showsLockOption()443     public void testDestinationOptions_multiEngine_setLive_showsLockOption() {
444         launchActivityIntentWithWallpaper(mTestLiveWallpaper);
445         mWallpaperStatusChecker.setHomeStaticWallpaperSet(true);
446         mWallpaperStatusChecker.setLockWallpaperSet(false);
447 
448         onView(withId(R.id.button_set_wallpaper)).perform(click());
449 
450         onView(withText(R.string.set_wallpaper_lock_screen_destination)).inRoot(isDialog())
451                 .check(matches(isDisplayed()));
452     }
453 
454     @Test
455     @Ignore("b/248538709")
testSetsDefaultWallpaperZoomAndScroll()456     public void testSetsDefaultWallpaperZoomAndScroll() {
457         float expectedWallpaperZoom;
458         int expectedWallpaperScrollX;
459         int expectedWallpaperScrollY;
460 
461         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
462         PreviewActivity activity = mActivityRule.getActivity();
463         SubsamplingScaleImageView fullResImageView = getFullResImageView(activity);
464 
465         Point defaultCropSurfaceSize = WallpaperCropUtils.getDefaultCropSurfaceSize(
466                 activity.getResources(), activity.getWindowManager().getDefaultDisplay());
467         Point screenSize = ScreenSizeCalculator.getInstance().getScreenSize(
468                 activity.getWindowManager().getDefaultDisplay());
469         TestAsset asset = (TestAsset) mTestStaticWallpaper.getAsset(activity);
470         Point wallpaperSize = new Point(asset.getBitmap().getWidth(),
471                 asset.getBitmap().getHeight());
472 
473         expectedWallpaperZoom = WallpaperCropUtils.calculateMinZoom(
474                 wallpaperSize, defaultCropSurfaceSize);
475 
476         // Current zoom should match the minimum zoom required to fit wallpaper
477         // completely on the crop surface.
478         assertEquals(expectedWallpaperZoom, fullResImageView.getScale(), FLOAT_ERROR_MARGIN);
479 
480         Point scaledWallpaperSize = new Point(
481                 (int) (wallpaperSize.x * expectedWallpaperZoom),
482                 (int) (wallpaperSize.y * expectedWallpaperZoom));
483         Point cropSurfaceToScreen = WallpaperCropUtils.calculateCenterPosition(
484                 defaultCropSurfaceSize, screenSize, true /* alignStart */, false /* isRtl */);
485         Point wallpaperToCropSurface = WallpaperCropUtils.calculateCenterPosition(
486                 scaledWallpaperSize, defaultCropSurfaceSize, false /* alignStart */,
487                 false /* isRtl */);
488 
489         expectedWallpaperScrollX = wallpaperToCropSurface.x + cropSurfaceToScreen.x;
490         expectedWallpaperScrollY = wallpaperToCropSurface.y + cropSurfaceToScreen.y;
491 
492         // ZoomView should be scrolled in X and Y directions such that the crop surface is centered
493         // relative to the wallpaper and the screen is centered (and aligned left) relative to the
494         // crop surface.
495         assertEquals(expectedWallpaperScrollX, fullResImageView.getScrollX());
496         assertEquals(expectedWallpaperScrollY, fullResImageView.getScrollY());
497     }
498 
499     @Test
testShowSetWallpaperDialog_TemporarilyLocksScreenOrientation()500     public void testShowSetWallpaperDialog_TemporarilyLocksScreenOrientation() {
501         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
502         PreviewActivity activity = mActivityRule.getActivity();
503         assertNotEquals(ActivityInfo.SCREEN_ORIENTATION_LOCKED, activity.getRequestedOrientation());
504 
505         // Show SetWallpaperDialog.
506         onView(withId(R.id.button_set_wallpaper)).perform(click());
507 
508         assertEquals(ActivityInfo.SCREEN_ORIENTATION_LOCKED, activity.getRequestedOrientation());
509 
510         // Press back to dismiss the dialog.
511         onView(isRoot()).perform(pressBack());
512 
513         assertNotEquals(ActivityInfo.SCREEN_ORIENTATION_LOCKED, activity.getRequestedOrientation());
514     }
515 
516     @Test
testSetWallpaper_TemporarilyLocksScreenOrientation()517     public void testSetWallpaper_TemporarilyLocksScreenOrientation() {
518         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
519         PreviewActivity activity = mActivityRule.getActivity();
520         assertNotEquals(ActivityInfo.SCREEN_ORIENTATION_LOCKED, activity.getRequestedOrientation());
521 
522         // Show SetWallpaperDialog.
523         onView(withId(R.id.button_set_wallpaper)).perform(click());
524 
525         // Destination dialog is shown; click "Home screen".
526         onView(withText(R.string.set_wallpaper_home_screen_destination)).perform(click());
527 
528         assertEquals(ActivityInfo.SCREEN_ORIENTATION_LOCKED, activity.getRequestedOrientation());
529 
530         // Finish setting the wallpaper to check that the screen orientation is no longer locked.
531         finishSettingWallpaperThenDo(() -> assertNotEquals(ActivityInfo.SCREEN_ORIENTATION_LOCKED,
532                 activity.getRequestedOrientation()));
533     }
534 
535     @Test
testShowsWallpaperAttribution()536     public void testShowsWallpaperAttribution() {
537         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
538         PreviewActivity activity = mActivityRule.getActivity();
539 
540         TextView titleView = activity.findViewById(R.id.wallpaper_info_title);
541         assertEquals("Title", titleView.getText());
542 
543         TextView subtitle1View = activity.findViewById(R.id.wallpaper_info_subtitle1);
544         assertEquals("Subtitle 1", subtitle1View.getText());
545 
546         TextView subtitle2View = activity.findViewById(R.id.wallpaper_info_subtitle2);
547         assertEquals("Subtitle 2", subtitle2View.getText());
548     }
549 
550     /**
551      * Tests that if there was a failure decoding the wallpaper bitmap, then the activity shows an
552      * informative error dialog with an "OK" button, when clicked finishes the activity.
553      */
554     @Test
testLoadWallpaper_Failed_ShowsErrorDialog()555     public void testLoadWallpaper_Failed_ShowsErrorDialog() {
556         // Simulate a corrupted asset that fails to perform decoding operations.
557         mTestStaticWallpaper.corruptAssets();
558         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
559 
560         onView(withText(R.string.load_wallpaper_error_message)).inRoot(isDialog()).check(
561                 matches(isDisplayed()));
562 
563         onView(withText(android.R.string.ok)).perform(click());
564 
565         assertTrue(mActivityRule.getActivity().isFinishing());
566     }
567 
568     /**
569      * Tests that the explore button is not visible, even if there is an action URL present, if
570      * there is no activity on the device which can handle such an explore action.
571      */
572     @Test
testNoActionViewHandler_ExploreButtonNotVisible()573     public void testNoActionViewHandler_ExploreButtonNotVisible() {
574         mExploreIntentChecker.setViewHandlerExists(false);
575 
576         launchActivityIntentWithWallpaper(mTestStaticWallpaper);
577         onView(withId(R.id.wallpaper_info_explore_button)).check(
578                 matches(not(isDisplayed())));
579     }
580 }
581