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