Home | History | Annotate | Download | only in helpers
      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 android.platform.test.helpers;
     18 
     19 import android.app.Instrumentation;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.provider.Settings;
     24 import android.provider.Settings.SettingNotFoundException;
     25 import android.support.test.uiautomator.By;
     26 import android.support.test.uiautomator.BySelector;
     27 import android.support.test.uiautomator.Direction;
     28 import android.support.test.uiautomator.UiObject2;
     29 import android.support.test.uiautomator.Until;
     30 import android.platform.test.helpers.AbstractSettingsHelper;
     31 import android.util.Log;
     32 
     33 import junit.framework.Assert;
     34 
     35 import java.util.regex.Pattern;
     36 
     37 public class SettingsHelperImpl extends AbstractSettingsHelper {
     38 
     39     private static final int SETTINGS_DASH_TIMEOUT = 3000;
     40     private static final String UI_PACKAGE_NAME = "com.android.settings";
     41     private static final BySelector SETTINGS_DASHBOARD = By.res(UI_PACKAGE_NAME,
     42             "dashboard_container");
     43     private static final int TIMEOUT = 2000;
     44     private static final String LOG_TAG = SettingsHelperImpl.class.getSimpleName();
     45 
     46     private ContentResolver mResolver;
     47 
     48     public static enum SettingsType {
     49         SYSTEM,
     50         SECURE,
     51         GLOBAL
     52     }
     53 
     54     public SettingsHelperImpl(Instrumentation instr) {
     55         super(instr);
     56         mResolver = instr.getContext().getContentResolver();
     57     }
     58 
     59     /**
     60      * {@inheritDoc}
     61      */
     62     @Override
     63     public String getPackage() {
     64         return "com.android.settings";
     65     }
     66 
     67     /**
     68      * {@inheritDoc}
     69      */
     70     @Override
     71     public String getLauncherName() {
     72         return "Settings";
     73     }
     74 
     75     /**
     76      * {@inheritDoc}
     77      */
     78     @Override
     79     public void dismissInitialDialogs() {
     80     }
     81 
     82      /**
     83      * {@inheritDoc}
     84      */
     85     @Override
     86     public void scrollThroughSettings(int numberOfFlings) throws Exception {
     87         UiObject2 settingsList = loadAllSettings();
     88         int count = 0;
     89         while (count <= numberOfFlings && settingsList.fling(Direction.DOWN)) {
     90             count++;
     91         }
     92     }
     93 
     94     /**
     95      * {@inheritDoc}
     96      */
     97     @Override
     98     public void flingSettingsToStart() throws Exception {
     99         UiObject2 settingsList = loadAllSettings();
    100         while (settingsList.fling(Direction.UP));
    101     }
    102 
    103     public static void launchSettingsPage(Context ctx, String pageName) throws Exception {
    104         Intent intent = new Intent(pageName);
    105         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    106         ctx.startActivity(intent);
    107         Thread.sleep(TIMEOUT * 2);
    108     }
    109 
    110     public void scrollVert(boolean isUp) {
    111         int w = mDevice.getDisplayWidth();
    112         int h = mDevice.getDisplayHeight();
    113         mDevice.swipe(w / 2, h / 2, w / 2, isUp ? h : 0, 2);
    114     }
    115 
    116     /**
    117      * On N, the settingsDashboard is initially collapsed, and the user can see the "See all"
    118      * element. On hitting "See all", the same settings dashboard element is now scrollable. For
    119      * pre-N, the settings Dashboard is always scrollable, hence the check in the while loop. All
    120      * this method does is expand the Settings list if needed, before returning the element.
    121      */
    122     private UiObject2 loadAllSettings() throws Exception {
    123         UiObject2 settingsDashboard = mDevice.wait(Until.findObject(SETTINGS_DASHBOARD),
    124                 SETTINGS_DASH_TIMEOUT);
    125         Assert.assertNotNull("Could not find the settings dashboard object.", settingsDashboard);
    126         int count = 0;
    127         while (!settingsDashboard.isScrollable() && count <= 2) {
    128             mDevice.wait(Until.findObject(By.text("SEE ALL")), SETTINGS_DASH_TIMEOUT).click();
    129             settingsDashboard = mDevice.wait(Until.findObject(SETTINGS_DASHBOARD),
    130                     SETTINGS_DASH_TIMEOUT);
    131             count++;
    132         }
    133         return settingsDashboard;
    134     }
    135 
    136     public void clickSetting(String settingName) throws InterruptedException {
    137         mDevice.wait(Until.findObject(By.text(settingName)), TIMEOUT).click();
    138         Thread.sleep(400);
    139     }
    140 
    141     public void clickSetting(Pattern settingName) throws InterruptedException {
    142         mDevice.wait(Until.findObject(By.text(settingName)), TIMEOUT).click();
    143         Thread.sleep(400);
    144     }
    145 
    146     public boolean verifyToggleSetting(SettingsType type, String settingAction,
    147             String settingName, String internalName) throws Exception {
    148         return verifyToggleSetting(
    149                 type, settingAction, Pattern.compile(settingName), internalName, true);
    150     }
    151 
    152     public boolean verifyToggleSetting(SettingsType type, String settingAction,
    153             Pattern settingName, String internalName) throws Exception {
    154         return verifyToggleSetting(type, settingAction, settingName, internalName, true);
    155     }
    156 
    157     public boolean verifyToggleSetting(SettingsType type, String settingAction,
    158             String settingName, String internalName, boolean doLaunch) throws Exception {
    159         return verifyToggleSetting(
    160                 type, settingAction, Pattern.compile(settingName), internalName, doLaunch);
    161     }
    162 
    163     public boolean verifyToggleSetting(SettingsType type, String settingAction,
    164             Pattern settingName, String internalName, boolean doLaunch) throws Exception {
    165         String onSettingBaseVal = getStringSetting(type, internalName);
    166         if (onSettingBaseVal == null) {
    167             onSettingBaseVal = "0";
    168         }
    169         int onSetting = Integer.parseInt(onSettingBaseVal);
    170         Log.d(null, "On Setting value is : " + onSetting);
    171         if (doLaunch) {
    172             launchSettingsPage(mInstrumentation.getContext(), settingAction);
    173         }
    174         clickSetting(settingName);
    175         Log.d(null, "Clicked setting : " + settingName);
    176         Thread.sleep(1000);
    177         String changedSetting = getStringSetting(type, internalName);
    178         Log.d(null, "Changed Setting value is : " + changedSetting);
    179         if (changedSetting == null) {
    180             Log.d(null, "Changed Setting value is : NULL");
    181             changedSetting = "0";
    182         }
    183         return (1 - onSetting) == Integer.parseInt(changedSetting);
    184     }
    185 
    186     public boolean verifyRadioSetting(SettingsType type, String settingAction,
    187             String baseName, String settingName,
    188             String internalName, String testVal) throws Exception {
    189         if (baseName != null) clickSetting(baseName);
    190         clickSetting(settingName);
    191         Thread.sleep(500);
    192         return getStringSetting(type, internalName).equals(testVal);
    193     }
    194 
    195     private String getStringSetting(SettingsType type, String sName) {
    196         switch (type) {
    197             case SYSTEM:
    198                 return Settings.System.getString(mResolver, sName);
    199             case GLOBAL:
    200                 return Settings.Global.getString(mResolver, sName);
    201             case SECURE:
    202                 return Settings.Secure.getString(mResolver, sName);
    203         }
    204         return "";
    205     }
    206 
    207     private int getIntSetting(SettingsType type, String sName) throws SettingNotFoundException {
    208         switch (type) {
    209             case SYSTEM:
    210                 return Settings.System.getInt(mResolver, sName);
    211             case GLOBAL:
    212                 return Settings.Global.getInt(mResolver, sName);
    213             case SECURE:
    214                 return Settings.Secure.getInt(mResolver, sName);
    215         }
    216         return Integer.MIN_VALUE;
    217     }
    218 }
    219