Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 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/path_service.h" | |
| 6 #include "chrome/browser/extensions/browser_action_test_util.h" | |
| 7 #include "chrome/browser/extensions/extension_browsertest.h" | |
| 8 #include "chrome/browser/extensions/extension_service.h" | |
| 9 #include "chrome/browser/extensions/sidebar_manager.h" | |
| 10 #include "chrome/browser/extensions/test_extension_system.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/ui/browser.h" | |
| 13 #include "chrome/browser/ui/browser_commands.h" | |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 15 #include "chrome/common/chrome_paths.h" | |
| 16 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 17 #include "chrome/test/base/ui_test_utils.h" | |
| 18 #include "content/public/browser/web_contents.h" | |
| 19 #include "extensions/browser/extension_system.h" | |
| 20 #include "extensions/common/extension.h" | |
| 21 #include "extensions/common/file_util.h" | |
| 22 | |
| 23 using content::NavigationController; | |
| 24 using content::WebContents; | |
| 25 using extensions::SidebarManager; | |
| 26 | |
| 27 namespace extensions { | |
| 28 | |
| 29 const char kSimplePage[] = "/simple_page.html"; | |
| 30 | |
| 31 class SidebarManagerTest : public BrowserWithTestWindowTest { | |
| 32 public: | |
| 33 SidebarManagerTest() {} | |
| 34 ~SidebarManagerTest() {} | |
| 35 | |
| 36 protected: | |
| 37 void SetUp() override { | |
| 38 BrowserWithTestWindowTest::SetUp(); | |
| 39 | |
| 40 // Load test sidebar extension. | |
| 41 base::FilePath extension_path; | |
| 42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path)); | |
| 43 | |
| 44 TestExtensionSystem* system = static_cast<TestExtensionSystem*>( | |
| 45 ExtensionSystem::Get(browser()->profile())); | |
| 46 | |
| 47 ExtensionService* extension_service = system->CreateExtensionService( | |
| 48 base::CommandLine::ForCurrentProcess(), extension_path, false); | |
| 49 | |
| 50 extension_path = extension_path.AppendASCII("sidebar"); | |
| 51 | |
| 52 std::string error; | |
| 53 extension_ = file_util::LoadExtension(extension_path, Manifest::UNPACKED, | |
| 54 Extension::NO_FLAGS, &error); | |
| 55 | |
| 56 ASSERT_TRUE(extension_.get()); | |
| 57 | |
| 58 extension_service->AddExtension(extension_.get()); | |
| 59 | |
| 60 browser_action_test_util_.reset(new BrowserActionTestUtil(browser())); | |
| 61 | |
| 62 chrome::NewTab(browser()); | |
| 63 browser()->tab_strip_model()->ActivateTabAt(0, false); | |
| 64 } | |
| 65 | |
| 66 void CreateSidebarForCurrentTab() { | |
| 67 CreateSidebar(browser()->tab_strip_model()->GetActiveWebContents()); | |
| 68 } | |
| 69 | |
| 70 void CreateSidebar(WebContents* temp) { | |
| 71 SidebarManager* sidebar_manager = | |
| 72 SidebarManager::GetFromContext(browser()->profile()); | |
| 73 GURL url("chrome-extension://" + extension_.get()->id() + kSimplePage); | |
| 74 sidebar_manager->CreateSidebar(temp, url, browser()); | |
| 75 } | |
| 76 | |
| 77 void HideSidebar(WebContents* temp) { | |
| 78 SidebarManager* sidebar_manager = | |
| 79 SidebarManager::GetFromContext(browser()->profile()); | |
| 80 sidebar_manager->HideSidebar(temp); | |
| 81 EXPECT_FALSE(sidebar_manager->GetSidebarContainerFor(temp) != nullptr); | |
| 82 } | |
| 83 | |
| 84 WebContents* web_contents(int i) { | |
| 85 return browser()->tab_strip_model()->GetWebContentsAt(i); | |
| 86 } | |
| 87 | |
| 88 bool HasSidebarForCurrentTab() { | |
| 89 SidebarManager* sidebar_manager = | |
| 90 SidebarManager::GetFromContext(browser()->profile()); | |
| 91 return sidebar_manager->GetSidebarContainerFor( | |
| 92 browser()->tab_strip_model()->GetActiveWebContents()) != nullptr; | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 scoped_refptr<Extension> extension_; | |
| 97 scoped_ptr<BrowserActionTestUtil> browser_action_test_util_; | |
| 98 }; | |
| 99 | |
| 100 // Tests that creating/hiding sidebar | |
| 101 TEST_F(SidebarManagerTest, CreateSidebar) { | |
|
Devlin
2015/07/07 21:39:30
Won't this be implicitly tested by any other test?
ltilve
2015/07/09 22:15:51
Done.
| |
| 102 CreateSidebarForCurrentTab(); | |
| 103 EXPECT_TRUE(HasSidebarForCurrentTab()); | |
| 104 HideSidebar(web_contents(0)); | |
| 105 EXPECT_FALSE(HasSidebarForCurrentTab()); | |
| 106 } | |
| 107 | |
| 108 // Tests that sidebar is only visible at the proper tab | |
| 109 TEST_F(SidebarManagerTest, SwitchingTabs) { | |
| 110 CreateSidebarForCurrentTab(); | |
| 111 chrome::NewTab(browser()); | |
|
Devlin
2015/07/07 21:39:30
prefer BrowserWithTestWindowTest::AddTab().
ltilve
2015/07/09 22:15:51
Done.
| |
| 112 | |
| 113 // Make sure sidebar is not visbile for the newly opened tab. | |
| 114 EXPECT_FALSE(HasSidebarForCurrentTab()); | |
| 115 | |
| 116 // Switch back to the first tab. | |
| 117 TabStripModel* tab_strip_model = browser()->tab_strip_model(); | |
| 118 tab_strip_model->ActivateTabAt(0, false); | |
| 119 | |
| 120 // Make sure it is visible now. | |
| 121 EXPECT_TRUE(HasSidebarForCurrentTab()); | |
| 122 | |
| 123 HideSidebar(web_contents(0)); | |
| 124 | |
| 125 // Make sure it is not visible any more | |
| 126 EXPECT_FALSE(HasSidebarForCurrentTab()); | |
| 127 } | |
| 128 | |
| 129 // Tests hiding sidebars on inactive tabs | |
| 130 TEST_F(SidebarManagerTest, SidebarOnInactiveTab) { | |
|
Devlin
2015/07/07 21:39:30
All these tests can probably be combined into one.
ltilve
2015/07/09 22:15:51
Done.
| |
| 131 CreateSidebarForCurrentTab(); | |
| 132 chrome::NewTab(browser()); | |
| 133 | |
| 134 // Hide sidebar on inactive (first) tab. | |
| 135 HideSidebar(web_contents(0)); | |
| 136 | |
| 137 // Switch back to the first tab. | |
| 138 TabStripModel* tab_strip_model = browser()->tab_strip_model(); | |
| 139 tab_strip_model->ActivateTabAt(0, false); | |
| 140 | |
| 141 // Make sure sidebar is not visbile anymore. | |
| 142 EXPECT_FALSE(HasSidebarForCurrentTab()); | |
| 143 | |
| 144 // Show sidebar on inactive (second) tab. | |
| 145 CreateSidebar(web_contents(1)); | |
| 146 | |
| 147 // Make sure sidebar is not visible yet. | |
| 148 EXPECT_FALSE(HasSidebarForCurrentTab()); | |
| 149 } | |
| 150 | |
| 151 } // namespace extensions | |
| OLD | NEW |