WebViewCompatTest.java revision f9916c36b6536b496976413527bb71f3554282ab
1/* 2 * Copyright 2018 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 17package androidx.webkit; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertTrue; 21import static org.junit.Assert.fail; 22 23import android.support.test.filters.MediumTest; 24import android.support.test.runner.AndroidJUnit4; 25import android.support.v4.os.BuildCompat; 26 27import org.junit.Before; 28import org.junit.Test; 29import org.junit.runner.RunWith; 30 31import java.util.concurrent.CountDownLatch; 32import java.util.concurrent.TimeUnit; 33 34@RunWith(AndroidJUnit4.class) 35public class WebViewCompatTest { 36 WebViewOnUiThread mWebViewOnUiThread; 37 38 private static final long TEST_TIMEOUT = 20000L; 39 40 @Before 41 public void setUp() { 42 mWebViewOnUiThread = new androidx.webkit.WebViewOnUiThread(); 43 } 44 45 @MediumTest 46 @Test 47 public void testVisualStateCallbackCalled() throws Exception { 48 // TODO(gsennton) activate this test for pre-P devices when we can pre-install a WebView APK 49 // containing support for the WebView Support Library, see b/73454652. 50 if (!BuildCompat.isAtLeastP()) return; 51 52 final CountDownLatch callbackLatch = new CountDownLatch(1); 53 final long kRequest = 100; 54 55 mWebViewOnUiThread.loadUrl("about:blank"); 56 57 mWebViewOnUiThread.postVisualStateCallbackCompat(kRequest, 58 new WebViewCompat.VisualStateCallback() { 59 public void onComplete(long requestId) { 60 assertEquals(kRequest, requestId); 61 callbackLatch.countDown(); 62 } 63 }); 64 65 assertTrue(callbackLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)); 66 } 67 68 @MediumTest 69 @Test 70 public void testCheckThread() { 71 try { 72 WebViewCompat.postVisualStateCallback(mWebViewOnUiThread.getWebViewOnCurrentThread(), 5, 73 new WebViewCompat.VisualStateCallback() { 74 @Override 75 public void onComplete(long requestId) { 76 } 77 }); 78 } catch (RuntimeException e) { 79 return; 80 } 81 fail("Calling a WebViewCompat method on the wrong thread must cause a run-time exception"); 82 } 83} 84