| OLD | NEW |
| (Empty) |
| 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 | |
| 5 package org.chromium.chrome.test.util; | |
| 6 | |
| 7 import android.view.ContextMenu; | |
| 8 | |
| 9 import org.chromium.chrome.browser.EmptyTabObserver; | |
| 10 import org.chromium.chrome.browser.TabBase; | |
| 11 import org.chromium.content.browser.ContentViewClient; | |
| 12 import org.chromium.content.browser.test.util.CallbackHelper; | |
| 13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | |
| 14 import org.chromium.content.browser.test.util.TestContentViewClient; | |
| 15 import org.chromium.content.browser.test.util.TestContentViewClientWrapper; | |
| 16 import org.chromium.content.browser.test.util.TestWebContentsObserver; | |
| 17 | |
| 18 import java.lang.ref.WeakReference; | |
| 19 | |
| 20 /** | |
| 21 * A utility class that contains methods generic to all Tabs tests. | |
| 22 */ | |
| 23 public class TabBaseTabUtils { | |
| 24 private static TestContentViewClient createTestContentViewClientForTab(TabBa
se tab) { | |
| 25 ContentViewClient client = tab.getContentView().getContentViewClient(); | |
| 26 if (client instanceof TestContentViewClient) return (TestContentViewClie
nt) client; | |
| 27 | |
| 28 TestContentViewClient testClient = new TestContentViewClientWrapper(clie
nt); | |
| 29 tab.getContentView().setContentViewClient(testClient); | |
| 30 return testClient; | |
| 31 } | |
| 32 | |
| 33 public static class TestCallbackHelperContainerForTab extends TestCallbackHe
lperContainer { | |
| 34 private final OnCloseTabHelper mOnCloseTabHelper; | |
| 35 private final OnContextMenuShownHelper mOnContextMenuShownHelper; | |
| 36 | |
| 37 public TestCallbackHelperContainerForTab(TabBase tab) { | |
| 38 super(createTestContentViewClientForTab(tab), | |
| 39 new TestWebContentsObserver(tab.getContentView().getContentV
iewCore())); | |
| 40 mOnCloseTabHelper = new OnCloseTabHelper(); | |
| 41 mOnContextMenuShownHelper = new OnContextMenuShownHelper(); | |
| 42 tab.addObserver(new EmptyTabObserver() { | |
| 43 @Override | |
| 44 public void onDestroyed(TabBase tab) { | |
| 45 mOnCloseTabHelper.notifyCalled(); | |
| 46 } | |
| 47 | |
| 48 @Override | |
| 49 public void onContextMenuShown(TabBase tab, ContextMenu menu) { | |
| 50 mOnContextMenuShownHelper.notifyCalled(menu); | |
| 51 } | |
| 52 }); | |
| 53 } | |
| 54 | |
| 55 public static class OnCloseTabHelper extends CallbackHelper { | |
| 56 } | |
| 57 | |
| 58 public static class OnContextMenuShownHelper extends CallbackHelper { | |
| 59 private WeakReference<ContextMenu> mContextMenu; | |
| 60 | |
| 61 public void notifyCalled(ContextMenu menu) { | |
| 62 mContextMenu = new WeakReference<ContextMenu>(menu); | |
| 63 notifyCalled(); | |
| 64 } | |
| 65 | |
| 66 public ContextMenu getContextMenu() { | |
| 67 assert getCallCount() > 0; | |
| 68 return mContextMenu.get(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 public OnCloseTabHelper getOnCloseTabHelper() { | |
| 73 return mOnCloseTabHelper; | |
| 74 } | |
| 75 | |
| 76 public OnContextMenuShownHelper getOnContextMenuShownHelper() { | |
| 77 return mOnContextMenuShownHelper; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Creates, binds and returns a TestCallbackHelperContainer for a given Tab. | |
| 83 */ | |
| 84 public static TestCallbackHelperContainerForTab getTestCallbackHelperContain
er(TabBase tab) { | |
| 85 return tab == null ? null : new TestCallbackHelperContainerForTab(tab); | |
| 86 } | |
| 87 } | |
| OLD | NEW |