- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / android / testshell / javatests / src / org / chromium / chrome / testshell / TabShellTabUtils.java
1 // Copyright (c) 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.testshell;
6
7 import org.chromium.chrome.browser.EmptyTabObserver;
8 import org.chromium.chrome.browser.TabBase;
9 import org.chromium.content.browser.ContentViewClient;
10 import org.chromium.content.browser.test.util.CallbackHelper;
11 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
12 import org.chromium.content.browser.test.util.TestContentViewClient;
13 import org.chromium.content.browser.test.util.TestContentViewClientWrapper;
14 import org.chromium.content.browser.test.util.TestWebContentsObserver;
15
16 /**
17  * A utility class that contains methods generic to all Tabs tests.
18  */
19 public class TabShellTabUtils {
20     private static TestContentViewClient createTestContentViewClientForTab(TestShellTab tab) {
21         ContentViewClient client = tab.getContentView().getContentViewClient();
22         if (client instanceof TestContentViewClient) return (TestContentViewClient) client;
23
24         TestContentViewClient testClient = new TestContentViewClientWrapper(client);
25         tab.getContentView().setContentViewClient(testClient);
26         return testClient;
27     }
28
29     public static class TestCallbackHelperContainerForTab extends TestCallbackHelperContainer {
30         private final OnCloseTabHelper mOnCloseTabHelper;
31         public TestCallbackHelperContainerForTab(TestShellTab tab) {
32             super(createTestContentViewClientForTab(tab),
33                     new TestWebContentsObserver(tab.getContentView().getContentViewCore()));
34             mOnCloseTabHelper = new OnCloseTabHelper();
35             tab.addObserver(new EmptyTabObserver() {
36                 @Override
37                 public void onDestroyed(TabBase tab) {
38                     mOnCloseTabHelper.notifyCalled();
39                 }
40             });
41         }
42
43         public static class OnCloseTabHelper extends CallbackHelper {
44         }
45
46         public OnCloseTabHelper getOnCloseTabHelper() {
47             return mOnCloseTabHelper;
48         }
49     }
50
51     /**
52      * Creates, binds and returns a TestCallbackHelperContainer for a given Tab.
53      */
54     public static TestCallbackHelperContainerForTab getTestCallbackHelperContainer(
55             final TestShellTab tab) {
56         return tab == null ? null : new TestCallbackHelperContainerForTab(tab);
57     }
58 }