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. 4 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" 15 16 namespace { 17 18 class ClipboardApiTest : public ExtensionApiTest { 19 public: 20 bool LoadHostedApp(const std::string& app_name, 21 const std::string& launch_page); 22 bool ExecuteCopyInSelectedTab(bool* result); 23 bool ExecutePasteInSelectedTab(bool* result); 24 25 private: 26 bool ExecuteScriptInSelectedTab(const std::string& script, bool* result); 27 }; 28 29 bool ClipboardApiTest::LoadHostedApp(const std::string& app_name, 30 const std::string& launch_page) { 31 host_resolver()->AddRule("*", "127.0.0.1"); 32 33 if (!StartEmbeddedTestServer()) { 34 message_ = "Failed to start test server."; 35 return false; 36 } 37 38 if (!LoadExtension(test_data_dir_.AppendASCII("clipboard") 39 .AppendASCII(app_name))) { 40 message_ = "Failed to load hosted app."; 41 return false; 42 } 43 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); 50 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)); 54 55 return true; 56 } 57 58 bool ClipboardApiTest::ExecuteCopyInSelectedTab(bool* result) { 59 const char kScript[] = 60 "window.domAutomationController.send(document.execCommand('copy'))"; 61 return ExecuteScriptInSelectedTab(kScript, result); 62 } 63 64 bool ClipboardApiTest::ExecutePasteInSelectedTab(bool* result) { 65 const char kScript[] = 66 "window.domAutomationController.send(document.execCommand('paste'))"; 67 return ExecuteScriptInSelectedTab(kScript, result); 68 } 69 70 bool ClipboardApiTest::ExecuteScriptInSelectedTab(const std::string& script, 71 bool* result) { 72 if (!content::ExecuteScriptAndExtractBool( 73 browser()->tab_strip_model()->GetActiveWebContents(), 74 script, 75 result)) { 76 message_ = "Failed to execute script in selected tab."; 77 return false; 78 } 79 return true; 80 } 81 82 } // namespace 83 84 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, Extension) { 85 ASSERT_TRUE(StartEmbeddedTestServer()); 86 ASSERT_TRUE(RunExtensionTest("clipboard/extension")) << message_; 87 } 88 89 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, ExtensionNoPermission) { 90 ASSERT_TRUE(StartEmbeddedTestServer()); 91 ASSERT_TRUE(RunExtensionTest("clipboard/extension_no_permission")) 92 << message_; 93 } 94 95 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedApp) { 96 ASSERT_TRUE(LoadHostedApp("hosted_app", "main.html")) << message_; 97 98 bool result = false; 99 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; 100 EXPECT_TRUE(result); 101 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; 102 EXPECT_TRUE(result); 103 } 104 105 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedAppNoPermission) { 106 ASSERT_TRUE(LoadHostedApp("hosted_app_no_permission", "main.html")) 107 << message_; 108 109 bool result = false; 110 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; 111 EXPECT_FALSE(result); 112 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; 113 EXPECT_FALSE(result); 114 } 115 116