1 // Copyright 2014 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.test.util;
7 import android.view.ContextMenu;
9 import org.chromium.base.ThreadUtils;
10 import org.chromium.chrome.browser.EmptyTabObserver;
11 import org.chromium.chrome.browser.Tab;
12 import org.chromium.content.browser.ContentViewClient;
13 import org.chromium.content.browser.test.util.CallbackHelper;
14 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
15 import org.chromium.content.browser.test.util.TestContentViewClient;
16 import org.chromium.content.browser.test.util.TestContentViewClientWrapper;
17 import org.chromium.content.browser.test.util.TestWebContentsObserver;
19 import java.lang.ref.WeakReference;
20 import java.util.concurrent.atomic.AtomicReference;
23 * A utility class that contains methods generic to all Tabs tests.
25 public class TabUtils {
26 private static TestContentViewClient createTestContentViewClientForTab(Tab tab) {
27 ContentViewClient client = tab.getContentViewCore().getContentViewClient();
28 if (client instanceof TestContentViewClient) return (TestContentViewClient) client;
30 TestContentViewClient testClient = new TestContentViewClientWrapper(client);
31 tab.getContentViewCore().setContentViewClient(testClient);
36 * Provides some callback helpers when waiting for certain tab-based events to occur.
38 public static class TestCallbackHelperContainerForTab extends TestCallbackHelperContainer {
39 private final CallbackHelper mOnCloseTabHelper;
40 private final OnContextMenuShownHelper mOnContextMenuShownHelper;
42 private TestCallbackHelperContainerForTab(Tab tab) {
43 super(createTestContentViewClientForTab(tab),
44 new TestWebContentsObserver(tab.getContentViewCore()));
45 mOnCloseTabHelper = new CallbackHelper();
46 mOnContextMenuShownHelper = new OnContextMenuShownHelper();
47 tab.addObserver(new EmptyTabObserver() {
49 public void onDestroyed(Tab tab) {
50 mOnCloseTabHelper.notifyCalled();
54 public void onContextMenuShown(Tab tab, ContextMenu menu) {
55 mOnContextMenuShownHelper.notifyCalled(menu);
61 * Callback helper that also provides access to the last display ContextMenu.
63 public static class OnContextMenuShownHelper extends CallbackHelper {
64 private WeakReference<ContextMenu> mContextMenu;
66 public void notifyCalled(ContextMenu menu) {
67 mContextMenu = new WeakReference<ContextMenu>(menu);
71 public ContextMenu getContextMenu() {
72 assert getCallCount() > 0;
73 return mContextMenu.get();
77 public CallbackHelper getOnCloseTabHelper() {
78 return mOnCloseTabHelper;
81 public OnContextMenuShownHelper getOnContextMenuShownHelper() {
82 return mOnContextMenuShownHelper;
87 * Creates, binds and returns a TestCallbackHelperContainer for a given Tab.
89 public static TestCallbackHelperContainerForTab getTestCallbackHelperContainer(final Tab tab) {
93 final AtomicReference<TestCallbackHelperContainerForTab> result =
94 new AtomicReference<TestCallbackHelperContainerForTab>();
95 // TODO(yfriedman): Change callers to be executed on the UI thread. Unfortunately this is
96 // super convenient as the caller is nearly always on the test thread which is fine to block
97 // and it's cumbersome to keep bouncing to the UI thread.
98 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
101 result.set(new TestCallbackHelperContainerForTab(tab));