| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/test/values_test_util.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/common/extensions/features/feature_channel.h" | |
| 10 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h" | |
| 11 #include "content/public/common/content_switches.h" | |
| 12 #include "extensions/common/constants.h" | |
| 13 #include "extensions/common/error_utils.h" | |
| 14 #include "extensions/common/extension.h" | |
| 15 #include "extensions/common/manifest_constants.h" | |
| 16 #include "extensions/common/manifest_handlers/background_info.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 namespace extensions { | |
| 20 | |
| 21 class ExtensionManifestServiceWorkerTest : public ExtensionManifestTest { | |
| 22 public: | |
| 23 ExtensionManifestServiceWorkerTest() | |
| 24 : trunk_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {} | |
| 25 | |
| 26 void AddServiceWorkerCommandLineSwitch() { | |
| 27 CHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 28 switches::kEnableExperimentalWebPlatformFeatures)); | |
| 29 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 30 switches::kEnableExperimentalWebPlatformFeatures); | |
| 31 } | |
| 32 | |
| 33 // "app.service_worker" is restricted to trunk in _manifest_features.json. | |
| 34 extensions::ScopedCurrentChannel trunk_channel_; | |
| 35 }; | |
| 36 | |
| 37 // Checks that a service_worker key is ignored without | |
| 38 // enable-experimental-web-platform-features switch. When service workers are | |
| 39 // enabled by default please remove this test. | |
| 40 TEST_F(ExtensionManifestServiceWorkerTest, ServiceWorkerCommandLineRequired) { | |
| 41 CHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 42 ::switches::kEnableExperimentalWebPlatformFeatures)); | |
| 43 LoadFromStringAndExpectError( | |
| 44 "{" | |
| 45 " 'name': ''," | |
| 46 " 'manifest_version': 2," | |
| 47 " 'version': '1'," | |
| 48 " 'app': {" | |
| 49 " 'service_worker': {" | |
| 50 " 'script': 'service_worker.js'" | |
| 51 " }" | |
| 52 " }" | |
| 53 "}", | |
| 54 manifest_errors::kServiceWorkerRequiresFlag); | |
| 55 } | |
| 56 | |
| 57 // Checks that an app manifest with a service_worker key but no script fails. | |
| 58 TEST_F(ExtensionManifestServiceWorkerTest, ServiceWorkerEmpty) { | |
| 59 AddServiceWorkerCommandLineSwitch(); | |
| 60 LoadFromStringAndExpectError( | |
| 61 "{" | |
| 62 " 'name': ''," | |
| 63 " 'manifest_version': 2," | |
| 64 " 'version': '1'," | |
| 65 " 'app': {" | |
| 66 " 'service_worker': {}" // No script specified. | |
| 67 " }" | |
| 68 "}", | |
| 69 manifest_errors::kBackgroundRequiredForPlatformApps); | |
| 70 | |
| 71 LoadFromStringAndExpectError( | |
| 72 "{" | |
| 73 " 'name': ''," | |
| 74 " 'manifest_version': 2," | |
| 75 " 'version': '1'," | |
| 76 " 'app': {" | |
| 77 " 'service_worker': {" | |
| 78 " 'script': ''" // Empty script. | |
| 79 " }" | |
| 80 " }" | |
| 81 "}", | |
| 82 manifest_errors::kBackgroundRequiredForPlatformApps); | |
| 83 } | |
| 84 | |
| 85 // Checks that an app manifest with a single script is loaded. | |
| 86 TEST_F(ExtensionManifestServiceWorkerTest, ServiceWorkerScript) { | |
| 87 AddServiceWorkerCommandLineSwitch(); | |
| 88 scoped_refptr<Extension> extension(LoadFromStringAndExpectSuccess( | |
| 89 "{" | |
| 90 " 'name': ''," | |
| 91 " 'manifest_version': 2," | |
| 92 " 'version': '1'," | |
| 93 " 'app': {" | |
| 94 " 'service_worker': {" | |
| 95 " 'script': 'service_worker.js'" | |
| 96 " }" | |
| 97 " }" | |
| 98 "}")); | |
| 99 ASSERT_TRUE(extension.get()); | |
| 100 // "app.service_worker" key exists and access is permitted. | |
| 101 EXPECT_TRUE(extension->manifest()->HasPath("app.service_worker")); | |
| 102 EXPECT_EQ("service_worker.js", | |
| 103 BackgroundInfo::GetServiceWorkerScript(extension.get())); | |
| 104 | |
| 105 EXPECT_TRUE(BackgroundInfo::HasServiceWorker(extension.get())); | |
| 106 } | |
| 107 | |
| 108 // Checks that an app manifest with service worker and background script fails. | |
| 109 TEST_F(ExtensionManifestServiceWorkerTest, ServiceWorkerWithBackgroundScript) { | |
| 110 AddServiceWorkerCommandLineSwitch(); | |
| 111 LoadFromStringAndExpectError( | |
| 112 "{" | |
| 113 " 'name': ''," | |
| 114 " 'manifest_version': 2," | |
| 115 " 'version': '1'," | |
| 116 " 'app': {" | |
| 117 " 'service_worker': {" | |
| 118 " 'script': 'service_worker.js'" | |
| 119 " }," | |
| 120 " 'background': {" | |
| 121 " 'scripts': [ 'background.js' ]" | |
| 122 " }" | |
| 123 " }" | |
| 124 "}", | |
| 125 manifest_errors::kInvalidBackgroundCombination); | |
| 126 } | |
| 127 | |
| 128 } // namespace extensions | |
| OLD | NEW |