OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/frame_host/interstitial_page_impl.h" | 5 #include "content/browser/frame_host/interstitial_page_impl.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
10 #include "content/common/clipboard_messages.h" | 10 #include "content/common/clipboard_messages.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 namespace { | 24 namespace { |
25 | 25 |
26 class TestInterstitialPageDelegate : public InterstitialPageDelegate { | 26 class TestInterstitialPageDelegate : public InterstitialPageDelegate { |
27 private: | 27 private: |
28 // InterstitialPageDelegate: | 28 // InterstitialPageDelegate: |
29 std::string GetHTMLContents() override { | 29 std::string GetHTMLContents() override { |
30 return "<html>" | 30 return "<html>" |
31 "<head>" | 31 "<head>" |
32 "<title>LOADED</title>" | 32 "<title>LOADED</title>" |
33 "<script>" | 33 "<script>" |
34 "function create_input_and_set_text(text) {" | |
35 " var input = document.createElement('input');" | |
36 " input.id = 'input';" | |
37 " document.body.appendChild(input);" | |
38 " document.getElementById('input').value = text;" | |
39 " input.addEventListener('input'," | |
40 " function() { document.title='TEXT_CHANGED'; });" | |
41 "}" | |
34 "function focus_select_input() {" | 42 "function focus_select_input() {" |
35 " document.getElementById('input').select();" | 43 " document.getElementById('input').select();" |
36 "}" | 44 "}" |
37 "function set_input_text(text) {" | |
38 " document.getElementById('input').value = text;" | |
39 "}" | |
40 "function get_input_text() {" | 45 "function get_input_text() {" |
41 " window.domAutomationController.send(" | 46 " window.domAutomationController.send(" |
42 " document.getElementById('input').value);" | 47 " document.getElementById('input').value);" |
43 "}" | 48 "}" |
49 "function get_selection() {" | |
50 " window.domAutomationController.send(" | |
51 " window.getSelection().toString());" | |
52 "}" | |
44 "</script>" | 53 "</script>" |
45 "</head>" | 54 "</head>" |
46 "<body>" | 55 "<body>original body text</body>" |
47 " <input id='input' oninput='document.title=\"TEXT_CHANGED\"'>" | |
48 "</body>" | |
49 "</html>"; | 56 "</html>"; |
50 } | 57 } |
51 }; | 58 }; |
52 | 59 |
53 // A title watcher for interstitial pages. The existing TitleWatcher does not | 60 // A title watcher for interstitial pages. The existing TitleWatcher does not |
54 // work for interstitial pages. Note that this title watcher waits for the | 61 // work for interstitial pages. Note that this title watcher waits for the |
55 // title update IPC message not the actual title update. So, the new title is | 62 // title update IPC message not the actual title update. So, the new title is |
56 // probably not propagated completely, yet. | 63 // probably not propagated completely, yet. |
57 class InterstitialTitleUpdateWatcher : public BrowserMessageFilter { | 64 class InterstitialTitleUpdateWatcher : public BrowserMessageFilter { |
58 public: | 65 public: |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 | 263 |
257 bool FocusInputAndSelectText() { | 264 bool FocusInputAndSelectText() { |
258 return ExecuteScript(interstitial_->GetMainFrame(), "focus_select_input()"); | 265 return ExecuteScript(interstitial_->GetMainFrame(), "focus_select_input()"); |
259 } | 266 } |
260 | 267 |
261 bool GetInputText(std::string* input_text) { | 268 bool GetInputText(std::string* input_text) { |
262 return ExecuteScriptAndExtractString(interstitial_->GetMainFrame(), | 269 return ExecuteScriptAndExtractString(interstitial_->GetMainFrame(), |
263 "get_input_text()", input_text); | 270 "get_input_text()", input_text); |
264 } | 271 } |
265 | 272 |
266 bool SetInputText(const std::string& text) { | 273 bool GetSelection(std::string* input_text) { |
274 return ExecuteScriptAndExtractString(interstitial_->GetMainFrame(), | |
275 "get_selection()", input_text); | |
276 } | |
277 | |
278 bool CreateInputAndSetText(const std::string& text) { | |
267 return ExecuteScript(interstitial_->GetMainFrame(), | 279 return ExecuteScript(interstitial_->GetMainFrame(), |
268 "set_input_text('" + text + "')"); | 280 "create_input_and_set_text('" + text + "')"); |
269 } | 281 } |
270 | 282 |
271 std::string PerformCut() { | 283 std::string PerformCut() { |
272 clipboard_message_watcher_->InitWait(); | 284 clipboard_message_watcher_->InitWait(); |
273 title_update_watcher_->InitWait("TEXT_CHANGED"); | 285 title_update_watcher_->InitWait("TEXT_CHANGED"); |
274 RenderFrameHostImpl* rfh = | 286 RenderFrameHostImpl* rfh = |
275 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | 287 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
276 rfh->GetRenderWidgetHost()->delegate()->Cut(); | 288 rfh->GetRenderWidgetHost()->delegate()->Cut(); |
277 clipboard_message_watcher_->WaitForWriteCommit(); | 289 clipboard_message_watcher_->WaitForWriteCommit(); |
278 title_update_watcher_->Wait(); | 290 title_update_watcher_->Wait(); |
(...skipping 10 matching lines...) Expand all Loading... | |
289 } | 301 } |
290 | 302 |
291 void PerformPaste() { | 303 void PerformPaste() { |
292 title_update_watcher_->InitWait("TEXT_CHANGED"); | 304 title_update_watcher_->InitWait("TEXT_CHANGED"); |
293 RenderFrameHostImpl* rfh = | 305 RenderFrameHostImpl* rfh = |
294 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | 306 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
295 rfh->GetRenderWidgetHost()->delegate()->Paste(); | 307 rfh->GetRenderWidgetHost()->delegate()->Paste(); |
296 title_update_watcher_->Wait(); | 308 title_update_watcher_->Wait(); |
297 } | 309 } |
298 | 310 |
311 void PerformSelectAll() { | |
312 RenderFrameHostImpl* rfh = | |
313 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | |
314 rfh->GetRenderWidgetHost()->delegate()->SelectAll(); | |
315 } | |
316 | |
299 private: | 317 private: |
300 void RunTaskOnIOThreadAndWait(const base::Closure& task) { | 318 void RunTaskOnIOThreadAndWait(const base::Closure& task) { |
301 base::WaitableEvent completion(false, false); | 319 base::WaitableEvent completion(false, false); |
302 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 320 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
303 base::Bind(&InterstitialPageImplTest::RunTask, this, | 321 base::Bind(&InterstitialPageImplTest::RunTask, this, |
304 task, &completion)); | 322 task, &completion)); |
305 completion.Wait(); | 323 completion.Wait(); |
306 } | 324 } |
307 | 325 |
308 void RunTask(const base::Closure& task, base::WaitableEvent* completion) { | 326 void RunTask(const base::Closure& task, base::WaitableEvent* completion) { |
309 task.Run(); | 327 task.Run(); |
310 completion->Signal(); | 328 completion->Signal(); |
311 } | 329 } |
312 | 330 |
313 scoped_ptr<InterstitialPageImpl> interstitial_; | 331 scoped_ptr<InterstitialPageImpl> interstitial_; |
314 scoped_refptr<ClipboardMessageWatcher> clipboard_message_watcher_; | 332 scoped_refptr<ClipboardMessageWatcher> clipboard_message_watcher_; |
315 scoped_refptr<InterstitialTitleUpdateWatcher> title_update_watcher_; | 333 scoped_refptr<InterstitialTitleUpdateWatcher> title_update_watcher_; |
316 | 334 |
317 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); | 335 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); |
318 }; | 336 }; |
319 | 337 |
320 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { | 338 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { |
321 SetUpInterstitialPage(); | 339 SetUpInterstitialPage(); |
322 | 340 |
323 ASSERT_TRUE(SetInputText("text-to-cut")); | 341 ASSERT_TRUE(CreateInputAndSetText("text-to-cut")); |
324 ASSERT_TRUE(FocusInputAndSelectText()); | 342 ASSERT_TRUE(FocusInputAndSelectText()); |
325 | 343 |
326 std::string clipboard_text = PerformCut(); | 344 std::string clipboard_text = PerformCut(); |
327 EXPECT_EQ("text-to-cut", clipboard_text); | 345 EXPECT_EQ("text-to-cut", clipboard_text); |
328 | 346 |
329 std::string input_text; | 347 std::string input_text; |
330 ASSERT_TRUE(GetInputText(&input_text)); | 348 ASSERT_TRUE(GetInputText(&input_text)); |
331 EXPECT_EQ(std::string(), input_text); | 349 EXPECT_EQ(std::string(), input_text); |
332 | 350 |
333 TearDownInterstitialPage(); | 351 TearDownInterstitialPage(); |
334 } | 352 } |
335 | 353 |
336 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Copy) { | 354 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Copy) { |
337 SetUpInterstitialPage(); | 355 SetUpInterstitialPage(); |
338 | 356 |
339 ASSERT_TRUE(SetInputText("text-to-copy")); | 357 ASSERT_TRUE(CreateInputAndSetText("text-to-copy")); |
340 ASSERT_TRUE(FocusInputAndSelectText()); | 358 ASSERT_TRUE(FocusInputAndSelectText()); |
341 | 359 |
342 std::string clipboard_text = PerformCopy(); | 360 std::string clipboard_text = PerformCopy(); |
343 EXPECT_EQ("text-to-copy", clipboard_text); | 361 EXPECT_EQ("text-to-copy", clipboard_text); |
344 | 362 |
345 std::string input_text; | 363 std::string input_text; |
346 ASSERT_TRUE(GetInputText(&input_text)); | 364 ASSERT_TRUE(GetInputText(&input_text)); |
347 EXPECT_EQ("text-to-copy", input_text); | 365 EXPECT_EQ("text-to-copy", input_text); |
348 | 366 |
349 TearDownInterstitialPage(); | 367 TearDownInterstitialPage(); |
350 } | 368 } |
351 | 369 |
352 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) { | 370 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) { |
353 SetUpTestClipboard(); | 371 SetUpTestClipboard(); |
354 SetUpInterstitialPage(); | 372 SetUpInterstitialPage(); |
355 | 373 |
356 SetClipboardText("text-to-paste"); | 374 SetClipboardText("text-to-paste"); |
357 | 375 |
358 ASSERT_TRUE(SetInputText(std::string())); | 376 ASSERT_TRUE(CreateInputAndSetText(std::string())); |
359 ASSERT_TRUE(FocusInputAndSelectText()); | 377 ASSERT_TRUE(FocusInputAndSelectText()); |
360 | 378 |
361 PerformPaste(); | 379 PerformPaste(); |
362 | 380 |
363 std::string input_text; | 381 std::string input_text; |
364 ASSERT_TRUE(GetInputText(&input_text)); | 382 ASSERT_TRUE(GetInputText(&input_text)); |
365 EXPECT_EQ("text-to-paste", input_text); | 383 EXPECT_EQ("text-to-paste", input_text); |
366 | 384 |
367 TearDownInterstitialPage(); | 385 TearDownInterstitialPage(); |
386 } | |
387 | |
388 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, SelectAll) { | |
389 SetUpInterstitialPage(); | |
390 | |
391 std::string input_text; | |
392 ASSERT_TRUE(GetSelection(&input_text)); | |
393 EXPECT_EQ(std::string(), input_text); | |
394 | |
395 PerformSelectAll(); | |
396 ASSERT_TRUE(GetSelection(&input_text)); | |
397 EXPECT_EQ("original body text", input_text); | |
398 | |
399 TearDownInterstitialPage(); | |
368 TearDownTestClipboard(); | 400 TearDownTestClipboard(); |
mohsen
2015/07/07 16:47:32
This line belongs to 'Paste' test.
lgarron
2015/07/07 17:46:33
Done.
I assumed this was placed in the last of th
| |
369 } | 401 } |
370 | 402 |
371 } // namespace content | 403 } // namespace content |
OLD | NEW |