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