Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 26aa674

Browse files
Peter E ConnCommit Bot
Peter E Conn
authored and
Commit Bot
committed
✨ Wait for TWA provider to be set in tests.
Since the provider was set asynchronously occasionally it wouldn't be set in time for the tests to pick up on it. This change also includes the EnableComponentsTestRule that ensures only the required components are enabled in the manifest for the given tests. Bug: 925762 Change-Id: I68994ab27b55ae7cc516b75e1a088dd1acc03581 Reviewed-on: https://chromium-review.googlesource.com/c/custom-tabs-client/+/1534216 Reviewed-by: Pavel Shmakov <[email protected]> Commit-Queue: Peter Conn <[email protected]>
1 parent da65829 commit 26aa674

12 files changed

+211
-74
lines changed

Application/src/main/java/org/chromium/customtabsclient/MainActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void onClick(View v) {
287287
this, Uri.parse(url), BrowserActionsIntent.URL_TYPE_NONE, items, defaultAction);
288288
} else if (viewId == R.id.register_twa_service) {
289289
if (mPackageNameToBind != null) {
290-
TrustedWebActivityService.setVerifiedProviderForTesting(this, mPackageNameToBind);
290+
TrustedWebActivityService.setVerifiedProvider(this, mPackageNameToBind);
291291
}
292292
}
293293
}

customtabs/AndroidManifest.xml

-8
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,4 @@
2323
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
2424
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2525

26-
<application>
27-
<service
28-
android:name=".TestCustomTabsServiceSupportsTwas"
29-
android:enabled="true"
30-
android:exported="true">
31-
</service>
32-
</application>
33-
3426
</manifest>

customtabs/src/android/support/customtabs/trusted/TrustedWebActivityService.java

+18-16
Original file line numberDiff line numberDiff line change
@@ -349,31 +349,19 @@ private static SharedPreferences getPreferences(Context context) {
349349
}
350350

351351
/**
352-
* Sets the package that this service will accept connections from. This should only be used for
353-
* testing as the appropriate provider will be set when the client app launches a Trusted
354-
* Web Activity.
355-
* @param context A context to be used to access SharedPreferences.
356-
* @param provider The package of the provider to accept connections from or null to clear.
357-
*/
358-
public static final void setVerifiedProviderForTesting(Context context,
359-
@Nullable String provider) {
360-
setVerifiedProvider(context, provider);
361-
}
362-
363-
/**
364-
* Sets the package that this service will accept connections from.
352+
* Sets (asynchronously) the package that this service will accept connections from.
365353
* @param context A context to be used to access SharedPreferences.
366354
* @param provider The package of the provider to accept connections from or null to clear.
355+
* @param callback A {@link Runnable} to be run on completion.
367356
* @hide
368357
*/
369-
public static final void setVerifiedProvider(final Context context,
370-
@Nullable String provider) {
358+
public static final void setVerifiedProvider(final Context context, @Nullable String provider,
359+
@Nullable Runnable callback) {
371360
final String providerEmptyChecked =
372361
(provider == null || provider.isEmpty()) ? null : provider;
373362

374363
// Perform on a background thread as accessing Preferences may cause disk access.
375364
new AsyncTask<Void, Void, Void>() {
376-
377365
@Override
378366
protected Void doInBackground(Void... voids) {
379367
SharedPreferences.Editor editor = getPreferences(context).edit();
@@ -382,9 +370,23 @@ protected Void doInBackground(Void... voids) {
382370
return null;
383371
}
384372

373+
@Override
374+
protected void onPostExecute(Void aVoid) {
375+
if (callback != null) callback.run();
376+
}
385377
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
386378
}
387379

380+
/**
381+
* Sets (asynchronously) the package that this service will accept connections from.
382+
* @param context A context to be used to access SharedPreferences.
383+
* @param provider The package of the provider to accept connections from or null to clear.
384+
* @hide
385+
*/
386+
public static final void setVerifiedProvider(Context context, @Nullable String provider) {
387+
setVerifiedProvider(context, provider, null);
388+
}
389+
388390
private static String channelNameToId(String name) {
389391
return name.toLowerCase(Locale.ROOT).replace(' ', '_') + "_channel_id";
390392
}

customtabs/tests/AndroidManifest.xml

+14-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package="android.support.customtabs">
1818
<uses-sdk android:minSdkVersion="16"/>
1919
<application>
20-
<activity android:name="android.support.customtabs.TestActivity">
20+
<activity android:name="android.support.customtabs.TestActivity"
21+
android:enabled="false">
2122
<!-- A browsable intent filter is required for the TrustedWebActivityService. -->
2223
<intent-filter>
2324
<action android:name="android.intent.action.VIEW"/>
@@ -29,15 +30,18 @@
2930
</intent-filter>
3031
</activity>
3132

32-
<service android:name="android.support.customtabs.PostMessageService"/>
33+
<service android:name="android.support.customtabs.PostMessageService"
34+
android:enabled="false"/>
3335

34-
<service android:name="android.support.customtabs.TestCustomTabsService">
36+
<service android:name="android.support.customtabs.TestCustomTabsService"
37+
android:enabled="false">
3538
<intent-filter>
3639
<category android:name="androidx.browser.trusted.category.TrustedWebActivities" />
3740
</intent-filter>
3841
</service>
3942

40-
<service android:name="android.support.customtabs.TestCustomTabsServiceSupportsTwas">
43+
<service android:name="android.support.customtabs.TestCustomTabsServiceSupportsTwas"
44+
android:enabled="false">
4145
<intent-filter>
4246
<action android:name="android.support.customtabs.action.CustomTabsService" />
4347
<category android:name="androidx.browser.trusted.category.TrustedWebActivities" />
@@ -46,7 +50,8 @@
4650

4751
<activity android:name=".trusted.TestBrowser"
4852
android:theme="@style/Theme.AppCompat.Light"
49-
android:exported="true">
53+
android:exported="true"
54+
android:enabled="false">
5055
<intent-filter>
5156
<action android:name="android.intent.action.VIEW" />
5257
<category android:name="android.intent.category.DEFAULT" />
@@ -57,7 +62,8 @@
5762
</intent-filter>
5863
</activity>
5964

60-
<service android:name="android.support.customtabs.trusted.TestTrustedWebActivityService">
65+
<service android:name="android.support.customtabs.trusted.TestTrustedWebActivityService"
66+
android:enabled="false">
6167
<intent-filter>
6268
<action android:name="android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"/>
6369
<category android:name="android.intent.category.DEFAULT"/>
@@ -66,7 +72,8 @@
6672

6773
<activity android:name="android.support.customtabs.trusted.LauncherActivity"
6874
android:label="Test Launcher Activity"
69-
android:theme="@style/Theme.AppCompat.Light">
75+
android:theme="@style/Theme.AppCompat.Light"
76+
android:enabled="false">
7077

7178
<meta-data android:name="android.support.customtabs.trusted.DEFAULT_URL"
7279
android:value="https://www.test.com/default_url/" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package android.support.customtabs;
2+
3+
import android.content.ComponentName;
4+
import android.content.Context;
5+
import android.content.pm.PackageManager;
6+
import android.support.test.InstrumentationRegistry;
7+
8+
import org.junit.rules.TestWatcher;
9+
import org.junit.runner.Description;
10+
11+
import java.util.Arrays;
12+
import java.util.LinkedList;
13+
import java.util.List;
14+
15+
/**
16+
* The Custom Tabs and Trusted Web Activity functionality require Activities and Services to be
17+
* found in the manifest. We want to make it explicit which Activities/Services are required for
18+
* each test and not let components for one test interfere with another.
19+
*/
20+
public class EnableComponentsTestRule extends TestWatcher {
21+
private final List<Class> mComponents;
22+
23+
/**
24+
* Creates this TestRule which will enable the given components and disable them after the
25+
* tests.
26+
*/
27+
public EnableComponentsTestRule(Class ... components) {
28+
// TODO(peconn): Figure out some generic bounds that allows a list of Classes that are
29+
// either Services or Actvities.
30+
mComponents = Arrays.asList(components);
31+
}
32+
33+
@Override
34+
protected void starting(Description description) {
35+
setEnabled(true);
36+
}
37+
38+
@Override
39+
protected void finished(Description description) {
40+
setEnabled(false);
41+
}
42+
43+
/**
44+
* Manually disables an already enabled component.
45+
*/
46+
public void manuallyDisable(Class clazz) {
47+
// If we were to add a manuallyEnable method, you'd need to add the enabled clazz to
48+
// mComponents to ensure it is disabled after the test.
49+
setComponentEnabled(clazz, false);
50+
}
51+
52+
private void setEnabled(boolean enabled) {
53+
for (Class component : mComponents) {
54+
setComponentEnabled(component, enabled);
55+
}
56+
}
57+
58+
private static void setComponentEnabled(Class clazz, boolean enabled) {
59+
Context context = InstrumentationRegistry.getContext();
60+
PackageManager pm = context.getPackageManager();
61+
ComponentName name = new ComponentName(context, clazz);
62+
63+
int newState = enabled
64+
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
65+
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
66+
int flags = PackageManager.DONT_KILL_APP;
67+
68+
if (pm.getComponentEnabledSetting(name) != newState) {
69+
pm.setComponentEnabledSetting(name, newState, flags);
70+
}
71+
}
72+
}

customtabs/tests/src/android/support/customtabs/PostMessageServiceConnectionTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.support.test.runner.AndroidJUnit4;
2626

2727
import org.junit.Before;
28+
import org.junit.Rule;
2829
import org.junit.Test;
2930
import org.junit.runner.RunWith;
3031

@@ -34,6 +35,11 @@
3435
@RunWith(AndroidJUnit4.class)
3536
@SmallTest
3637
public class PostMessageServiceConnectionTest {
38+
@Rule
39+
public final EnableComponentsTestRule mEnableComponents = new EnableComponentsTestRule(
40+
PostMessageService.class
41+
);
42+
3743
private TestCustomTabsCallback mCallback;
3844
private Context mContext;
3945
private PostMessageServiceConnection mConnection;

customtabs/tests/src/android/support/customtabs/PostMessageTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public class PostMessageTest {
5252
public final ServiceTestRule mServiceRule;
5353
@Rule
5454
public final ActivityTestRule<TestActivity> mActivityTestRule;
55+
@Rule
56+
public final EnableComponentsTestRule mEnableComponents = new EnableComponentsTestRule(
57+
TestActivity.class,
58+
TestCustomTabsService.class,
59+
PostMessageService.class
60+
);
61+
5562
private TestCustomTabsCallback mCallback;
5663
private Context mContext;
5764
private CustomTabsServiceConnection mCustomTabsServiceConnection;

customtabs/tests/src/android/support/customtabs/browseractions/BrowserActionsFallbackMenuUiTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.content.Context;
2323
import android.content.Intent;
2424
import android.net.Uri;
25+
import android.support.customtabs.EnableComponentsTestRule;
2526
import android.support.customtabs.R;
2627
import android.support.customtabs.TestActivity;
2728
import android.support.test.filters.SmallTest;
@@ -52,6 +53,12 @@ public class BrowserActionsFallbackMenuUiTest {
5253
@Rule
5354
public final ActivityTestRule<TestActivity> mActivityTestRule =
5455
new ActivityTestRule<>(TestActivity.class);
56+
57+
@Rule
58+
public final EnableComponentsTestRule mEnableComponents = new EnableComponentsTestRule(
59+
TestActivity.class
60+
);
61+
5562
private Context mContext;
5663
private List<BrowserActionItem> mMenuItems;
5764
private List<String> mMenuItemTitles;

customtabs/tests/src/android/support/customtabs/trusted/LauncherActivityTest.java

+18-33
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import static org.junit.Assert.assertFalse;
2323

2424
import android.app.Instrumentation;
25-
import android.content.ComponentName;
2625
import android.content.Context;
27-
import android.content.pm.PackageManager;
2826
import android.net.Uri;
2927
import android.support.customtabs.CustomTabsIntent;
28+
import android.support.customtabs.EnableComponentsTestRule;
3029
import android.support.customtabs.R;
30+
import android.support.customtabs.TestCustomTabsService;
3131
import android.support.customtabs.TestCustomTabsServiceSupportsTwas;
3232
import android.support.test.InstrumentationRegistry;
3333
import android.support.test.filters.MediumTest;
@@ -56,6 +56,13 @@ public class LauncherActivityTest {
5656

5757
private Context mContext = InstrumentationRegistry.getContext();
5858

59+
@Rule
60+
public final EnableComponentsTestRule mEnableComponents = new EnableComponentsTestRule(
61+
LauncherActivity.class,
62+
TestBrowser.class,
63+
TestCustomTabsServiceSupportsTwas.class,
64+
TestCustomTabsService.class
65+
);
5966
@Rule
6067
public final ActivityTestRule<LauncherActivity> mActivityTestRule =
6168
new ActivityTestRule<>(LauncherActivity.class, false, false);
@@ -72,32 +79,35 @@ public void tearDown() {
7279

7380
@Test
7481
public void launchesTwa() {
75-
launchAsTwa();
82+
launch();
7683
}
7784

7885
@Test
7986
public void readsUrlFromManifest() {
80-
TestBrowser browser = launchAsTwa();
87+
TestBrowser browser = launch();
8188

8289
assertEquals(DEFAULT_URL, browser.getLaunchIntent().getData());
8390
}
8491

8592
@Test
8693
public void readsStatusBarColorFromManifest() {
87-
TestBrowser browser = launchAsTwa();
94+
TestBrowser browser = launch();
8895
checkColor(browser);
8996
}
9097

9198
@Test
9299
public void fallsBackToCustomTab() {
93-
TestBrowser browser = launchAsCustomTab();
100+
mEnableComponents.manuallyDisable(TestCustomTabsServiceSupportsTwas.class);
101+
TestBrowser browser = launch();
94102

95103
assertFalse(browser.getLaunchIntent().hasExtra(EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY));
96104
}
97105

98106
@Test
99107
public void customTabHasStatusBarColor() {
100-
TestBrowser browser = launchAsCustomTab();
108+
mEnableComponents.manuallyDisable(TestCustomTabsServiceSupportsTwas.class);
109+
TestBrowser browser = launch();
110+
101111
checkColor(browser);
102112
}
103113

@@ -110,18 +120,7 @@ private void checkColor(TestBrowser browser) {
110120
assertEquals(expectedColor, requestedColor);
111121
}
112122

113-
private TestBrowser launchAsCustomTab() {
114-
setComponentEnabled(TestCustomTabsServiceSupportsTwas.class, false);
115-
TestBrowser browser = launchAs(TwaProviderPicker.LaunchMode.CUSTOM_TAB);
116-
setComponentEnabled(TestCustomTabsServiceSupportsTwas.class, true);
117-
return browser;
118-
}
119-
120-
private TestBrowser launchAsTwa() {
121-
return launchAs(TwaProviderPicker.LaunchMode.TRUSTED_WEB_ACTIVITY);
122-
}
123-
124-
private TestBrowser launchAs(@TwaProviderPicker.LaunchMode int launchMode) {
123+
private TestBrowser launch() {
125124
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
126125
Instrumentation.ActivityMonitor monitor
127126
= instrumentation.addMonitor(TestBrowser.class.getName(), null, false);
@@ -132,18 +131,4 @@ private TestBrowser launchAs(@TwaProviderPicker.LaunchMode int launchMode) {
132131
instrumentation.removeMonitor(monitor);
133132
return browserActivity;
134133
}
135-
136-
private <T> void setComponentEnabled(Class<T> clazz, boolean enabled) {
137-
PackageManager pm = mContext.getPackageManager();
138-
ComponentName name = new ComponentName(mContext, clazz);
139-
140-
int newState = enabled
141-
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
142-
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
143-
int flags = PackageManager.DONT_KILL_APP;
144-
145-
if (pm.getComponentEnabledSetting(name) != newState) {
146-
pm.setComponentEnabledSetting(name, newState, flags);
147-
}
148-
}
149134
}

0 commit comments

Comments
 (0)