- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / input / SelectPopupTest.java
1 // Copyright (c) 2012 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.input;
6
7 import android.test.suitebuilder.annotation.LargeTest;
8
9 import org.chromium.base.test.util.Feature;
10 import org.chromium.base.test.util.UrlUtils;
11 import org.chromium.content.browser.ContentView;
12 import org.chromium.content.browser.test.util.Criteria;
13 import org.chromium.content.browser.test.util.CriteriaHelper;
14 import org.chromium.content.browser.test.util.DOMUtils;
15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
17 import org.chromium.content_shell_apk.ContentShellTestBase;
18
19 import java.util.concurrent.TimeUnit;
20
21 public class SelectPopupTest extends ContentShellTestBase {
22     private static final int WAIT_TIMEOUT_SECONDS = 2;
23     private static final String SELECT_URL = UrlUtils.encodeHtmlDataUri(
24             "<html><head><meta name=\"viewport\"" +
25             "content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0\" /></head>" +
26             "<body>Which animal is the strongest:<br/>" +
27             "<select id=\"select\">" +
28             "<option>Black bear</option>" +
29             "<option>Polar bear</option>" +
30             "<option>Grizzly</option>" +
31             "<option>Tiger</option>" +
32             "<option>Lion</option>" +
33             "<option>Gorilla</option>" +
34             "<option>Chipmunk</option>" +
35             "</select>" +
36             "</body></html>");
37
38     private static class PopupShowingCriteria implements Criteria {
39         @Override
40         public boolean isSatisfied() {
41             return SelectPopupDialog.getCurrent() != null;
42         }
43     }
44
45     private static class PopupHiddenCriteria implements Criteria {
46         @Override
47         public boolean isSatisfied() {
48             return SelectPopupDialog.getCurrent() == null;
49         }
50     }
51
52     @Override
53     public void setUp() throws Exception {
54         super.setUp();
55         launchContentShellWithUrl(SELECT_URL);
56         assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
57         // TODO(aurimas) remove this wait once crbug.com/179511 is fixed.
58         assertWaitForPageScaleFactorMatch(1);
59     }
60
61     /**
62      * Tests that showing a select popup and having the page reload while the popup is showing does
63      * not assert.
64      */
65     @LargeTest
66     @Feature({"Browser"})
67     public void testReloadWhilePopupShowing() throws InterruptedException, Exception, Throwable {
68         // The popup should be hidden before the click.
69         assertTrue("The select popup is shown after load.",
70                 CriteriaHelper.pollForCriteria(new PopupHiddenCriteria()));
71
72         final ContentView view = getActivity().getActiveContentView();
73         final TestCallbackHelperContainer viewClient =
74                 new TestCallbackHelperContainer(view);
75         final OnPageFinishedHelper onPageFinishedHelper =
76                 viewClient.getOnPageFinishedHelper();
77
78         // Once clicked, the popup should show up.
79         DOMUtils.clickNode(this, view, viewClient, "select");
80         assertTrue("The select popup did not show up on click.",
81                 CriteriaHelper.pollForCriteria(new PopupShowingCriteria()));
82
83         // Reload the test page.
84         int currentCallCount = onPageFinishedHelper.getCallCount();
85         getInstrumentation().runOnMainSync(new Runnable() {
86             @Override
87             public void run() {
88                 // Now reload the page while the popup is showing, it gets hidden.
89                 getActivity().getActiveShell().loadUrl(SELECT_URL);
90             }
91         });
92         onPageFinishedHelper.waitForCallback(currentCallCount, 1,
93                 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
94
95         // The popup should be hidden after the page reload.
96         assertTrue("The select popup did not hide after reload.",
97                 CriteriaHelper.pollForCriteria(new PopupHiddenCriteria()));
98
99         // Click the select and wait for the popup to show.
100         DOMUtils.clickNode(this, view, viewClient, "select");
101         assertTrue("The select popup did not show on click after reload.",
102                 CriteriaHelper.pollForCriteria(new PopupShowingCriteria()));
103     }
104 }