1 /* 2 * Copyright (C) 2018 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.settings.ui; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.nfc.NfcAdapter; 22 import android.nfc.NfcManager; 23 import android.os.RemoteException; 24 import android.support.test.InstrumentationRegistry; 25 import android.support.test.filters.MediumTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.support.test.uiautomator.By; 28 import android.support.test.uiautomator.UiDevice; 29 import android.support.test.uiautomator.UiObject2; 30 import android.support.test.uiautomator.Until; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import static org.junit.Assert.assertFalse; 38 import static org.junit.Assert.assertTrue; 39 40 @MediumTest 41 @RunWith(AndroidJUnit4.class) 42 public class ConnectedDeviceTests { 43 44 private static final String SETTINGS_PACKAGE = "com.android.settings"; 45 private static final int TIMEOUT = 2000; 46 private UiDevice mDevice; 47 48 @Before 49 public void setUp() throws Exception { 50 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 51 try { 52 mDevice.setOrientationNatural(); 53 } catch (RemoteException e) { 54 throw new RuntimeException("failed to freeze device orientation", e); 55 } 56 } 57 58 @After 59 public void tearDown() throws Exception { 60 mDevice.pressBack(); 61 mDevice.pressHome(); 62 } 63 64 // This NFC toggle test is set up this way since there's no way to set 65 // the NFC flag to enabled or disabled without touching UI. 66 // This way, we get coverage for whether or not the toggle button works. 67 @Test 68 public void testNFCToggle() throws Exception { 69 NfcManager manager = (NfcManager) InstrumentationRegistry.getTargetContext() 70 .getSystemService(Context.NFC_SERVICE); 71 NfcAdapter nfcAdapter = manager.getDefaultAdapter(); 72 boolean nfcInitiallyEnabled = nfcAdapter.isEnabled(); 73 InstrumentationRegistry.getContext().startActivity(new Intent() 74 .setClassName( 75 SETTINGS_PACKAGE, 76 "com.android.settings.Settings$ConnectedDeviceDashboardActivity")); 77 UiObject2 nfcSetting = mDevice.wait(Until.findObject(By.text("NFC")), TIMEOUT); 78 nfcSetting.click(); 79 Thread.sleep(TIMEOUT * 2); 80 if (nfcInitiallyEnabled) { 81 assertFalse("NFC wasn't disabled on toggle", nfcAdapter.isEnabled()); 82 nfcSetting.click(); 83 Thread.sleep(TIMEOUT * 2); 84 assertTrue("NFC wasn't enabled on toggle", nfcAdapter.isEnabled()); 85 } else { 86 assertTrue("NFC wasn't enabled on toggle", nfcAdapter.isEnabled()); 87 nfcSetting.click(); 88 Thread.sleep(TIMEOUT * 2); 89 assertFalse("NFC wasn't disabled on toggle", nfcAdapter.isEnabled()); 90 } 91 } 92 } 93