1 // Copyright (c) 2013 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.
5 #include "base/utf_string_conversions.h"
6 #include "chrome/browser/profiles/profile.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/common/chrome_notification_types.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "chrome/test/base/interactive_test_utils.h"
12 #include "content/public/test/test_utils.h"
13 #include "ui/base/clipboard/scoped_clipboard_writer.h"
14 #include "ui/base/events/event_constants.h"
16 class IncognitoSelectionClipboardTest
: public InProcessBrowserTest
{
18 static ui::Clipboard
* clipboard() {
19 static ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
23 static void ExpectStringInClipboard(const string16
& pattern
,
24 ui::Clipboard::Buffer buffer
) {
26 clipboard()->ReadText(buffer
, &content
);
27 EXPECT_EQ(pattern
, content
);
30 void CloseBrowser(Browser
* browser
) {
31 content::WindowedNotificationObserver
signal(
32 chrome::NOTIFICATION_BROWSER_CLOSED
,
33 content::Source
<Browser
>(browser
));
34 chrome::CloseWindow(browser
);
39 static void SendKeyForBrowser(const Browser
* browser
,
42 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
44 (modifiers
& ui::EF_CONTROL_DOWN
) != 0,
45 (modifiers
& ui::EF_SHIFT_DOWN
) != 0,
46 (modifiers
& ui::EF_ALT_DOWN
) != 0,
47 (modifiers
& ui::EF_COMMAND_DOWN
) != 0));
50 static void WriteClipboardCallback(ui::Clipboard::Buffer expected
,
51 const base::Closure
& closure
,
52 ui::Clipboard::Buffer actual
) {
53 if (expected
== actual
)
58 // Tests that data selected in content area of incognito window is destroyed in
59 // the selection clipboard after profile destruction.
60 // The test is executed on Linux only because it's the only OS with the
61 // selection clipboard.
62 #if defined(USE_X11) && !defined(OS_CHROMEOS)
63 IN_PROC_BROWSER_TEST_F(IncognitoSelectionClipboardTest
,
64 ClearContentDataOnSelect
) {
65 EXPECT_FALSE(browser()->profile()->IsOffTheRecord());
66 Browser
* browser_incognito
= CreateIncognitoBrowser();
67 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser_incognito
));
68 ui_test_utils::NavigateToURL(browser_incognito
, GURL("data:text/plain,foo"));
69 ui_test_utils::ClickOnView(browser_incognito
, VIEW_ID_TAB_CONTAINER
);
70 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser_incognito
,
71 VIEW_ID_TAB_CONTAINER
));
72 // Select web-page content.
73 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(browser_incognito
,
75 ui::EF_CONTROL_DOWN
));
76 // Run message loop until data have been written to the selection clipboard.
77 // This happens on UI thread.
78 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner(
79 new content::MessageLoopRunner
);
80 clipboard()->set_write_objects_callback_for_testing(
81 base::Bind(&WriteClipboardCallback
,
82 ui::Clipboard::BUFFER_SELECTION
,
83 message_loop_runner
->QuitClosure()));
84 message_loop_runner
->Run();
85 ExpectStringInClipboard(ASCIIToUTF16("foo"), ui::Clipboard::BUFFER_SELECTION
);
87 CloseBrowser(browser_incognito
);
88 ExpectStringInClipboard(string16(), ui::Clipboard::BUFFER_SELECTION
);
92 // Tests that data copied in content area of incognito window is destroyed in
93 // the selection clipboard after profile destruction.
94 // The test is executed on Linux only because it's the only OS with the
95 // selection clipboard.
96 #if defined(USE_X11) && !defined(OS_CHROMEOS)
97 IN_PROC_BROWSER_TEST_F(IncognitoSelectionClipboardTest
,
98 ClearContentDataOnCopy
) {
99 EXPECT_FALSE(browser()->profile()->IsOffTheRecord());
100 Browser
* browser_incognito
= CreateIncognitoBrowser();
101 ui_test_utils::NavigateToURL(browser_incognito
, GURL("data:text/plain,foo"));
102 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser_incognito
));
103 // Select and copy web-page content.
104 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(browser_incognito
,
106 ui::EF_CONTROL_DOWN
));
107 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(browser_incognito
,
109 ui::EF_CONTROL_DOWN
));
110 // Run message loop until data have been written to the primary clipboard and
111 // automatically propagated to the selection clipboard by ui::Clipboard.
112 // This happens on UI thread.
113 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner(
114 new content::MessageLoopRunner
);
115 clipboard()->set_write_objects_callback_for_testing(
116 base::Bind(&WriteClipboardCallback
,
117 ui::Clipboard::BUFFER_STANDARD
,
118 message_loop_runner
->QuitClosure()));
119 message_loop_runner
->Run();
120 ExpectStringInClipboard(ASCIIToUTF16("foo"), ui::Clipboard::BUFFER_SELECTION
);
121 ExpectStringInClipboard(ASCIIToUTF16("foo"), ui::Clipboard::BUFFER_STANDARD
);
123 CloseBrowser(browser_incognito
);
124 ExpectStringInClipboard(string16(), ui::Clipboard::BUFFER_SELECTION
);
125 ExpectStringInClipboard(string16(), ui::Clipboard::BUFFER_STANDARD
);