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
5package org.chromium.chrome.testshell;
6
7import org.chromium.content.browser.ContentViewClient;
8import org.chromium.content.browser.test.util.CallbackHelper;
9import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
10import org.chromium.content.browser.test.util.TestContentViewClient;
11import org.chromium.content.browser.test.util.TestContentViewClientWrapper;
12import org.chromium.content.browser.test.util.TestWebContentsObserver;
13
14/**
15 * A utility class that contains methods generic to all Tabs tests.
16 */
17public class TabShellTabUtils {
18    private static TestContentViewClient createTestContentViewClientForTab(TestShellTab tab) {
19        ContentViewClient client = tab.getContentView().getContentViewClient();
20        if (client instanceof TestContentViewClient) return (TestContentViewClient) client;
21
22        TestContentViewClient testClient = new TestContentViewClientWrapper(client);
23        tab.getContentView().setContentViewClient(testClient);
24        return testClient;
25    }
26
27    public static class TestCallbackHelperContainerForTab
28            extends TestCallbackHelperContainer implements TestShellTabObserver {
29        private final OnCloseTabHelper mOnCloseTabHelper;
30        public TestCallbackHelperContainerForTab(TestShellTab tab) {
31            super(createTestContentViewClientForTab(tab),
32                    new TestWebContentsObserver(tab.getContentView().getContentViewCore()));
33            mOnCloseTabHelper = new OnCloseTabHelper();
34            tab.addObserver(this);
35        }
36
37        public static class OnCloseTabHelper extends CallbackHelper {
38        }
39
40        public OnCloseTabHelper getOnCloseTabHelper() {
41            return mOnCloseTabHelper;
42        }
43
44        @Override
45        public void onLoadProgressChanged(TestShellTab tab, int progress) {
46        }
47
48        @Override
49        public void onUpdateUrl(TestShellTab tab, String url) {
50        }
51
52        @Override
53        public void onCloseTab(TestShellTab tab) {
54            mOnCloseTabHelper.notifyCalled();
55        }
56    }
57
58    /**
59     * Creates, binds and returns a TestCallbackHelperContainer for a given Tab.
60     */
61    public static TestCallbackHelperContainerForTab getTestCallbackHelperContainer(
62            final TestShellTab tab) {
63        return tab == null ? null : new TestCallbackHelperContainerForTab(tab);
64    }
65}
66