[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 5 | #include "base/stringprintf.h" |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame^] | 6 | #include "base/utf_string_conversions.h" |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 7 | #include "chrome/browser/background_contents_service.h" |
[email protected] | d2fad14 | 2011-04-15 10:18:44 | [diff] [blame] | 8 | #include "chrome/browser/background_contents_service_factory.h" |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_apitest.h" |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_service.h" |
| 11 | #include "chrome/browser/profiles/profile.h" |
| 12 | #include "chrome/browser/ui/browser.h" |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 13 | #include "chrome/common/chrome_switches.h" |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 14 | #include "chrome/common/extensions/extension.h" |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 15 | #include "chrome/test/ui_test_utils.h" |
| 16 | #include "net/base/mock_host_resolver.h" |
| 17 | |
| 18 | class AppBackgroundPageApiTest : public ExtensionApiTest { |
| 19 | public: |
| 20 | void SetUpCommandLine(CommandLine* command_line) { |
| 21 | ExtensionApiTest::SetUpCommandLine(command_line); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 22 | command_line->AppendSwitch(switches::kDisablePopupBlocking); |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 23 | command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 24 | } |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 25 | |
| 26 | bool CreateApp(const std::string& app_manifest, |
| 27 | FilePath* app_dir) { |
| 28 | if (!app_dir_.CreateUniqueTempDir()) { |
| 29 | LOG(ERROR) << "Unable to create a temporary directory."; |
| 30 | return false; |
| 31 | } |
| 32 | FilePath manifest_path = app_dir_.path().AppendASCII("manifest.json"); |
| 33 | int bytes_written = file_util::WriteFile(manifest_path, |
| 34 | app_manifest.data(), |
| 35 | app_manifest.size()); |
| 36 | if (bytes_written != static_cast<int>(app_manifest.size())) { |
| 37 | LOG(ERROR) << "Unable to write complete manifest to file. Return code=" |
| 38 | << bytes_written; |
| 39 | return false; |
| 40 | } |
| 41 | *app_dir = app_dir_.path(); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | ScopedTempDir app_dir_; |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 47 | }; |
| 48 | |
[email protected] | 4dca317 | 2010-09-19 20:40:15 | [diff] [blame] | 49 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, Basic) { |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 50 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 51 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 52 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 53 | std::string app_manifest = base::StringPrintf( |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 54 | "{" |
| 55 | " \"name\": \"App\"," |
| 56 | " \"version\": \"0.1\"," |
| 57 | " \"app\": {" |
| 58 | " \"urls\": [" |
| 59 | " \"http://a.com/\"" |
| 60 | " ]," |
| 61 | " \"launch\": {" |
| 62 | " \"web_url\": \"http://a.com:%d/\"" |
| 63 | " }" |
| 64 | " }," |
| 65 | " \"permissions\": [\"background\"]" |
| 66 | "}", |
| 67 | test_server()->host_port_pair().port()); |
| 68 | |
| 69 | FilePath app_dir; |
| 70 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 71 | ASSERT_TRUE(LoadExtension(app_dir)); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 72 | ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; |
| 73 | } |
| 74 | |
[email protected] | 4b5e29f | 2011-01-11 07:44:08 | [diff] [blame] | 75 | // Crashy, http://crbug.com/49215. |
| 76 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 77 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 78 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 79 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 80 | std::string app_manifest = base::StringPrintf( |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 81 | "{" |
| 82 | " \"name\": \"App\"," |
| 83 | " \"version\": \"0.1\"," |
| 84 | " \"app\": {" |
| 85 | " \"urls\": [" |
| 86 | " \"http://a.com/\"" |
| 87 | " ]," |
| 88 | " \"launch\": {" |
| 89 | " \"web_url\": \"http://a.com:%d/\"" |
| 90 | " }" |
| 91 | " }" |
| 92 | "}", |
| 93 | test_server()->host_port_pair().port()); |
| 94 | |
| 95 | FilePath app_dir; |
| 96 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 97 | ASSERT_TRUE(LoadExtension(app_dir)); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 98 | ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) |
| 99 | << message_; |
| 100 | } |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 101 | |
| 102 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { |
| 103 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 104 | ASSERT_TRUE(StartTestServer()); |
| 105 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 106 | std::string app_manifest = base::StringPrintf( |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 107 | "{" |
| 108 | " \"name\": \"App\"," |
| 109 | " \"version\": \"0.1\"," |
| 110 | " \"app\": {" |
| 111 | " \"urls\": [" |
| 112 | " \"http://a.com/\"" |
| 113 | " ]," |
| 114 | " \"launch\": {" |
| 115 | " \"web_url\": \"http://a.com:%d/\"" |
| 116 | " }" |
| 117 | " }," |
| 118 | " \"permissions\": [\"background\"]," |
| 119 | " \"background_page\": \"http://a.com:%d/test.html\"" |
| 120 | "}", |
| 121 | test_server()->host_port_pair().port(), |
| 122 | test_server()->host_port_pair().port()); |
| 123 | |
| 124 | FilePath app_dir; |
| 125 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 126 | ASSERT_TRUE(LoadExtension(app_dir)); |
| 127 | |
| 128 | const Extension* extension = GetSingleLoadedExtension(); |
[email protected] | d2fad14 | 2011-04-15 10:18:44 | [diff] [blame] | 129 | ASSERT_TRUE( |
| 130 | BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
| 131 | GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 132 | } |