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/extension_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 "extensions/common/constants.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "extensions/common/manifest_handlers/background_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace extensions
{
20 namespace errors
= manifest_errors
;
21 namespace keys
= manifest_keys
;
23 class ExtensionManifestBackgroundTest
: public ExtensionManifestTest
{
26 TEST_F(ExtensionManifestBackgroundTest
, BackgroundPermission
) {
27 LoadAndExpectError("background_permission.json",
28 errors::kBackgroundPermissionNeeded
);
31 TEST_F(ExtensionManifestBackgroundTest
, BackgroundScripts
) {
33 scoped_ptr
<base::DictionaryValue
> manifest(
34 LoadManifest("background_scripts.json", &error
));
35 ASSERT_TRUE(manifest
.get());
37 scoped_refptr
<Extension
> extension(
38 LoadAndExpectSuccess(Manifest(manifest
.get(), "")));
39 ASSERT_TRUE(extension
.get());
40 const std::vector
<std::string
>& background_scripts
=
41 BackgroundInfo::GetBackgroundScripts(extension
.get());
42 ASSERT_EQ(2u, background_scripts
.size());
43 EXPECT_EQ("foo.js", background_scripts
[0u]);
44 EXPECT_EQ("bar/baz.js", background_scripts
[1u]);
46 EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension
.get()));
48 std::string("/") + kGeneratedBackgroundPageFilename
,
49 BackgroundInfo::GetBackgroundURL(extension
.get()).path());
51 manifest
->SetString("background_page", "monkey.html");
52 LoadAndExpectError(Manifest(manifest
.get(), ""),
53 errors::kInvalidBackgroundCombination
);
56 TEST_F(ExtensionManifestBackgroundTest
, BackgroundPage
) {
57 scoped_refptr
<Extension
> extension(
58 LoadAndExpectSuccess("background_page.json"));
59 ASSERT_TRUE(extension
.get());
60 EXPECT_EQ("/foo.html",
61 BackgroundInfo::GetBackgroundURL(extension
.get()).path());
62 EXPECT_TRUE(BackgroundInfo::AllowJSAccess(extension
.get()));
65 scoped_ptr
<base::DictionaryValue
> manifest(
66 LoadManifest("background_page_legacy.json", &error
));
67 ASSERT_TRUE(manifest
.get());
68 extension
= LoadAndExpectSuccess(Manifest(manifest
.get(), ""));
69 ASSERT_TRUE(extension
.get());
70 EXPECT_EQ("/foo.html",
71 BackgroundInfo::GetBackgroundURL(extension
.get()).path());
73 manifest
->SetInteger(keys::kManifestVersion
, 2);
75 Manifest(manifest
.get(), ""),
76 "'background_page' requires manifest version of 1 or lower.");
79 TEST_F(ExtensionManifestBackgroundTest
, BackgroundAllowNoJsAccess
) {
80 scoped_refptr
<Extension
> extension
;
81 extension
= LoadAndExpectSuccess("background_allow_no_js_access.json");
82 ASSERT_TRUE(extension
.get());
83 EXPECT_FALSE(BackgroundInfo::AllowJSAccess(extension
.get()));
85 extension
= LoadAndExpectSuccess("background_allow_no_js_access2.json");
86 ASSERT_TRUE(extension
.get());
87 EXPECT_FALSE(BackgroundInfo::AllowJSAccess(extension
.get()));
90 TEST_F(ExtensionManifestBackgroundTest
, BackgroundPageWebRequest
) {
91 ScopedCurrentChannel
current_channel(chrome::VersionInfo::CHANNEL_DEV
);
94 scoped_ptr
<base::DictionaryValue
> manifest(
95 LoadManifest("background_page.json", &error
));
96 ASSERT_TRUE(manifest
.get());
97 manifest
->SetBoolean(keys::kBackgroundPersistent
, false);
98 manifest
->SetInteger(keys::kManifestVersion
, 2);
99 scoped_refptr
<Extension
> extension(
100 LoadAndExpectSuccess(Manifest(manifest
.get(), "")));
101 ASSERT_TRUE(extension
.get());
102 EXPECT_TRUE(BackgroundInfo::HasLazyBackgroundPage(extension
.get()));
104 base::ListValue
* permissions
= new base::ListValue();
105 permissions
->Append(new base::StringValue("webRequest"));
106 manifest
->Set(keys::kPermissions
, permissions
);
107 LoadAndExpectError(Manifest(manifest
.get(), ""),
108 errors::kWebRequestConflictsWithLazyBackground
);
111 } // namespace extensions