1 /*
2  * Copyright (C) 2017 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.settings.accessibility;
18 
19 import static androidx.test.espresso.Espresso.onView;
20 import static androidx.test.espresso.assertion.ViewAssertions.matches;
21 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
22 import static androidx.test.espresso.matcher.ViewMatchers.withText;
23 
24 import android.app.Instrumentation;
25 import android.os.Bundle;
26 import android.os.Looper;
27 
28 import androidx.test.InstrumentationRegistry;
29 import androidx.test.rule.ActivityTestRule;
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import com.android.settings.Settings.AccessibilitySettingsActivity;
33 import com.android.settings.core.InstrumentedPreferenceFragment;
34 import com.android.settings.core.SubSettingLauncher;
35 
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 @RunWith(AndroidJUnit4.class)
43 public class ToggleFeaturePreferenceFragmentTest {
44     private static final String SUMMARY_TEXT = "Here's some summary text";
45 
46     @Rule
47     public final ActivityTestRule<AccessibilitySettingsActivity> mActivityRule =
48             new ActivityTestRule<>(AccessibilitySettingsActivity.class, true);
49 
50     private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
51 
52     @BeforeClass
oneTimeSetup()53     public static void oneTimeSetup() {
54         if (Looper.myLooper() == null) {
55             Looper.prepare();
56         }
57     }
58 
59     @Before
setUp()60     public void setUp() {
61         mInstrumentation.runOnMainSync(() -> {
62             MyToggleFeaturePreferenceFragment fragment = new MyToggleFeaturePreferenceFragment();
63             Bundle args = new Bundle();
64             args.putString(AccessibilitySettings.EXTRA_SUMMARY, SUMMARY_TEXT);
65             fragment.setArguments(args);
66             new SubSettingLauncher(mActivityRule.getActivity())
67                     .setDestination(MyToggleFeaturePreferenceFragment.class.getName())
68                     .setArguments(args)
69                     .setSourceMetricsCategory(
70                             InstrumentedPreferenceFragment.METRICS_CATEGORY_UNKNOWN)
71                     .launch();
72         });
73     }
74 
75     @Test
testSummaryTestDisplayed()76     public void testSummaryTestDisplayed() {
77         onView(withText(SUMMARY_TEXT)).check(matches(isDisplayed()));
78     }
79 
80     public static class MyToggleFeaturePreferenceFragment extends ToggleFeaturePreferenceFragment {
81         @Override
onPreferenceToggled(String preferenceKey, boolean enabled)82         protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
83         }
84 
85         @Override
getMetricsCategory()86         public int getMetricsCategory() {
87             return 0;
88         }
89 
90         @Override
getUserShortcutTypes()91         int getUserShortcutTypes() {
92             return 0;
93         }
94     }
95 }