Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: content/browser/frame_host/interstitial_page_impl_browsertest.cc

Issue 1162373002: Make some editing/selection functions accessible to c/b/renderer_host/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed review comments Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 " window.domAutomationController.send("
40 " document.getElementById('input').value);"
41 "}"
42 "</script>"
43 "</head>"
44 "<body>"
45 " <input id='input' oninput='document.title=\"TEXT_CHANGED\"'>"
46 "</body>"
47 "</html>";
48 }
49 };
50
51 // A title watcher for interstitial pages. The existing TitleWatcher does not
52 // work for interstitial pages. Note that this title watcher waits for the
53 // title update IPC message not the actual title update. So, the new title is
54 // probably not propagated completely, yet.
55 class InterstitialTitleUpdateWatcher : public BrowserMessageFilter {
56 public:
57 InterstitialTitleUpdateWatcher()
58 : BrowserMessageFilter(FrameMsgStart),
59 waiting_(false),
60 title_updated_(false) {}
61
62 void InitWait(const std::string& expected_title) {
63 expected_title_ = base::ASCIIToUTF16(expected_title);
64 waiting_ = false;
65 title_updated_ = false;
66 }
67
68 void Wait() {
69 DCHECK(!waiting_);
70 if (title_updated_)
71 return;
72 base::RunLoop run_loop;
73 base::AutoReset<base::Closure> reset_quit_closure(&quit_closure_,
74 run_loop.QuitClosure());
75 base::AutoReset<bool> reset_waiting(&waiting_, true);
76 run_loop.Run();
77 }
78
79 private:
80 ~InterstitialTitleUpdateWatcher() override {}
81
82 void OnTitleUpdateReceived(const base::string16& title) {
83 if (title == expected_title_) {
84 title_updated_ = true;
85 if (waiting_) {
86 waiting_ = false;
87 quit_closure_.Run();
88 }
89 }
90 }
91
92 // BrowserMessageFilter:
93 bool OnMessageReceived(const IPC::Message& message) override {
94 if (message.type() == FrameHostMsg_UpdateTitle::ID) {
95 FrameHostMsg_UpdateTitle::Param params;
96 if (FrameHostMsg_UpdateTitle::Read(&message, &params)) {
97 BrowserThread::PostTask(
98 BrowserThread::UI, FROM_HERE,
99 base::Bind(&InterstitialTitleUpdateWatcher::OnTitleUpdateReceived,
100 this, base::get<0>(params)));
101 }
102 }
103 return false;
104 }
105
106 base::string16 expected_title_;
107 bool waiting_;
108 bool title_updated_;
109 base::Closure quit_closure_;
110
111 DISALLOW_COPY_AND_ASSIGN(InterstitialTitleUpdateWatcher);
112 };
113
114 } // namespace
115
116 class InterstitialPageImplTest : public ContentBrowserTest {
117 public:
118 InterstitialPageImplTest()
119 : clipboard_(ui::TestClipboard::CreateForCurrentThread()),
120 title_update_watcher_(nullptr) {}
121
122 ~InterstitialPageImplTest() override {
123 ui::Clipboard::DestroyClipboardForCurrentThread();
124 }
125
126 protected:
127 void SetUpInterstitialPage() {
128 WebContentsImpl* web_contents =
129 static_cast<WebContentsImpl*>(shell()->web_contents());
130
131 // Create the interstitial page.
132 TestInterstitialPageDelegate* interstitial_delegate =
133 new TestInterstitialPageDelegate;
134 GURL url("http://interstitial");
135 interstitial_.reset(new InterstitialPageImpl(
136 web_contents, static_cast<RenderWidgetHostDelegate*>(web_contents),
137 true, url, interstitial_delegate));
138 interstitial_->Show();
139 WaitForInterstitialAttach(web_contents);
140
141 // Focus the interstitial frame
142 FrameTree* frame_tree = static_cast<RenderViewHostDelegate*>(
143 interstitial_.get())->GetFrameTree();
144 frame_tree->SetFocusedFrame(frame_tree->root());
145
146 title_update_watcher_ = new InterstitialTitleUpdateWatcher;
147 interstitial_->GetMainFrame()->GetProcess()->AddFilter(
148 title_update_watcher_);
149
150 if (web_contents->GetTitle() != base::ASCIIToUTF16("LOADED")) {
151 title_update_watcher_->InitWait("LOADED");
152 title_update_watcher_->Wait();
153 }
154
155 ClearClipboard();
156 }
157
158 void TearDownInterstitialPage() {
159 // Close the interstitial.
160 interstitial_->DontProceed();
161 WaitForInterstitialDetach(shell()->web_contents());
162 interstitial_.reset();
163 }
164
165 bool FocusInputAndSelectText() {
166 return ExecuteScript(interstitial_->GetMainFrame(), "focus_select_input()");
167 }
168
169 bool GetInputText(std::string* input_text) {
170 return ExecuteScriptAndExtractString(interstitial_->GetMainFrame(),
171 "get_input_text()", input_text);
172 }
173
174 bool SetInputText(const std::string& text) {
175 return ExecuteScript(interstitial_->GetMainFrame(),
176 "set_input_text('" + text + "')");
177 }
178
179 void PerformCut() {
180 clipboard_->ResetWaits();
181 title_update_watcher_->InitWait("TEXT_CHANGED");
182 RenderFrameHostImpl* rfh =
183 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame());
184 rfh->GetRenderWidgetHost()->delegate()->Cut();
185 clipboard_->WaitForWriteText();
186 title_update_watcher_->Wait();
187 }
188
189 void PerformCopy() {
190 clipboard_->ResetWaits();
191 RenderFrameHostImpl* rfh =
192 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame());
193 rfh->GetRenderWidgetHost()->delegate()->Copy();
194 clipboard_->WaitForWriteText();
195 }
196
197 void PerformPaste() {
198 title_update_watcher_->InitWait("TEXT_CHANGED");
199 RenderFrameHostImpl* rfh =
200 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame());
201 rfh->GetRenderWidgetHost()->delegate()->Paste();
202 title_update_watcher_->Wait();
203 }
204
205 void ClearClipboard() { clipboard_->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE); }
206
207 std::string GetClipboardText() {
208 std::string clipboard_text;
209 clipboard_->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_text);
210 return clipboard_text;
211 }
212
213 void SetClipboardText(const std::string& text) {
214 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
215 clipboard_writer.WriteText(base::ASCIIToUTF16(text));
216 }
217
218 private:
219 scoped_ptr<InterstitialPageImpl> interstitial_;
220 ui::TestClipboard* const clipboard_;
221 InterstitialTitleUpdateWatcher* title_update_watcher_;
222
223 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest);
224 };
225
226 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) {
227 SetUpInterstitialPage();
228
229 ASSERT_TRUE(SetInputText("text-to-cut"));
230 ASSERT_TRUE(FocusInputAndSelectText());
231
232 PerformCut();
233
234 std::string input_text;
235 ASSERT_TRUE(GetInputText(&input_text));
236 EXPECT_EQ(std::string(), input_text);
237 EXPECT_EQ("text-to-cut", GetClipboardText());
238
239 TearDownInterstitialPage();
240 }
241
242 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Copy) {
243 SetUpInterstitialPage();
244
245 ASSERT_TRUE(SetInputText("text-to-copy"));
246 ASSERT_TRUE(FocusInputAndSelectText());
247
248 PerformCopy();
249
250 std::string input_text;
251 ASSERT_TRUE(GetInputText(&input_text));
252 EXPECT_EQ("text-to-copy", input_text);
253 EXPECT_EQ("text-to-copy", GetClipboardText());
254
255 TearDownInterstitialPage();
256 }
257
258 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) {
259 SetUpInterstitialPage();
260
261 SetClipboardText("text-to-paste");
262
263 ASSERT_TRUE(SetInputText(""));
dcheng 2015/06/29 22:02:56 Nit: std::string()
mohsen 2015/06/29 22:56:29 Done.
264 ASSERT_TRUE(FocusInputAndSelectText());
265
266 PerformPaste();
267
268 std::string input_text;
269 ASSERT_TRUE(GetInputText(&input_text));
270 EXPECT_EQ("text-to-paste", input_text);
271
272 TearDownInterstitialPage();
273 }
274
275 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698