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.
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"
19 namespace extensions
{
21 class ExtensionManifestServiceWorkerTest
: public ExtensionManifestTest
{
23 ExtensionManifestServiceWorkerTest()
24 : trunk_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN
) {}
26 void AddServiceWorkerCommandLineSwitch() {
27 CHECK(!CommandLine::ForCurrentProcess()->HasSwitch(
28 switches::kEnableServiceWorker
));
29 CommandLine::ForCurrentProcess()->AppendSwitch(
30 switches::kEnableServiceWorker
);
33 // "app.service_worker" is restricted to trunk in _manifest_features.json.
34 extensions::ScopedCurrentChannel trunk_channel_
;
37 // Checks that a service_worker key is ignored without enable-service-worker
38 // switch. When service workers are enabled by default please remove this
40 TEST_F(ExtensionManifestServiceWorkerTest
, ServiceWorkerCommandLineRequired
) {
41 CHECK(!CommandLine::ForCurrentProcess()->HasSwitch(
42 ::switches::kEnableServiceWorker
));
43 LoadFromStringAndExpectError(
46 " 'manifest_version': 2,"
49 " 'service_worker': {"
50 " 'script': 'service_worker.js'"
54 manifest_errors::kServiceWorkerRequiresFlag
);
57 // Checks that an app manifest with a service_worker key but no script fails.
58 TEST_F(ExtensionManifestServiceWorkerTest
, ServiceWorkerEmpty
) {
59 AddServiceWorkerCommandLineSwitch();
60 LoadFromStringAndExpectError(
63 " 'manifest_version': 2,"
66 " 'service_worker': {}" // No script specified.
69 manifest_errors::kBackgroundRequiredForPlatformApps
);
71 LoadFromStringAndExpectError(
74 " 'manifest_version': 2,"
77 " 'service_worker': {"
78 " 'script': ''" // Empty script.
82 manifest_errors::kBackgroundRequiredForPlatformApps
);
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(
91 " 'manifest_version': 2,"
94 " 'service_worker': {"
95 " 'script': 'service_worker.js'"
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()));
105 EXPECT_TRUE(BackgroundInfo::HasServiceWorker(extension
.get()));
108 // Checks that an app manifest with service worker and background script fails.
109 TEST_F(ExtensionManifestServiceWorkerTest
, ServiceWorkerWithBackgroundScript
) {
110 AddServiceWorkerCommandLineSwitch();
111 LoadFromStringAndExpectError(
114 " 'manifest_version': 2,"
117 " 'service_worker': {"
118 " 'script': 'service_worker.js'"
121 " 'scripts': [ 'background.js' ]"
125 manifest_errors::kInvalidBackgroundCombination
);
128 } // namespace extensions