1 /* 2 * Copyright (C) 2021 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 17 package com.android.launcher3.settings; 18 19 import static androidx.preference.PreferenceFragmentCompat.ARG_PREFERENCE_ROOT; 20 import static androidx.test.espresso.Espresso.onView; 21 import static androidx.test.espresso.action.ViewActions.click; 22 import static androidx.test.espresso.assertion.ViewAssertions.matches; 23 import static androidx.test.espresso.contrib.RecyclerViewActions.actionOnItem; 24 import static androidx.test.espresso.intent.Intents.intended; 25 import static androidx.test.espresso.intent.matcher.BundleMatchers.hasEntry; 26 import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent; 27 import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra; 28 import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant; 29 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; 30 import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription; 31 import static androidx.test.espresso.matcher.ViewMatchers.withId; 32 import static androidx.test.espresso.matcher.ViewMatchers.withText; 33 34 import static com.android.launcher3.settings.SettingsActivity.EXTRA_FRAGMENT; 35 import static com.android.launcher3.settings.SettingsActivity.EXTRA_FRAGMENT_ARGS; 36 37 import static com.google.common.truth.Truth.assertThat; 38 39 import static org.hamcrest.Matchers.allOf; 40 import static org.hamcrest.Matchers.equalTo; 41 42 import android.content.Context; 43 import android.content.Intent; 44 import android.os.Bundle; 45 46 import androidx.preference.PreferenceFragmentCompat; 47 import androidx.test.core.app.ActivityScenario; 48 import androidx.test.core.app.ApplicationProvider; 49 import androidx.test.espresso.intent.Intents; 50 import androidx.test.ext.junit.runners.AndroidJUnit4; 51 52 import com.android.launcher3.R; 53 import com.android.launcher3.uioverrides.flags.DeveloperOptionsFragment; 54 import com.android.systemui.shared.plugins.PluginPrefs; 55 56 import org.junit.After; 57 import org.junit.Assert; 58 import org.junit.Before; 59 import org.junit.Ignore; 60 import org.junit.Test; 61 import org.junit.runner.RunWith; 62 63 @RunWith(AndroidJUnit4.class) 64 public class SettingsActivityTest { 65 66 private Context mApplicationContext; 67 68 @Before setUp()69 public void setUp() { 70 mApplicationContext = ApplicationProvider.getApplicationContext(); 71 Intents.init(); 72 } 73 74 @After tearDown()75 public void tearDown() { 76 Intents.release(); 77 } 78 79 @Test 80 @Ignore // b/199309785 testSettings_aboutTap_launchesActivity()81 public void testSettings_aboutTap_launchesActivity() { 82 ActivityScenario.launch(SettingsActivity.class); 83 onView(withId(R.id.recycler_view)).perform( 84 actionOnItem(hasDescendant(withText("About")), click())); 85 86 intended(allOf( 87 hasComponent(SettingsActivity.class.getName()), 88 hasExtra( 89 equalTo(EXTRA_FRAGMENT_ARGS), 90 hasEntry(ARG_PREFERENCE_ROOT, "about_screen")))); 91 } 92 93 @Test 94 @Ignore // b/199309785 testSettings_developerOptionsTap_launchesActivityWithFragment()95 public void testSettings_developerOptionsTap_launchesActivityWithFragment() { 96 PluginPrefs.setHasPlugins(mApplicationContext); 97 ActivityScenario.launch(SettingsActivity.class); 98 onView(withId(R.id.recycler_view)).perform( 99 actionOnItem(hasDescendant(withText("Developer Options")), click())); 100 101 intended(allOf( 102 hasComponent(SettingsActivity.class.getName()), 103 hasExtra(EXTRA_FRAGMENT, DeveloperOptionsFragment.class.getName()))); 104 } 105 106 @Test 107 @Ignore // b/199309785 testSettings_aboutScreenIntent()108 public void testSettings_aboutScreenIntent() { 109 Bundle fragmentArgs = new Bundle(); 110 fragmentArgs.putString(ARG_PREFERENCE_ROOT, "about_screen"); 111 112 Intent intent = new Intent(mApplicationContext, SettingsActivity.class) 113 .putExtra(EXTRA_FRAGMENT_ARGS, fragmentArgs); 114 ActivityScenario.launch(intent); 115 116 onView(withText("About")).check(matches(isDisplayed())); 117 onView(withText("Version")).check(matches(isDisplayed())); 118 onView(withContentDescription("Navigate up")).check(matches(isDisplayed())); 119 } 120 121 @Test 122 @Ignore // b/199309785 testSettings_developerOptionsFragmentIntent()123 public void testSettings_developerOptionsFragmentIntent() { 124 Intent intent = new Intent(mApplicationContext, SettingsActivity.class) 125 .putExtra(EXTRA_FRAGMENT, DeveloperOptionsFragment.class.getName()); 126 ActivityScenario.launch(intent); 127 128 onView(withText("Developer Options")).check(matches(isDisplayed())); 129 onView(withId(R.id.filter_box)).check(matches(isDisplayed())); 130 onView(withContentDescription("Navigate up")).check(matches(isDisplayed())); 131 } 132 133 @Test 134 @Ignore // b/199309785 testSettings_intentWithUnknownFragment()135 public void testSettings_intentWithUnknownFragment() { 136 String fragmentClass = PreferenceFragmentCompat.class.getName(); 137 Intent intent = new Intent(mApplicationContext, SettingsActivity.class) 138 .putExtra(EXTRA_FRAGMENT, fragmentClass); 139 140 try { 141 ActivityScenario.launch(intent); 142 Assert.fail("Should have thrown an IllegalArgumentException."); 143 } catch (IllegalArgumentException e) { 144 assertThat(e.getMessage()).contains(fragmentClass); 145 } 146 } 147 148 @Test 149 @Ignore // b/199309785 testSettings_backButtonFinishesActivity()150 public void testSettings_backButtonFinishesActivity() { 151 Bundle fragmentArgs = new Bundle(); 152 fragmentArgs.putString(ARG_PREFERENCE_ROOT, "about_screen"); 153 Intent intent = new Intent(mApplicationContext, SettingsActivity.class) 154 .putExtra(EXTRA_FRAGMENT_ARGS, fragmentArgs); 155 ActivityScenario<SettingsActivity> scenario = ActivityScenario.launch(intent); 156 157 onView(withContentDescription("Navigate up")).perform(click()); 158 scenario.onActivity(activity -> assertThat(activity.isFinishing()).isTrue()); 159 } 160 } 161