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 17package com.android.settings.tests; 18 19import android.app.Instrumentation; 20import android.content.Context; 21import android.content.Intent; 22import android.os.BatteryManager; 23import android.provider.Settings; 24import android.support.test.InstrumentationRegistry; 25import org.junit.After; 26import org.junit.Before; 27import org.junit.runner.RunWith; 28import android.support.test.filters.SmallTest; 29import android.support.test.runner.AndroidJUnit4; 30import org.junit.Test; 31import com.android.settings.R; 32 33import static android.support.test.espresso.Espresso.onView; 34import static android.support.test.espresso.action.ViewActions.click; 35import static android.support.test.espresso.matcher.ViewMatchers.*; 36import static junit.framework.Assert.assertEquals; 37 38@RunWith(AndroidJUnit4.class) 39@SmallTest 40public class KeepOnScreenTest { 41 private static int EXPECTED_FLAG = BatteryManager.BATTERY_PLUGGED_AC 42 | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS; 43 44 @Test 45 public void testStayAwake_turnOn_StayAwakeWhileWirelessCharging() throws Exception{ 46 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 47 instrumentation.startActivitySync(new Intent(android.provider.Settings 48 .ACTION_APPLICATION_DEVELOPMENT_SETTINGS)); 49 50 final Context targetContext = instrumentation.getTargetContext(); 51 final int prevFlag = Settings.Global.getInt(targetContext.getContentResolver(), Settings 52 .Global.STAY_ON_WHILE_PLUGGED_IN); 53 54 // Turn on "Stay Awake" if needed 55 if (prevFlag == 0) { 56 onView(withText(R.string.keep_screen_on)).perform(click()); 57 } 58 59 final int currentFlag = Settings.Global.getInt(targetContext.getContentResolver(), Settings 60 .Global.STAY_ON_WHILE_PLUGGED_IN); 61 62 assertEquals(EXPECTED_FLAG, currentFlag); 63 64 // Since this app doesn't have permission(and shouldn't have) to change global setting, we 65 // can only tearDown in this way 66 if (prevFlag != currentFlag) { 67 onView(withText(R.string.keep_screen_on)).perform(click()); 68 } 69 } 70} 71