1 /* 2 * Copyright (C) 2015 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 android.voicesettings.cts; 18 19 import static android.provider.Settings.ACTION_VOICE_CONTROL_DO_NOT_DISTURB_MODE; 20 import static android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_ENABLED; 21 import static android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_MINUTES; 22 23 import android.cts.util.BroadcastTestBase; 24 import android.cts.util.BroadcastUtils; 25 import android.provider.Settings; 26 import android.provider.Settings.Global; 27 import android.util.Log; 28 29 public class ZenModeTest extends BroadcastTestBase { 30 static final String TAG = "ZenModeTest"; 31 private static final String VOICE_SETTINGS_PACKAGE = "android.voicesettings.service"; 32 private static final String VOICE_INTERACTION_CLASS = 33 "android.voicesettings.service.VoiceInteractionMain"; 34 protected static final String FEATURE_VOICE_RECOGNIZERS = "android.software.voice_recognizers"; 35 36 @Override setUp()37 protected void setUp() throws Exception { 38 super.setUp(); 39 mContext = getInstrumentation().getTargetContext(); 40 mHasFeature = mContext.getPackageManager().hasSystemFeature(FEATURE_VOICE_RECOGNIZERS); 41 } 42 43 // The following are hidden in frameworks/base/core/java/android/provider/Settings.java 44 // If they weren't, we could have used them directly here. 45 private static final String ZEN_MODE = "zen_mode"; 46 private static final int ZEN_MODE_IS_OFF = 0; 47 private static final int ZEN_MODE_IS_ALARMS = 3; 48 ZenModeTest()49 public ZenModeTest() { 50 super(); 51 } 52 testAll()53 public void testAll() throws Exception { 54 if (!mHasFeature) { 55 Log.i(TAG, "The device doesn't support feature: " + FEATURE_VOICE_RECOGNIZERS); 56 return; 57 } 58 if (!isIntentSupported(ACTION_VOICE_CONTROL_DO_NOT_DISTURB_MODE)) { 59 Log.e(TAG, "Voice intent for Zen Mode NOT supported. existing the test"); 60 return; 61 } 62 int mode; 63 try { 64 mode = getMode(); 65 Log.i(TAG, "Before testing, zen-mode is set to: " + mode); 66 } catch (Settings.SettingNotFoundException e) { 67 // if the mode is not supported, don't run the test. 68 Log.i(TAG, "zen_mode is not found in Settings. Skipping ZenModeTest"); 69 return; 70 } 71 startTestActivity("ZEN_MODE"); 72 if (mode == ZEN_MODE_IS_OFF) { 73 // mode is currently OFF. 74 // run a test to turn it on. 75 // After successful run of the test, run a test to turn it back off. 76 if (!runTest(BroadcastUtils.TestcaseType.ZEN_MODE_ON, ZEN_MODE_IS_ALARMS)) { 77 // the test failed. don't test the next one. 78 return; 79 } 80 runTest(BroadcastUtils.TestcaseType.ZEN_MODE_OFF, ZEN_MODE_IS_OFF); 81 } else { 82 // mode is currently ON. 83 // run a test to turn it off. 84 // After successful run of the test, run a test to turn it back on. 85 if (!runTest(BroadcastUtils.TestcaseType.ZEN_MODE_OFF, ZEN_MODE_IS_OFF)) { 86 // the test failed. don't test the next one. 87 return; 88 } 89 runTest(BroadcastUtils.TestcaseType.ZEN_MODE_ON, ZEN_MODE_IS_ALARMS); 90 } 91 } 92 runTest(BroadcastUtils.TestcaseType test, int expectedMode)93 private boolean runTest(BroadcastUtils.TestcaseType test, int expectedMode) throws Exception { 94 if (!startTestAndWaitForBroadcast(test, VOICE_SETTINGS_PACKAGE, VOICE_INTERACTION_CLASS)) { 95 return false; 96 } 97 98 // verify the test results 99 int mode = getMode(); 100 Log.i(TAG, "After testing, zen-mode is set to: " + mode); 101 assertEquals(expectedMode, mode); 102 Log.i(TAG, "results_received: " + BroadcastUtils.toBundleString(mResultExtras)); 103 assertNotNull(mResultExtras); 104 if (expectedMode == ZEN_MODE_IS_ALARMS) { 105 assertTrue(mResultExtras.getBoolean(EXTRA_DO_NOT_DISTURB_MODE_ENABLED)); 106 assertEquals(BroadcastUtils.NUM_MINUTES_FOR_ZENMODE, 107 mResultExtras.getInt(EXTRA_DO_NOT_DISTURB_MODE_MINUTES)); 108 } else { 109 assertFalse(mResultExtras.getBoolean(EXTRA_DO_NOT_DISTURB_MODE_ENABLED)); 110 } 111 Log.i(TAG, "Successfully Tested: " + test); 112 return true; 113 } 114 getMode()115 private int getMode() throws Settings.SettingNotFoundException { 116 return Settings.Global.getInt(mContext.getContentResolver(), ZEN_MODE); 117 } 118 } 119