1 /*
2  * Copyright (C) 2016 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.cts.net.hostside;
18 
19 import android.os.SystemClock;
20 import android.util.Log;
21 
22 /**
23  * Base class for metered and non-metered tests on idle apps.
24  */
25 abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetworkTestCase {
26 
27     @Override
setUp()28     protected final void setUp() throws Exception {
29         super.setUp();
30 
31         if (!isSupported()) return;
32 
33         // Set initial state.
34         removePowerSaveModeWhitelist(TEST_APP2_PKG);
35         removePowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG);
36         setAppIdle(false);
37         turnBatteryOff();
38 
39         registerBroadcastReceiver();
40     }
41 
42     @Override
tearDown()43     protected final void tearDown() throws Exception {
44         super.tearDown();
45 
46         if (!isSupported()) return;
47 
48         try {
49             tearDownMeteredNetwork();
50         } finally {
51             turnBatteryOn();
52             setAppIdle(false);
53         }
54     }
55 
56     @Override
isSupported()57     protected boolean isSupported() throws Exception {
58         boolean supported = isDozeModeEnabled();
59         if (!supported) {
60             Log.i(TAG, "Skipping " + getClass() + "." + getName()
61                     + "() because device does not support Doze Mode");
62         }
63         return supported;
64     }
65 
66     /**
67      * Resets the (non) metered network state.
68      *
69      * <p>By default is empty - it's up to subclasses to override.
70      */
tearDownMeteredNetwork()71     protected void tearDownMeteredNetwork() throws Exception {
72     }
73 
testBackgroundNetworkAccess_enabled()74     public void testBackgroundNetworkAccess_enabled() throws Exception {
75         if (!isSupported()) return;
76 
77         setAppIdle(true);
78         assertBackgroundNetworkAccess(false);
79 
80         assertsForegroundAlwaysHasNetworkAccess();
81         setAppIdle(true);
82         assertBackgroundNetworkAccess(false);
83 
84         // Make sure foreground app doesn't lose access upon enabling it.
85         setAppIdle(true);
86         launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY);
87         finishActivity();
88         assertAppIdle(false); // Sanity check - not idle anymore, since activity was launched...
89         assertBackgroundNetworkAccess(true);
90         setAppIdle(true);
91         assertBackgroundNetworkAccess(false);
92 
93         // Same for foreground service.
94         setAppIdle(true);
95         launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_FOREGROUND_SERVICE);
96         stopForegroundService();
97         assertAppIdle(true);
98         assertBackgroundNetworkAccess(false);
99     }
100 
testBackgroundNetworkAccess_whitelisted()101     public void testBackgroundNetworkAccess_whitelisted() throws Exception {
102         if (!isSupported()) return;
103 
104         setAppIdle(true);
105         assertBackgroundNetworkAccess(false);
106 
107         addPowerSaveModeWhitelist(TEST_APP2_PKG);
108         assertAppIdle(false); // Sanity check - not idle anymore, since whitelisted
109         assertBackgroundNetworkAccess(true);
110 
111         removePowerSaveModeWhitelist(TEST_APP2_PKG);
112         assertAppIdle(true); // Sanity check - idle again, once whitelisted was removed
113         assertBackgroundNetworkAccess(false);
114 
115         addPowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG);
116         assertAppIdle(false); // Sanity check - not idle anymore, since whitelisted
117         assertBackgroundNetworkAccess(true);
118 
119         removePowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG);
120         assertAppIdle(true); // Sanity check - idle again, once whitelisted was removed
121         assertBackgroundNetworkAccess(false);
122 
123         assertsForegroundAlwaysHasNetworkAccess();
124 
125         // Sanity check - no whitelist, no access!
126         setAppIdle(true);
127         assertBackgroundNetworkAccess(false);
128     }
129 
testBackgroundNetworkAccess_disabled()130     public void testBackgroundNetworkAccess_disabled() throws Exception {
131         if (!isSupported()) return;
132 
133         assertBackgroundNetworkAccess(true);
134 
135         assertsForegroundAlwaysHasNetworkAccess();
136         assertBackgroundNetworkAccess(true);
137     }
138 
testAppIdleNetworkAccess_whenCharging()139     public void testAppIdleNetworkAccess_whenCharging() throws Exception {
140         if (!isSupported()) return;
141 
142         // Check that app is paroled when charging
143         setAppIdle(true);
144         assertBackgroundNetworkAccess(false);
145         turnBatteryOn();
146         assertBackgroundNetworkAccess(true);
147         turnBatteryOff();
148         assertBackgroundNetworkAccess(false);
149 
150         // Check that app is restricted when not idle but power-save is on
151         setAppIdle(false);
152         assertBackgroundNetworkAccess(true);
153         setBatterySaverMode(true);
154         assertBackgroundNetworkAccess(false);
155         // Use setBatterySaverMode API to leave power-save mode instead of plugging in charger
156         setBatterySaverMode(false);
157         turnBatteryOn();
158         assertBackgroundNetworkAccess(true);
159 
160         // And when no longer charging, it still has network access, since it's not idle
161         turnBatteryOff();
162         assertBackgroundNetworkAccess(true);
163     }
164 
testAppIdle_toast()165     public void testAppIdle_toast() throws Exception {
166         if (!isSupported()) return;
167 
168         setAppIdle(true);
169         assertAppIdle(true);
170         assertEquals("Shown", showToast());
171         assertAppIdle(true);
172         // Wait for a couple of seconds for the toast to actually be shown
173         SystemClock.sleep(2000);
174         assertAppIdle(true);
175     }
176 }
177