Home | History | Annotate | Download | only in hostside
      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
     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         turnBatteryOn();
     38 
     39         registerBroadcastReceiver();
     40     }
     41 
     42     @Override
     43     protected final void tearDown() throws Exception {
     44         super.tearDown();
     45 
     46         if (!isSupported()) return;
     47 
     48         try {
     49             tearDownMeteredNetwork();
     50         } finally {
     51             turnBatteryOff();
     52             setAppIdle(false);
     53         }
     54     }
     55 
     56     @Override
     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      */
     71     protected void tearDownMeteredNetwork() throws Exception {
     72     }
     73 
     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 
    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 
    130     public void testBackgroundNetworkAccess_tempWhitelisted() throws Exception {
    131         if (!isSupported()) return;
    132 
    133         setAppIdle(true);
    134         assertBackgroundNetworkAccess(false);
    135 
    136         addTempPowerSaveModeWhitelist(TEST_APP2_PKG, TEMP_POWERSAVE_WHITELIST_DURATION_MS);
    137         assertBackgroundNetworkAccess(true);
    138         // Wait until the whitelist duration is expired.
    139         SystemClock.sleep(TEMP_POWERSAVE_WHITELIST_DURATION_MS);
    140         assertBackgroundNetworkAccess(false);
    141     }
    142 
    143     public void testBackgroundNetworkAccess_disabled() throws Exception {
    144         if (!isSupported()) return;
    145 
    146         assertBackgroundNetworkAccess(true);
    147 
    148         assertsForegroundAlwaysHasNetworkAccess();
    149         assertBackgroundNetworkAccess(true);
    150     }
    151 
    152     public void testAppIdleNetworkAccess_whenCharging() throws Exception {
    153         if (!isSupported()) return;
    154 
    155         // Check that app is paroled when charging
    156         setAppIdle(true);
    157         assertBackgroundNetworkAccess(false);
    158         turnBatteryOff();
    159         assertBackgroundNetworkAccess(true);
    160         turnBatteryOn();
    161         assertBackgroundNetworkAccess(false);
    162 
    163         // Check that app is restricted when not idle but power-save is on
    164         setAppIdle(false);
    165         assertBackgroundNetworkAccess(true);
    166         setBatterySaverMode(true);
    167         assertBackgroundNetworkAccess(false);
    168         // Use setBatterySaverMode API to leave power-save mode instead of plugging in charger
    169         setBatterySaverMode(false);
    170         turnBatteryOff();
    171         assertBackgroundNetworkAccess(true);
    172 
    173         // And when no longer charging, it still has network access, since it's not idle
    174         turnBatteryOn();
    175         assertBackgroundNetworkAccess(true);
    176     }
    177 
    178     public void testAppIdle_toast() throws Exception {
    179         if (!isSupported()) return;
    180 
    181         setAppIdle(true);
    182         assertAppIdle(true);
    183         assertEquals("Shown", showToast());
    184         assertAppIdle(true);
    185         // Wait for a couple of seconds for the toast to actually be shown
    186         SystemClock.sleep(2000);
    187         assertAppIdle(true);
    188     }
    189 }
    190