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