|
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 #ifndef CHROME_BROWSER_UI_PANELS_BASE_PANEL_BROWSER_TEST_H_ | |
6 #define CHROME_BROWSER_UI_PANELS_BASE_PANEL_BROWSER_TEST_H_ | |
7 #pragma once | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/task.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/ui/browser.h" | |
17 #include "chrome/browser/ui/panels/auto_hide_bottom_bar.h" | |
18 #include "chrome/browser/ui/panels/panel_manager.h" | |
19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
20 #include "chrome/common/extensions/extension.h" | |
21 #include "chrome/test/base/in_process_browser_test.h" | |
22 #include "content/browser/tab_contents/test_tab_contents.h" | |
23 #include "ui/gfx/rect.h" | |
24 | |
25 #if defined(OS_MACOSX) | |
26 #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h" | |
27 #endif | |
28 | |
29 namespace { | |
30 const int kTestingWorkAreaWidth = 800; | |
31 const int kTestingWorkAreaHeight = 600; | |
32 const int kDefaultBottomBarHeight = 40; | |
33 const int kDefaultPanelWidth = 150; | |
34 const int kDefaultPanelHeight = 120; | |
35 } | |
36 | |
37 class BasePanelBrowserTest : public InProcessBrowserTest { | |
38 private: | |
39 class MockAutoHideBottomBar; | |
40 | |
41 public: | |
42 BasePanelBrowserTest() | |
43 : InProcessBrowserTest(), | |
44 testing_work_area_(0, 0, kTestingWorkAreaWidth, | |
45 kTestingWorkAreaHeight) { | |
46 #if defined(OS_MACOSX) | |
47 FindBarBridge::disable_animations_during_testing_ = true; | |
48 #endif | |
49 } | |
50 | |
51 virtual ~BasePanelBrowserTest() { } | |
52 | |
53 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
54 command_line->AppendSwitch(switches::kEnablePanels); | |
55 } | |
56 | |
57 virtual void SetUp() OVERRIDE { | |
58 // Note: our setup should be done before InProcessBrowserTest::SetUp since | |
59 // it will start the main browser loop. | |
60 mock_auto_hide_bottom_bar_ = new MockAutoHideBottomBar(); | |
jennb
2011/08/22 21:00:16
Hmm...now even tests that don't care about auto hi
jianli
2011/08/22 23:28:10
As discussed, this is needed to setup a consistent
| |
61 PanelManager::CreateForTesting(testing_work_area_, | |
62 mock_auto_hide_bottom_bar_.get()); | |
63 mock_auto_hide_bottom_bar_->set_observer(PanelManager::GetInstance()); | |
64 | |
65 InProcessBrowserTest::SetUp(); | |
66 } | |
67 | |
68 protected: | |
69 enum ShowFlag { SHOW_AS_ACTIVE, SHOW_AS_INACTIVE }; | |
70 | |
71 Panel* CreatePanel(const std::string& panel_name, | |
jennb
2011/08/22 21:00:16
Usually, panels will be active when created in tes
jianli
2011/08/22 23:28:10
Done.
| |
72 const gfx::Rect& bounds, | |
73 ShowFlag show_flag) { | |
74 gfx::Rect panel_bounds = bounds; | |
75 if (bounds.IsEmpty()) | |
76 panel_bounds.set_size(gfx::Size(kDefaultPanelWidth, kDefaultPanelHeight)); | |
77 Browser* panel_browser = Browser::CreateForApp(Browser::TYPE_PANEL, | |
78 panel_name, | |
79 panel_bounds, | |
80 browser()->profile()); | |
81 EXPECT_TRUE(panel_browser->is_type_panel()); | |
82 | |
83 TabContentsWrapper* tab_contents = | |
84 new TabContentsWrapper(new TestTabContents(browser()->profile(), NULL)); | |
85 panel_browser->AddTab(tab_contents, PageTransition::LINK); | |
86 | |
87 Panel* panel = static_cast<Panel*>(panel_browser->window()); | |
88 if (show_flag == SHOW_AS_ACTIVE) | |
89 panel->Show(); | |
90 else | |
91 panel->ShowInactive(); | |
92 MessageLoopForUI::current()->RunAllPending(); | |
93 | |
94 return panel; | |
95 } | |
96 | |
97 scoped_refptr<Extension> CreateExtension(const FilePath::StringType& path, | |
98 Extension::Location location, | |
99 const DictionaryValue& extra_value) { | |
100 #if defined(OS_WIN) | |
101 FilePath full_path(FILE_PATH_LITERAL("c:\\")); | |
102 #else | |
103 FilePath full_path(FILE_PATH_LITERAL("/")); | |
104 #endif | |
105 full_path = full_path.Append(path); | |
106 | |
107 scoped_ptr<DictionaryValue> input_value(extra_value.DeepCopy()); | |
108 input_value->SetString(extension_manifest_keys::kVersion, "1.0.0.0"); | |
109 input_value->SetString(extension_manifest_keys::kName, "Sample Extension"); | |
110 | |
111 std::string error; | |
112 scoped_refptr<Extension> extension = Extension::Create( | |
113 full_path, location, *input_value, | |
114 Extension::STRICT_ERROR_CHECKS, &error); | |
115 EXPECT_TRUE(extension.get()); | |
116 EXPECT_STREQ("", error.c_str()); | |
117 browser()->GetProfile()->GetExtensionService()->OnLoadSingleExtension( | |
118 extension.get(), false); | |
119 return extension; | |
120 } | |
121 | |
122 gfx::Rect testing_work_area() const { return testing_work_area_; } | |
123 | |
124 MockAutoHideBottomBar* mock_auto_hide_bottom_bar() const { | |
125 return mock_auto_hide_bottom_bar_.get(); | |
126 } | |
127 | |
128 private: | |
129 class MockAutoHideBottomBar : public AutoHideBottomBar { | |
130 public: | |
131 MockAutoHideBottomBar() | |
132 : enabled_(false), | |
133 height_(kDefaultBottomBarHeight), | |
134 visibility_(VISIBLE), | |
135 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
136 } | |
137 | |
138 virtual ~MockAutoHideBottomBar() { } | |
139 | |
140 virtual void UpdateWorkArea(const gfx::Rect& work_area) OVERRIDE { | |
141 } | |
142 | |
143 virtual bool Exists() OVERRIDE { | |
144 return enabled_; | |
145 } | |
146 | |
147 virtual int GetHeight() OVERRIDE { | |
148 return height_; | |
149 } | |
150 | |
151 virtual Visibility GetVisibility() OVERRIDE { | |
152 return visibility_; | |
153 } | |
154 | |
155 void enable_auto_hide(bool enabled) { | |
156 if (enabled == enabled_) | |
157 return; | |
158 enabled_ = enabled; | |
159 visibility_ = enabled_ ? HIDDEN : VISIBLE; | |
160 } | |
161 | |
162 void SetVisibility(Visibility visibility) { | |
163 if (!enabled_ || visibility == visibility_) | |
164 return; | |
165 visibility_ = visibility; | |
166 MessageLoop::current()->PostTask( | |
167 FROM_HERE, | |
168 method_factory_.NewRunnableMethod( | |
169 &MockAutoHideBottomBar::NotifyVisibilityChange)); | |
170 } | |
171 | |
172 void SetHeight(int height) { | |
173 if (!enabled_ || height == height_) | |
174 return; | |
175 height_ = height; | |
176 MessageLoop::current()->PostTask( | |
177 FROM_HERE, | |
178 method_factory_.NewRunnableMethod( | |
179 &MockAutoHideBottomBar::NotifyHeightChange)); | |
180 } | |
181 | |
182 void set_observer(Observer* observer) { observer_ = observer; } | |
183 | |
184 private: | |
185 void NotifyVisibilityChange() { | |
186 observer_->OnAutoHideBottomBarVisibilityChanged(visibility_); | |
187 } | |
188 | |
189 void NotifyHeightChange() { | |
190 observer_->OnAutoHideBottomBarHeightChanged(height_); | |
191 } | |
192 | |
193 Observer* observer_; | |
194 bool enabled_; | |
195 int height_; // If 0, auto-hide is disabled. | |
196 Visibility visibility_; | |
197 ScopedRunnableMethodFactory<MockAutoHideBottomBar> method_factory_; | |
198 | |
199 DISALLOW_COPY_AND_ASSIGN(MockAutoHideBottomBar); | |
200 }; | |
201 | |
202 gfx::Rect testing_work_area_; | |
203 scoped_refptr<MockAutoHideBottomBar> mock_auto_hide_bottom_bar_; | |
204 }; | |
205 | |
206 #endif // CHROME_BROWSER_UI_PANELS_BASE_PANEL_BROWSER_TEST_H_ | |
OLD | NEW |