22
22
import static org .junit .Assert .assertFalse ;
23
23
24
24
import android .app .Instrumentation ;
25
- import android .content .ComponentName ;
26
25
import android .content .Context ;
27
- import android .content .pm .PackageManager ;
28
26
import android .net .Uri ;
29
27
import android .support .customtabs .CustomTabsIntent ;
28
+ import android .support .customtabs .EnableComponentsTestRule ;
30
29
import android .support .customtabs .R ;
30
+ import android .support .customtabs .TestCustomTabsService ;
31
31
import android .support .customtabs .TestCustomTabsServiceSupportsTwas ;
32
32
import android .support .test .InstrumentationRegistry ;
33
33
import android .support .test .filters .MediumTest ;
@@ -56,6 +56,13 @@ public class LauncherActivityTest {
56
56
57
57
private Context mContext = InstrumentationRegistry .getContext ();
58
58
59
+ @ Rule
60
+ public final EnableComponentsTestRule mEnableComponents = new EnableComponentsTestRule (
61
+ LauncherActivity .class ,
62
+ TestBrowser .class ,
63
+ TestCustomTabsServiceSupportsTwas .class ,
64
+ TestCustomTabsService .class
65
+ );
59
66
@ Rule
60
67
public final ActivityTestRule <LauncherActivity > mActivityTestRule =
61
68
new ActivityTestRule <>(LauncherActivity .class , false , false );
@@ -72,32 +79,35 @@ public void tearDown() {
72
79
73
80
@ Test
74
81
public void launchesTwa () {
75
- launchAsTwa ();
82
+ launch ();
76
83
}
77
84
78
85
@ Test
79
86
public void readsUrlFromManifest () {
80
- TestBrowser browser = launchAsTwa ();
87
+ TestBrowser browser = launch ();
81
88
82
89
assertEquals (DEFAULT_URL , browser .getLaunchIntent ().getData ());
83
90
}
84
91
85
92
@ Test
86
93
public void readsStatusBarColorFromManifest () {
87
- TestBrowser browser = launchAsTwa ();
94
+ TestBrowser browser = launch ();
88
95
checkColor (browser );
89
96
}
90
97
91
98
@ Test
92
99
public void fallsBackToCustomTab () {
93
- TestBrowser browser = launchAsCustomTab ();
100
+ mEnableComponents .manuallyDisable (TestCustomTabsServiceSupportsTwas .class );
101
+ TestBrowser browser = launch ();
94
102
95
103
assertFalse (browser .getLaunchIntent ().hasExtra (EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY ));
96
104
}
97
105
98
106
@ Test
99
107
public void customTabHasStatusBarColor () {
100
- TestBrowser browser = launchAsCustomTab ();
108
+ mEnableComponents .manuallyDisable (TestCustomTabsServiceSupportsTwas .class );
109
+ TestBrowser browser = launch ();
110
+
101
111
checkColor (browser );
102
112
}
103
113
@@ -110,18 +120,7 @@ private void checkColor(TestBrowser browser) {
110
120
assertEquals (expectedColor , requestedColor );
111
121
}
112
122
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 () {
125
124
Instrumentation instrumentation = InstrumentationRegistry .getInstrumentation ();
126
125
Instrumentation .ActivityMonitor monitor
127
126
= instrumentation .addMonitor (TestBrowser .class .getName (), null , false );
@@ -132,18 +131,4 @@ private TestBrowser launchAs(@TwaProviderPicker.LaunchMode int launchMode) {
132
131
instrumentation .removeMonitor (monitor );
133
132
return browserActivity ;
134
133
}
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
- }
149
134
}
0 commit comments