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.EXTRA_DO_NOT_DISTURB_MODE_ENABLED;
20 import static android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_MINUTES;
21 
22 import android.provider.Settings;
23 import android.provider.Settings.Global;
24 import android.util.Log;
25 
26 import common.src.android.voicesettings.common.Utils;
27 
28 public class ZenModeTest extends VoiceSettingsTestBase {
29     static final String TAG = "ZenModeTest";
30 
31     // The following are hidden in frameworks/base/core/java/android/provider/Settings.java
32     // If they weren't, we could have used them directly here.
33     private static final String ZEN_MODE = "zen_mode";
34     private static final int ZEN_MODE_IS_OFF = 0;
35     private static final int ZEN_MODE_IS_ALARMS = 3;
36 
ZenModeTest()37     public ZenModeTest() {
38         super();
39     }
40 
testAll()41     public void testAll() throws Exception {
42         int mode;
43         try {
44             mode = getMode();
45             Log.i(TAG, "Before testing, zen-mode is set to: " + mode);
46         } catch (Settings.SettingNotFoundException e) {
47             // if the mode is not supported, don't run the test.
48             Log.i(TAG, "zen_mode is not found in Settings. Skipping ZenModeTest");
49             return;
50         }
51         startTestActivity("ZEN_MODE");
52         if (mode == ZEN_MODE_IS_OFF) {
53             // mode is currently OFF.
54             // run a test to turn it on.
55             // After successful run of the test, run a test to turn it back off.
56             if (!runTest(Utils.TestcaseType.ZEN_MODE_ON, ZEN_MODE_IS_ALARMS)) {
57                 // the test failed. don't test the next one.
58                 return;
59             }
60             runTest(Utils.TestcaseType.ZEN_MODE_OFF, ZEN_MODE_IS_OFF);
61         } else {
62             // mode is currently ON.
63             // run a test to turn it off.
64             // After successful run of the test, run a test to turn it back on.
65             if (!runTest(Utils.TestcaseType.ZEN_MODE_OFF, ZEN_MODE_IS_OFF)) {
66                 // the test failed. don't test the next one.
67                 return;
68             }
69             runTest(Utils.TestcaseType.ZEN_MODE_ON, ZEN_MODE_IS_ALARMS);
70         }
71     }
72 
runTest(Utils.TestcaseType test, int expectedMode)73     private boolean runTest(Utils.TestcaseType test, int expectedMode) throws Exception {
74         if (!startTestAndWaitForBroadcast(test)) {
75             return false;
76         }
77 
78         // verify the test results
79         int mode = getMode();
80         Log.i(TAG, "After testing, zen-mode is set to: " + mode);
81         assertEquals(expectedMode, mode);
82         Log.i(TAG, "results_received: " + Utils.toBundleString(mResultExtras));
83         assertNotNull(mResultExtras);
84         if (expectedMode == ZEN_MODE_IS_ALARMS) {
85             assertTrue(mResultExtras.getBoolean(EXTRA_DO_NOT_DISTURB_MODE_ENABLED));
86             assertEquals(Utils.NUM_MINUTES_FOR_ZENMODE,
87                     mResultExtras.getInt(EXTRA_DO_NOT_DISTURB_MODE_MINUTES));
88         } else {
89             assertFalse(mResultExtras.getBoolean(EXTRA_DO_NOT_DISTURB_MODE_ENABLED));
90         }
91         Log.i(TAG, "Successfully Tested: " + test);
92         return true;
93     }
94 
getMode()95     private int getMode() throws Settings.SettingNotFoundException {
96         return Settings.Global.getInt(mContext.getContentResolver(), ZEN_MODE);
97     }
98 }
99