| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/strings/stringprintf.h" | 5 #include "base/strings/stringprintf.h" |
| 6 #include "chrome/browser/extensions/extension_apitest.h" | 6 #include "chrome/browser/extensions/extension_apitest.h" |
| 7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 9 #include "chrome/test/base/ui_test_utils.h" | 9 #include "chrome/test/base/ui_test_utils.h" |
| 10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/test/browser_test_utils.h" | 11 #include "content/public/test/browser_test_utils.h" |
| 12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 13 #include "net/dns/mock_host_resolver.h" | 13 #include "net/dns/mock_host_resolver.h" |
| 14 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 class ClipboardApiTest : public ExtensionApiTest { | 18 class ClipboardApiTest : public ExtensionApiTest { |
| 18 public: | 19 public: |
| 19 bool LoadHostedApp(const std::string& app_name, | 20 bool LoadHostedApp(const std::string& app_name, |
| 20 const std::string& launch_page); | 21 const std::string& launch_page); |
| 21 bool ExecuteCopyInSelectedTab(bool* result); | 22 bool ExecuteCopyInSelectedTab(bool* result); |
| 22 bool ExecutePasteInSelectedTab(bool* result); | 23 bool ExecutePasteInSelectedTab(bool* result); |
| 23 | 24 |
| 24 private: | 25 private: |
| 25 bool ExecuteScriptInSelectedTab(const std::string& script, bool* result); | 26 bool ExecuteScriptInSelectedTab(const std::string& script, bool* result); |
| 26 }; | 27 }; |
| 27 | 28 |
| 28 bool ClipboardApiTest::LoadHostedApp(const std::string& app_name, | 29 bool ClipboardApiTest::LoadHostedApp(const std::string& app_name, |
| 29 const std::string& launch_page) { | 30 const std::string& launch_page) { |
| 30 host_resolver()->AddRule("*", "127.0.0.1"); | 31 host_resolver()->AddRule("*", "127.0.0.1"); |
| 31 | 32 |
| 32 if (!StartTestServer()) { | 33 if (!StartEmbeddedTestServer()) { |
| 33 message_ = "Failed to start test server."; | 34 message_ = "Failed to start test server."; |
| 34 return false; | 35 return false; |
| 35 } | 36 } |
| 36 | 37 |
| 37 if (!LoadExtension(test_data_dir_.AppendASCII("clipboard") | 38 if (!LoadExtension(test_data_dir_.AppendASCII("clipboard") |
| 38 .AppendASCII(app_name))) { | 39 .AppendASCII(app_name))) { |
| 39 message_ = "Failed to load hosted app."; | 40 message_ = "Failed to load hosted app."; |
| 40 return false; | 41 return false; |
| 41 } | 42 } |
| 42 | 43 |
| 43 GURL base_url = test_server()->GetURL("files/extensions/api_test/clipboard/"); | 44 GURL base_url = embedded_test_server()->GetURL( |
| 45 "/extensions/api_test/clipboard/"); |
| 44 GURL::Replacements replace_host; | 46 GURL::Replacements replace_host; |
| 45 std::string host_str("localhost"); // Must stay in scope with replace_host. | 47 std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 46 replace_host.SetHostStr(host_str); | 48 replace_host.SetHostStr(host_str); |
| 47 base_url = base_url.ReplaceComponents(replace_host); | 49 base_url = base_url.ReplaceComponents(replace_host); |
| 48 | 50 |
| 49 std::string launch_page_path = | 51 std::string launch_page_path = |
| 50 base::StringPrintf("%s/%s", app_name.c_str(), launch_page.c_str()); | 52 base::StringPrintf("%s/%s", app_name.c_str(), launch_page.c_str()); |
| 51 ui_test_utils::NavigateToURL(browser(), base_url.Resolve(launch_page_path)); | 53 ui_test_utils::NavigateToURL(browser(), base_url.Resolve(launch_page_path)); |
| 52 | 54 |
| 53 return true; | 55 return true; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 73 result)) { | 75 result)) { |
| 74 message_ = "Failed to execute script in selected tab."; | 76 message_ = "Failed to execute script in selected tab."; |
| 75 return false; | 77 return false; |
| 76 } | 78 } |
| 77 return true; | 79 return true; |
| 78 } | 80 } |
| 79 | 81 |
| 80 } // namespace | 82 } // namespace |
| 81 | 83 |
| 82 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, Extension) { | 84 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, Extension) { |
| 83 ASSERT_TRUE(StartTestServer()); | 85 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 84 ASSERT_TRUE(RunExtensionTest("clipboard/extension")) << message_; | 86 ASSERT_TRUE(RunExtensionTest("clipboard/extension")) << message_; |
| 85 } | 87 } |
| 86 | 88 |
| 87 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, ExtensionNoPermission) { | 89 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, ExtensionNoPermission) { |
| 88 ASSERT_TRUE(StartTestServer()); | 90 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 89 ASSERT_TRUE(RunExtensionTest("clipboard/extension_no_permission")) | 91 ASSERT_TRUE(RunExtensionTest("clipboard/extension_no_permission")) |
| 90 << message_; | 92 << message_; |
| 91 } | 93 } |
| 92 | 94 |
| 93 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedApp) { | 95 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedApp) { |
| 94 ASSERT_TRUE(LoadHostedApp("hosted_app", "main.html")) << message_; | 96 ASSERT_TRUE(LoadHostedApp("hosted_app", "main.html")) << message_; |
| 95 | 97 |
| 96 bool result = false; | 98 bool result = false; |
| 97 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; | 99 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; |
| 98 EXPECT_TRUE(result); | 100 EXPECT_TRUE(result); |
| 99 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; | 101 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; |
| 100 EXPECT_TRUE(result); | 102 EXPECT_TRUE(result); |
| 101 } | 103 } |
| 102 | 104 |
| 103 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedAppNoPermission) { | 105 IN_PROC_BROWSER_TEST_F(ClipboardApiTest, HostedAppNoPermission) { |
| 104 ASSERT_TRUE(LoadHostedApp("hosted_app_no_permission", "main.html")) | 106 ASSERT_TRUE(LoadHostedApp("hosted_app_no_permission", "main.html")) |
| 105 << message_; | 107 << message_; |
| 106 | 108 |
| 107 bool result = false; | 109 bool result = false; |
| 108 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; | 110 ASSERT_TRUE(ExecuteCopyInSelectedTab(&result)) << message_; |
| 109 EXPECT_FALSE(result); | 111 EXPECT_FALSE(result); |
| 110 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; | 112 ASSERT_TRUE(ExecutePasteInSelectedTab(&result)) << message_; |
| 111 EXPECT_FALSE(result); | 113 EXPECT_FALSE(result); |
| 112 } | 114 } |
| 113 | 115 |
| OLD | NEW |