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.
5 package org.chromium.chrome.browser;
7 import android.graphics.Bitmap;
8 import android.test.suitebuilder.annotation.MediumTest;
9 import android.test.suitebuilder.annotation.SmallTest;
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.base.test.util.UrlUtils;
14 import org.chromium.chrome.shell.ChromeShellActivity;
15 import org.chromium.chrome.shell.ChromeShellTestBase;
16 import org.chromium.content.browser.NavigationClient;
17 import org.chromium.content.browser.test.util.Criteria;
18 import org.chromium.content.browser.test.util.CriteriaHelper;
19 import org.chromium.content_public.browser.NavigationEntry;
20 import org.chromium.content_public.browser.NavigationHistory;
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.ExecutionException;
26 * Tests for the navigation popup.
28 public class NavigationPopupTest extends ChromeShellTestBase {
30 private static final int INVALID_NAVIGATION_INDEX = -1;
32 private ChromeShellActivity mActivity;
35 public void setUp() throws Exception {
38 mActivity = launchChromeShellWithBlankPage();
41 // Exists solely to expose protected methods to this test.
42 private static class TestNavigationHistory extends NavigationHistory {
44 public void addEntry(NavigationEntry entry) {
45 super.addEntry(entry);
49 // Exists solely to expose protected methods to this test.
50 private static class TestNavigationEntry extends NavigationEntry {
51 public TestNavigationEntry(int index, String url, String virtualUrl, String originalUrl,
52 String title, Bitmap favicon) {
53 super(index, url, virtualUrl, originalUrl, title, favicon);
57 private static class TestNavigationClient implements NavigationClient {
58 private TestNavigationHistory mHistory;
59 private int mNavigatedIndex = INVALID_NAVIGATION_INDEX;
61 public TestNavigationClient() {
62 mHistory = new TestNavigationHistory();
63 mHistory.addEntry(new TestNavigationEntry(
64 1, "about:blank", null, null, "About Blank", null));
65 mHistory.addEntry(new TestNavigationEntry(
66 5, UrlUtils.encodeHtmlDataUri("<html>1</html>"), null, null, null, null));
70 public NavigationHistory getDirectedNavigationHistory(boolean isForward, int itemLimit) {
75 public void goToNavigationIndex(int index) {
76 mNavigatedIndex = index;
81 @Feature({"Navigation"})
82 public void testFaviconFetching() throws InterruptedException {
83 final TestNavigationClient client = new TestNavigationClient();
84 final NavigationPopup popup = new NavigationPopup(
85 mActivity, client, true);
87 popup.setAnchorView(mActivity.getActiveContentViewCore().getContainerView());
88 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
95 assertTrue("All favicons did not get updated.",
96 CriteriaHelper.pollForCriteria(new Criteria() {
98 public boolean isSatisfied() {
100 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
102 public Boolean call() throws Exception {
103 NavigationHistory history = client.mHistory;
104 for (int i = 0; i < history.getEntryCount(); i++) {
105 if (history.getEntryAtIndex(i).getFavicon() == null) return false;
110 } catch (ExecutionException e) {
116 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
125 @Feature({"Navigation"})
126 public void testItemSelection() {
127 final TestNavigationClient client = new TestNavigationClient();
128 final NavigationPopup popup = new NavigationPopup(
129 mActivity, client, true);
131 popup.setAnchorView(mActivity.getActiveContentViewCore().getContainerView());
132 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
139 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
142 popup.performItemClick(1);
146 assertFalse("Popup did not hide as expected.", popup.isShowing());
147 assertEquals("Popup attempted to navigate to the wrong index", 5, client.mNavigatedIndex);