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 "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/common/extensions/features/feature_channel.h"
11 #include "components/version_info/version_info.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 namespace errors
= manifest_errors
;
22 namespace keys
= manifest_keys
;
24 class ExtensionManifestBackgroundTest
: public ChromeManifestTest
{
27 TEST_F(ExtensionManifestBackgroundTest
, BackgroundPermission
) {
28 LoadAndExpectError("background_permission.json",
29 errors::kBackgroundPermissionNeeded
);
32 TEST_F(ExtensionManifestBackgroundTest
, BackgroundScripts
) {
34 scoped_ptr
<base::DictionaryValue
> manifest(
35 LoadManifest("background_scripts.json", &error
));
36 ASSERT_TRUE(manifest
.get());
38 scoped_refptr
<Extension
> extension(
39 LoadAndExpectSuccess(ManifestData(manifest
.get(), "")));
40 ASSERT_TRUE(extension
.get());
41 const std::vector
<std::string
>& background_scripts
=
42 BackgroundInfo::GetBackgroundScripts(extension
.get());
43 ASSERT_EQ(2u, background_scripts
.size());
44 EXPECT_EQ("foo.js", background_scripts
[0u]);
45 EXPECT_EQ("bar/baz.js", background_scripts
[1u]);
47 EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension
.get()));
49 std::string("/") + kGeneratedBackgroundPageFilename
,
50 BackgroundInfo::GetBackgroundURL(extension
.get()).path());
52 manifest
->SetString("background_page", "monkey.html");
53 LoadAndExpectError(ManifestData(manifest
.get(), ""),
54 errors::kInvalidBackgroundCombination
);
57 TEST_F(ExtensionManifestBackgroundTest
, BackgroundPage
) {
58 scoped_refptr
<Extension
> extension(
59 LoadAndExpectSuccess("background_page.json"));
60 ASSERT_TRUE(extension
.get());
61 EXPECT_EQ("/foo.html",
62 BackgroundInfo::GetBackgroundURL(extension
.get()).path());
63 EXPECT_TRUE(BackgroundInfo::AllowJSAccess(extension
.get()));
66 scoped_ptr
<base::DictionaryValue
> manifest(
67 LoadManifest("background_page_legacy.json", &error
));
68 ASSERT_TRUE(manifest
.get());
69 extension
= LoadAndExpectSuccess(ManifestData(manifest
.get(), ""));
70 ASSERT_TRUE(extension
.get());
71 EXPECT_EQ("/foo.html",
72 BackgroundInfo::GetBackgroundURL(extension
.get()).path());
74 manifest
->SetInteger(keys::kManifestVersion
, 2);
76 ManifestData(manifest
.get(), ""),
77 "'background_page' requires manifest version of 1 or lower.");
80 TEST_F(ExtensionManifestBackgroundTest
, BackgroundAllowNoJsAccess
) {
81 scoped_refptr
<Extension
> extension
;
82 extension
= LoadAndExpectSuccess("background_allow_no_js_access.json");
83 ASSERT_TRUE(extension
.get());
84 EXPECT_FALSE(BackgroundInfo::AllowJSAccess(extension
.get()));
86 extension
= LoadAndExpectSuccess("background_allow_no_js_access2.json");
87 ASSERT_TRUE(extension
.get());
88 EXPECT_FALSE(BackgroundInfo::AllowJSAccess(extension
.get()));
91 TEST_F(ExtensionManifestBackgroundTest
, BackgroundPageWebRequest
) {
92 ScopedCurrentChannel
current_channel(version_info::Channel::DEV
);
95 scoped_ptr
<base::DictionaryValue
> manifest(
96 LoadManifest("background_page.json", &error
));
97 ASSERT_TRUE(manifest
.get());
98 manifest
->SetBoolean(keys::kBackgroundPersistent
, false);
99 manifest
->SetInteger(keys::kManifestVersion
, 2);
100 scoped_refptr
<Extension
> extension(
101 LoadAndExpectSuccess(ManifestData(manifest
.get(), "")));
102 ASSERT_TRUE(extension
.get());
103 EXPECT_TRUE(BackgroundInfo::HasLazyBackgroundPage(extension
.get()));
105 base::ListValue
* permissions
= new base::ListValue();
106 permissions
->Append(new base::StringValue("webRequest"));
107 manifest
->Set(keys::kPermissions
, permissions
);
108 LoadAndExpectError(ManifestData(manifest
.get(), ""),
109 errors::kWebRequestConflictsWithLazyBackground
);
112 } // namespace extensions