1 /* 2 * Copyright (C) 2015 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.cts.managedprofile; 18 19 import android.content.Intent; 20 import android.content.pm.PackageManager; 21 import android.content.pm.ResolveInfo; 22 import android.test.AndroidTestCase; 23 24 public class NfcTest extends AndroidTestCase { 25 private static final String SAMPLE_TEXT = "This is my text to send."; 26 private static final String TEXT_MIME_TYPE = "text/plain"; 27 private static final String NFC_BEAM_ACTIVITY = "com.android.nfc.BeamShareActivity"; 28 private static int NFC_RESOLVE_TIME_STEP_MILLIS = 1000; 29 private static int NFC_RESOLVE_TIMEOUT_MILLIS = 16000; 30 testNfcShareDisabled()31 public void testNfcShareDisabled() throws Exception { 32 Intent intent = getTextShareIntent(); 33 // After the "no_outgoing_beam" configuration item is modified, it takes a while 34 // until NFC receives the DEVICE_POLICY_MANAGER_STATE_CHANGED broadcast 35 waitForNfcBeamActivityDisabled(intent, NFC_RESOLVE_TIMEOUT_MILLIS); 36 assertFalse("Nfc beam activity should not be resolved", isNfcBeamActivityResolved(intent)); 37 } 38 testNfcShareEnabled()39 public void testNfcShareEnabled() throws Exception { 40 Intent intent = getTextShareIntent(); 41 assertTrue("Nfc beam activity should be resolved", isNfcBeamActivityResolved(intent)); 42 } 43 waitForNfcBeamActivityDisabled(Intent intent, int maxDelayMillis)44 private void waitForNfcBeamActivityDisabled(Intent intent, int maxDelayMillis) 45 throws Exception { 46 int totalDelayedMillis = 0; 47 while (isNfcBeamActivityResolved(intent) && totalDelayedMillis <= maxDelayMillis) { 48 Thread.sleep(NFC_RESOLVE_TIME_STEP_MILLIS); 49 totalDelayedMillis += NFC_RESOLVE_TIME_STEP_MILLIS; 50 } 51 } 52 getTextShareIntent()53 private Intent getTextShareIntent() { 54 Intent intent = new Intent(); 55 intent.setAction(Intent.ACTION_SEND); 56 intent.putExtra(Intent.EXTRA_TEXT, SAMPLE_TEXT); 57 intent.setType(TEXT_MIME_TYPE); 58 return intent; 59 } 60 isNfcBeamActivityResolved(Intent intent)61 private boolean isNfcBeamActivityResolved(Intent intent) { 62 PackageManager pm = mContext.getPackageManager(); 63 for (ResolveInfo resolveInfo : pm.queryIntentActivities(intent, 0)) { 64 if (NFC_BEAM_ACTIVITY.equals(resolveInfo.activityInfo.name)) { 65 return true; 66 } 67 } 68 69 return false; 70 } 71 } 72 73