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 { | |
nasko
2015/06/26 11:35:39
Is the existing TitleWatcher insufficient for this
mohsen
2015/06/26 19:57:56
The existing one does not work for interstitial pa
nasko
2015/06/29 09:30:24
If this is the case, let's name the class to indic
mohsen
2015/06/29 21:46:53
Done.
| |
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() { | |
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 interstitial_delegate_(nullptr), | |
116 clipboard_(static_cast<ui::TestClipboard*>( | |
117 ui::TestClipboard::CreateForCurrentThread())), | |
118 title_update_watcher_(nullptr) {} | |
119 | |
120 ~InterstitialPageImplTest() override {} | |
121 | |
122 protected: | |
123 void FocusInputAndSelectText() { | |
124 ExecuteScriptAndGetValue(interstitial_->GetMainFrame(), | |
125 "focus_select_input()"); | |
126 } | |
127 | |
128 std::string GetInputText() { | |
129 scoped_ptr<base::Value> input_value = ExecuteScriptAndGetValue( | |
130 interstitial_->GetMainFrame(), "get_input_text()"); | |
131 std::string input_text; | |
132 input_value->GetAsString(&input_text); | |
133 return input_text; | |
134 } | |
135 | |
136 void SetInputText(const std::string& text) { | |
137 ExecuteScriptAndGetValue(interstitial_->GetMainFrame(), | |
138 "set_input_text('" + text + "')"); | |
139 } | |
140 | |
141 void PerformCut() { | |
142 clipboard_->ResetWaits(); | |
143 title_update_watcher_->InitWait("TEXT_CHANGED"); | |
144 RenderFrameHostImpl* rfh = | |
145 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | |
146 rfh->GetRenderWidgetHost()->delegate()->Cut(); | |
147 clipboard_->WaitForWriteText(); | |
148 title_update_watcher_->Wait(); | |
149 } | |
150 | |
151 void PerformCopy() { | |
152 clipboard_->ResetWaits(); | |
nasko
2015/06/26 11:35:38
Why not use the title watcher here as well? Mostly
mohsen
2015/06/26 19:57:56
The title is only updated on events that change va
| |
153 RenderFrameHostImpl* rfh = | |
154 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | |
155 rfh->GetRenderWidgetHost()->delegate()->Copy(); | |
156 clipboard_->WaitForWriteText(); | |
157 } | |
158 | |
159 void PerformPaste() { | |
160 title_update_watcher_->InitWait("TEXT_CHANGED"); | |
161 RenderFrameHostImpl* rfh = | |
162 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | |
163 rfh->GetRenderWidgetHost()->delegate()->Paste(); | |
164 title_update_watcher_->Wait(); | |
165 } | |
166 | |
167 void ClearClipboard() { | |
168 clipboard_->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
169 } | |
170 | |
171 std::string GetClipboardText() { | |
172 std::string clipboard_text; | |
173 clipboard_->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, | |
174 &clipboard_text); | |
nasko
2015/06/26 11:35:38
nit: This indentation doesn't look right. Should b
mohsen
2015/06/26 19:57:56
Done.
| |
175 return clipboard_text; | |
176 } | |
177 | |
178 void SetClipboardText(const std::string& text) { | |
179 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
180 clipboard_writer.WriteText(base::ASCIIToUTF16(text)); | |
181 } | |
182 | |
183 private: | |
184 void SetUpOnMainThread() override { | |
185 ContentBrowserTest::SetUpOnMainThread(); | |
186 | |
187 web_contents_ = static_cast<WebContentsImpl*>(shell()->web_contents()); | |
188 | |
189 // Create the interstitial page. | |
nasko
2015/06/26 11:35:38
This is a lot of setup that is happening before th
mohsen
2015/06/26 19:57:56
Done.
| |
190 interstitial_delegate_ = new TestInterstitialPageDelegate; | |
191 GURL url("http://interstitial"); | |
192 interstitial_.reset(new InterstitialPageImpl( | |
193 web_contents_, static_cast<RenderWidgetHostDelegate*>(web_contents_), | |
194 true, url, interstitial_delegate_)); | |
195 interstitial_->Show(); | |
196 WaitForInterstitialAttach(web_contents_); | |
197 | |
198 // Focus the interstitial frame | |
199 FrameTree* frame_tree = static_cast<RenderViewHostDelegate*>( | |
200 interstitial_.get())->GetFrameTree(); | |
201 frame_tree->SetFocusedFrame(frame_tree->root()); | |
202 | |
203 title_update_watcher_ = new TitleUpdateWatcher; | |
204 interstitial_->GetMainFrame()->GetProcess()->AddFilter( | |
205 title_update_watcher_); | |
206 | |
207 if (web_contents_->GetTitle() != base::ASCIIToUTF16("LOADED")) { | |
208 title_update_watcher_->InitWait("LOADED"); | |
209 title_update_watcher_->Wait(); | |
210 } | |
211 } | |
212 | |
213 void TearDownOnMainThread() override { | |
214 // Close the interstitial. | |
215 interstitial_->DontProceed(); | |
216 WaitForInterstitialDetach(web_contents_); | |
217 interstitial_.reset(); | |
218 interstitial_delegate_ = nullptr; | |
219 web_contents_ = nullptr; | |
220 | |
221 ContentBrowserTest::TearDownOnMainThread(); | |
222 } | |
223 | |
224 WebContentsImpl* web_contents_; | |
225 TestInterstitialPageDelegate* interstitial_delegate_; | |
226 scoped_ptr<InterstitialPageImpl> interstitial_; | |
227 ui::TestClipboard* const clipboard_; | |
228 TitleUpdateWatcher* title_update_watcher_; | |
229 | |
230 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); | |
231 }; | |
232 | |
233 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { | |
234 ClearClipboard(); | |
235 | |
236 SetInputText("text-to-cut"); | |
237 FocusInputAndSelectText(); | |
238 | |
239 PerformCut(); | |
240 | |
241 EXPECT_EQ(std::string(), GetInputText()); | |
242 EXPECT_EQ("text-to-cut", GetClipboardText()); | |
243 } | |
244 | |
245 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Copy) { | |
246 ClearClipboard(); | |
247 | |
248 SetInputText("text-to-copy"); | |
249 FocusInputAndSelectText(); | |
250 | |
251 PerformCopy(); | |
252 | |
253 EXPECT_EQ("text-to-copy", GetInputText()); | |
254 EXPECT_EQ("text-to-copy", GetClipboardText()); | |
255 } | |
256 | |
257 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) { | |
258 SetClipboardText("text-to-paste"); | |
259 | |
260 SetInputText(""); | |
261 FocusInputAndSelectText(); | |
262 | |
263 PerformPaste(); | |
264 | |
265 EXPECT_EQ("text-to-paste", GetInputText()); | |
266 } | |
267 | |
268 } // namespace content | |
OLD | NEW |