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