| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/stringprintf.h" |
| 5 #include "chrome/browser/extensions/extension_apitest.h" | 6 #include "chrome/browser/extensions/extension_apitest.h" |
| 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/test/ui_test_utils.h" |
| 9 #include "content/browser/tab_contents/tab_contents.h" |
| 10 #include "googleurl/src/gurl.h" |
| 11 #include "net/base/mock_host_resolver.h" |
| 6 | 12 |
| 7 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Clipboard) { | 13 namespace { |
| 14 |
| 15 class ClipboardApiTest : public ExtensionApiTest { |
| 16 public: |
| 17 bool LoadHostedApp(const std::string& app_name, |
| 18 const std::string& launch_page); |
| 19 bool ExecuteCopyInSelectedTab(bool* result); |
| 20 bool ExecutePasteInSelectedTab(bool* result); |
| 21 |
| 22 private: |
| 23 bool ExecuteScriptInSelectedTab(const std::wstring& script, bool* result); |
| 24 }; |
| 25 |
| 26 bool ClipboardApiTest::LoadHostedApp(const std::string& app_name, |
| 27 const std::string& launch_page) { |
| 28 host_resolver()->AddRule("*", "127.0.0.1"); |
| 29 |
| 30 if (!StartTestServer()) { |
| 31 message_ = "Failed to start test server."; |
| 32 return false; |
| 33 } |
| 34 |
| 35 if (!LoadExtension(test_data_dir_.AppendASCII("clipboard") |
| 36 .AppendASCII(app_name))) { |
| 37 message_ = "Failed to load hosted app."; |
| 38 return false; |
| 39 } |
| 40 |
| 41 GURL base_url = test_server()->GetURL("files/extensions/api_test/clipboard/"); |
| 42 GURL::Replacements replace_host; |
| 43 std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 44 replace_host.SetHostStr(host_str); |
| 45 base_url = base_url.ReplaceComponents(replace_host); |
| 46 |
| 47 std::string launch_page_path = |
| 48 base::StringPrintf("%s/%s", app_name.c_str(), launch_page.c_str()); |
| 49 ui_test_utils::NavigateToURL(browser(), base_url.Resolve(launch_page_path)); |
| 50 |
| 51 return true; |
| 52 } |
| 53 |
| 54 bool ClipboardApiTest::ExecuteCopyInSelectedTab(bool* result) { |
| 55 const wchar_t kScript[] = |
| 56 L"window.domAutomationController.send(document.execCommand('copy'))"; |
| 57 return ExecuteScriptInSelectedTab(kScript, result); |
| 58 } |
| 59 |
| 60 bool ClipboardApiTest::ExecutePasteInSelectedTab(bool* result) { |
| 61 const wchar_t kScript[] = |
| 62 L"window.domAutomationController.send(document.execCommand('paste'))"; |
| 63 return ExecuteScriptInSelectedTab(kScript, result); |
| 64 } |
| 65 |
| 66 bool ClipboardApiTest::ExecuteScriptInSelectedTab(const std::wstring& script, |
| 67 bool* result) { |
| 68 if (!ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 69 browser()->GetSelectedTabContents()->render_view_host(), |
| 70 L"", |
| 71 script, |
| 72 result)) { |
| 73 message_ = "Failed to execute script in selected tab."; |
| 74 return false; |
| 75 } |
| 76 return true; |
| 77 } |
| 78 |
| 79 } // namespace |
| 80 |
| 81 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, Extension) { |
| 8 ASSERT_TRUE(StartTestServer()); | 82 ASSERT_TRUE(StartTestServer()); |
| 9 ASSERT_TRUE(RunExtensionTest("clipboard")) << message_; | 83 ASSERT_TRUE(RunExtensionTest("clipboard/extension")) << message_; |
| 10 } | 84 } |
| 85 |
| 86 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, ExtensionNoPermission) { |
| 87 ASSERT_TRUE(StartTestServer()); |
| 88 ASSERT_TRUE(RunExtensionTest("clipboard/extension_no_permission")) |
| 89 << message_; |
| 90 } |
| 91 |
| 92 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedApp) { |
| 93 ASSERT_TRUE(LoadHostedApp("hosted_app", "main.html")) << message_; |
| 94 |
| 95 bool result = false; |
| 96 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; |
| 97 EXPECT_TRUE(result); |
| 98 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; |
| 99 EXPECT_TRUE(result); |
| 100 } |
| 101 |
| 102 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedAppNoPermission) { |
| 103 ASSERT_TRUE(LoadHostedApp("hosted_app_no_permission", "main.html")) |
| 104 << message_; |
| 105 |
| 106 bool result = false; |
| 107 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; |
| 108 EXPECT_FALSE(result); |
| 109 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; |
| 110 EXPECT_FALSE(result); |
| 111 } |
| 112 |
| OLD | NEW |