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