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