Home | History | Annotate | Download | only in functional
      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.settings.functional;
     18 
     19 import android.content.Context;
     20 import android.net.wifi.WifiManager;
     21 import android.nfc.NfcAdapter;
     22 import android.nfc.NfcManager;
     23 import android.os.RemoteException;
     24 import android.provider.Settings;
     25 import android.platform.test.helpers.SettingsHelperImpl;
     26 import android.support.test.uiautomator.By;
     27 import android.support.test.uiautomator.Direction;
     28 import android.support.test.uiautomator.UiDevice;
     29 import android.support.test.uiautomator.UiObject2;
     30 import android.support.test.uiautomator.Until;
     31 import android.test.InstrumentationTestCase;
     32 import android.test.suitebuilder.annotation.MediumTest;
     33 
     34 
     35 public class LocationSettingsTests extends InstrumentationTestCase {
     36 
     37     private static final String SETTINGS_PACKAGE = "com.android.settings";
     38     private static final int TIMEOUT = 2000;
     39     private UiDevice mDevice;
     40 
     41     @Override
     42     public void setUp() throws Exception {
     43         super.setUp();
     44         mDevice = UiDevice.getInstance(getInstrumentation());
     45         try {
     46             mDevice.setOrientationNatural();
     47         } catch (RemoteException e) {
     48             throw new RuntimeException("failed to freeze device orientaion", e);
     49         }
     50     }
     51 
     52     @Override
     53     protected void tearDown() throws Exception {
     54         mDevice.pressBack();
     55         mDevice.pressHome();
     56         super.tearDown();
     57     }
     58 
     59     @MediumTest
     60     public void testLoadingLocationSettings () throws Exception {
     61         // Load settings
     62         SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(),
     63                 Settings.ACTION_SETTINGS);
     64         // Tap on location
     65         UiObject2 settingsPanel = mDevice.wait(Until.findObject
     66                 (By.res(SETTINGS_PACKAGE, "dashboard_container")), TIMEOUT);
     67         int count = 0;
     68         UiObject2 locationTitle = null;
     69         while(count < 6 && locationTitle == null) {
     70             locationTitle = mDevice.wait(Until.findObject(By.text("Location")), TIMEOUT);
     71             if (locationTitle == null) {
     72                 settingsPanel.scroll(Direction.DOWN, 1.0f);
     73             }
     74             count++;
     75         }
     76         // Verify location settings loads.
     77         locationTitle.click();
     78         Thread.sleep(TIMEOUT);
     79         assertNotNull("Location screen has not loaded correctly",
     80                 mDevice.wait(Until.findObject(By.text("Location services")), TIMEOUT));
     81     }
     82 
     83     @MediumTest
     84     public void testLocationSettingOn() throws Exception {
     85         verifyLocationSettingsOnOrOff(true);
     86     }
     87 
     88     @MediumTest
     89     public void testLocationSettingOff() throws Exception {
     90         verifyLocationSettingsOnOrOff(false);
     91     }
     92 
     93     @MediumTest
     94     public void testLocationDeviceOnlyMode() throws Exception {
     95         // Changing the value from default before testing the toggle to Device only mode
     96         Settings.Secure.putInt(getInstrumentation().getContext().getContentResolver(),
     97                 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
     98         Thread.sleep(TIMEOUT);
     99         verifyLocationSettingsMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
    100     }
    101 
    102     @MediumTest
    103     public void testLocationBatterySavingMode() throws Exception {
    104         Settings.Secure.putInt(getInstrumentation().getContext().getContentResolver(),
    105                 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
    106         Thread.sleep(TIMEOUT);
    107         verifyLocationSettingsMode(Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
    108     }
    109 
    110     @MediumTest
    111     public void testLocationHighAccuracyMode() throws Exception {
    112         Settings.Secure.putInt(getInstrumentation().getContext().getContentResolver(),
    113                 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
    114         Thread.sleep(TIMEOUT);
    115         verifyLocationSettingsMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
    116     }
    117 
    118     @MediumTest
    119     public void testLocationSettingsElements() throws Exception {
    120         String[] textElements = {"Location", "On", "Mode", "Recent location requests",
    121                 "Location services"};
    122         SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(),
    123                 Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    124         Thread.sleep(TIMEOUT);
    125         for (String element : textElements) {
    126             assertNotNull(element + " item not found under Location Settings",
    127                     mDevice.wait(Until.findObject(By.text(element)), TIMEOUT));
    128         }
    129     }
    130 
    131     @MediumTest
    132     public void testLocationSettingsOverflowMenuElements() throws Exception {
    133         SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(),
    134                             Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    135         // Tap on overflow menu
    136         mDevice.wait(Until.findObject(By.desc("More options")), TIMEOUT).click();
    137         // Verify scanning
    138         assertNotNull("Scanning item not found under Location Settings",
    139                 mDevice.wait(Until.findObject(By.text("Scanning")), TIMEOUT));
    140         // Verify help & feedback
    141         assertNotNull("Help & feedback item not found under Location Settings",
    142                 mDevice.wait(Until.findObject(By.text("Help & feedback")), TIMEOUT));
    143     }
    144 
    145     private void verifyLocationSettingsMode(int mode) throws Exception {
    146         int modeIntValue = 1;
    147         String textMode = "Device only";
    148         if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
    149             modeIntValue = 3;
    150             textMode = "High accuracy";
    151         }
    152         else if (mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
    153             modeIntValue = 2;
    154             textMode = "Battery saving";
    155         }
    156         // Load location settings
    157         SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(),
    158                 Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    159         // Tap on mode
    160         mDevice.wait(Until.findObject(By.text("Mode")), TIMEOUT).click();
    161         Thread.sleep(TIMEOUT);
    162         assertNotNull("Location mode screen not loaded", mDevice.wait(Until.findObject
    163                 (By.text("Location mode")), TIMEOUT));
    164         // Choose said mode
    165         mDevice.wait(Until.findObject(By.text(textMode)), TIMEOUT).click();
    166         Thread.sleep(TIMEOUT);
    167         if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY ||
    168                 mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
    169             // Expect another dialog here at improving location tracking
    170             // for the first time
    171             UiObject2 agreeDialog = mDevice.wait(Until.findObject
    172                     (By.text("Improve location accuracy?")), TIMEOUT);
    173             if (agreeDialog != null) {
    174             mDevice.wait(Until.findObject
    175                                 (By.text("AGREE")), TIMEOUT).click();
    176             Thread.sleep(TIMEOUT);
    177             assertNull("Improve location dialog not dismissed", mDevice.wait(Until.findObject
    178                     (By.text("Improve location accuracy?")), TIMEOUT));
    179             }
    180         }
    181         // get setting and verify value
    182         // Verify change of mode
    183         int locationSettingMode =
    184                 Settings.Secure.getInt(getInstrumentation().getContext().getContentResolver(),
    185                 Settings.Secure.LOCATION_MODE);
    186         assertEquals(mode + " value not set correctly for location.", modeIntValue,
    187                 locationSettingMode);
    188     }
    189 
    190     private void verifyLocationSettingsOnOrOff(boolean verifyOn) throws Exception {
    191         // Set location flag
    192         if (verifyOn) {
    193             Settings.Secure.putInt(getInstrumentation().getContext().getContentResolver(),
    194                     Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
    195         }
    196         else {
    197             Settings.Secure.putInt(getInstrumentation().getContext().getContentResolver(),
    198                     Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
    199         }
    200         // Load location settings
    201         SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(),
    202                 Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    203         // Toggle UI
    204         mDevice.wait(Until.findObject(By.res(SETTINGS_PACKAGE, "switch_widget")), TIMEOUT).click();
    205         Thread.sleep(TIMEOUT);
    206         // Verify change in setting
    207         int locationEnabled = Settings.Secure.getInt(getInstrumentation()
    208                  .getContext().getContentResolver(),
    209                  Settings.Secure.LOCATION_MODE);
    210         if (verifyOn) {
    211             assertFalse("Location not enabled correctly", locationEnabled == 0);
    212         }
    213         else {
    214             assertEquals("Location not disabled correctly", 0, locationEnabled);
    215         }
    216     }
    217 }
    218