1 package com.android.customization.picker; 2 3 import static android.content.Intent.ACTION_SET_WALLPAPER; 4 5 import static androidx.test.espresso.Espresso.onView; 6 import static androidx.test.espresso.action.ViewActions.click; 7 import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist; 8 import static androidx.test.espresso.assertion.ViewAssertions.matches; 9 import static androidx.test.espresso.intent.matcher.IntentMatchers.filterEquals; 10 import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed; 11 import static androidx.test.espresso.matcher.ViewMatchers.isSelected; 12 import static androidx.test.espresso.matcher.ViewMatchers.withId; 13 14 import static com.android.customization.picker.CustomizationPickerActivity.WALLPAPER_FLAVOR_EXTRA; 15 import static com.android.customization.picker.CustomizationPickerActivity.WALLPAPER_FOCUS; 16 import static com.android.customization.picker.CustomizationPickerActivity.WALLPAPER_ONLY; 17 18 import static org.junit.Assert.assertTrue; 19 20 import android.content.Intent; 21 import android.text.TextUtils; 22 23 import androidx.fragment.app.Fragment; 24 import androidx.test.espresso.intent.Intents; 25 import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; 26 import androidx.test.rule.ActivityTestRule; 27 28 import com.android.customization.picker.theme.ThemeFragment; 29 import com.android.customization.testing.TestCustomizationInjector; 30 import com.android.customization.testing.TestThemeManager; 31 import com.android.wallpaper.R; 32 import com.android.wallpaper.module.InjectorProvider; 33 import com.android.wallpaper.picker.CategoryFragment; 34 import com.android.wallpaper.picker.TopLevelPickerActivity; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 /** 43 * Tests for {@link CustomizationPickerActivity}. 44 */ 45 @RunWith(AndroidJUnit4ClassRunner.class) 46 public class CustomizationPickerActivityTest { 47 48 @Rule 49 public ActivityTestRule<CustomizationPickerActivity> mActivityRule = 50 new ActivityTestRule<>(CustomizationPickerActivity.class, false, false); 51 52 @Before setUp()53 public void setUp() { 54 Intents.init(); 55 } 56 57 @After tearDown()58 public void tearDown() { 59 mActivityRule.finishActivity(); 60 Intents.release(); 61 } 62 63 @Test launchActivity_doesNotSupportCustomization_launchesTopLevelPickerActivity()64 public void launchActivity_doesNotSupportCustomization_launchesTopLevelPickerActivity() { 65 TestThemeManager.setAvailable(false); 66 67 launchActivity(); 68 69 Intents.intended(filterEquals(new Intent(mActivityRule.getActivity(), 70 TopLevelPickerActivity.class))); 71 } 72 73 // ---------------------------------------------------------------------- 74 // Tests for customization supports. 75 // ---------------------------------------------------------------------- 76 77 @Test launchActivity_themeManagerIsAvailable_NavBarShowsStyleAndWallpaperItem()78 public void launchActivity_themeManagerIsAvailable_NavBarShowsStyleAndWallpaperItem() { 79 TestThemeManager.setAvailable(true); 80 81 launchActivity(); 82 83 onView(withId(R.id.nav_theme)).check(matches(isCompletelyDisplayed())); 84 onView(withId(R.id.nav_wallpaper)).check(matches(isCompletelyDisplayed())); 85 onView(withId(R.id.nav_clock)).check(doesNotExist()); 86 onView(withId(R.id.nav_grid)).check(doesNotExist()); 87 } 88 89 @Test launchActivity_navigateToTheStyleTabByDefault()90 public void launchActivity_navigateToTheStyleTabByDefault() { 91 TestThemeManager.setAvailable(true); 92 93 launchActivity(); 94 95 onView(withId(R.id.nav_theme)).check(matches(isSelected())); 96 assertTrue(getCurrentShowingFragment() instanceof ThemeFragment); 97 } 98 99 @Test launchActivity_withExtraWallpaperOnly_launchesTopLevelPickerActivity()100 public void launchActivity_withExtraWallpaperOnly_launchesTopLevelPickerActivity() { 101 TestThemeManager.setAvailable(true); 102 103 launchActivityWithFlavorExtra(WALLPAPER_ONLY); 104 105 // While receiving WALLPAPER_ONLY extra, it should launch TopLevelPickerActivity whatever it 106 // supports customization. 107 Intents.intended(filterEquals(new Intent(mActivityRule.getActivity(), 108 TopLevelPickerActivity.class))); 109 } 110 111 @Test clickStyleButton_showsThemeFragment()112 public void clickStyleButton_showsThemeFragment() { 113 TestThemeManager.setAvailable(true); 114 launchActivity(); 115 116 onView(withId(R.id.nav_theme)).perform(click()); 117 118 onView(withId(R.id.nav_theme)).check(matches(isSelected())); 119 assertTrue(getCurrentShowingFragment() instanceof ThemeFragment); 120 } 121 122 @Test clickWallpaperButton_showsCategoryFragment()123 public void clickWallpaperButton_showsCategoryFragment() { 124 TestThemeManager.setAvailable(true); 125 launchActivity(); 126 127 onView(withId(R.id.nav_wallpaper)).perform(click()); 128 129 onView(withId(R.id.nav_wallpaper)).check(matches(isSelected())); 130 assertTrue(getCurrentShowingFragment() instanceof CategoryFragment); 131 } 132 133 @Test launchActivity_withExtraWallpaperFocus_navigateToTheWallpaperTab()134 public void launchActivity_withExtraWallpaperFocus_navigateToTheWallpaperTab() { 135 TestThemeManager.setAvailable(true); 136 137 launchActivityWithFlavorExtra(WALLPAPER_FOCUS); 138 139 onView(withId(R.id.nav_wallpaper)).check(matches(isSelected())); 140 assertTrue(getCurrentShowingFragment() instanceof CategoryFragment); 141 } 142 launchActivity()143 private void launchActivity() { 144 launchActivityWithFlavorExtra(""); 145 } 146 launchActivityWithFlavorExtra(String extra)147 private void launchActivityWithFlavorExtra(String extra) { 148 InjectorProvider.setInjector(new TestCustomizationInjector()); 149 Intent intent = new Intent(ACTION_SET_WALLPAPER); 150 if (!TextUtils.isEmpty(extra)) { 151 intent.putExtra(WALLPAPER_FLAVOR_EXTRA, extra); 152 } 153 mActivityRule.launchActivity(intent); 154 } 155 getCurrentShowingFragment()156 private Fragment getCurrentShowingFragment() { 157 return mActivityRule.getActivity().getSupportFragmentManager() 158 .findFragmentById(R.id.fragment_container); 159 } 160 } 161