1 /*
2  * Copyright (C) 2016 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 package com.android.managedprovisioning.preprovisioning;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.app.Activity;
21 import android.app.Instrumentation;
22 import android.content.Intent;
23 import android.view.ViewGroup;
24 import android.webkit.WebView;
25 
26 import androidx.test.InstrumentationRegistry;
27 import androidx.test.filters.SmallTest;
28 import androidx.test.rule.ActivityTestRule;
29 
30 import org.junit.Ignore;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.junit.MockitoJUnitRunner;
35 
36 import java.util.concurrent.atomic.AtomicReference;
37 
38 @SmallTest
39 @RunWith(MockitoJUnitRunner.class)
40 public class WebActivityTest {
41     private static final String TEST_URL = "http://www.test.com/support";
42 
43     @Rule
44     public ActivityTestRule<WebActivity> mActivityRule = new ActivityTestRule<>(
45             WebActivity.class, true, false);
46     private Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
47 
48     @Test
testNoUrl()49     public void testNoUrl() {
50         Intent intent = WebActivity.createIntent(mInstrumentation.getTargetContext(), null);
51 
52         assertThat(intent).isNull();
53     }
54 
55     @Ignore("b/181323689")
56     @Test
testUrlLaunched()57     public void testUrlLaunched() {
58         final AtomicReference<String> urlRef = new AtomicReference<>(null);
59 
60         Activity activity = mActivityRule.launchActivity(
61                 WebActivity.createIntent(mInstrumentation.getTargetContext(),
62                         TEST_URL));
63         InstrumentationRegistry.getInstrumentation().runOnMainSync(() ->
64                 urlRef.set(((WebView) ((ViewGroup) activity
65                         .findViewById(android.R.id.content)).getChildAt(0)).getUrl()));
66 
67         assertThat(activity.isFinishing()).isFalse();
68         assertThat(urlRef.get()).isEqualTo(TEST_URL);
69     }
70 }