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 org.hamcrest.CoreMatchers.nullValue;
19 import static org.hamcrest.MatcherAssert.assertThat;
20 
21 import android.app.Instrumentation;
22 import android.content.Intent;
23 import android.graphics.Color;
24 import android.test.ActivityUnitTestCase;
25 import android.view.ViewGroup;
26 import android.webkit.WebView;
27 
28 import androidx.test.InstrumentationRegistry;
29 import androidx.test.filters.SmallTest;
30 
31 import com.android.managedprovisioning.common.CustomizationVerifier;
32 
33 import java.util.concurrent.atomic.AtomicReference;
34 
35 @SmallTest
36 public class WebActivityTest extends ActivityUnitTestCase<WebActivity> {
37     private static final String TEST_URL = "http://www.test.com/support";
38     private static final int STATUS_BAR_COLOR = Color.parseColor("#BDBDBD"); // any color would do
39 
WebActivityTest()40     public WebActivityTest() {
41         super(WebActivity.class);
42     }
43 
testNoUrl()44     public void testNoUrl() {
45         Intent intent = WebActivity.createIntent(getInstrumentation().getTargetContext(), null,
46                 STATUS_BAR_COLOR);
47         assertThat(intent, nullValue());
48     }
49 
testUrlLaunched()50     public void testUrlLaunched() {
51         startActivityOnMainSync(WebActivity.createIntent(getInstrumentation().getTargetContext(),
52                 TEST_URL, STATUS_BAR_COLOR));
53         assertFalse(isFinishCalled());
54         final AtomicReference<String> urlRef = new AtomicReference<>(null);
55         getInstrumentation().runOnMainSync(() ->
56                 urlRef.set(((WebView) ((ViewGroup) getActivity()
57                         .findViewById(android.R.id.content)).getChildAt(0)).getUrl()));
58         assertEquals(TEST_URL, urlRef.get());
59         new CustomizationVerifier(getActivity()).assertStatusBarColorCorrect(STATUS_BAR_COLOR);
60     }
61 
startActivityOnMainSync(final Intent intent)62     private void startActivityOnMainSync(final Intent intent) {
63         getInstrumentation().runOnMainSync(() -> startActivity(intent, null, null));
64     }
65 
66     @Override
getInstrumentation()67     public Instrumentation getInstrumentation() {
68         return InstrumentationRegistry.getInstrumentation();
69     }
70 }