Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / chrome / test / android / javatests / src / org / chromium / chrome / test / util / TabUtils.java
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.
4
5 package org.chromium.chrome.test.util;
6
7 import android.view.ContextMenu;
8
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;
18
19 import java.lang.ref.WeakReference;
20 import java.util.concurrent.atomic.AtomicReference;
21
22 /**
23  * A utility class that contains methods generic to all Tabs tests.
24  */
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;
29
30         TestContentViewClient testClient = new TestContentViewClientWrapper(client);
31         tab.getContentViewCore().setContentViewClient(testClient);
32         return testClient;
33     }
34
35     /**
36      * Provides some callback helpers when waiting for certain tab-based events to occur.
37      */
38     public static class TestCallbackHelperContainerForTab extends TestCallbackHelperContainer {
39         private final CallbackHelper mOnCloseTabHelper;
40         private final OnContextMenuShownHelper mOnContextMenuShownHelper;
41
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() {
48                 @Override
49                 public void onDestroyed(Tab tab) {
50                     mOnCloseTabHelper.notifyCalled();
51                 }
52
53                 @Override
54                 public void onContextMenuShown(Tab tab, ContextMenu menu) {
55                     mOnContextMenuShownHelper.notifyCalled(menu);
56                 }
57             });
58         }
59
60         /**
61          * Callback helper that also provides access to the last display ContextMenu.
62          */
63         public static class OnContextMenuShownHelper extends CallbackHelper {
64             private WeakReference<ContextMenu> mContextMenu;
65
66             public void notifyCalled(ContextMenu menu) {
67                 mContextMenu = new WeakReference<ContextMenu>(menu);
68                 notifyCalled();
69             }
70
71             public ContextMenu getContextMenu() {
72                 assert getCallCount() > 0;
73                 return mContextMenu.get();
74             }
75         }
76
77         public CallbackHelper getOnCloseTabHelper() {
78             return mOnCloseTabHelper;
79         }
80
81         public OnContextMenuShownHelper getOnContextMenuShownHelper() {
82             return mOnContextMenuShownHelper;
83         }
84     }
85
86     /**
87      * Creates, binds and returns a TestCallbackHelperContainer for a given Tab.
88      */
89     public static TestCallbackHelperContainerForTab getTestCallbackHelperContainer(final Tab tab) {
90         if (tab == null) {
91             return null;
92         }
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() {
99             @Override
100             public void run() {
101                 result.set(new TestCallbackHelperContainerForTab(tab));
102             }
103         });
104         return result.get();
105     }
106 }