[email protected] | eb75f7b | 2012-01-04 09:07:28 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | a07676b2 | 2011-06-17 16:36:53 | [diff] [blame] | 7 | #include "chrome/browser/background/background_contents_service.h" |
| 8 | #include "chrome/browser/background/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] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 15 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 16 | #include "net/base/mock_host_resolver.h" |
| 17 | |
| 18 | class AppBackgroundPageApiTest : public ExtensionApiTest { |
| 19 | public: |
[email protected] | 7886385 | 2011-10-14 19:56:26 | [diff] [blame] | 20 | void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 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] | 5d5c1ed | 2011-09-02 00:01:11 | [diff] [blame] | 49 | // Disable on Mac only. http://crbug.com/95139 |
| 50 | #if defined(OS_MACOSX) |
| 51 | #define MAYBE_Basic DISABLED_Basic |
| 52 | #else |
| 53 | #define MAYBE_Basic Basic |
| 54 | #endif |
| 55 | |
| 56 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 57 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 58 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 59 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 60 | std::string app_manifest = base::StringPrintf( |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 61 | "{" |
| 62 | " \"name\": \"App\"," |
| 63 | " \"version\": \"0.1\"," |
[email protected] | 1c0132c | 2011-12-03 04:19:32 | [diff] [blame] | 64 | " \"manifest_version\": 2," |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 65 | " \"app\": {" |
| 66 | " \"urls\": [" |
| 67 | " \"http://a.com/\"" |
| 68 | " ]," |
| 69 | " \"launch\": {" |
| 70 | " \"web_url\": \"http://a.com:%d/\"" |
| 71 | " }" |
| 72 | " }," |
| 73 | " \"permissions\": [\"background\"]" |
| 74 | "}", |
| 75 | test_server()->host_port_pair().port()); |
| 76 | |
| 77 | FilePath app_dir; |
| 78 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 79 | ASSERT_TRUE(LoadExtension(app_dir)); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 80 | ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; |
| 81 | } |
| 82 | |
[email protected] | 6aa25edc1 | 2011-10-06 02:16:11 | [diff] [blame] | 83 | // Crashy, http://crbug.com/69215. |
[email protected] | 4b5e29f | 2011-01-11 07:44:08 | [diff] [blame] | 84 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 85 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 86 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 87 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 88 | std::string app_manifest = base::StringPrintf( |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 89 | "{" |
| 90 | " \"name\": \"App\"," |
| 91 | " \"version\": \"0.1\"," |
[email protected] | 1c0132c | 2011-12-03 04:19:32 | [diff] [blame] | 92 | " \"manifest_version\": 2," |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 93 | " \"app\": {" |
| 94 | " \"urls\": [" |
| 95 | " \"http://a.com/\"" |
| 96 | " ]," |
| 97 | " \"launch\": {" |
| 98 | " \"web_url\": \"http://a.com:%d/\"" |
| 99 | " }" |
| 100 | " }" |
| 101 | "}", |
| 102 | test_server()->host_port_pair().port()); |
| 103 | |
| 104 | FilePath app_dir; |
| 105 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 106 | ASSERT_TRUE(LoadExtension(app_dir)); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 107 | ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) |
| 108 | << message_; |
| 109 | } |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 110 | |
| 111 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { |
| 112 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 113 | ASSERT_TRUE(StartTestServer()); |
| 114 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 115 | std::string app_manifest = base::StringPrintf( |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 116 | "{" |
| 117 | " \"name\": \"App\"," |
| 118 | " \"version\": \"0.1\"," |
[email protected] | 1c0132c | 2011-12-03 04:19:32 | [diff] [blame] | 119 | " \"manifest_version\": 2," |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 120 | " \"app\": {" |
| 121 | " \"urls\": [" |
| 122 | " \"http://a.com/\"" |
| 123 | " ]," |
| 124 | " \"launch\": {" |
| 125 | " \"web_url\": \"http://a.com:%d/\"" |
| 126 | " }" |
| 127 | " }," |
| 128 | " \"permissions\": [\"background\"]," |
[email protected] | eb75f7b | 2012-01-04 09:07:28 | [diff] [blame] | 129 | " \"background\": {" |
| 130 | " \"page\": \"http://a.com:%d/test.html\"" |
| 131 | " }" |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 132 | "}", |
| 133 | test_server()->host_port_pair().port(), |
| 134 | test_server()->host_port_pair().port()); |
| 135 | |
| 136 | FilePath app_dir; |
| 137 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 138 | ASSERT_TRUE(LoadExtension(app_dir)); |
| 139 | |
| 140 | const Extension* extension = GetSingleLoadedExtension(); |
[email protected] | d2fad14 | 2011-04-15 10:18:44 | [diff] [blame] | 141 | ASSERT_TRUE( |
| 142 | BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
| 143 | GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 144 | } |
[email protected] | 6aa25edc1 | 2011-10-06 02:16:11 | [diff] [blame] | 145 | |
| 146 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) { |
| 147 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 148 | ASSERT_TRUE(StartTestServer()); |
| 149 | |
| 150 | std::string app_manifest = base::StringPrintf( |
| 151 | "{" |
| 152 | " \"name\": \"App\"," |
| 153 | " \"version\": \"0.1\"," |
[email protected] | 1c0132c | 2011-12-03 04:19:32 | [diff] [blame] | 154 | " \"manifest_version\": 2," |
[email protected] | 6aa25edc1 | 2011-10-06 02:16:11 | [diff] [blame] | 155 | " \"app\": {" |
| 156 | " \"urls\": [" |
| 157 | " \"http://a.com/\"" |
| 158 | " ]," |
| 159 | " \"launch\": {" |
| 160 | " \"web_url\": \"http://a.com:%d/\"" |
| 161 | " }" |
| 162 | " }," |
| 163 | " \"permissions\": [\"background\"]" |
| 164 | "}", |
| 165 | test_server()->host_port_pair().port()); |
| 166 | |
| 167 | FilePath app_dir; |
| 168 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 169 | ASSERT_TRUE(LoadExtension(app_dir)); |
| 170 | ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_; |
| 171 | } |
[email protected] | 7886385 | 2011-10-14 19:56:26 | [diff] [blame] | 172 | |
| 173 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoPagesWithManifest) { |
| 174 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 175 | ASSERT_TRUE(StartTestServer()); |
| 176 | |
| 177 | std::string app_manifest = base::StringPrintf( |
| 178 | "{" |
| 179 | " \"name\": \"App\"," |
| 180 | " \"version\": \"0.1\"," |
[email protected] | 1c0132c | 2011-12-03 04:19:32 | [diff] [blame] | 181 | " \"manifest_version\": 2," |
[email protected] | 7886385 | 2011-10-14 19:56:26 | [diff] [blame] | 182 | " \"app\": {" |
| 183 | " \"urls\": [" |
| 184 | " \"http://a.com/\"" |
| 185 | " ]," |
| 186 | " \"launch\": {" |
| 187 | " \"web_url\": \"http://a.com:%d/\"" |
| 188 | " }" |
| 189 | " }," |
[email protected] | eb75f7b | 2012-01-04 09:07:28 | [diff] [blame] | 190 | " \"background\": {" |
| 191 | " \"page\": \"http://a.com:%d/bg.html\"" |
| 192 | " }," |
[email protected] | 7886385 | 2011-10-14 19:56:26 | [diff] [blame] | 193 | " \"permissions\": [\"background\"]" |
| 194 | "}", |
| 195 | test_server()->host_port_pair().port(), |
| 196 | test_server()->host_port_pair().port()); |
| 197 | |
| 198 | FilePath app_dir; |
| 199 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 200 | ASSERT_TRUE(LoadExtension(app_dir)); |
| 201 | ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) << |
| 202 | message_; |
| 203 | } |
[email protected] | ae80a88 | 2011-12-14 05:59:05 | [diff] [blame] | 204 | |
[email protected] | eef2e6c | 2011-12-22 23:34:50 | [diff] [blame] | 205 | // Times out occasionally -- see crbug.com/108493 |
[email protected] | 378f498 | 2011-12-23 01:55:48 | [diff] [blame] | 206 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenPopupFromBGPage) { |
[email protected] | ae80a88 | 2011-12-14 05:59:05 | [diff] [blame] | 207 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 208 | ASSERT_TRUE(StartTestServer()); |
| 209 | |
| 210 | std::string app_manifest = base::StringPrintf( |
| 211 | "{" |
| 212 | " \"name\": \"App\"," |
| 213 | " \"version\": \"0.1\"," |
| 214 | " \"manifest_version\": 2," |
| 215 | " \"app\": {" |
| 216 | " \"urls\": [" |
| 217 | " \"http://a.com/\"" |
| 218 | " ]," |
| 219 | " \"launch\": {" |
| 220 | " \"web_url\": \"http://a.com:%d/\"" |
| 221 | " }" |
| 222 | " }," |
[email protected] | eb75f7b | 2012-01-04 09:07:28 | [diff] [blame] | 223 | " \"background\": { \"page\": \"http://a.com:%d/files/extensions/api_test/" |
| 224 | "app_background_page/bg_open/bg_open_bg.html\" }," |
[email protected] | ae80a88 | 2011-12-14 05:59:05 | [diff] [blame] | 225 | " \"permissions\": [\"background\"]" |
| 226 | "}", |
| 227 | test_server()->host_port_pair().port(), |
| 228 | test_server()->host_port_pair().port()); |
| 229 | |
| 230 | FilePath app_dir; |
| 231 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 232 | ASSERT_TRUE(LoadExtension(app_dir)); |
| 233 | ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; |
| 234 | } |