1 /* 2 * Copyright (C) 2022 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.security.sts.sts_test_app_package; 18 19 import static androidx.test.core.app.ApplicationProvider.getApplicationContext; 20 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 21 import static org.junit.Assert.assertNotEquals; 22 import static org.junit.Assume.assumeNoException; 23 24 import android.content.Context; 25 import android.content.SharedPreferences; 26 27 import androidx.annotation.IntegerRes; 28 import androidx.annotation.StringRes; 29 import androidx.test.runner.AndroidJUnit4; 30 import androidx.test.uiautomator.UiDevice; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 @RunWith(AndroidJUnit4.class) 36 public class DeviceTest { 37 38 Context mAppContext; 39 getInteger(@ntegerRes int resId)40 int getInteger(@IntegerRes int resId) { 41 return mAppContext.getResources().getInteger(resId); 42 } 43 getString(@tringRes int resId)44 String getString(@StringRes int resId) { 45 return mAppContext.getResources().getString(resId); 46 } 47 48 @Test testDeviceSideMethod()49 public void testDeviceSideMethod() { 50 try { 51 mAppContext = getApplicationContext(); 52 UiDevice device = UiDevice.getInstance(getInstrumentation()); 53 device.executeShellCommand( 54 "am start -n com.android.nfc/.handover.ConfirmConnectActivity"); 55 long startTime = System.currentTimeMillis(); 56 while ((System.currentTimeMillis() - startTime) 57 < getInteger(R.integer.MAX_WAIT_TIME_MS)) { 58 SharedPreferences sh = 59 mAppContext.getSharedPreferences( 60 getString(R.string.SHARED_PREFERENCE), Context.MODE_APPEND); 61 int result = 62 sh.getInt(getString(R.string.RESULT_KEY), getInteger(R.integer.DEFAULT)); 63 assertNotEquals( 64 "NFC Android App broadcasts Bluetooth device information!", 65 result, 66 getInteger(R.integer.FAIL)); 67 Thread.sleep(getInteger(R.integer.SLEEP_TIME_MS)); 68 } 69 } catch (Exception e) { 70 assumeNoException(e); 71 } 72 } 73 } 74