|
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" | |
jennb
2011/08/23 20:28:34
alpha ordering wrong
jianli
2011/08/26 00:18:16
Done.
| |
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 { | |
jennb
2011/08/23 20:28:34
Comments in InProcessBrowserTest header say to ove
jianli
2011/08/26 00:18:16
Changed to override SetUpOnMainThread since the me
| |
58 // Setup the work area and bottom bar so that we have consistent testing | |
59 // environment for all panel related tests. | |
60 // Note: our setup should be done before InProcessBrowserTest::SetUp since | |
61 // it will start the main browser loop. | |
62 mock_auto_hide_bottom_bar_ = new MockAutoHideBottomBar(); | |
63 PanelManager::CreateForTesting(testing_work_area_, | |
64 mock_auto_hide_bottom_bar_.get()); | |
65 mock_auto_hide_bottom_bar_->set_observer(PanelManager::GetInstance()); | |
66 | |
67 InProcessBrowserTest::SetUp(); | |
68 } | |
69 | |
70 protected: | |
71 enum ShowFlag { SHOW_AS_ACTIVE, SHOW_AS_INACTIVE }; | |
72 | |
73 struct CreatePanelParams { | |
74 std::string name; | |
75 gfx::Rect bounds; | |
76 ShowFlag show_flag; | |
77 | |
78 CreatePanelParams(const std::string& name, | |
79 const gfx::Rect& bounds, | |
80 ShowFlag show_flag) | |
81 : name(name), | |
82 bounds(bounds), | |
83 show_flag(show_flag) { | |
84 } | |
85 }; | |
86 | |
87 Panel* CreatePanelWithParams(const CreatePanelParams& params) { | |
88 gfx::Rect bounds = params.bounds; | |
89 if (bounds.IsEmpty()) | |
90 bounds.set_size(gfx::Size(kDefaultPanelWidth, kDefaultPanelHeight)); | |
jennb
2011/08/23 20:28:34
Is this necessary? PanelManager will pick default
jianli
2011/08/26 00:18:16
Done.
| |
91 Browser* panel_browser = Browser::CreateForApp(Browser::TYPE_PANEL, | |
92 params.name, | |
93 bounds, | |
94 browser()->profile()); | |
95 EXPECT_TRUE(panel_browser->is_type_panel()); | |
96 | |
97 TabContentsWrapper* tab_contents = | |
98 new TabContentsWrapper(new TestTabContents(browser()->profile(), NULL)); | |
99 panel_browser->AddTab(tab_contents, PageTransition::LINK); | |
100 | |
101 Panel* panel = static_cast<Panel*>(panel_browser->window()); | |
102 if (params.show_flag == SHOW_AS_ACTIVE) | |
103 panel->Show(); | |
104 else | |
105 panel->ShowInactive(); | |
106 MessageLoopForUI::current()->RunAllPending(); | |
107 | |
108 return panel; | |
109 } | |
110 | |
111 Panel* CreatePanelWithBounds(const std::string& panel_name, | |
112 const gfx::Rect& bounds) { | |
113 CreatePanelParams params(panel_name, bounds, SHOW_AS_ACTIVE); | |
114 return CreatePanelWithParams(params); | |
115 } | |
116 | |
117 Panel* CreatePanel(const std::string& panel_name) { | |
118 CreatePanelParams params(panel_name, gfx::Rect(), SHOW_AS_ACTIVE); | |
119 return CreatePanelWithParams(params); | |
120 } | |
121 | |
122 scoped_refptr<Extension> CreateExtension(const FilePath::StringType& path, | |
123 Extension::Location location, | |
124 const DictionaryValue& extra_value) { | |
125 #if defined(OS_WIN) | |
126 FilePath full_path(FILE_PATH_LITERAL("c:\\")); | |
127 #else | |
128 FilePath full_path(FILE_PATH_LITERAL("/")); | |
129 #endif | |
130 full_path = full_path.Append(path); | |
131 | |
132 scoped_ptr<DictionaryValue> input_value(extra_value.DeepCopy()); | |
133 input_value->SetString(extension_manifest_keys::kVersion, "1.0.0.0"); | |
134 input_value->SetString(extension_manifest_keys::kName, "Sample Extension"); | |
135 | |
136 std::string error; | |
137 scoped_refptr<Extension> extension = Extension::Create( | |
138 full_path, location, *input_value, | |
139 Extension::STRICT_ERROR_CHECKS, &error); | |
140 EXPECT_TRUE(extension.get()); | |
141 EXPECT_STREQ("", error.c_str()); | |
142 browser()->GetProfile()->GetExtensionService()->OnLoadSingleExtension( | |
143 extension.get(), false); | |
144 return extension; | |
145 } | |
146 | |
147 gfx::Rect testing_work_area() const { return testing_work_area_; } | |
148 | |
149 MockAutoHideBottomBar* mock_auto_hide_bottom_bar() const { | |
150 return mock_auto_hide_bottom_bar_.get(); | |
151 } | |
152 | |
153 private: | |
154 class MockAutoHideBottomBar : public AutoHideBottomBar { | |
jennb
2011/08/23 20:28:34
Would code be cleaner if this class was NOT nested
jianli
2011/08/26 00:18:16
Done.
| |
155 public: | |
156 MockAutoHideBottomBar() | |
157 : enabled_(false), | |
158 height_(kDefaultBottomBarHeight), | |
159 visibility_(VISIBLE), | |
160 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
161 } | |
162 | |
163 virtual ~MockAutoHideBottomBar() { } | |
164 | |
165 virtual void UpdateWorkArea(const gfx::Rect& work_area) OVERRIDE { | |
166 } | |
167 | |
168 virtual bool IsEnabled() OVERRIDE { | |
169 return enabled_; | |
170 } | |
171 | |
172 virtual int GetHeight() OVERRIDE { | |
173 return height_; | |
174 } | |
175 | |
176 virtual Visibility GetVisibility() OVERRIDE { | |
177 return visibility_; | |
178 } | |
179 | |
180 void enable_auto_hide(bool enabled) { | |
181 if (enabled == enabled_) | |
182 return; | |
183 enabled_ = enabled; | |
184 visibility_ = enabled_ ? HIDDEN : VISIBLE; | |
185 } | |
186 | |
187 void SetVisibility(Visibility visibility) { | |
188 if (!enabled_ || visibility == visibility_) | |
189 return; | |
190 visibility_ = visibility; | |
191 MessageLoop::current()->PostTask( | |
192 FROM_HERE, | |
193 method_factory_.NewRunnableMethod( | |
194 &MockAutoHideBottomBar::NotifyVisibilityChange)); | |
195 } | |
196 | |
197 void SetHeight(int height) { | |
198 if (!enabled_ || height == height_) | |
199 return; | |
200 height_ = height; | |
201 MessageLoop::current()->PostTask( | |
202 FROM_HERE, | |
203 method_factory_.NewRunnableMethod( | |
204 &MockAutoHideBottomBar::NotifyHeightChange)); | |
205 } | |
206 | |
207 void set_observer(Observer* observer) { observer_ = observer; } | |
208 | |
209 private: | |
210 void NotifyVisibilityChange() { | |
211 observer_->OnAutoHideBottomBarVisibilityChanged(visibility_); | |
212 } | |
213 | |
214 void NotifyHeightChange() { | |
215 observer_->OnAutoHideBottomBarHeightChanged(height_); | |
216 } | |
217 | |
218 Observer* observer_; | |
219 bool enabled_; | |
220 int height_; | |
221 Visibility visibility_; | |
222 ScopedRunnableMethodFactory<MockAutoHideBottomBar> method_factory_; | |
223 | |
224 DISALLOW_COPY_AND_ASSIGN(MockAutoHideBottomBar); | |
225 }; | |
226 | |
227 gfx::Rect testing_work_area_; | |
228 scoped_refptr<MockAutoHideBottomBar> mock_auto_hide_bottom_bar_; | |
229 }; | |
230 | |
231 #endif // CHROME_BROWSER_UI_PANELS_BASE_PANEL_BROWSER_TEST_H_ | |
OLD | NEW |