ContentViewPopupZoomerTest.java revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
5package org.chromium.content.browser;
6
7import android.test.suitebuilder.annotation.MediumTest;
8import android.view.View;
9
10import org.chromium.base.test.util.DisabledTest;
11import org.chromium.base.test.util.Feature;
12import org.chromium.base.test.util.UrlUtils;
13import org.chromium.content.browser.test.util.Criteria;
14import org.chromium.content.browser.test.util.CriteriaHelper;
15import org.chromium.content.browser.test.util.DOMUtils;
16import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
17import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
18import org.chromium.content_shell_apk.ContentShellTestBase;
19
20import java.util.concurrent.TimeUnit;
21import java.util.concurrent.TimeoutException;
22
23public class ContentViewPopupZoomerTest extends ContentShellTestBase {
24    private static final int WAIT_TIMEOUT_SECONDS = 2;
25
26    private static PopupZoomer findPopupZoomer(ContentView view) {
27        assert view != null;
28        for (int i = 0; i < view.getChildCount(); i++) {
29            View child = view.getChildAt(i);
30            if (child instanceof PopupZoomer) return (PopupZoomer) child;
31        }
32        return null;
33    }
34
35    private static class PopupShowingCriteria implements Criteria {
36        private final ContentView mView;
37        private final boolean mShouldBeShown;
38        public PopupShowingCriteria(ContentView view, boolean shouldBeShown) {
39            mView = view;
40            mShouldBeShown = shouldBeShown;
41        }
42        @Override
43        public boolean isSatisfied() {
44            PopupZoomer popup = findPopupZoomer(mView);
45            boolean isVisibilitySet = popup == null ? false : popup.getVisibility() == View.VISIBLE;
46            return isVisibilitySet ? mShouldBeShown : !mShouldBeShown;
47        }
48    }
49
50    private static class PopupHasNonZeroDimensionsCriteria implements Criteria {
51        private final ContentView mView;
52        public PopupHasNonZeroDimensionsCriteria(ContentView view) {
53            mView = view;
54        }
55        @Override
56        public boolean isSatisfied() {
57            PopupZoomer popup = findPopupZoomer(mView);
58            if (popup == null) return false;
59            return popup.getWidth() != 0 && popup.getHeight() != 0;
60        }
61    }
62
63    private String generateTestUrl(int totalUrls, int targetIdAt, String targetId) {
64        StringBuilder testUrl = new StringBuilder();
65        testUrl.append("<html><body>");
66        for (int i = 0; i < totalUrls; i++) {
67            boolean isTargeted = i == targetIdAt;
68            testUrl.append("<a href=\"data:text/html;utf-8,<html><head><script>" +
69                    "function doesItWork() { return 'yes'; }</script></head></html>\"" +
70                    (isTargeted ? (" id=\"" + targetId + "\"") : "") + ">" +
71                    "<small><sup>" +
72                    (isTargeted ? "<b>" : "") + i + (isTargeted ? "</b>" : "") +
73                    "</sup></small></a>");
74        }
75        testUrl.append("</small></div></body></html>");
76        return UrlUtils.encodeHtmlDataUri(testUrl.toString());
77    }
78
79    public ContentViewPopupZoomerTest() {
80    }
81
82    /**
83     * Tests that shows a zoomer popup and makes sure it has valid dimensions.
84     */
85    //@MediumTest
86    //@Feature({"Browser"})
87    @DisabledTest // crbug.com/167045
88    public void testPopupZoomerShowsUp() throws InterruptedException, TimeoutException {
89        launchContentShellWithUrl(generateTestUrl(100, 15, "clickme"));
90        assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
91
92        final ContentView view = getActivity().getActiveContentView();
93        final TestCallbackHelperContainer viewClient =
94                new TestCallbackHelperContainer(view);
95
96        // The popup should be hidden before the click.
97        assertTrue("The zoomer popup is shown after load.",
98                CriteriaHelper.pollForCriteria(new PopupShowingCriteria(view, false)));
99
100        // Once clicked, the popup should show up.
101        DOMUtils.clickNode(this, view, viewClient, "clickme");
102        assertTrue("The zoomer popup did not show up on click.",
103                CriteriaHelper.pollForCriteria(new PopupShowingCriteria(view, true)));
104
105        // The shown popup should have valid dimensions eventually.
106        assertTrue("The zoomer popup has zero dimensions.",
107                CriteriaHelper.pollForCriteria(new PopupHasNonZeroDimensionsCriteria(view)));
108    }
109}
110