Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / extension_dom_clipboard_apitest.cc
blobf48ce692b6536f176f5e584fd2ae707622b973dd
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"
14 #include "url/gurl.h"
16 namespace {
18 class ClipboardApiTest : public ExtensionApiTest {
19 public:
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);
26 private:
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.";
36 return false;
39 if (!LoadExtension(test_data_dir_.AppendASCII("clipboard")
40 .AppendASCII(app_name))) {
41 message_ = "Failed to load hosted app.";
42 return false;
45 GURL base_url = embedded_test_server()->GetURL(
46 "/extensions/api_test/clipboard/");
47 GURL::Replacements replace_host;
48 replace_host.SetHostStr("localhost");
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));
55 return true;
58 bool ClipboardApiTest::ExecuteCopyInSelectedTab() {
59 const char kScript[] =
60 "window.domAutomationController.send(document.execCommand('copy'))";
61 return ExecuteScriptInSelectedTab(kScript);
64 bool ClipboardApiTest::ExecutePasteInSelectedTab() {
65 const char kScript[] =
66 "window.domAutomationController.send(document.execCommand('paste'))";
67 return ExecuteScriptInSelectedTab(kScript);
70 bool ClipboardApiTest::ExecuteCommandInIframeInSelectedTab(
71 const char* command) {
72 const char kScript[] =
73 "var ifr = document.createElement('iframe');\n"
74 "document.body.appendChild(ifr);\n"
75 "ifr.contentDocument.write('<script>parent.domAutomationController.send("
76 "document.execCommand(\"%s\"))</script>');";
77 return ExecuteScriptInSelectedTab(base::StringPrintf(kScript, command));
80 bool ClipboardApiTest::ExecuteScriptInSelectedTab(const std::string& script) {
81 bool result;
82 CHECK(content::ExecuteScriptAndExtractBool(
83 browser()->tab_strip_model()->GetActiveWebContents(),
84 script,
85 &result));
86 return result;
89 } // namespace
91 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, Extension) {
92 ASSERT_TRUE(StartEmbeddedTestServer());
93 ASSERT_TRUE(RunExtensionTest("clipboard/extension")) << message_;
96 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, ExtensionNoPermission) {
97 ASSERT_TRUE(StartEmbeddedTestServer());
98 ASSERT_TRUE(RunExtensionTest("clipboard/extension_no_permission"))
99 << message_;
102 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedApp) {
103 ASSERT_TRUE(LoadHostedApp("hosted_app", "main.html")) << message_;
105 EXPECT_TRUE(ExecuteCopyInSelectedTab()) << message_;
106 EXPECT_TRUE(ExecutePasteInSelectedTab()) << message_;
107 EXPECT_TRUE(ExecuteCommandInIframeInSelectedTab("copy")) << message_;
108 EXPECT_TRUE(ExecuteCommandInIframeInSelectedTab("paste")) << message_;
111 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedAppNoPermission) {
112 ASSERT_TRUE(LoadHostedApp("hosted_app_no_permission", "main.html"))
113 << message_;
115 // TODO(dcheng): The test coverage here is incomplete. The content test utils
116 // for executing script force a user gesture, so it's impossible to test
117 // the no user gesture case without a lot of code duplication.
118 EXPECT_TRUE(ExecuteCopyInSelectedTab()) << message_;
119 EXPECT_FALSE(ExecutePasteInSelectedTab()) << message_;
120 EXPECT_TRUE(ExecuteCommandInIframeInSelectedTab("copy")) << message_;
121 EXPECT_FALSE(ExecuteCommandInIframeInSelectedTab("paste")) << message_;