Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/frame_host/interstitial_page_impl.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "content/browser/web_contents/web_contents_impl.h" | |
| 10 #include "content/common/frame_messages.h" | |
| 11 #include "content/public/browser/browser_message_filter.h" | |
| 12 #include "content/public/browser/interstitial_page_delegate.h" | |
| 13 #include "content/public/test/browser_test_utils.h" | |
| 14 #include "content/public/test/content_browser_test.h" | |
| 15 #include "content/public/test/test_utils.h" | |
| 16 #include "content/shell/browser/shell.h" | |
| 17 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
| 18 #include "ui/base/test/test_clipboard.h" | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 class TestInterstitialPageDelegate : public InterstitialPageDelegate { | |
| 25 private: | |
| 26 // InterstitialPageDelegate: | |
| 27 std::string GetHTMLContents() override { | |
| 28 return "<html>" | |
| 29 "<head>" | |
| 30 "<title>LOADED</title>" | |
| 31 "<script>" | |
| 32 "function focus_select_input() {" | |
| 33 " document.getElementById('input').select();" | |
| 34 "}" | |
| 35 "function set_input_text(text) {" | |
| 36 " document.getElementById('input').value = text;" | |
| 37 "}" | |
| 38 "function get_input_text() {" | |
| 39 " return document.getElementById('input').value;" | |
| 40 "}" | |
| 41 "</script>" | |
| 42 "</head>" | |
| 43 "<body>" | |
| 44 " <input id='input' oninput='document.title=\"TEXT_CHANGED\"'>" | |
| 45 "</body>" | |
| 46 "</html>"; | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 class TitleUpdateWatcher : public BrowserMessageFilter { | |
|
dcheng
2015/06/29 17:58:02
How come we can't just use TitleWatcher?
mohsen
2015/06/29 21:46:53
TitleWatcher is not working for interstitial pages
| |
| 51 public: | |
| 52 TitleUpdateWatcher() | |
| 53 : BrowserMessageFilter(FrameMsgStart), | |
| 54 waiting_(false), | |
| 55 title_updated_(false) {} | |
| 56 | |
| 57 void InitWait(const std::string& expected_title) { | |
| 58 expected_title_ = base::ASCIIToUTF16(expected_title); | |
| 59 waiting_ = false; | |
| 60 title_updated_ = false; | |
| 61 } | |
| 62 | |
| 63 void Wait() { | |
|
dcheng
2015/06/29 17:58:02
You should probably just combine this with InitWai
mohsen
2015/06/29 21:46:53
The problem is that the message we are waiting for
dcheng
2015/06/29 22:02:56
You can call RunLoop::Quit before calling RunLoop:
mohsen
2015/06/29 22:56:29
If this is meant to be allocated on stack, I think
dcheng
2015/06/29 23:15:41
You can still simplify the internal state manageme
| |
| 64 DCHECK(!waiting_); | |
| 65 if (title_updated_) | |
| 66 return; | |
| 67 base::RunLoop run_loop; | |
| 68 base::AutoReset<base::Closure> reset_quit_closure(&quit_closure_, | |
| 69 run_loop.QuitClosure()); | |
| 70 base::AutoReset<bool> reset_waiting(&waiting_, true); | |
| 71 run_loop.Run(); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 ~TitleUpdateWatcher() override {} | |
| 76 | |
| 77 void OnTitleUpdateReceived(const base::string16& title) { | |
| 78 if (title == expected_title_) { | |
| 79 title_updated_ = true; | |
| 80 if (waiting_) { | |
| 81 waiting_ = false; | |
| 82 quit_closure_.Run(); | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 // BrowserMessageFilter: | |
| 88 bool OnMessageReceived(const IPC::Message& message) override { | |
| 89 if (message.type() == FrameHostMsg_UpdateTitle::ID) { | |
| 90 FrameHostMsg_UpdateTitle::Param params; | |
| 91 if (FrameHostMsg_UpdateTitle::Read(&message, ¶ms)) { | |
| 92 BrowserThread::PostTask( | |
| 93 BrowserThread::UI, FROM_HERE, | |
| 94 base::Bind(&TitleUpdateWatcher::OnTitleUpdateReceived, this, | |
| 95 base::get<0>(params))); | |
| 96 } | |
| 97 } | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 base::string16 expected_title_; | |
| 102 bool waiting_; | |
| 103 bool title_updated_; | |
| 104 base::Closure quit_closure_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(TitleUpdateWatcher); | |
| 107 }; | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 111 class InterstitialPageImplTest : public ContentBrowserTest { | |
| 112 public: | |
| 113 InterstitialPageImplTest() | |
| 114 : web_contents_(nullptr), | |
| 115 clipboard_(ui::TestClipboard::CreateForCurrentThread()), | |
| 116 title_update_watcher_(nullptr) {} | |
| 117 | |
| 118 ~InterstitialPageImplTest() override { | |
| 119 ui::Clipboard::DestroyClipboardForCurrentThread(); | |
| 120 } | |
| 121 | |
| 122 protected: | |
| 123 void SetUpInterstitialPage() { | |
| 124 web_contents_ = static_cast<WebContentsImpl*>(shell()->web_contents()); | |
| 125 | |
| 126 // Create the interstitial page. | |
| 127 TestInterstitialPageDelegate* interstitial_delegate = | |
| 128 new TestInterstitialPageDelegate; | |
| 129 GURL url("http://interstitial"); | |
| 130 interstitial_.reset(new InterstitialPageImpl( | |
| 131 web_contents_, static_cast<RenderWidgetHostDelegate*>(web_contents_), | |
| 132 true, url, interstitial_delegate)); | |
| 133 interstitial_->Show(); | |
| 134 WaitForInterstitialAttach(web_contents_); | |
| 135 | |
| 136 // Focus the interstitial frame | |
| 137 FrameTree* frame_tree = static_cast<RenderViewHostDelegate*>( | |
| 138 interstitial_.get())->GetFrameTree(); | |
| 139 frame_tree->SetFocusedFrame(frame_tree->root()); | |
| 140 | |
| 141 title_update_watcher_ = new TitleUpdateWatcher; | |
|
dcheng
2015/06/29 22:02:56
Incidentally, this is leaking atm, but with the su
| |
| 142 interstitial_->GetMainFrame()->GetProcess()->AddFilter( | |
| 143 title_update_watcher_); | |
| 144 | |
| 145 if (web_contents_->GetTitle() != base::ASCIIToUTF16("LOADED")) { | |
| 146 title_update_watcher_->InitWait("LOADED"); | |
| 147 title_update_watcher_->Wait(); | |
| 148 } | |
| 149 | |
| 150 ClearClipboard(); | |
| 151 } | |
| 152 | |
| 153 void TearDownInterstitialPage() { | |
| 154 // Close the interstitial. | |
| 155 interstitial_->DontProceed(); | |
| 156 WaitForInterstitialDetach(web_contents_); | |
| 157 interstitial_.reset(); | |
| 158 | |
| 159 web_contents_ = nullptr; | |
| 160 } | |
| 161 | |
| 162 void FocusInputAndSelectText() { | |
| 163 ExecuteScriptAndGetValue(interstitial_->GetMainFrame(), | |
|
dcheng
2015/06/29 17:58:02
The comment on this function is:
// This should no
mohsen
2015/06/29 21:46:53
Done.
| |
| 164 "focus_select_input()"); | |
| 165 } | |
| 166 | |
| 167 std::string GetInputText() { | |
| 168 scoped_ptr<base::Value> input_value = ExecuteScriptAndGetValue( | |
| 169 interstitial_->GetMainFrame(), "get_input_text()"); | |
| 170 std::string input_text; | |
| 171 input_value->GetAsString(&input_text); | |
|
dcheng
2015/06/29 17:58:02
GetAsString() can fail.
mohsen
2015/06/29 21:46:53
Not applicable, anymore.
| |
| 172 return input_text; | |
| 173 } | |
| 174 | |
| 175 void SetInputText(const std::string& text) { | |
| 176 ExecuteScriptAndGetValue(interstitial_->GetMainFrame(), | |
| 177 "set_input_text('" + text + "')"); | |
| 178 } | |
| 179 | |
| 180 void PerformCut() { | |
| 181 clipboard_->ResetWaits(); | |
| 182 title_update_watcher_->InitWait("TEXT_CHANGED"); | |
| 183 RenderFrameHostImpl* rfh = | |
| 184 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | |
| 185 rfh->GetRenderWidgetHost()->delegate()->Cut(); | |
| 186 clipboard_->WaitForWriteText(); | |
| 187 title_update_watcher_->Wait(); | |
| 188 } | |
| 189 | |
| 190 void PerformCopy() { | |
| 191 clipboard_->ResetWaits(); | |
| 192 RenderFrameHostImpl* rfh = | |
| 193 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | |
| 194 rfh->GetRenderWidgetHost()->delegate()->Copy(); | |
| 195 clipboard_->WaitForWriteText(); | |
| 196 } | |
| 197 | |
| 198 void PerformPaste() { | |
| 199 title_update_watcher_->InitWait("TEXT_CHANGED"); | |
| 200 RenderFrameHostImpl* rfh = | |
| 201 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | |
| 202 rfh->GetRenderWidgetHost()->delegate()->Paste(); | |
| 203 title_update_watcher_->Wait(); | |
| 204 } | |
| 205 | |
| 206 void ClearClipboard() { clipboard_->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE); } | |
| 207 | |
| 208 std::string GetClipboardText() { | |
| 209 std::string clipboard_text; | |
| 210 clipboard_->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_text); | |
| 211 return clipboard_text; | |
| 212 } | |
| 213 | |
| 214 void SetClipboardText(const std::string& text) { | |
| 215 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
| 216 clipboard_writer.WriteText(base::ASCIIToUTF16(text)); | |
| 217 } | |
| 218 | |
| 219 private: | |
| 220 WebContentsImpl* web_contents_; | |
| 221 scoped_ptr<InterstitialPageImpl> interstitial_; | |
| 222 ui::TestClipboard* const clipboard_; | |
| 223 TitleUpdateWatcher* title_update_watcher_; | |
| 224 | |
| 225 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); | |
| 226 }; | |
| 227 | |
| 228 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { | |
| 229 SetUpInterstitialPage(); | |
| 230 | |
| 231 SetInputText("text-to-cut"); | |
| 232 FocusInputAndSelectText(); | |
| 233 | |
| 234 PerformCut(); | |
| 235 | |
| 236 EXPECT_EQ(std::string(), GetInputText()); | |
| 237 EXPECT_EQ("text-to-cut", GetClipboardText()); | |
| 238 | |
| 239 TearDownInterstitialPage(); | |
| 240 } | |
| 241 | |
| 242 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Copy) { | |
| 243 SetUpInterstitialPage(); | |
| 244 | |
| 245 SetInputText("text-to-copy"); | |
| 246 FocusInputAndSelectText(); | |
| 247 | |
| 248 PerformCopy(); | |
| 249 | |
| 250 EXPECT_EQ("text-to-copy", GetInputText()); | |
| 251 EXPECT_EQ("text-to-copy", GetClipboardText()); | |
| 252 | |
| 253 TearDownInterstitialPage(); | |
| 254 } | |
| 255 | |
| 256 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) { | |
| 257 SetUpInterstitialPage(); | |
| 258 | |
| 259 SetClipboardText("text-to-paste"); | |
| 260 | |
| 261 SetInputText(""); | |
| 262 FocusInputAndSelectText(); | |
| 263 | |
| 264 PerformPaste(); | |
| 265 | |
| 266 EXPECT_EQ("text-to-paste", GetInputText()); | |
| 267 | |
| 268 TearDownInterstitialPage(); | |
| 269 } | |
| 270 | |
| 271 } // namespace content | |
| OLD | NEW |