OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
5 #include "components/web_modal/native_web_contents_modal_dialog_manager.h" | 8 #include "components/web_modal/native_web_contents_modal_dialog_manager.h" |
6 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 9 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
10 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" | |
11 #include "content/public/browser/notification_details.h" | |
12 #include "content/public/browser/notification_service.h" | |
13 #include "content/public/browser/notification_source.h" | |
14 #include "content/public/browser/notification_types.h" | |
7 #include "content/public/test/test_renderer_host.h" | 15 #include "content/public/test/test_renderer_host.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
9 | 17 |
10 namespace web_modal { | 18 namespace web_modal { |
11 | 19 |
12 class WebContentsModalDialogManagerTest | 20 class TestNativeWebContentsModalDialogManager |
13 : public content::RenderViewHostTestHarness { | |
14 public: | |
15 virtual void SetUp() { | |
16 content::RenderViewHostTestHarness::SetUp(); | |
17 WebContentsModalDialogManager::CreateForWebContents(web_contents()); | |
18 } | |
19 }; | |
20 | |
21 class NativeWebContentsModalDialogManagerCloseTest | |
22 : public NativeWebContentsModalDialogManager { | 21 : public NativeWebContentsModalDialogManager { |
23 public: | 22 public: |
24 explicit NativeWebContentsModalDialogManagerCloseTest( | 23 enum DialogState { |
24 UNKNOWN, | |
25 NOT_SHOWN, | |
26 SHOWN, | |
27 HIDDEN, | |
28 CLOSED | |
29 }; | |
30 | |
31 explicit TestNativeWebContentsModalDialogManager( | |
25 NativeWebContentsModalDialogManagerDelegate* delegate) | 32 NativeWebContentsModalDialogManagerDelegate* delegate) |
26 : close_count_(0), delegate_(delegate) {} | 33 : delegate_(delegate) {} |
27 virtual void ManageDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 34 virtual void ManageDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
35 dialog_state_[dialog] = NOT_SHOWN; | |
28 } | 36 } |
29 virtual void ShowDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 37 virtual void ShowDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
38 dialog_state_[dialog] = SHOWN; | |
30 } | 39 } |
31 virtual void HideDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 40 virtual void HideDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
41 dialog_state_[dialog] = HIDDEN; | |
32 } | 42 } |
33 virtual void CloseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 43 virtual void CloseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
34 delegate_->WillClose(dialog); | 44 delegate_->WillClose(dialog); |
35 ++close_count_; | 45 dialog_state_[dialog] = CLOSED; |
36 } | 46 } |
37 virtual void FocusDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 47 virtual void FocusDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
38 } | 48 } |
39 virtual void PulseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 49 virtual void PulseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
40 } | 50 } |
41 virtual void HostChanged(WebContentsModalDialogHost* new_host) OVERRIDE { | 51 virtual void HostChanged(WebContentsModalDialogHost* new_host) OVERRIDE { |
42 } | 52 } |
43 | 53 |
44 int close_count() const { return close_count_; } | 54 int GetCloseCount() const { |
55 int count = 0; | |
56 for (DialogStateMap::const_iterator it = dialog_state_.begin(); | |
57 it != dialog_state_.end(); ++it) { | |
58 if (it->second == CLOSED) | |
59 count++; | |
60 } | |
61 return count; | |
62 } | |
63 | |
64 DialogState GetDialogState(NativeWebContentsModalDialog dialog) const { | |
65 DialogStateMap::const_iterator loc = dialog_state_.find(dialog); | |
66 return loc == dialog_state_.end() ? UNKNOWN : loc->second; | |
67 } | |
45 | 68 |
46 private: | 69 private: |
47 int close_count_; | 70 typedef std::map<NativeWebContentsModalDialog, DialogState> DialogStateMap; |
71 | |
48 NativeWebContentsModalDialogManagerDelegate* delegate_; | 72 NativeWebContentsModalDialogManagerDelegate* delegate_; |
49 | 73 DialogStateMap dialog_state_; |
50 DISALLOW_COPY_AND_ASSIGN(NativeWebContentsModalDialogManagerCloseTest); | 74 |
75 DISALLOW_COPY_AND_ASSIGN(TestNativeWebContentsModalDialogManager); | |
76 }; | |
77 | |
78 class TestWebContentsModalDialogManagerDelegate | |
79 : public WebContentsModalDialogManagerDelegate { | |
80 public: | |
81 TestWebContentsModalDialogManagerDelegate() | |
82 : web_contents_visible_(true), | |
83 web_contents_blocked_(false) { | |
84 } | |
85 | |
86 // WebContentsModalDialogManagerDelegate overrides | |
87 virtual void SetWebContentsBlocked(content::WebContents* web_contents, | |
88 bool blocked) OVERRIDE { | |
89 web_contents_blocked_ = blocked; | |
90 } | |
91 | |
92 virtual WebContentsModalDialogHost* GetWebContentsModalDialogHost() OVERRIDE { | |
93 return NULL; | |
94 } | |
95 | |
96 virtual bool IsWebContentsVisible( | |
97 content::WebContents* web_contents) OVERRIDE { | |
98 return web_contents_visible_; | |
99 } | |
100 | |
101 void set_web_contents_visible(bool visible) { | |
102 web_contents_visible_ = visible; | |
103 } | |
104 | |
105 bool web_contents_blocked() const { return web_contents_blocked_; } | |
106 | |
107 private: | |
108 bool web_contents_visible_; | |
109 bool web_contents_blocked_; | |
110 }; | |
sky
2013/09/05 15:59:12
DISALLOW_...
Mike Wittman
2013/09/05 18:42:09
Done.
| |
111 | |
112 class WebContentsModalDialogManagerTest | |
113 : public content::RenderViewHostTestHarness { | |
114 public: | |
115 WebContentsModalDialogManagerTest() | |
116 : manager(NULL), | |
117 native_manager(NULL) { | |
118 } | |
119 | |
120 virtual void SetUp() { | |
121 content::RenderViewHostTestHarness::SetUp(); | |
122 | |
123 delegate.reset(new TestWebContentsModalDialogManagerDelegate); | |
124 WebContentsModalDialogManager::CreateForWebContents(web_contents()); | |
125 manager = WebContentsModalDialogManager::FromWebContents(web_contents()); | |
126 manager->SetDelegate(delegate.get()); | |
127 test_api.reset(new WebContentsModalDialogManager::TestApi(manager)); | |
128 native_manager = new TestNativeWebContentsModalDialogManager(manager); | |
129 | |
130 // |manager| owns |native_manager| as a result. | |
131 test_api->ResetNativeManager(native_manager); | |
132 } | |
133 | |
134 virtual void TearDown() { | |
135 test_api.reset(); | |
136 content::RenderViewHostTestHarness::TearDown(); | |
137 } | |
138 | |
139 protected: | |
140 NativeWebContentsModalDialog MakeFakeDialog(int id) { | |
141 // WebContentsModalDialogManager treats the NativeWebContentsModalDialog as | |
142 // an opaque type, so creating fake NativeWebContentsModalDialogs using | |
143 // reinterpret_cast is valid. | |
144 return reinterpret_cast<NativeWebContentsModalDialog>(id); | |
145 } | |
146 | |
147 scoped_ptr<TestWebContentsModalDialogManagerDelegate> delegate; | |
148 WebContentsModalDialogManager* manager; | |
149 scoped_ptr<WebContentsModalDialogManager::TestApi> test_api; | |
150 TestNativeWebContentsModalDialogManager* native_manager; | |
51 }; | 151 }; |
sky
2013/09/05 15:59:12
DISALLOW_...
Mike Wittman
2013/09/05 18:42:09
Done.
| |
52 | 152 |
53 NativeWebContentsModalDialogManager* | 153 NativeWebContentsModalDialogManager* |
54 WebContentsModalDialogManager::CreateNativeManager( | 154 WebContentsModalDialogManager::CreateNativeManager( |
55 NativeWebContentsModalDialogManagerDelegate* native_delegate) { | 155 NativeWebContentsModalDialogManagerDelegate* native_delegate) { |
56 return new NativeWebContentsModalDialogManagerCloseTest(native_delegate); | 156 return new TestNativeWebContentsModalDialogManager(native_delegate); |
57 } | 157 } |
58 | 158 |
59 TEST_F(WebContentsModalDialogManagerTest, WebContentsModalDialogs) { | 159 TEST_F(WebContentsModalDialogManagerTest, WebContentsVisible) { |
sky
2013/09/05 15:59:12
It's nice to have a description for each test. Tha
Mike Wittman
2013/09/05 18:42:09
Done.
| |
60 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 160 // Dialog should be shown while WebContents is visible. |
61 WebContentsModalDialogManager::FromWebContents(web_contents()); | 161 const NativeWebContentsModalDialog dialog1 = MakeFakeDialog(1); |
62 WebContentsModalDialogManager::TestApi test_api( | 162 |
63 web_contents_modal_dialog_manager); | 163 manager->ShowDialog(dialog1); |
64 | 164 |
65 NativeWebContentsModalDialogManagerCloseTest* native_manager = | 165 EXPECT_EQ(TestNativeWebContentsModalDialogManager::SHOWN, |
66 new NativeWebContentsModalDialogManagerCloseTest( | 166 native_manager->GetDialogState(dialog1)); |
67 web_contents_modal_dialog_manager); | 167 EXPECT_TRUE(manager->IsShowingDialog()); |
68 | 168 EXPECT_TRUE(delegate->web_contents_blocked()); |
69 // |web_contents_modal_dialog_manager| owns |native_manager| as a result. | 169 } |
70 test_api.ResetNativeManager(native_manager); | 170 |
71 | 171 TEST_F(WebContentsModalDialogManagerTest, WebContentsNotVisible) { |
172 // Dialog should not be shown while WebContents is not visible. | |
173 delegate->set_web_contents_visible(false); | |
174 | |
175 const NativeWebContentsModalDialog dialog1 = MakeFakeDialog(1); | |
176 | |
177 manager->ShowDialog(dialog1); | |
178 | |
179 EXPECT_EQ(TestNativeWebContentsModalDialogManager::NOT_SHOWN, | |
180 native_manager->GetDialogState(dialog1)); | |
181 EXPECT_TRUE(manager->IsShowingDialog()); | |
182 EXPECT_TRUE(delegate->web_contents_blocked()); | |
183 } | |
184 | |
185 TEST_F(WebContentsModalDialogManagerTest, ShowDialogs) { | |
186 const NativeWebContentsModalDialog dialog1 = MakeFakeDialog(1); | |
187 const NativeWebContentsModalDialog dialog2 = MakeFakeDialog(2); | |
188 const NativeWebContentsModalDialog dialog3 = MakeFakeDialog(3); | |
189 | |
190 manager->ShowDialog(dialog1); | |
191 manager->ShowDialog(dialog2); | |
192 manager->ShowDialog(dialog3); | |
193 | |
194 EXPECT_TRUE(delegate->web_contents_blocked()); | |
195 EXPECT_EQ(TestNativeWebContentsModalDialogManager::SHOWN, | |
196 native_manager->GetDialogState(dialog1)); | |
197 EXPECT_EQ(TestNativeWebContentsModalDialogManager::NOT_SHOWN, | |
198 native_manager->GetDialogState(dialog2)); | |
199 EXPECT_EQ(TestNativeWebContentsModalDialogManager::NOT_SHOWN, | |
200 native_manager->GetDialogState(dialog3)); | |
201 } | |
202 | |
203 TEST_F(WebContentsModalDialogManagerTest, VisibilityObservation) { | |
204 const NativeWebContentsModalDialog dialog1 = MakeFakeDialog(1); | |
205 bool web_contents_visible = true; | |
206 | |
207 manager->ShowDialog(dialog1); | |
208 | |
209 EXPECT_TRUE(manager->IsShowingDialog()); | |
210 EXPECT_TRUE(delegate->web_contents_blocked()); | |
211 EXPECT_EQ(TestNativeWebContentsModalDialogManager::SHOWN, | |
212 native_manager->GetDialogState(dialog1)); | |
213 | |
214 web_contents_visible = false; | |
215 manager->Observe(content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
216 content::NotificationService::AllSources(), | |
217 content::Details<bool>(&web_contents_visible)); | |
218 | |
219 EXPECT_TRUE(manager->IsShowingDialog()); | |
220 EXPECT_TRUE(delegate->web_contents_blocked()); | |
221 EXPECT_EQ(TestNativeWebContentsModalDialogManager::HIDDEN, | |
222 native_manager->GetDialogState(dialog1)); | |
223 | |
224 web_contents_visible = true; | |
225 manager->Observe(content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
226 content::NotificationService::AllSources(), | |
227 content::Details<bool>(&web_contents_visible)); | |
228 | |
229 EXPECT_TRUE(manager->IsShowingDialog()); | |
230 EXPECT_TRUE(delegate->web_contents_blocked()); | |
231 EXPECT_EQ(TestNativeWebContentsModalDialogManager::SHOWN, | |
232 native_manager->GetDialogState(dialog1)); | |
233 } | |
234 | |
235 TEST_F(WebContentsModalDialogManagerTest, CloseDialogs) { | |
236 // The front dialog is always shown regardless of dialog close order. | |
237 const NativeWebContentsModalDialog dialog1 = MakeFakeDialog(1); | |
238 const NativeWebContentsModalDialog dialog2 = MakeFakeDialog(2); | |
239 const NativeWebContentsModalDialog dialog3 = MakeFakeDialog(3); | |
240 const NativeWebContentsModalDialog dialog4 = MakeFakeDialog(4); | |
241 | |
242 manager->ShowDialog(dialog1); | |
243 manager->ShowDialog(dialog2); | |
244 manager->ShowDialog(dialog3); | |
245 manager->ShowDialog(dialog4); | |
246 | |
247 native_manager->CloseDialog(dialog1); | |
248 | |
249 EXPECT_TRUE(manager->IsShowingDialog()); | |
250 EXPECT_TRUE(delegate->web_contents_blocked()); | |
251 EXPECT_EQ(TestNativeWebContentsModalDialogManager::CLOSED, | |
252 native_manager->GetDialogState(dialog1)); | |
253 EXPECT_EQ(TestNativeWebContentsModalDialogManager::SHOWN, | |
254 native_manager->GetDialogState(dialog2)); | |
255 EXPECT_EQ(TestNativeWebContentsModalDialogManager::NOT_SHOWN, | |
256 native_manager->GetDialogState(dialog3)); | |
257 EXPECT_EQ(TestNativeWebContentsModalDialogManager::NOT_SHOWN, | |
258 native_manager->GetDialogState(dialog4)); | |
259 | |
260 native_manager->CloseDialog(dialog3); | |
261 | |
262 EXPECT_TRUE(manager->IsShowingDialog()); | |
263 EXPECT_TRUE(delegate->web_contents_blocked()); | |
264 EXPECT_EQ(TestNativeWebContentsModalDialogManager::SHOWN, | |
265 native_manager->GetDialogState(dialog2)); | |
266 EXPECT_EQ(TestNativeWebContentsModalDialogManager::CLOSED, | |
267 native_manager->GetDialogState(dialog3)); | |
268 EXPECT_EQ(TestNativeWebContentsModalDialogManager::NOT_SHOWN, | |
269 native_manager->GetDialogState(dialog4)); | |
270 | |
271 native_manager->CloseDialog(dialog2); | |
272 | |
273 EXPECT_TRUE(manager->IsShowingDialog()); | |
274 EXPECT_TRUE(delegate->web_contents_blocked()); | |
275 EXPECT_EQ(TestNativeWebContentsModalDialogManager::CLOSED, | |
276 native_manager->GetDialogState(dialog2)); | |
277 EXPECT_EQ(TestNativeWebContentsModalDialogManager::SHOWN, | |
278 native_manager->GetDialogState(dialog4)); | |
279 | |
280 native_manager->CloseDialog(dialog4); | |
281 | |
282 EXPECT_FALSE(manager->IsShowingDialog()); | |
283 EXPECT_FALSE(delegate->web_contents_blocked()); | |
284 EXPECT_EQ(TestNativeWebContentsModalDialogManager::CLOSED, | |
285 native_manager->GetDialogState(dialog4)); | |
286 } | |
287 | |
288 TEST_F(WebContentsModalDialogManagerTest, CloseAllDialogs) { | |
72 const int kWindowCount = 4; | 289 const int kWindowCount = 4; |
73 for (int i = 0; i < kWindowCount; i++) { | 290 for (int i = 0; i < kWindowCount; i++) |
74 // WebContentsModalDialogManager treats the NativeWebContentsModalDialog as | 291 manager->ShowDialog(MakeFakeDialog(i)); |
75 // an opaque type, so creating fake NativeWebContentsModalDialogs using | 292 |
76 // reinterpret_cast is valid. | 293 EXPECT_EQ(0, native_manager->GetCloseCount()); |
77 web_contents_modal_dialog_manager->ShowDialog( | 294 |
78 reinterpret_cast<NativeWebContentsModalDialog>(i)); | 295 test_api->CloseAllDialogs(); |
79 } | 296 EXPECT_FALSE(delegate->web_contents_blocked()); |
80 EXPECT_EQ(0, native_manager->close_count()); | 297 EXPECT_FALSE(manager->IsShowingDialog()); |
81 | 298 EXPECT_EQ(kWindowCount, native_manager->GetCloseCount()); |
82 test_api.CloseAllDialogs(); | |
83 EXPECT_EQ(kWindowCount, native_manager->close_count()); | |
84 } | 299 } |
85 | 300 |
86 } // namespace web_modal | 301 } // namespace web_modal |
OLD | NEW |