Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / WebContentsObserverAndroidTest.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.content.browser;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8
9 import org.chromium.base.ThreadUtils;
10 import org.chromium.base.test.util.Feature;
11 import org.chromium.base.test.util.UrlUtils;
12 import org.chromium.content.browser.test.util.CallbackHelper;
13 import org.chromium.content_public.browser.WebContents;
14 import org.chromium.content_shell_apk.ContentShellActivity;
15 import org.chromium.content_shell_apk.ContentShellTestBase;
16
17 import java.util.concurrent.Callable;
18
19 /**
20  * Tests for the WebContentsObserverAndroid APIs.
21  */
22 public class WebContentsObserverAndroidTest extends ContentShellTestBase {
23     private static final String URL = UrlUtils.encodeHtmlDataUri(
24             "<html><head></head><body>didFirstVisuallyNonEmptyPaint test</body></html>");
25
26     private static class TestWebContentsObserverAndroid extends WebContentsObserverAndroid {
27         private CallbackHelper mDidFirstVisuallyNonEmptyPaintCallbackHelper = new CallbackHelper();
28
29         public TestWebContentsObserverAndroid(WebContents webContents) {
30             super(webContents);
31         }
32
33         public CallbackHelper getDidFirstVisuallyNonEmptyPaintCallbackHelper() {
34             return mDidFirstVisuallyNonEmptyPaintCallbackHelper;
35         }
36
37         @Override
38         public void didFirstVisuallyNonEmptyPaint() {
39             mDidFirstVisuallyNonEmptyPaintCallbackHelper.notifyCalled();
40         }
41     }
42
43     @Override
44     protected void setUp() throws Exception {
45         super.setUp();
46         ContentShellActivity activity = launchContentShellWithUrl(null);
47         assertNotNull(activity);
48         waitForActiveShellToBeDoneLoading();
49     }
50
51     @SmallTest
52     @Feature({"Navigation"})
53     public void testDidFirstVisuallyNonEmptyPaint() throws Throwable {
54         TestWebContentsObserverAndroid observer = ThreadUtils.runOnUiThreadBlocking(
55                 new Callable<TestWebContentsObserverAndroid>() {
56                     @Override
57                     public TestWebContentsObserverAndroid call() throws Exception {
58                         return new TestWebContentsObserverAndroid(
59                                 getContentViewCore().getWebContents());
60                     }
61                 });
62
63         int callCount = observer.getDidFirstVisuallyNonEmptyPaintCallbackHelper().getCallCount();
64         getInstrumentation().runOnMainSync(new Runnable() {
65             @Override
66             public void run() {
67                 getContentViewCore().loadUrl(new LoadUrlParams(URL));
68             }
69         });
70         observer.getDidFirstVisuallyNonEmptyPaintCallbackHelper().waitForCallback(callCount);
71     }
72 }