Home | History | Annotate | Download | only in deviceidle
      1 /*
      2  * Copyright (C) 2017 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.deviceidle;
     18 
     19 import static org.junit.Assert.*;
     20 
     21 import com.android.tradefed.device.DeviceNotAvailableException;
     22 import com.android.tradefed.log.LogUtil;
     23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
     24 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
     25 
     26 import org.junit.After;
     27 import org.junit.Assume;
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 
     32 import java.util.ArrayList;
     33 import java.util.List;
     34 
     35 /**
     36  * Tests that it is possible to remove apps from the system whitelist
     37  */
     38 @RunWith(DeviceJUnit4ClassRunner.class)
     39 public class DeviceIdleWhitelistTest extends BaseHostJUnit4Test {
     40 
     41     private static final String DEVICE_IDLE_COMMAND_PREFIX = "cmd deviceidle sys-whitelist ";
     42     private static final String RESET_SYS_WHITELIST_COMMAND = "cmd deviceidle sys-whitelist reset";
     43     private static final String SHOW_SYS_WHITELIST_COMMAND = DEVICE_IDLE_COMMAND_PREFIX;
     44 
     45     private List<String> mOriginalSystemWhitelist;
     46 
     47     @Before
     48     public void setUp() throws Exception {
     49         getDevice().executeShellCommand(RESET_SYS_WHITELIST_COMMAND);
     50         mOriginalSystemWhitelist = getSystemWhitelist();
     51         if (mOriginalSystemWhitelist.size() < 1) {
     52             LogUtil.CLog.w("No packages found in system whitelist");
     53             Assume.assumeTrue(false);
     54         }
     55     }
     56 
     57     @After
     58     public void tearDown() throws Exception {
     59         getDevice().executeShellCommand(RESET_SYS_WHITELIST_COMMAND);
     60     }
     61 
     62     @Test
     63     public void testRemoveFromSysWhitelist() throws Exception {
     64         final String packageToRemove = mOriginalSystemWhitelist.get(0);
     65         getDevice().executeShellCommand(DEVICE_IDLE_COMMAND_PREFIX + "-" + packageToRemove);
     66         final List<String> newWhitelist = getSystemWhitelist();
     67         assertFalse("Package " + packageToRemove + " not removed from whitelist",
     68                 newWhitelist.contains(packageToRemove));
     69     }
     70 
     71     @Test
     72     public void testRemovesPersistedAcrossReboots() throws Exception {
     73         for (int i = 0; i < mOriginalSystemWhitelist.size(); i+=2) {
     74             // remove odd indexed packages from the whitelist
     75             getDevice().executeShellCommand(
     76                     DEVICE_IDLE_COMMAND_PREFIX + "-" + mOriginalSystemWhitelist.get(i));
     77         }
     78         final List<String> whitelistBeforeReboot = getSystemWhitelist();
     79         Thread.sleep(10_000); // write to disk happens after 5 seconds
     80         getDevice().reboot();
     81         Thread.sleep(5_000); // to make sure service is initialized
     82         final List<String> whitelistAfterReboot = getSystemWhitelist();
     83         assertEquals(whitelistBeforeReboot.size(), whitelistAfterReboot.size());
     84         for (int i = 0; i < whitelistBeforeReboot.size(); i++) {
     85             assertTrue(whitelistAfterReboot.contains(whitelistBeforeReboot.get(i)));
     86         }
     87     }
     88 
     89     private List<String> getSystemWhitelist() throws DeviceNotAvailableException {
     90         final String output = getDevice().executeShellCommand(SHOW_SYS_WHITELIST_COMMAND).trim();
     91         final List<String> packages = new ArrayList<>();
     92         for (String line : output.split("\n")) {
     93             final int i = line.indexOf(',');
     94             packages.add(line.substring(0, i));
     95         }
     96         return packages;
     97     }
     98 }
     99