1 /* 2 * Copyright (C) 2018 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 org.hamcrest.CoreMatchers.instanceOf; 20 import static org.hamcrest.CoreMatchers.not; 21 import static org.hamcrest.Matchers.allOf; 22 import static org.hamcrest.collection.IsIn.oneOf; 23 24 import static android.support.test.espresso.Espresso.onView; 25 import static android.support.test.espresso.assertion.ViewAssertions.matches; 26 import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; 27 import static android.support.test.espresso.matcher.ViewMatchers.isChecked; 28 import static android.support.test.espresso.matcher.ViewMatchers.isNotChecked; 29 import static android.support.test.espresso.matcher.ViewMatchers.withText; 30 import static android.support.test.espresso.matcher.ViewMatchers.withParent; 31 32 import android.app.Activity; 33 import android.app.Instrumentation; 34 import android.content.Context; 35 import android.os.Bundle; 36 import android.provider.Settings; 37 import android.support.test.InstrumentationRegistry; 38 import android.support.test.rule.ActivityTestRule; 39 import android.support.test.runner.AndroidJUnit4; 40 import android.widget.CompoundButton; 41 42 import com.android.settings.R; 43 import com.android.settings.Settings.AccessibilitySettingsActivity; 44 import com.android.settings.core.InstrumentedPreferenceFragment; 45 import com.android.settings.core.SubSettingLauncher; 46 47 import org.hamcrest.Matcher; 48 import org.junit.Before; 49 import org.junit.Rule; 50 import org.junit.Test; 51 import org.junit.runner.RunWith; 52 53 @RunWith(AndroidJUnit4.class) 54 public class AccessibilityShortcutPreferenceFragmentTest { 55 @Rule 56 public final ActivityTestRule<AccessibilitySettingsActivity> mActivityRule = 57 new ActivityTestRule<>(AccessibilitySettingsActivity.class, true); 58 59 private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation(); 60 private AccessibilityShortcutPreferenceFragment mAccessibilityShortcutPreferenceFragment; 61 private AccessibilitySettingsActivity mActivity; 62 63 @Before 64 public void setUp() { 65 mActivity = mActivityRule.getActivity(); 66 } 67 68 @Test 69 public void lockScreenPreference_defaultBeforeDialogShown_isOff() { 70 setDialogShown(false); 71 setOnLockscreen(null); 72 startFragment(); 73 assertLockscreenSwitchIsCheckedIs(false); 74 } 75 76 @Test 77 public void lockScreenPreference_setOnBeforeDialogShown_isOn() { 78 setDialogShown(false); 79 setOnLockscreen(true); 80 startFragment(); 81 assertLockscreenSwitchIsCheckedIs(true); 82 } 83 84 @Test 85 public void lockScreenPreference_defaultAfterDialogShown_isOn() { 86 setDialogShown(true); 87 setOnLockscreen(null); 88 startFragment(); 89 assertLockscreenSwitchIsCheckedIs(true); 90 } 91 92 @Test 93 public void lockScreenPreference_setOffAfterDialogShown_isOn() { 94 setDialogShown(true); 95 setOnLockscreen(false); 96 startFragment(); 97 assertLockscreenSwitchIsCheckedIs(false); 98 } 99 100 private void startFragment() { 101 mInstrumentation.runOnMainSync(() -> { 102 new SubSettingLauncher(mActivity) 103 .setDestination(AccessibilityShortcutPreferenceFragment.class.getName()) 104 .setArguments(new Bundle()) 105 .setSourceMetricsCategory( 106 InstrumentedPreferenceFragment.METRICS_CATEGORY_UNKNOWN) 107 .launch(); 108 }); 109 } 110 111 private void setDialogShown(boolean shown) { 112 Settings.Secure.putInt(mActivity.getContentResolver(), 113 Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, shown ? 1 : 0); 114 } 115 116 private void setOnLockscreen(Boolean onLockscreen) { 117 if (onLockscreen == null) { 118 Settings.Secure.putString(mActivity.getContentResolver(), 119 Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, null); 120 } else { 121 Settings.Secure.putInt(mActivity.getContentResolver(), 122 Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, onLockscreen ? 1 : 0); 123 } 124 } 125 126 private void assertLockscreenSwitchIsCheckedIs(boolean isChecked) { 127 // Identify the switch by looking for a grandparent that has a descendent with the 128 // switch label. To disambiguate, make sure that grandparent doesn't also have a descendant 129 // with the title of the main switch 130 final String lockScreenSwitchTitle = 131 mActivity.getString(R.string.accessibility_shortcut_service_on_lock_screen_title); 132 final String mainSwitchTitle = 133 mActivity.getString(R.string.accessibility_service_master_switch_title); 134 Matcher isCheckedMatcher = (isChecked) ? isChecked() : isNotChecked(); 135 Matcher hasLockScreenTitleDescendant = hasDescendant(withText(lockScreenSwitchTitle)); 136 Matcher noMainSwitchTitleDescendant = not(hasDescendant(withText(mainSwitchTitle))); 137 onView(allOf(withParent(withParent(allOf( 138 hasLockScreenTitleDescendant, noMainSwitchTitleDescendant))), 139 instanceOf(CompoundButton.class))).check(matches(isCheckedMatcher)); 140 } 141 } 142