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(bool* result
);
23 bool ExecutePasteInSelectedTab(bool* result
);
26 bool ExecuteScriptInSelectedTab(const std::string
& script
, bool* result
);
29 bool ClipboardApiTest::LoadHostedApp(const std::string
& app_name
,
30 const std::string
& launch_page
) {
31 host_resolver()->AddRule("*", "127.0.0.1");
33 if (!StartEmbeddedTestServer()) {
34 message_
= "Failed to start test server.";
38 if (!LoadExtension(test_data_dir_
.AppendASCII("clipboard")
39 .AppendASCII(app_name
))) {
40 message_
= "Failed to load hosted app.";
44 GURL base_url
= embedded_test_server()->GetURL(
45 "/extensions/api_test/clipboard/");
46 GURL::Replacements replace_host
;
47 std::string
host_str("localhost"); // Must stay in scope with replace_host.
48 replace_host
.SetHostStr(host_str
);
49 base_url
= base_url
.ReplaceComponents(replace_host
);
51 std::string launch_page_path
=
52 base::StringPrintf("%s/%s", app_name
.c_str(), launch_page
.c_str());
53 ui_test_utils::NavigateToURL(browser(), base_url
.Resolve(launch_page_path
));
58 bool ClipboardApiTest::ExecuteCopyInSelectedTab(bool* result
) {
59 const char kScript
[] =
60 "window.domAutomationController.send(document.execCommand('copy'))";
61 return ExecuteScriptInSelectedTab(kScript
, result
);
64 bool ClipboardApiTest::ExecutePasteInSelectedTab(bool* result
) {
65 const char kScript
[] =
66 "window.domAutomationController.send(document.execCommand('paste'))";
67 return ExecuteScriptInSelectedTab(kScript
, result
);
70 bool ClipboardApiTest::ExecuteScriptInSelectedTab(const std::string
& script
,
72 if (!content::ExecuteScriptAndExtractBool(
73 browser()->tab_strip_model()->GetActiveWebContents(),
76 message_
= "Failed to execute script in selected tab.";
84 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, Extension
) {
85 ASSERT_TRUE(StartEmbeddedTestServer());
86 ASSERT_TRUE(RunExtensionTest("clipboard/extension")) << message_
;
89 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, ExtensionNoPermission
) {
90 ASSERT_TRUE(StartEmbeddedTestServer());
91 ASSERT_TRUE(RunExtensionTest("clipboard/extension_no_permission"))
95 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, HostedApp
) {
96 ASSERT_TRUE(LoadHostedApp("hosted_app", "main.html")) << message_
;
99 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result
)) << message_
;
101 ASSERT_TRUE(ExecutePasteInSelectedTab(&result
)) << message_
;
105 IN_PROC_BROWSER_TEST_F(ClipboardApiTest
, HostedAppNoPermission
) {
106 ASSERT_TRUE(LoadHostedApp("hosted_app_no_permission", "main.html"))
110 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result
)) << message_
;
111 EXPECT_FALSE(result
);
112 ASSERT_TRUE(ExecutePasteInSelectedTab(&result
)) << message_
;
113 EXPECT_FALSE(result
);