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.util.Log; 20 21 /** 22 * Base class for metered and non-metered Battery Saver Mode tests. 23 */ 24 abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgroundNetworkTestCase { 25 26 @Override 27 protected final void setUp() throws Exception { 28 super.setUp(); 29 30 if (!isSupported()) return; 31 32 // Set initial state. 33 removePowerSaveModeWhitelist(TEST_APP2_PKG); 34 removePowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG); 35 setBatterySaverMode(false); 36 37 registerBroadcastReceiver(); 38 } 39 40 @Override 41 protected final void tearDown() throws Exception { 42 super.tearDown(); 43 44 if (!isSupported()) return; 45 46 try { 47 tearDownMeteredNetwork(); 48 } finally { 49 setBatterySaverMode(false); 50 } 51 } 52 53 @Override 54 protected boolean isSupported() throws Exception { 55 boolean supported = isDozeModeEnabled(); 56 if (!supported) { 57 Log.i(TAG, "Skipping " + getClass() + "." + getName() 58 + "() because device does not support Doze Mode"); 59 } 60 return supported; 61 } 62 63 /** 64 * Sets the initial (non) metered network state. 65 * 66 * <p>By default is empty - it's up to subclasses to override. 67 */ 68 protected void setUpMeteredNetwork() throws Exception { 69 } 70 71 /** 72 * Resets the (non) metered network state. 73 * 74 * <p>By default is empty - it's up to subclasses to override. 75 */ 76 protected void tearDownMeteredNetwork() throws Exception { 77 } 78 79 public void testBackgroundNetworkAccess_enabled() throws Exception { 80 if (!isSupported()) return; 81 82 setBatterySaverMode(true); 83 assertBackgroundNetworkAccess(false); 84 85 assertsForegroundAlwaysHasNetworkAccess(); 86 assertBackgroundNetworkAccess(false); 87 88 // Make sure foreground app doesn't lose access upon Battery Saver. 89 setBatterySaverMode(false); 90 launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY); 91 setBatterySaverMode(true); 92 assertForegroundNetworkAccess(); 93 94 // Although it should not have access while the screen is off. 95 turnScreenOff(); 96 assertBackgroundNetworkAccess(false); 97 turnScreenOn(); 98 assertForegroundNetworkAccess(); 99 100 // Goes back to background state. 101 finishActivity(); 102 assertBackgroundNetworkAccess(false); 103 104 // Make sure foreground service doesn't lose access upon enabling Battery Saver. 105 setBatterySaverMode(false); 106 launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_FOREGROUND_SERVICE); 107 setBatterySaverMode(true); 108 assertForegroundNetworkAccess(); 109 stopForegroundService(); 110 assertBackgroundNetworkAccess(false); 111 } 112 113 public void testBackgroundNetworkAccess_whitelisted() throws Exception { 114 if (!isSupported()) return; 115 116 setBatterySaverMode(true); 117 assertBackgroundNetworkAccess(false); 118 119 addPowerSaveModeWhitelist(TEST_APP2_PKG); 120 assertBackgroundNetworkAccess(true); 121 122 removePowerSaveModeWhitelist(TEST_APP2_PKG); 123 assertBackgroundNetworkAccess(false); 124 125 addPowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG); 126 assertBackgroundNetworkAccess(true); 127 128 removePowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG); 129 assertBackgroundNetworkAccess(false); 130 131 assertsForegroundAlwaysHasNetworkAccess(); 132 assertBackgroundNetworkAccess(false); 133 } 134 135 public void testBackgroundNetworkAccess_disabled() throws Exception { 136 if (!isSupported()) return; 137 138 assertBackgroundNetworkAccess(true); 139 140 assertsForegroundAlwaysHasNetworkAccess(); 141 assertBackgroundNetworkAccess(true); 142 } 143 } 144