1// Copyright 2013 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.chrome.browser.infobar;
6
7import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9import android.test.suitebuilder.annotation.MediumTest;
10import android.test.suitebuilder.annotation.Smoke;
11
12import org.chromium.base.test.util.Feature;
13import org.chromium.base.test.util.UrlUtils;
14import org.chromium.chrome.shell.ChromeShellTestBase;
15import org.chromium.chrome.test.util.InfoBarTestAnimationListener;
16import org.chromium.chrome.test.util.InfoBarUtil;
17import org.chromium.chrome.test.util.TestHttpServerClient;
18import org.chromium.content.browser.test.util.Criteria;
19import org.chromium.content.browser.test.util.CriteriaHelper;
20
21import java.util.List;
22
23public class InfoBarTest extends ChromeShellTestBase {
24    private static final long MAX_TIMEOUT = scaleTimeout(2000);
25    private static final int CHECK_INTERVAL = 500;
26    private static final String GEOLOCATION_PAGE =
27            "chrome/test/data/geolocation/geolocation_on_load.html";
28    private static final String POPUP_PAGE =
29            "chrome/test/data/popup_blocker/popup-window-open.html";
30    public static final String HELLO_WORLD_URL = UrlUtils.encodeHtmlDataUri(
31            "<html>" +
32            "<head><title>Hello, World!</title></head>" +
33            "<body>Hello, World!</body>" +
34            "</html>");
35
36    private InfoBarTestAnimationListener mListener;
37
38    @Override
39    protected void setUp() throws Exception {
40        super.setUp();
41
42        // Register for animation notifications
43        InfoBarContainer container =
44                getActivity().getActiveTab().getInfoBarContainer();
45        mListener =  new InfoBarTestAnimationListener();
46        container.setAnimationListener(mListener);
47    }
48
49    /**
50     * Verify PopUp InfoBar. Only basic triggering verified due to lack of tabs
51     * in ChromeShell
52     */
53    @Smoke
54    @MediumTest
55    @Feature({"Browser", "Main"})
56    public void testInfoBarForPopUp() throws InterruptedException {
57        loadUrlWithSanitization(TestHttpServerClient.getUrl(POPUP_PAGE));
58        assertTrue("InfoBar not added", mListener.addInfoBarAnimationFinished());
59
60        List<InfoBar> infoBars = getActivity().getActiveTab().getInfoBarContainer().getInfoBars();
61        assertEquals("Wrong infobar count", 1, infoBars.size());
62        assertTrue(InfoBarUtil.hasPrimaryButton(this, infoBars.get(0)));
63        assertFalse(InfoBarUtil.hasSecondaryButton(this, infoBars.get(0)));
64        InfoBarUtil.clickPrimaryButton(this, infoBars.get(0));
65        assertTrue("InfoBar not removed.", mListener.removeInfoBarAnimationFinished());
66        assertEquals("Wrong infobar count", 0, infoBars.size());
67
68        // A second load should not show the infobar.
69        loadUrlWithSanitization(TestHttpServerClient.getUrl(POPUP_PAGE));
70        assertFalse("InfoBar added when it should not", mListener.addInfoBarAnimationFinished());
71    }
72
73    /**
74     * Verify Geolocation creates an InfoBar.
75     */
76    @Smoke
77    @MediumTest
78    @Feature({"Browser", "Main"})
79    public void testInfoBarForGeolocation() throws InterruptedException {
80        loadUrlWithSanitization(TestHttpServerClient.getUrl(GEOLOCATION_PAGE));
81        assertTrue("InfoBar not added", mListener.addInfoBarAnimationFinished());
82
83        // Make sure it has OK/Cancel buttons.
84        List<InfoBar> infoBars = getActivity().getActiveTab().getInfoBarContainer().getInfoBars();
85        assertEquals("Wrong infobar count", 1, infoBars.size());
86        assertTrue(InfoBarUtil.hasPrimaryButton(this, infoBars.get(0)));
87        assertTrue(InfoBarUtil.hasSecondaryButton(this, infoBars.get(0)));
88
89        loadUrlWithSanitization(HELLO_WORLD_URL);
90        assertTrue("InfoBar not removed.", mListener.removeInfoBarAnimationFinished());
91        infoBars = getActivity().getActiveTab().getInfoBarContainer().getInfoBars();
92        assertTrue("Wrong infobar count", infoBars.isEmpty());
93    }
94
95
96    /**
97     * Verify Geolocation creates an InfoBar and that it's destroyed when navigating back.
98     *
99     */
100    @MediumTest
101    @Feature({"Browser"})
102    public void testInfoBarForGeolocationDisappearsOnBack() throws InterruptedException {
103        loadUrlWithSanitization(HELLO_WORLD_URL);
104        loadUrlWithSanitization(TestHttpServerClient.getUrl(GEOLOCATION_PAGE));
105        assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
106
107        List<InfoBar> infoBars = getActivity().getActiveTab().getInfoBarContainer().getInfoBars();
108        assertEquals("Wrong infobar count", 1, infoBars.size());
109
110        // Navigate back and ensure the InfoBar has been removed.
111        getInstrumentation().runOnMainSync(
112            new Runnable() {
113                @Override
114                public void run() {
115                    getActivity().getActiveTab().goBack();
116                }
117            });
118        CriteriaHelper.pollForCriteria(
119            new Criteria() {
120                @Override
121                public boolean isSatisfied() {
122                    List<InfoBar> infoBars =
123                            getActivity().getActiveTab().getInfoBarContainer().getInfoBars();
124                    return infoBars.isEmpty();
125                }
126            },
127            MAX_TIMEOUT, CHECK_INTERVAL);
128        assertTrue("InfoBar not removed.", mListener.removeInfoBarAnimationFinished());
129    }
130}
131