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.intent.sender; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.net.Uri; 23 import android.test.InstrumentationTestCase; 24 25 public class AppLinkTest extends InstrumentationTestCase { 26 27 private static final String TAG = "AppLinkTest"; 28 29 private Context mContext; 30 private IntentSenderActivity mActivity; 31 32 private static final String EXTRA_IN_MANAGED_PROFILE = "extra_in_managed_profile"; 33 private static final String EXTRA_RECEIVER_CLASS = "extra_receiver_class"; 34 private static final String APP_LINK_ACTIVITY 35 = "com.android.cts.intent.receiver.AppLinkActivity"; 36 private static final String BROWSER_ACTIVITY 37 = "com.android.cts.intent.receiver.BrowserActivity"; 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 mContext = getInstrumentation().getTargetContext(); 43 mActivity = launchActivity(mContext.getPackageName(), IntentSenderActivity.class, null); 44 } 45 46 @Override 47 public void tearDown() throws Exception { 48 mActivity.finish(); 49 super.tearDown(); 50 } 51 52 public void testReceivedByAppLinkActivityInPrimary() throws Exception { 53 checkHttpIntentResult(APP_LINK_ACTIVITY, false); 54 } 55 56 public void testReceivedByAppLinkActivityInManaged() throws Exception { 57 checkHttpIntentResult(APP_LINK_ACTIVITY, true); 58 } 59 60 public void testReceivedByBrowserActivityInManaged() throws Exception { 61 checkHttpIntentResult(BROWSER_ACTIVITY, true); 62 } 63 64 public void testTwoReceivers() { 65 assertNumberOfReceivers(2); 66 } 67 68 public void testThreeReceivers() { 69 assertNumberOfReceivers(3); 70 } 71 72 // Should not be called if there are several possible receivers to the intent 73 // (see getHttpIntent) 74 private void checkHttpIntentResult(String receiverClassName, boolean inManagedProfile) 75 throws Exception { 76 PackageManager pm = mContext.getPackageManager(); 77 78 Intent result = mActivity.getResult(getHttpIntent()); 79 assertNotNull(result); 80 81 // If it is received in the other profile, we cannot check the class from the ResolveInfo 82 // returned by queryIntentActivities. So we rely on the receiver telling us its class. 83 assertEquals(receiverClassName, result.getStringExtra(EXTRA_RECEIVER_CLASS)); 84 assertTrue(result.hasExtra(EXTRA_IN_MANAGED_PROFILE)); 85 assertEquals(inManagedProfile, result.getBooleanExtra(EXTRA_IN_MANAGED_PROFILE, false)); 86 } 87 88 private void assertNumberOfReceivers(int n) { 89 PackageManager pm = mContext.getPackageManager(); 90 assertEquals(n, pm.queryIntentActivities(getHttpIntent(), /* flags = */ 0).size()); 91 } 92 93 private Intent getHttpIntent() { 94 Intent i = new Intent(Intent.ACTION_VIEW); 95 i.addCategory(Intent.CATEGORY_BROWSABLE); 96 i.setData(Uri.parse("http://com.android.cts.intent.receiver")); 97 return i; 98 } 99 } 100