1 // Copyright (c) 2012 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/strings/stringprintf.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/test/base/ui_test_utils.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "net/test/embedded_test_server/embedded_test_server.h"
18 class ClipboardApiTest
: public ExtensionApiTest
{
20 bool LoadHostedApp(const std::string
& app_name
,
21 const std::string
& launch_page
);
22 bool ExecuteCopyInSelectedTab();
23 bool ExecutePasteInSelectedTab();
24 bool ExecuteCommandInIframeInSelectedTab(const char* command
);
27 bool ExecuteScriptInSelectedTab(const std::string
& script
);
30 bool ClipboardApiTest::LoadHostedApp(const std::string
& app_name
,
31 const std::string
& launch_page
) {
32 host_resolver()->AddRule("*", "127.0.0.1");
34 if (!StartEmbeddedTestServer()) {
35 message_
= "Failed to start test server.";
39 if (!LoadExtension(test_data_dir_
.AppendASCII("clipboard")
40 .AppendASCII(app_name
))) {
41 message_
= "Failed to load hosted app.";
45 GURL base_url
= embedded_test_server()->GetURL(
46 "/extensions/api_test/clipboard/");
47 GURL::Replacements replace_host
;
48 std::string
host_str("localhost"); // Must stay in scope with replace_host.
49 replace_host
.SetHostStr(host_str
);
50 base_url
= base_url
.ReplaceComponents(replace_host
);
52 std::string launch_page_path
=
53 base::StringPrintf("%s/%s", app_name
.c_str(), launch_page
.c_str());
54 ui_test_utils::NavigateToURL(browser(), base_url
.Resolve(launch_page_path
));
59 bool ClipboardApiTest::ExecuteCopyInSelectedTab() {
60 const char kScript
[] =
61 "window.domAutomationController.send(document.execCommand('copy'))";
62 return ExecuteScriptInSelectedTab(kScript
);
65 bool ClipboardApiTest::ExecutePasteInSelectedTab() {
66 const char kScript
[] =
67 "window.domAutomationController.send(document.execCommand('paste'))";
68 return ExecuteScriptInSelectedTab(kScript
);
71 bool ClipboardApiTest::ExecuteCommandInIframeInSelectedTab(
72 const char* command
) {
73 const char kScript
[] =
74 "var ifr = document.createElement('iframe');\n"
75 "document.body.appendChild(ifr);\n"
76 "ifr.contentDocument.write('<script>parent.domAutomationController.send("
77 "document.execCommand(\"%s\"))</script>');";
78 return ExecuteScriptInSelectedTab(base::StringPrintf(kScript
, command
));
81 bool ClipboardApiTest::ExecuteScriptInSelectedTab(const std::string
& script
) {
83 CHECK(content::ExecuteScriptAndExtractBool(
84 browser()->tab_strip_model()->GetActiveWebContents(),
92 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, Extension
) {
93 ASSERT_TRUE(StartEmbeddedTestServer());
94 ASSERT_TRUE(RunExtensionTest("clipboard/extension")) << message_
;
97 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, ExtensionNoPermission
) {
98 ASSERT_TRUE(StartEmbeddedTestServer());
99 ASSERT_TRUE(RunExtensionTest("clipboard/extension_no_permission"))
103 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, HostedApp
) {
104 ASSERT_TRUE(LoadHostedApp("hosted_app", "main.html")) << message_
;
106 EXPECT_TRUE(ExecuteCopyInSelectedTab()) << message_
;
107 EXPECT_TRUE(ExecutePasteInSelectedTab()) << message_
;
108 EXPECT_TRUE(ExecuteCommandInIframeInSelectedTab("copy")) << message_
;
109 EXPECT_TRUE(ExecuteCommandInIframeInSelectedTab("paste")) << message_
;
112 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, HostedAppNoPermission
) {
113 ASSERT_TRUE(LoadHostedApp("hosted_app_no_permission", "main.html"))
116 EXPECT_FALSE(ExecuteCopyInSelectedTab()) << message_
;
117 EXPECT_FALSE(ExecutePasteInSelectedTab()) << message_
;
118 EXPECT_FALSE(ExecuteCommandInIframeInSelectedTab("copy")) << message_
;
119 EXPECT_FALSE(ExecuteCommandInIframeInSelectedTab("paste")) << message_
;