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 <tuple> | 7 #include <tuple> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "base/synchronization/waitable_event.h" | |
12 #include "build/build_config.h" | |
13 #include "content/browser/web_contents/web_contents_impl.h" | 11 #include "content/browser/web_contents/web_contents_impl.h" |
14 #include "content/common/clipboard_messages.h" | 12 #include "content/common/clipboard_messages.h" |
15 #include "content/common/frame_messages.h" | 13 #include "content/common/frame_messages.h" |
16 #include "content/public/browser/browser_message_filter.h" | 14 #include "content/public/browser/browser_message_filter.h" |
| 15 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/interstitial_page_delegate.h" | 16 #include "content/public/browser/interstitial_page_delegate.h" |
18 #include "content/public/test/browser_test_utils.h" | 17 #include "content/public/test/browser_test_utils.h" |
19 #include "content/public/test/content_browser_test.h" | 18 #include "content/public/test/content_browser_test.h" |
20 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
21 #include "content/shell/browser/shell.h" | 20 #include "content/shell/browser/shell.h" |
22 #include "ipc/message_filter.h" | 21 #include "ipc/message_filter.h" |
23 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
24 #include "ui/base/test/test_clipboard.h" | |
25 | 22 |
26 namespace content { | 23 namespace content { |
27 | 24 |
28 namespace { | 25 namespace { |
29 | 26 |
30 class TestInterstitialPageDelegate : public InterstitialPageDelegate { | 27 class TestInterstitialPageDelegate : public InterstitialPageDelegate { |
31 private: | 28 private: |
32 // InterstitialPageDelegate: | 29 // InterstitialPageDelegate: |
33 std::string GetHTMLContents() override { | 30 std::string GetHTMLContents() override { |
34 return "<html>" | 31 return "<html>" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 125 |
129 } // namespace | 126 } // namespace |
130 | 127 |
131 class InterstitialPageImplTest : public ContentBrowserTest { | 128 class InterstitialPageImplTest : public ContentBrowserTest { |
132 public: | 129 public: |
133 InterstitialPageImplTest() {} | 130 InterstitialPageImplTest() {} |
134 | 131 |
135 ~InterstitialPageImplTest() override {} | 132 ~InterstitialPageImplTest() override {} |
136 | 133 |
137 protected: | 134 protected: |
138 void SetUpTestClipboard() { | |
139 #if defined(OS_WIN) | |
140 // On Windows, clipboard reads are handled on the IO thread. So, the test | |
141 // clipboard should be created for the IO thread. | |
142 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
143 RunTaskOnIOThreadAndWait( | |
144 base::Bind(&InterstitialPageImplTest::SetUpTestClipboard, this)); | |
145 return; | |
146 } | |
147 #endif | |
148 ui::TestClipboard::CreateForCurrentThread(); | |
149 } | |
150 | |
151 void TearDownTestClipboard() { | |
152 #if defined(OS_WIN) | |
153 // On Windows, test clipboard is created for the IO thread. So, destroy it | |
154 // for the IO thread, too. | |
155 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
156 RunTaskOnIOThreadAndWait( | |
157 base::Bind(&InterstitialPageImplTest::TearDownTestClipboard, this)); | |
158 return; | |
159 } | |
160 #endif | |
161 ui::Clipboard::DestroyClipboardForCurrentThread(); | |
162 } | |
163 | |
164 void SetClipboardText(const std::string& text) { | |
165 #if defined(OS_WIN) | |
166 // On Windows, clipboard reads are handled on the IO thread. So, set the | |
167 // text for the IO thread clipboard. | |
168 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
169 RunTaskOnIOThreadAndWait( | |
170 base::Bind(&InterstitialPageImplTest::SetClipboardText, this, text)); | |
171 return; | |
172 } | |
173 #endif | |
174 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
175 clipboard_writer.WriteText(base::ASCIIToUTF16(text)); | |
176 } | |
177 | |
178 void SetUpInterstitialPage() { | 135 void SetUpInterstitialPage() { |
179 WebContentsImpl* web_contents = | 136 WebContentsImpl* web_contents = |
180 static_cast<WebContentsImpl*>(shell()->web_contents()); | 137 static_cast<WebContentsImpl*>(shell()->web_contents()); |
181 | 138 |
182 // Create the interstitial page. | 139 // Create the interstitial page. |
183 TestInterstitialPageDelegate* interstitial_delegate = | 140 TestInterstitialPageDelegate* interstitial_delegate = |
184 new TestInterstitialPageDelegate; | 141 new TestInterstitialPageDelegate; |
185 GURL url("http://interstitial"); | 142 GURL url("http://interstitial"); |
186 interstitial_.reset(new InterstitialPageImpl( | 143 interstitial_.reset(new InterstitialPageImpl( |
187 web_contents, static_cast<RenderWidgetHostDelegate*>(web_contents), | 144 web_contents, static_cast<RenderWidgetHostDelegate*>(web_contents), |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 base::UTF8ToUTF16("SELECTION_CHANGED"); | 227 base::UTF8ToUTF16("SELECTION_CHANGED"); |
271 content::TitleWatcher title_watcher(shell()->web_contents(), | 228 content::TitleWatcher title_watcher(shell()->web_contents(), |
272 expected_title); | 229 expected_title); |
273 RenderFrameHostImpl* rfh = | 230 RenderFrameHostImpl* rfh = |
274 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | 231 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
275 rfh->GetRenderWidgetHost()->delegate()->SelectAll(); | 232 rfh->GetRenderWidgetHost()->delegate()->SelectAll(); |
276 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); | 233 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
277 } | 234 } |
278 | 235 |
279 private: | 236 private: |
280 void RunTaskOnIOThreadAndWait(const base::Closure& task) { | |
281 base::WaitableEvent completion( | |
282 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
283 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
284 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
285 base::Bind(&InterstitialPageImplTest::RunTask, this, | |
286 task, &completion)); | |
287 completion.Wait(); | |
288 } | |
289 | |
290 void RunTask(const base::Closure& task, base::WaitableEvent* completion) { | |
291 task.Run(); | |
292 completion->Signal(); | |
293 } | |
294 | |
295 std::unique_ptr<InterstitialPageImpl> interstitial_; | 237 std::unique_ptr<InterstitialPageImpl> interstitial_; |
296 scoped_refptr<ClipboardMessageWatcher> clipboard_message_watcher_; | 238 scoped_refptr<ClipboardMessageWatcher> clipboard_message_watcher_; |
297 | 239 |
298 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); | 240 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); |
299 }; | 241 }; |
300 | 242 |
301 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { | 243 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { |
302 SetUpInterstitialPage(); | 244 SetUpInterstitialPage(); |
303 | 245 |
304 ASSERT_TRUE(CreateInputAndSetText("text-to-cut")); | 246 ASSERT_TRUE(CreateInputAndSetText("text-to-cut")); |
(...skipping 19 matching lines...) Expand all Loading... |
324 EXPECT_EQ("text-to-copy", clipboard_text); | 266 EXPECT_EQ("text-to-copy", clipboard_text); |
325 | 267 |
326 std::string input_text; | 268 std::string input_text; |
327 ASSERT_TRUE(GetInputText(&input_text)); | 269 ASSERT_TRUE(GetInputText(&input_text)); |
328 EXPECT_EQ("text-to-copy", input_text); | 270 EXPECT_EQ("text-to-copy", input_text); |
329 | 271 |
330 TearDownInterstitialPage(); | 272 TearDownInterstitialPage(); |
331 } | 273 } |
332 | 274 |
333 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) { | 275 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Paste) { |
334 SetUpTestClipboard(); | 276 BrowserTestClipboardScope clipboard; |
335 SetUpInterstitialPage(); | 277 SetUpInterstitialPage(); |
336 | 278 |
337 SetClipboardText("text-to-paste"); | 279 clipboard.SetText("text-to-paste"); |
338 | 280 |
339 ASSERT_TRUE(CreateInputAndSetText(std::string())); | 281 ASSERT_TRUE(CreateInputAndSetText(std::string())); |
340 ASSERT_TRUE(FocusInputAndSelectText()); | 282 ASSERT_TRUE(FocusInputAndSelectText()); |
341 | 283 |
342 PerformPaste(); | 284 PerformPaste(); |
343 | 285 |
344 std::string input_text; | 286 std::string input_text; |
345 ASSERT_TRUE(GetInputText(&input_text)); | 287 ASSERT_TRUE(GetInputText(&input_text)); |
346 EXPECT_EQ("text-to-paste", input_text); | 288 EXPECT_EQ("text-to-paste", input_text); |
347 | 289 |
348 TearDownInterstitialPage(); | 290 TearDownInterstitialPage(); |
349 TearDownTestClipboard(); | |
350 } | 291 } |
351 | 292 |
352 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, SelectAll) { | 293 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, SelectAll) { |
353 SetUpInterstitialPage(); | 294 SetUpInterstitialPage(); |
354 ASSERT_TRUE(SetSelectionChangeListener()); | 295 ASSERT_TRUE(SetSelectionChangeListener()); |
355 | 296 |
356 std::string input_text; | 297 std::string input_text; |
357 ASSERT_TRUE(GetSelection(&input_text)); | 298 ASSERT_TRUE(GetSelection(&input_text)); |
358 EXPECT_EQ(std::string(), input_text); | 299 EXPECT_EQ(std::string(), input_text); |
359 | 300 |
360 PerformSelectAll(); | 301 PerformSelectAll(); |
361 | 302 |
362 ASSERT_TRUE(GetSelection(&input_text)); | 303 ASSERT_TRUE(GetSelection(&input_text)); |
363 EXPECT_EQ("original body text", input_text); | 304 EXPECT_EQ("original body text", input_text); |
364 | 305 |
365 TearDownInterstitialPage(); | 306 TearDownInterstitialPage(); |
366 } | 307 } |
367 | 308 |
368 } // namespace content | 309 } // namespace content |
OLD | NEW |