OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/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/sidebar/sidebar_manager.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_window.h" | |
13 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
14 #include "chrome/browser/ui/views/frame/browser_view.h" | |
15 #include "chrome/common/chrome_paths.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "chrome/common/extensions/extension.h" | |
18 #include "chrome/test/base/in_process_browser_test.h" | |
19 #include "chrome/test/base/ui_test_utils.h" | |
20 #include "content/browser/tab_contents/tab_contents.h" | |
21 #include "net/test/test_server.h" | |
22 | |
23 #include "chrome/browser/extensions/extension_service.h" | |
24 #include "chrome/browser/profiles/profile.h" | |
25 | |
26 using content::NavigationController; | |
27 using content::WebContents; | |
28 | |
29 namespace { | |
30 | |
31 const char kSimplePage[] = "files/sidebar/simple_page.html"; | |
32 | |
33 class SidebarTest : public ExtensionBrowserTest { | |
34 public: | |
35 SidebarTest() { | |
36 CommandLine::ForCurrentProcess()->AppendSwitch( | |
37 switches::kEnableExperimentalExtensionApis); | |
38 } | |
39 | |
40 protected: | |
41 // InProcessBrowserTest overrides. | |
42 virtual void SetUpOnMainThread() { | |
43 ExtensionBrowserTest::SetUpOnMainThread(); | |
44 | |
45 // Load test sidebar extension. | |
46 FilePath extension_path; | |
47 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path)); | |
48 extension_path = extension_path.AppendASCII("sidebar"); | |
49 | |
50 ASSERT_TRUE(LoadExtension(extension_path)); | |
51 | |
52 // For now content_id == extension_id. | |
53 content_id_ = last_loaded_extension_id_; | |
54 } | |
55 | |
56 void ShowSidebarForCurrentTab() { | |
57 ShowSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); | |
58 } | |
59 | |
60 void ExpandSidebarForCurrentTab() { | |
61 ExpandSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); | |
62 } | |
63 | |
64 void CollapseSidebarForCurrentTab() { | |
65 CollapseSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); | |
66 } | |
67 | |
68 void HideSidebarForCurrentTab() { | |
69 HideSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); | |
70 } | |
71 | |
72 void NavigateSidebarForCurrentTabTo(const std::string& test_page) { | |
73 GURL url = test_server()->GetURL(test_page); | |
74 | |
75 TabContents* tab = static_cast<TabContents*>( | |
76 browser()->GetSelectedTabContentsWrapper()->web_contents()); | |
77 | |
78 SidebarManager* sidebar_manager = SidebarManager::GetInstance(); | |
79 SidebarContainer* sidebar_container = | |
80 sidebar_manager->GetSidebarContainerFor(tab, content_id_); | |
81 TabContents* client_contents = sidebar_container->sidebar_contents(); | |
82 | |
83 ui_test_utils::WindowedNotificationObserver observer( | |
84 content::NOTIFICATION_LOAD_STOP, | |
85 content::Source<NavigationController>( | |
86 &client_contents->GetController())); | |
87 sidebar_manager->NavigateSidebar(tab, content_id_, url); | |
88 observer.Wait(); | |
89 } | |
90 | |
91 void ShowSidebar(WebContents* temp) { | |
92 TabContents* tab = static_cast<TabContents*>(temp); | |
93 SidebarManager* sidebar_manager = SidebarManager::GetInstance(); | |
94 sidebar_manager->ShowSidebar(tab, content_id_); | |
95 } | |
96 | |
97 void ExpandSidebar(WebContents* temp) { | |
98 TabContents* tab = static_cast<TabContents*>(temp); | |
99 SidebarManager* sidebar_manager = SidebarManager::GetInstance(); | |
100 sidebar_manager->ExpandSidebar(tab, content_id_); | |
101 if (browser()->GetSelectedTabContentsWrapper()->web_contents() == tab) | |
102 EXPECT_GT(browser_view()->GetSidebarWidth(), 0); | |
103 } | |
104 | |
105 void CollapseSidebar(WebContents* temp) { | |
106 TabContents* tab = static_cast<TabContents*>(temp); | |
107 SidebarManager* sidebar_manager = SidebarManager::GetInstance(); | |
108 sidebar_manager->CollapseSidebar(tab, content_id_); | |
109 if (browser()->GetSelectedTabContentsWrapper()->web_contents() == tab) | |
110 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); | |
111 } | |
112 | |
113 void HideSidebar(WebContents* temp) { | |
114 TabContents* tab = static_cast<TabContents*>(temp); | |
115 SidebarManager* sidebar_manager = SidebarManager::GetInstance(); | |
116 sidebar_manager->HideSidebar(tab, content_id_); | |
117 if (browser()->GetSelectedTabContentsWrapper()->web_contents() == tab) | |
118 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); | |
119 } | |
120 | |
121 TabContents* tab_contents(int i) { | |
122 return static_cast<TabContents*>( | |
123 browser()->GetTabContentsWrapperAt(i)->web_contents()); | |
124 } | |
125 | |
126 BrowserView* browser_view() const { | |
127 return static_cast<BrowserView*>(browser()->window()); | |
128 } | |
129 | |
130 private: | |
131 std::string content_id_; | |
132 }; | |
133 | |
134 IN_PROC_BROWSER_TEST_F(SidebarTest, OpenClose) { | |
135 ShowSidebarForCurrentTab(); | |
136 | |
137 ExpandSidebarForCurrentTab(); | |
138 CollapseSidebarForCurrentTab(); | |
139 | |
140 ExpandSidebarForCurrentTab(); | |
141 CollapseSidebarForCurrentTab(); | |
142 | |
143 ExpandSidebarForCurrentTab(); | |
144 CollapseSidebarForCurrentTab(); | |
145 | |
146 HideSidebarForCurrentTab(); | |
147 | |
148 ShowSidebarForCurrentTab(); | |
149 | |
150 ExpandSidebarForCurrentTab(); | |
151 CollapseSidebarForCurrentTab(); | |
152 | |
153 HideSidebarForCurrentTab(); | |
154 } | |
155 | |
156 IN_PROC_BROWSER_TEST_F(SidebarTest, SwitchingTabs) { | |
157 ShowSidebarForCurrentTab(); | |
158 ExpandSidebarForCurrentTab(); | |
159 | |
160 browser()->NewTab(); | |
161 | |
162 // Make sure sidebar is not visbile for the newly opened tab. | |
163 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); | |
164 | |
165 // Switch back to the first tab. | |
166 browser()->SelectNumberedTab(0); | |
167 | |
168 // Make sure it is visible now. | |
169 EXPECT_GT(browser_view()->GetSidebarWidth(), 0); | |
170 | |
171 HideSidebarForCurrentTab(); | |
172 } | |
173 | |
174 IN_PROC_BROWSER_TEST_F(SidebarTest, SidebarOnInactiveTab) { | |
175 ShowSidebarForCurrentTab(); | |
176 ExpandSidebarForCurrentTab(); | |
177 | |
178 browser()->NewTab(); | |
179 | |
180 // Hide sidebar on inactive (first) tab. | |
181 HideSidebar(tab_contents(0)); | |
182 | |
183 // Switch back to the first tab. | |
184 browser()->SelectNumberedTab(0); | |
185 | |
186 // Make sure sidebar is not visbile anymore. | |
187 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); | |
188 | |
189 // Show sidebar on inactive (second) tab. | |
190 ShowSidebar(tab_contents(1)); | |
191 ExpandSidebar(tab_contents(1)); | |
192 // Make sure sidebar is not visible yet. | |
193 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); | |
194 | |
195 // Switch back to the second tab. | |
196 browser()->SelectNumberedTab(1); | |
197 // Make sure sidebar is visible now. | |
198 EXPECT_GT(browser_view()->GetSidebarWidth(), 0); | |
199 | |
200 HideSidebarForCurrentTab(); | |
201 } | |
202 | |
203 IN_PROC_BROWSER_TEST_F(SidebarTest, SidebarNavigate) { | |
204 ASSERT_TRUE(test_server()->Start()); | |
205 | |
206 ShowSidebarForCurrentTab(); | |
207 | |
208 NavigateSidebarForCurrentTabTo(kSimplePage); | |
209 | |
210 HideSidebarForCurrentTab(); | |
211 } | |
212 | |
213 } // namespace | |
OLD | NEW |