OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 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 #include "base/command_line.h" | |
6 #include "base/files/file_path.h" | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/path_service.h" | |
9 #include "chrome/browser/extensions/extension_browsertest.h" | |
10 #include "chrome/browser/extensions/extension_service.h" | |
11 #include "chrome/browser/extensions/sidebar_manager.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/browser_window.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "chrome/browser/ui/views/frame/browser_view.h" | |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "chrome/test/base/in_process_browser_test.h" | |
20 #include "chrome/test/base/ui_test_utils.h" | |
21 #include "content/public/browser/web_contents.h" | |
22 #include "extensions/common/extension.h" | |
23 | |
24 using content::NavigationController; | |
25 using content::WebContents; | |
26 using extensions::SidebarManager; | |
27 | |
28 namespace { | |
29 | |
30 const char kSimplePage[] = "/simple_page.html"; | |
31 | |
32 class SidebarTest : public ExtensionBrowserTest { | |
Devlin
2015/06/16 17:56:12
Is there a reason this needs to be a browser test?
Devlin
2015/06/19 19:56:08
Two notes:
- When replying to specific comments in
| |
33 public: | |
34 SidebarTest() {} | |
35 | |
36 protected: | |
37 // InProcessBrowserTest overrides. | |
38 void SetUpOnMainThread() override { | |
39 ExtensionBrowserTest::SetUpOnMainThread(); | |
40 | |
41 // Load test sidebar extension. | |
42 base::FilePath extension_path; | |
43 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path)); | |
44 extension_path = extension_path.AppendASCII("sidebar"); | |
45 | |
46 ASSERT_TRUE(LoadExtension(extension_path)); | |
47 | |
48 // For now content_id == extension_id. | |
49 content_id_ = last_loaded_extension_id(); | |
50 } | |
51 | |
52 void CreateSidebarForCurrentTab() { | |
53 CreateSidebar(browser()->tab_strip_model()->GetActiveWebContents()); | |
54 } | |
55 | |
56 void HideSidebarForCurrentTab() { | |
57 HideSidebar(browser()->tab_strip_model()->GetActiveWebContents()); | |
58 } | |
59 | |
60 void CreateSidebar(WebContents* temp) { | |
61 WebContents* tab = static_cast<WebContents*>(temp); | |
62 SidebarManager* sidebar_manager = | |
63 SidebarManager::GetFromContext(browser()->profile()); | |
64 GURL url("chrome-extension://" + content_id_ + kSimplePage); | |
65 sidebar_manager->CreateSidebar(tab, url, browser()); | |
66 } | |
67 | |
68 void HideSidebar(WebContents* temp) { | |
69 WebContents* tab = static_cast<WebContents*>(temp); | |
70 SidebarManager* sidebar_manager = | |
71 SidebarManager::GetFromContext(browser()->profile()); | |
72 sidebar_manager->HideSidebar(tab); | |
73 EXPECT_FALSE(sidebar_manager->HasSidebar(tab)); | |
74 } | |
75 // Opens a new tab and waits for navigations to finish. If there are pending | |
76 // navigations, the constrained prompt might be dismissed when the navigation | |
77 // completes. | |
78 void OpenNewTab() { | |
79 ui_test_utils::NavigateToURLWithDisposition( | |
80 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, | |
81 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | |
82 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
83 } | |
84 | |
85 WebContents* web_contents(int i) { | |
86 return static_cast<WebContents*>( | |
87 browser()->tab_strip_model()->GetWebContentsAt(i)); | |
88 } | |
89 | |
90 BrowserView* browser_view() const { | |
91 return BrowserView::GetBrowserViewForBrowser(browser()); | |
92 } | |
93 | |
94 private: | |
95 std::string content_id_; | |
96 }; | |
97 | |
98 IN_PROC_BROWSER_TEST_F(SidebarTest, OpenClose) { | |
99 CreateSidebarForCurrentTab(); | |
100 | |
101 HideSidebarForCurrentTab(); | |
102 | |
103 CreateSidebarForCurrentTab(); | |
104 | |
105 HideSidebarForCurrentTab(); | |
106 } | |
107 | |
108 IN_PROC_BROWSER_TEST_F(SidebarTest, SwitchingTabs) { | |
109 SidebarManager* sidebar_manager = | |
110 SidebarManager::GetFromContext(browser()->profile()); | |
111 | |
112 CreateSidebarForCurrentTab(); | |
113 | |
114 OpenNewTab(); | |
115 | |
116 // Make sure sidebar is not visbile for the newly opened tab. | |
117 EXPECT_FALSE(sidebar_manager->HasSidebar( | |
118 browser()->tab_strip_model()->GetActiveWebContents())); | |
119 | |
120 // Switch back to the first tab. | |
121 TabStripModel* tab_strip_model = browser()->tab_strip_model(); | |
122 tab_strip_model->ActivateTabAt(0, false); | |
123 | |
124 // Make sure it is visible now. | |
125 EXPECT_TRUE(sidebar_manager->HasSidebar( | |
126 browser()->tab_strip_model()->GetActiveWebContents())); | |
127 | |
128 HideSidebarForCurrentTab(); | |
129 } | |
130 | |
131 IN_PROC_BROWSER_TEST_F(SidebarTest, SidebarOnInactiveTab) { | |
132 SidebarManager* sidebar_manager = | |
133 SidebarManager::GetFromContext(browser()->profile()); | |
134 | |
135 CreateSidebarForCurrentTab(); | |
136 | |
137 OpenNewTab(); | |
138 | |
139 // Hide sidebar on inactive (first) tab. | |
140 HideSidebar(web_contents(0)); | |
141 | |
142 // Switch back to the first tab. | |
143 TabStripModel* tab_strip_model = browser()->tab_strip_model(); | |
144 tab_strip_model->ActivateTabAt(0, false); | |
145 | |
146 // Make sure sidebar is not visbile anymore. | |
147 EXPECT_FALSE(sidebar_manager->HasSidebar( | |
148 browser()->tab_strip_model()->GetActiveWebContents())); | |
149 | |
150 // Show sidebar on inactive (second) tab. | |
151 CreateSidebar(web_contents(1)); | |
152 // Make sure sidebar is not visible yet. | |
153 EXPECT_FALSE(sidebar_manager->HasSidebar( | |
154 browser()->tab_strip_model()->GetActiveWebContents())); | |
155 | |
156 // Switch back to the second tab. | |
157 tab_strip_model->ActivateTabAt(1, false); | |
158 // Make sure sidebar is visible now. | |
159 EXPECT_TRUE(sidebar_manager->HasSidebar( | |
160 browser()->tab_strip_model()->GetActiveWebContents())); | |
161 | |
162 HideSidebarForCurrentTab(); | |
163 } | |
164 | |
165 } // namespace | |
OLD | NEW |