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