Index: content/browser/frame_host/interstitial_page_impl_browsertest.cc |
diff --git a/content/browser/frame_host/interstitial_page_impl_browsertest.cc b/content/browser/frame_host/interstitial_page_impl_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c3b30892bb2a158129019e273b0e36d45f39d65e |
--- /dev/null |
+++ b/content/browser/frame_host/interstitial_page_impl_browsertest.cc |
@@ -0,0 +1,275 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/frame_host/interstitial_page_impl.h" |
+ |
+#include "base/auto_reset.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "content/browser/web_contents/web_contents_impl.h" |
+#include "content/common/frame_messages.h" |
+#include "content/public/browser/browser_message_filter.h" |
+#include "content/public/browser/interstitial_page_delegate.h" |
+#include "content/public/test/browser_test_utils.h" |
+#include "content/public/test/content_browser_test.h" |
+#include "content/public/test/test_utils.h" |
+#include "content/shell/browser/shell.h" |
+#include "ui/base/clipboard/scoped_clipboard_writer.h" |
+#include "ui/base/test/test_clipboard.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+class TestInterstitialPageDelegate : public InterstitialPageDelegate { |
+ private: |
+ // InterstitialPageDelegate: |
+ std::string GetHTMLContents() override { |
+ return "<html>" |
+ "<head>" |
+ "<title>LOADED</title>" |
+ "<script>" |
+ "function focus_select_input() {" |
+ " document.getElementById('input').select();" |
+ "}" |
+ "function set_input_text(text) {" |
+ " document.getElementById('input').value = text;" |
+ "}" |
+ "function get_input_text() {" |
+ " window.domAutomationController.send(" |
+ " document.getElementById('input').value);" |
+ "}" |
+ "</script>" |
+ "</head>" |
+ "<body>" |
+ " <input id='input' oninput='document.title=\"TEXT_CHANGED\"'>" |
+ "</body>" |
+ "</html>"; |
+ } |
+}; |
+ |
+// A title watcher for interstitial pages. The existing TitleWatcher does not |
+// work for interstitial pages. Note that this title watcher waits for the |
+// title update IPC message not the actual title update. So, the new title is |
+// probably not propagated completely, yet. |
+class InterstitialTitleUpdateWatcher : public BrowserMessageFilter { |
+ public: |
+ InterstitialTitleUpdateWatcher() |
+ : BrowserMessageFilter(FrameMsgStart), |
+ waiting_(false), |
+ title_updated_(false) {} |
+ |
+ void InitWait(const std::string& expected_title) { |
+ expected_title_ = base::ASCIIToUTF16(expected_title); |
+ waiting_ = false; |
+ title_updated_ = false; |
+ } |
+ |
+ void Wait() { |
+ DCHECK(!waiting_); |
+ if (title_updated_) |
+ return; |
+ base::RunLoop run_loop; |
+ base::AutoReset<base::Closure> reset_quit_closure(&quit_closure_, |
+ run_loop.QuitClosure()); |
+ base::AutoReset<bool> reset_waiting(&waiting_, true); |
+ run_loop.Run(); |
+ } |
+ |
+ private: |
+ ~InterstitialTitleUpdateWatcher() override {} |
+ |
+ void OnTitleUpdateReceived(const base::string16& title) { |
+ if (title == expected_title_) { |
+ title_updated_ = true; |
+ if (waiting_) { |
+ waiting_ = false; |
+ quit_closure_.Run(); |
+ } |
+ } |
+ } |
+ |
+ // BrowserMessageFilter: |
+ bool OnMessageReceived(const IPC::Message& message) override { |
+ if (message.type() == FrameHostMsg_UpdateTitle::ID) { |
+ FrameHostMsg_UpdateTitle::Param params; |
+ if (FrameHostMsg_UpdateTitle::Read(&message, ¶ms)) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&InterstitialTitleUpdateWatcher::OnTitleUpdateReceived, |
+ this, base::get<0>(params))); |
+ } |
+ } |
+ return false; |
+ } |
+ |
+ base::string16 expected_title_; |
+ bool waiting_; |
+ bool title_updated_; |
+ base::Closure quit_closure_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(InterstitialTitleUpdateWatcher); |
+}; |
+ |
+} // namespace |
+ |
+class InterstitialPageImplTest : public ContentBrowserTest { |
+ public: |
+ InterstitialPageImplTest() |
+ : clipboard_(ui::TestClipboard::CreateForCurrentThread()), |
+ title_update_watcher_(nullptr) {} |
+ |
+ ~InterstitialPageImplTest() override { |
+ ui::Clipboard::DestroyClipboardForCurrentThread(); |
+ } |
+ |
+ protected: |
+ void SetUpInterstitialPage() { |
+ WebContentsImpl* web_contents = |
+ static_cast<WebContentsImpl*>(shell()->web_contents()); |
+ |
+ // Create the interstitial page. |
+ TestInterstitialPageDelegate* interstitial_delegate = |
+ new TestInterstitialPageDelegate; |
+ GURL url("http://interstitial"); |
+ interstitial_.reset(new InterstitialPageImpl( |
+ web_contents, static_cast<RenderWidgetHostDelegate*>(web_contents), |
+ true, url, interstitial_delegate)); |
+ interstitial_->Show(); |
+ WaitForInterstitialAttach(web_contents); |
+ |
+ // Focus the interstitial frame |
+ FrameTree* frame_tree = static_cast<RenderViewHostDelegate*>( |
+ interstitial_.get())->GetFrameTree(); |
+ frame_tree->SetFocusedFrame(frame_tree->root()); |
+ |
+ title_update_watcher_ = new InterstitialTitleUpdateWatcher; |
+ interstitial_->GetMainFrame()->GetProcess()->AddFilter( |
+ title_update_watcher_); |
+ |
+ if (web_contents->GetTitle() != base::ASCIIToUTF16("LOADED")) { |
+ title_update_watcher_->InitWait("LOADED"); |
+ title_update_watcher_->Wait(); |
+ } |
+ |
+ ClearClipboard(); |
+ } |
+ |
+ void TearDownInterstitialPage() { |
+ // Close the interstitial. |
+ interstitial_->DontProceed(); |
+ WaitForInterstitialDetach(shell()->web_contents()); |
+ interstitial_.reset(); |
+ } |
+ |
+ bool FocusInputAndSelectText() { |
+ return ExecuteScript(interstitial_->GetMainFrame(), "focus_select_input()"); |
+ } |
+ |
+ bool GetInputText(std::string* input_text) { |
+ return ExecuteScriptAndExtractString(interstitial_->GetMainFrame(), |
+ "get_input_text()", input_text); |
+ } |
+ |
+ bool SetInputText(const std::string& text) { |
+ return ExecuteScript(interstitial_->GetMainFrame(), |
+ "set_input_text('" + text + "')"); |
+ } |
+ |
+ void PerformCut() { |
+ clipboard_->ResetWaits(); |
+ title_update_watcher_->InitWait("TEXT_CHANGED"); |
+ RenderFrameHostImpl* rfh = |
+ static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
+ rfh->GetRenderWidgetHost()->delegate()->Cut(); |
+ clipboard_->WaitForWriteText(); |
+ title_update_watcher_->Wait(); |
+ } |
+ |
+ void PerformCopy() { |
+ clipboard_->ResetWaits(); |
+ RenderFrameHostImpl* rfh = |
+ static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
+ rfh->GetRenderWidgetHost()->delegate()->Copy(); |
+ clipboard_->WaitForWriteText(); |
+ } |
+ |
+ void PerformPaste() { |
+ title_update_watcher_->InitWait("TEXT_CHANGED"); |
+ RenderFrameHostImpl* rfh = |
+ static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
+ rfh->GetRenderWidgetHost()->delegate()->Paste(); |
+ title_update_watcher_->Wait(); |
+ } |
+ |
+ void ClearClipboard() { clipboard_->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE); } |
+ |
+ std::string GetClipboardText() { |
+ std::string clipboard_text; |
+ clipboard_->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_text); |
+ return clipboard_text; |
+ } |
+ |
+ void SetClipboardText(const std::string& text) { |
+ ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); |
+ clipboard_writer.WriteText(base::ASCIIToUTF16(text)); |
+ } |
+ |
+ private: |
+ scoped_ptr<InterstitialPageImpl> interstitial_; |
+ ui::TestClipboard* const clipboard_; |
+ InterstitialTitleUpdateWatcher* title_update_watcher_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { |
+ SetUpInterstitialPage(); |
+ |
+ ASSERT_TRUE(SetInputText("text-to-cut")); |
+ ASSERT_TRUE(FocusInputAndSelectText()); |
+ |
+ PerformCut(); |
+ |
+ std::string input_text; |
+ ASSERT_TRUE(GetInputText(&input_text)); |
+ EXPECT_EQ(std::string(), input_text); |
+ EXPECT_EQ("text-to-cut", GetClipboardText()); |
+ |
+ TearDownInterstitialPage(); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Copy) { |
+ SetUpInterstitialPage(); |
+ |
+ ASSERT_TRUE(SetInputText("text-to-copy")); |
+ ASSERT_TRUE(FocusInputAndSelectText()); |
+ |
+ PerformCopy(); |
+ |
+ std::string input_text; |
+ ASSERT_TRUE(GetInputText(&input_text)); |
+ EXPECT_EQ("text-to-copy", input_text); |
+ EXPECT_EQ("text-to-copy", GetClipboardText()); |
+ |
+ TearDownInterstitialPage(); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) { |
+ SetUpInterstitialPage(); |
+ |
+ SetClipboardText("text-to-paste"); |
+ |
+ ASSERT_TRUE(SetInputText("")); |
dcheng
2015/06/29 22:02:56
Nit: std::string()
mohsen
2015/06/29 22:56:29
Done.
|
+ ASSERT_TRUE(FocusInputAndSelectText()); |
+ |
+ PerformPaste(); |
+ |
+ std::string input_text; |
+ ASSERT_TRUE(GetInputText(&input_text)); |
+ EXPECT_EQ("text-to-paste", input_text); |
+ |
+ TearDownInterstitialPage(); |
+} |
+ |
+} // namespace content |