forked from auth0/Auth0.Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTabsOptionsTest.java
More file actions
227 lines (179 loc) · 10.1 KB
/
CustomTabsOptionsTest.java
File metadata and controls
227 lines (179 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.auth0.android.provider;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Parcel;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class CustomTabsOptionsTest {
private Activity context;
@Before
public void setUp() {
context = Robolectric.setupActivity(Activity.class);
}
@Test
public void shouldCreateNewBuilder() {
CustomTabsOptions.Builder builder = CustomTabsOptions.newBuilder();
assertThat(builder, is(notNullValue()));
}
@Test
public void shouldHaveCompatibleBrowser() {
PackageManager pm = mock(PackageManager.class);
BrowserPicker browserPicker = mock(BrowserPicker.class);
when(browserPicker.getBestBrowserPackage(any(PackageManager.class))).thenReturn("something");
CustomTabsOptions options = CustomTabsOptions.newBuilder()
.withBrowserPicker(browserPicker).build();
assertThat(options.getPreferredPackage(pm), is("something"));
assertThat(options.hasCompatibleBrowser(pm), is(true));
}
@Test
public void shouldNotHaveCompatibleBrowser() {
PackageManager pm = mock(PackageManager.class);
BrowserPicker browserPicker = mock(BrowserPicker.class);
when(browserPicker.getBestBrowserPackage(any(PackageManager.class))).thenReturn(null);
CustomTabsOptions options = CustomTabsOptions.newBuilder()
.withBrowserPicker(browserPicker).build();
assertThat(options.getPreferredPackage(pm), is(nullValue()));
assertThat(options.hasCompatibleBrowser(pm), is(false));
}
@Test
public void shouldHaveDefaultValues() {
CustomTabsOptions options = CustomTabsOptions.newBuilder().build();
assertThat(options, is(notNullValue()));
Intent intent = options.toIntent(context, null);
assertThat(intent, is(notNullValue()));
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(false));
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(true));
assertThat(intent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(0));
assertThat(intent.getIntExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE), is(CustomTabsIntent.NO_TITLE));
assertThat(intent.getIntExtra(CustomTabsIntent.EXTRA_SHARE_STATE, CustomTabsIntent.SHARE_STATE_OFF), is(CustomTabsIntent.SHARE_STATE_OFF));
Parcel parcel = Parcel.obtain();
options.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
assertThat(parceledOptions, is(notNullValue()));
Intent parceledIntent = parceledOptions.toIntent(context, null);
assertThat(parceledIntent, is(notNullValue()));
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(false));
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(true));
assertThat(parceledIntent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(0));
assertThat(parceledIntent.getIntExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE), is(CustomTabsIntent.NO_TITLE));
assertThat(parceledIntent.getIntExtra(CustomTabsIntent.EXTRA_SHARE_STATE, CustomTabsIntent.SHARE_STATE_OFF), is(CustomTabsIntent.SHARE_STATE_OFF));
}
@Test
public void shouldSetShowTitle() {
CustomTabsOptions options = CustomTabsOptions.newBuilder()
.showTitle(true)
.build();
assertThat(options, is(notNullValue()));
Intent intent = options.toIntent(context, null);
assertThat(intent, is(notNullValue()));
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(true));
assertThat(intent.getIntExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE), is(CustomTabsIntent.SHOW_PAGE_TITLE));
Parcel parcel = Parcel.obtain();
options.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
assertThat(parceledOptions, is(notNullValue()));
Intent parceledIntent = parceledOptions.toIntent(context, null);
assertThat(parceledIntent, is(notNullValue()));
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(true));
assertThat(parceledIntent.getIntExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE), is(CustomTabsIntent.SHOW_PAGE_TITLE));
}
@Test
public void shouldSetToolbarColor() {
CustomTabsOptions options = CustomTabsOptions.newBuilder()
.withToolbarColor(android.R.color.black)
.build();
assertThat(options, is(notNullValue()));
Intent intent = options.toIntent(context, null);
assertThat(intent, is(notNullValue()));
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(true));
int resolvedColor = ContextCompat.getColor(context, android.R.color.black);
assertThat(intent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(resolvedColor));
Parcel parcel = Parcel.obtain();
options.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
assertThat(parceledOptions, is(notNullValue()));
Intent parceledIntent = parceledOptions.toIntent(context, null);
assertThat(parceledIntent, is(notNullValue()));
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(true));
assertThat(parceledIntent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(resolvedColor));
}
@Test
public void shouldSetBrowserPicker() {
Activity activity = spy(Robolectric.setupActivity(Activity.class));
BrowserPickerTest.setupBrowserContext(activity, Collections.singletonList("com.auth0.browser"), null, null);
BrowserPicker browserPicker = BrowserPicker.newBuilder().build();
CustomTabsOptions options = CustomTabsOptions.newBuilder()
.withBrowserPicker(browserPicker)
.build();
assertThat(options, is(notNullValue()));
String preferredPackageBefore = options.getPreferredPackage(activity.getPackageManager());
assertThat(preferredPackageBefore, is("com.auth0.browser"));
Parcel parcel = Parcel.obtain();
options.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
assertThat(parceledOptions, is(notNullValue()));
String preferredPackageNow = parceledOptions.getPreferredPackage(activity.getPackageManager());
assertThat(preferredPackageNow, is("com.auth0.browser"));
}
@Test
public void shouldSetDisabledCustomTabPackages() {
Activity activity = spy(Robolectric.setupActivity(Activity.class));
BrowserPickerTest.setupBrowserContext(activity, Collections.singletonList("com.auth0.browser"), null, null);
BrowserPicker browserPicker = BrowserPicker.newBuilder().build();
CustomTabsOptions options = CustomTabsOptions.newBuilder()
.withBrowserPicker(browserPicker)
.withDisabledCustomTabsPackages(List.of("com.auth0.browser"))
.withToolbarColor(android.R.color.black)
.build();
assertThat(options, is(notNullValue()));
Intent intentNoExtras = options.toIntent(activity, null);
assertThat(intentNoExtras, is(notNullValue()));
assertThat(intentNoExtras.getExtras(), is(nullValue()));
assertEquals(intentNoExtras.getAction(), "android.intent.action.VIEW");
Parcel parcel = Parcel.obtain();
options.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
assertThat(parceledOptions, is(notNullValue()));
Intent parceledIntent = parceledOptions.toIntent(activity, null);
assertThat(parceledIntent, is(notNullValue()));
assertThat(parceledIntent.getExtras(), is(nullValue()));
assertEquals(parceledIntent.getAction(), "android.intent.action.VIEW");
BrowserPickerTest.setupBrowserContext(activity, Collections.singletonList("com.another.browser"), null, null);
BrowserPicker browserPicker2 = BrowserPicker.newBuilder().build();
CustomTabsOptions options2 = CustomTabsOptions.newBuilder()
.withBrowserPicker(browserPicker2)
.withDisabledCustomTabsPackages(List.of("com.auth0.browser"))
.withToolbarColor(android.R.color.black)
.build();
Intent intentWithToolbarExtra = options2.toIntent(activity, null);
assertThat(intentWithToolbarExtra, is(notNullValue()));
assertThat(intentWithToolbarExtra.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(true));
int resolvedColor = ContextCompat.getColor(activity, android.R.color.black);
assertThat(intentWithToolbarExtra.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(resolvedColor));
}
}