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 { |
| 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 ShowSidebarForCurrentTab() { |
| 53 ShowSidebar(browser()->tab_strip_model()->GetActiveWebContents()); |
| 54 } |
| 55 |
| 56 void HideSidebarForCurrentTab() { |
| 57 HideSidebar(browser()->tab_strip_model()->GetActiveWebContents()); |
| 58 } |
| 59 |
| 60 void NavigateSidebarForCurrentTabTo() { |
| 61 GURL url("chrome-extension://" + content_id_ + kSimplePage); |
| 62 |
| 63 WebContents* tab = static_cast<WebContents*>( |
| 64 browser()->tab_strip_model()->GetActiveWebContents()); |
| 65 |
| 66 SidebarManager* sidebar_manager = |
| 67 SidebarManager::GetFromContext(browser()->profile()); |
| 68 |
| 69 SidebarContainer* sidebar_container = |
| 70 sidebar_manager->GetSidebarContainerFor(tab, content_id_); |
| 71 WebContents* client_contents = sidebar_container->host_contents(); |
| 72 |
| 73 content::WindowedNotificationObserver observer( |
| 74 content::NOTIFICATION_LOAD_STOP, |
| 75 content::Source<NavigationController>( |
| 76 &client_contents->GetController())); |
| 77 sidebar_manager->NavigateSidebar(tab, content_id_, url); |
| 78 observer.Wait(); |
| 79 } |
| 80 |
| 81 void ShowSidebar(WebContents* temp) { |
| 82 WebContents* tab = static_cast<WebContents*>(temp); |
| 83 |
| 84 SidebarManager* sidebar_manager = |
| 85 SidebarManager::GetFromContext(browser()->profile()); |
| 86 GURL url("chrome-extension://" + content_id_ + kSimplePage); |
| 87 sidebar_manager->ShowSidebar(tab, content_id_, url, browser()); |
| 88 } |
| 89 |
| 90 void HideSidebar(WebContents* temp) { |
| 91 WebContents* tab = static_cast<WebContents*>(temp); |
| 92 SidebarManager* sidebar_manager = |
| 93 SidebarManager::GetFromContext(browser()->profile()); |
| 94 sidebar_manager->HideSidebar(tab, content_id_); |
| 95 if (browser()->tab_strip_model()->GetActiveWebContents() == tab) |
| 96 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); |
| 97 } |
| 98 // Opens a new tab and waits for navigations to finish. If there are pending |
| 99 // navigations, the constrained prompt might be dismissed when the navigation |
| 100 // completes. |
| 101 void OpenNewTab() { |
| 102 ui_test_utils::NavigateToURLWithDisposition( |
| 103 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, |
| 104 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 105 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 106 } |
| 107 |
| 108 WebContents* web_contents(int i) { |
| 109 return static_cast<WebContents*>( |
| 110 browser()->tab_strip_model()->GetWebContentsAt(i)); |
| 111 } |
| 112 |
| 113 BrowserView* browser_view() const { |
| 114 return BrowserView::GetBrowserViewForBrowser(browser()); |
| 115 } |
| 116 |
| 117 private: |
| 118 std::string content_id_; |
| 119 }; |
| 120 |
| 121 IN_PROC_BROWSER_TEST_F(SidebarTest, OpenClose) { |
| 122 ShowSidebarForCurrentTab(); |
| 123 |
| 124 HideSidebarForCurrentTab(); |
| 125 |
| 126 ShowSidebarForCurrentTab(); |
| 127 |
| 128 HideSidebarForCurrentTab(); |
| 129 } |
| 130 |
| 131 IN_PROC_BROWSER_TEST_F(SidebarTest, SwitchingTabs) { |
| 132 ShowSidebarForCurrentTab(); |
| 133 |
| 134 OpenNewTab(); |
| 135 |
| 136 // Make sure sidebar is not visbile for the newly opened tab. |
| 137 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); |
| 138 |
| 139 // Switch back to the first tab. |
| 140 TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 141 tab_strip_model->ActivateTabAt(0, false); |
| 142 |
| 143 // Make sure it is visible now. |
| 144 EXPECT_GT(browser_view()->GetSidebarWidth(), 0); |
| 145 |
| 146 HideSidebarForCurrentTab(); |
| 147 } |
| 148 |
| 149 IN_PROC_BROWSER_TEST_F(SidebarTest, SidebarOnInactiveTab) { |
| 150 ShowSidebarForCurrentTab(); |
| 151 |
| 152 OpenNewTab(); |
| 153 |
| 154 // Hide sidebar on inactive (first) tab. |
| 155 HideSidebar(web_contents(0)); |
| 156 |
| 157 // Switch back to the first tab. |
| 158 TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 159 tab_strip_model->ActivateTabAt(0, false); |
| 160 |
| 161 // Make sure sidebar is not visbile anymore. |
| 162 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); |
| 163 |
| 164 // Show sidebar on inactive (second) tab. |
| 165 ShowSidebar(web_contents(1)); |
| 166 // Make sure sidebar is not visible yet. |
| 167 EXPECT_EQ(0, browser_view()->GetSidebarWidth()); |
| 168 |
| 169 // Switch back to the second tab. |
| 170 tab_strip_model->ActivateTabAt(1, false); |
| 171 // Make sure sidebar is visible now. |
| 172 EXPECT_GT(browser_view()->GetSidebarWidth(), 0); |
| 173 |
| 174 HideSidebarForCurrentTab(); |
| 175 } |
| 176 |
| 177 IN_PROC_BROWSER_TEST_F(SidebarTest, SidebarNavigate) { |
| 178 ASSERT_TRUE(test_server()->Start()); |
| 179 |
| 180 ShowSidebarForCurrentTab(); |
| 181 |
| 182 NavigateSidebarForCurrentTabTo(); |
| 183 |
| 184 HideSidebarForCurrentTab(); |
| 185 } |
| 186 |
| 187 } // namespace |
OLD | NEW |