[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] | 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: |
| 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] | 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\"," |
| 64 | " \"app\": {" |
| 65 | " \"urls\": [" |
| 66 | " \"http://a.com/\"" |
| 67 | " ]," |
| 68 | " \"launch\": {" |
| 69 | " \"web_url\": \"http://a.com:%d/\"" |
| 70 | " }" |
| 71 | " }," |
| 72 | " \"permissions\": [\"background\"]" |
| 73 | "}", |
| 74 | test_server()->host_port_pair().port()); |
| 75 | |
| 76 | FilePath app_dir; |
| 77 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 78 | ASSERT_TRUE(LoadExtension(app_dir)); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 79 | ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; |
| 80 | } |
| 81 | |
[email protected] | 6aa25edc1 | 2011-10-06 02:16:11 | [diff] [blame^] | 82 | // Crashy, http://crbug.com/69215. |
[email protected] | 4b5e29f | 2011-01-11 07:44:08 | [diff] [blame] | 83 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 84 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 85 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 86 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 87 | std::string app_manifest = base::StringPrintf( |
[email protected] | f08debf | 2010-10-23 10:13:02 | [diff] [blame] | 88 | "{" |
| 89 | " \"name\": \"App\"," |
| 90 | " \"version\": \"0.1\"," |
| 91 | " \"app\": {" |
| 92 | " \"urls\": [" |
| 93 | " \"http://a.com/\"" |
| 94 | " ]," |
| 95 | " \"launch\": {" |
| 96 | " \"web_url\": \"http://a.com:%d/\"" |
| 97 | " }" |
| 98 | " }" |
| 99 | "}", |
| 100 | test_server()->host_port_pair().port()); |
| 101 | |
| 102 | FilePath app_dir; |
| 103 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 104 | ASSERT_TRUE(LoadExtension(app_dir)); |
[email protected] | 1c3cf8b | 2010-05-11 23:18:27 | [diff] [blame] | 105 | ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) |
| 106 | << message_; |
| 107 | } |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 108 | |
| 109 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { |
| 110 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 111 | ASSERT_TRUE(StartTestServer()); |
| 112 | |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 113 | std::string app_manifest = base::StringPrintf( |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 114 | "{" |
| 115 | " \"name\": \"App\"," |
| 116 | " \"version\": \"0.1\"," |
| 117 | " \"app\": {" |
| 118 | " \"urls\": [" |
| 119 | " \"http://a.com/\"" |
| 120 | " ]," |
| 121 | " \"launch\": {" |
| 122 | " \"web_url\": \"http://a.com:%d/\"" |
| 123 | " }" |
| 124 | " }," |
| 125 | " \"permissions\": [\"background\"]," |
| 126 | " \"background_page\": \"http://a.com:%d/test.html\"" |
| 127 | "}", |
| 128 | test_server()->host_port_pair().port(), |
| 129 | test_server()->host_port_pair().port()); |
| 130 | |
| 131 | FilePath app_dir; |
| 132 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 133 | ASSERT_TRUE(LoadExtension(app_dir)); |
| 134 | |
| 135 | const Extension* extension = GetSingleLoadedExtension(); |
[email protected] | d2fad14 | 2011-04-15 10:18:44 | [diff] [blame] | 136 | ASSERT_TRUE( |
| 137 | BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
| 138 | GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
[email protected] | 41619a9a | 2011-04-13 20:07:32 | [diff] [blame] | 139 | } |
[email protected] | 6aa25edc1 | 2011-10-06 02:16:11 | [diff] [blame^] | 140 | |
| 141 | IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) { |
| 142 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 143 | ASSERT_TRUE(StartTestServer()); |
| 144 | |
| 145 | std::string app_manifest = base::StringPrintf( |
| 146 | "{" |
| 147 | " \"name\": \"App\"," |
| 148 | " \"version\": \"0.1\"," |
| 149 | " \"app\": {" |
| 150 | " \"urls\": [" |
| 151 | " \"http://a.com/\"" |
| 152 | " ]," |
| 153 | " \"launch\": {" |
| 154 | " \"web_url\": \"http://a.com:%d/\"" |
| 155 | " }" |
| 156 | " }," |
| 157 | " \"permissions\": [\"background\"]" |
| 158 | "}", |
| 159 | test_server()->host_port_pair().port()); |
| 160 | |
| 161 | FilePath app_dir; |
| 162 | ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 163 | ASSERT_TRUE(LoadExtension(app_dir)); |
| 164 | ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_; |
| 165 | } |