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.systemui.shared.plugins.PluginPrefs;
54 
55 import org.junit.After;
56 import org.junit.Assert;
57 import org.junit.Before;
58 import org.junit.Ignore;
59 import org.junit.Test;
60 import org.junit.runner.RunWith;
61 
62 @RunWith(AndroidJUnit4.class)
63 public class SettingsActivityTest {
64 
65     private Context mApplicationContext;
66 
67     @Before
setUp()68     public void setUp() {
69         mApplicationContext = ApplicationProvider.getApplicationContext();
70         Intents.init();
71     }
72 
73     @After
tearDown()74     public void tearDown() {
75         Intents.release();
76     }
77 
78     @Test
79     @Ignore  // b/199309785
testSettings_aboutTap_launchesActivity()80     public void testSettings_aboutTap_launchesActivity() {
81         ActivityScenario.launch(SettingsActivity.class);
82         onView(withId(R.id.recycler_view)).perform(
83                 actionOnItem(hasDescendant(withText("About")), click()));
84 
85         intended(allOf(
86                 hasComponent(SettingsActivity.class.getName()),
87                 hasExtra(
88                         equalTo(EXTRA_FRAGMENT_ARGS),
89                         hasEntry(ARG_PREFERENCE_ROOT, "about_screen"))));
90     }
91 
92     @Test
93     @Ignore  // b/199309785
testSettings_developerOptionsTap_launchesActivityWithFragment()94     public void testSettings_developerOptionsTap_launchesActivityWithFragment() {
95         PluginPrefs.setHasPlugins(mApplicationContext);
96         ActivityScenario.launch(SettingsActivity.class);
97         onView(withId(R.id.recycler_view)).perform(
98                 actionOnItem(hasDescendant(withText("Developer Options")), click()));
99 
100         intended(allOf(
101                 hasComponent(SettingsActivity.class.getName()),
102                 hasExtra(EXTRA_FRAGMENT, DeveloperOptionsFragment.class.getName())));
103     }
104 
105     @Test
106     @Ignore  // b/199309785
testSettings_aboutScreenIntent()107     public void testSettings_aboutScreenIntent() {
108         Bundle fragmentArgs = new Bundle();
109         fragmentArgs.putString(ARG_PREFERENCE_ROOT, "about_screen");
110 
111         Intent intent = new Intent(mApplicationContext, SettingsActivity.class)
112                 .putExtra(EXTRA_FRAGMENT_ARGS, fragmentArgs);
113         ActivityScenario.launch(intent);
114 
115         onView(withText("About")).check(matches(isDisplayed()));
116         onView(withText("Version")).check(matches(isDisplayed()));
117         onView(withContentDescription("Navigate up")).check(matches(isDisplayed()));
118     }
119 
120     @Test
121     @Ignore  // b/199309785
testSettings_developerOptionsFragmentIntent()122     public void testSettings_developerOptionsFragmentIntent() {
123         Intent intent = new Intent(mApplicationContext, SettingsActivity.class)
124                 .putExtra(EXTRA_FRAGMENT, DeveloperOptionsFragment.class.getName());
125         ActivityScenario.launch(intent);
126 
127         onView(withText("Developer Options")).check(matches(isDisplayed()));
128         onView(withId(R.id.filter_box)).check(matches(isDisplayed()));
129         onView(withContentDescription("Navigate up")).check(matches(isDisplayed()));
130     }
131 
132     @Test
133     @Ignore  // b/199309785
testSettings_intentWithUnknownFragment()134     public void testSettings_intentWithUnknownFragment() {
135         String fragmentClass = PreferenceFragmentCompat.class.getName();
136         Intent intent = new Intent(mApplicationContext, SettingsActivity.class)
137                 .putExtra(EXTRA_FRAGMENT, fragmentClass);
138 
139         try {
140             ActivityScenario.launch(intent);
141             Assert.fail("Should have thrown an IllegalArgumentException.");
142         } catch (IllegalArgumentException e) {
143             assertThat(e.getMessage()).contains(fragmentClass);
144         }
145     }
146 
147     @Test
148     @Ignore  // b/199309785
testSettings_backButtonFinishesActivity()149     public void testSettings_backButtonFinishesActivity() {
150         Bundle fragmentArgs = new Bundle();
151         fragmentArgs.putString(ARG_PREFERENCE_ROOT, "about_screen");
152         Intent intent = new Intent(mApplicationContext, SettingsActivity.class)
153                 .putExtra(EXTRA_FRAGMENT_ARGS, fragmentArgs);
154         ActivityScenario<SettingsActivity> scenario = ActivityScenario.launch(intent);
155 
156         onView(withContentDescription("Navigate up")).perform(click());
157         scenario.onActivity(activity -> assertThat(activity.isFinishing()).isTrue());
158     }
159 }
160