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 "chrome/browser/ui/panels/base_panel_browser_test.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/mac/scoped_nsautorelease_pool.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/panels/panel_manager.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "content/browser/tab_contents/test_tab_contents.h" |
| 17 |
| 18 #if defined(OS_MACOSX) |
| 19 #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h" |
| 20 #endif |
| 21 |
| 22 namespace { |
| 23 |
| 24 const int kTestingWorkAreaWidth = 800; |
| 25 const int kTestingWorkAreaHeight = 600; |
| 26 const int kDefaultAutoHidingDesktopBarThickness = 40; |
| 27 const int kDefaultPanelWidth = 150; |
| 28 const int kDefaultPanelHeight = 120; |
| 29 |
| 30 struct MockDesktopBar { |
| 31 bool auto_hiding_enabled; |
| 32 AutoHidingDesktopBar::Visibility visibility; |
| 33 int thickness; |
| 34 }; |
| 35 |
| 36 class MockAutoHidingDesktopBarImpl : |
| 37 public BasePanelBrowserTest::MockAutoHidingDesktopBar { |
| 38 public: |
| 39 explicit MockAutoHidingDesktopBarImpl(Observer* observer); |
| 40 virtual ~MockAutoHidingDesktopBarImpl() { } |
| 41 |
| 42 // Overridden from AutoHidingDesktopBar: |
| 43 virtual void UpdateWorkArea(const gfx::Rect& work_area) OVERRIDE; |
| 44 virtual bool IsEnabled(Alignment alignment) OVERRIDE; |
| 45 virtual int GetThickness(Alignment alignment) const OVERRIDE; |
| 46 virtual Visibility GetVisibility(Alignment alignment) const OVERRIDE; |
| 47 |
| 48 // Overridden from MockAutoHidingDesktopBar: |
| 49 virtual void EnableAutoHiding(Alignment alignment, |
| 50 bool enabled, |
| 51 int thickness) OVERRIDE; |
| 52 virtual void SetVisibility(Alignment alignment, |
| 53 Visibility visibility) OVERRIDE; |
| 54 virtual void SetThickness(Alignment alignment, int thickness) OVERRIDE; |
| 55 |
| 56 void set_observer(Observer* observer) { observer_ = observer; } |
| 57 |
| 58 private: |
| 59 void NotifyVisibilityChange(Alignment alignment, Visibility visibility); |
| 60 void NotifyThicknessChange(); |
| 61 |
| 62 Observer* observer_; |
| 63 MockDesktopBar mock_desktop_bars[3]; |
| 64 ScopedRunnableMethodFactory<MockAutoHidingDesktopBarImpl> method_factory_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(MockAutoHidingDesktopBarImpl); |
| 67 }; |
| 68 |
| 69 |
| 70 MockAutoHidingDesktopBarImpl::MockAutoHidingDesktopBarImpl( |
| 71 AutoHidingDesktopBar::Observer* observer) |
| 72 : observer_(observer), |
| 73 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 74 memset(mock_desktop_bars, 0, sizeof(mock_desktop_bars)); |
| 75 } |
| 76 |
| 77 void MockAutoHidingDesktopBarImpl::UpdateWorkArea( |
| 78 const gfx::Rect& work_area) { |
| 79 } |
| 80 |
| 81 bool MockAutoHidingDesktopBarImpl::IsEnabled( |
| 82 AutoHidingDesktopBar::Alignment alignment) { |
| 83 return mock_desktop_bars[static_cast<int>(alignment)].auto_hiding_enabled; |
| 84 } |
| 85 |
| 86 int MockAutoHidingDesktopBarImpl::GetThickness( |
| 87 AutoHidingDesktopBar::Alignment alignment) const { |
| 88 return mock_desktop_bars[static_cast<int>(alignment)].thickness; |
| 89 } |
| 90 |
| 91 AutoHidingDesktopBar::Visibility |
| 92 MockAutoHidingDesktopBarImpl::GetVisibility( |
| 93 AutoHidingDesktopBar::Alignment alignment) const { |
| 94 return mock_desktop_bars[static_cast<int>(alignment)].visibility; |
| 95 } |
| 96 |
| 97 void MockAutoHidingDesktopBarImpl::EnableAutoHiding( |
| 98 AutoHidingDesktopBar::Alignment alignment, bool enabled, int thickness) { |
| 99 MockDesktopBar* bar = &(mock_desktop_bars[static_cast<int>(alignment)]); |
| 100 bar->auto_hiding_enabled = enabled; |
| 101 bar->thickness = thickness; |
| 102 observer_->OnAutoHidingDesktopBarThicknessChanged(); |
| 103 } |
| 104 |
| 105 void MockAutoHidingDesktopBarImpl::SetVisibility( |
| 106 AutoHidingDesktopBar::Alignment alignment, |
| 107 AutoHidingDesktopBar::Visibility visibility) { |
| 108 MockDesktopBar* bar = &(mock_desktop_bars[static_cast<int>(alignment)]); |
| 109 if (!bar->auto_hiding_enabled) |
| 110 return; |
| 111 if (visibility == bar->visibility) |
| 112 return; |
| 113 bar->visibility = visibility; |
| 114 MessageLoop::current()->PostTask( |
| 115 FROM_HERE, |
| 116 method_factory_.NewRunnableMethod( |
| 117 &MockAutoHidingDesktopBarImpl::NotifyVisibilityChange, |
| 118 alignment, |
| 119 visibility)); |
| 120 } |
| 121 |
| 122 void MockAutoHidingDesktopBarImpl::SetThickness( |
| 123 AutoHidingDesktopBar::Alignment alignment, int thickness) { |
| 124 MockDesktopBar* bar = &(mock_desktop_bars[static_cast<int>(alignment)]); |
| 125 if (!bar->auto_hiding_enabled) |
| 126 return; |
| 127 if (thickness == bar->thickness) |
| 128 return; |
| 129 bar->thickness = thickness; |
| 130 MessageLoop::current()->PostTask( |
| 131 FROM_HERE, |
| 132 method_factory_.NewRunnableMethod( |
| 133 &MockAutoHidingDesktopBarImpl::NotifyThicknessChange)); |
| 134 } |
| 135 |
| 136 void MockAutoHidingDesktopBarImpl::NotifyVisibilityChange( |
| 137 AutoHidingDesktopBar::Alignment alignment, |
| 138 AutoHidingDesktopBar::Visibility visibility) { |
| 139 observer_->OnAutoHidingDesktopBarVisibilityChanged(alignment, visibility); |
| 140 } |
| 141 |
| 142 void MockAutoHidingDesktopBarImpl::NotifyThicknessChange() { |
| 143 observer_->OnAutoHidingDesktopBarThicknessChanged(); |
| 144 } |
| 145 |
| 146 } // namespace |
| 147 |
| 148 BasePanelBrowserTest::BasePanelBrowserTest() |
| 149 : InProcessBrowserTest(), |
| 150 testing_work_area_(0, 0, kTestingWorkAreaWidth, |
| 151 kTestingWorkAreaHeight) { |
| 152 #if defined(OS_MACOSX) |
| 153 FindBarBridge::disable_animations_during_testing_ = true; |
| 154 #endif |
| 155 } |
| 156 |
| 157 BasePanelBrowserTest::~BasePanelBrowserTest() { |
| 158 } |
| 159 |
| 160 void BasePanelBrowserTest::SetUpCommandLine(CommandLine* command_line) { |
| 161 command_line->AppendSwitch(switches::kEnablePanels); |
| 162 } |
| 163 |
| 164 void BasePanelBrowserTest::SetUpOnMainThread() { |
| 165 InProcessBrowserTest::SetUpOnMainThread(); |
| 166 |
| 167 // Setup the work area and desktop bar so that we have consistent testing |
| 168 // environment for all panel related tests. |
| 169 PanelManager* panel_manager = PanelManager::GetInstance(); |
| 170 mock_auto_hiding_desktop_bar_ = new MockAutoHidingDesktopBarImpl( |
| 171 panel_manager); |
| 172 panel_manager->set_auto_hiding_desktop_bar(mock_auto_hiding_desktop_bar_); |
| 173 panel_manager->SetWorkAreaForTesting(testing_work_area_); |
| 174 } |
| 175 |
| 176 Panel* BasePanelBrowserTest::CreatePanelWithParams( |
| 177 const CreatePanelParams& params) { |
| 178 // Opening panels on a Mac causes NSWindowController of the Panel window |
| 179 // to be autoreleased. We need a pool drained after it's done so the test |
| 180 // can close correctly. The NSWindowController of the Panel window controls |
| 181 // lifetime of the Browser object so we want to release it as soon as |
| 182 // possible. In real Chrome, this is done by message pump. |
| 183 // On non-Mac platform, this is an empty class. |
| 184 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
| 185 |
| 186 Browser* panel_browser = Browser::CreateForApp(Browser::TYPE_PANEL, |
| 187 params.name, |
| 188 params.bounds, |
| 189 browser()->profile()); |
| 190 EXPECT_TRUE(panel_browser->is_type_panel()); |
| 191 |
| 192 TabContentsWrapper* tab_contents = |
| 193 new TabContentsWrapper(new TestTabContents(browser()->profile(), NULL)); |
| 194 panel_browser->AddTab(tab_contents, PageTransition::LINK); |
| 195 |
| 196 Panel* panel = static_cast<Panel*>(panel_browser->window()); |
| 197 if (params.show_flag == SHOW_AS_ACTIVE) |
| 198 panel->Show(); |
| 199 else |
| 200 panel->ShowInactive(); |
| 201 MessageLoopForUI::current()->RunAllPending(); |
| 202 |
| 203 return panel; |
| 204 } |
| 205 |
| 206 Panel* BasePanelBrowserTest::CreatePanelWithBounds( |
| 207 const std::string& panel_name, const gfx::Rect& bounds) { |
| 208 CreatePanelParams params(panel_name, bounds, SHOW_AS_ACTIVE); |
| 209 return CreatePanelWithParams(params); |
| 210 } |
| 211 |
| 212 Panel* BasePanelBrowserTest::CreatePanel(const std::string& panel_name) { |
| 213 CreatePanelParams params(panel_name, gfx::Rect(), SHOW_AS_ACTIVE); |
| 214 return CreatePanelWithParams(params); |
| 215 } |
| 216 |
| 217 scoped_refptr<Extension> BasePanelBrowserTest::CreateExtension( |
| 218 const FilePath::StringType& path, |
| 219 Extension::Location location, |
| 220 const DictionaryValue& extra_value) { |
| 221 #if defined(OS_WIN) |
| 222 FilePath full_path(FILE_PATH_LITERAL("c:\\")); |
| 223 #else |
| 224 FilePath full_path(FILE_PATH_LITERAL("/")); |
| 225 #endif |
| 226 full_path = full_path.Append(path); |
| 227 |
| 228 scoped_ptr<DictionaryValue> input_value(extra_value.DeepCopy()); |
| 229 input_value->SetString(extension_manifest_keys::kVersion, "1.0.0.0"); |
| 230 input_value->SetString(extension_manifest_keys::kName, "Sample Extension"); |
| 231 |
| 232 std::string error; |
| 233 scoped_refptr<Extension> extension = Extension::Create( |
| 234 full_path, location, *input_value, |
| 235 Extension::STRICT_ERROR_CHECKS, &error); |
| 236 EXPECT_TRUE(extension.get()); |
| 237 EXPECT_STREQ("", error.c_str()); |
| 238 browser()->GetProfile()->GetExtensionService()->OnLoadSingleExtension( |
| 239 extension.get(), false); |
| 240 return extension; |
| 241 } |
OLD | NEW |