1 // Copyright 2013 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_handlers/settings_overrides_handler.h"
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/extensions/features/feature_channel.h"
10 #include "chrome/common/extensions/manifest_url_handler.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 const char kManifest
[] = "{"
18 " \"version\" : \"1.0.0.0\","
19 " \"name\" : \"Test\","
20 " \"chrome_settings_overrides\" : {"
21 " \"homepage\" : \"http://www.homepage.com\","
22 " \"search_provider\" : {"
23 " \"name\" : \"first\","
24 " \"keyword\" : \"firstkey\","
25 " \"search_url\" : \"http://www.foo.com/s?q={searchTerms}\","
26 " \"favicon_url\" : \"http://www.foo.com/favicon.ico\","
27 " \"suggest_url\" : \"http://www.foo.com/s?q={searchTerms}\","
28 " \"encoding\" : \"UTF-8\","
29 " \"is_default\" : true"
31 " \"startup_pages\" : [\"http://www.startup.com\"]"
35 const char kBrokenManifest
[] = "{"
36 " \"version\" : \"1.0.0.0\","
37 " \"name\" : \"Test\","
38 " \"chrome_settings_overrides\" : {"
39 " \"homepage\" : \"{invalid}\","
40 " \"search_provider\" : {"
41 " \"name\" : \"first\","
42 " \"keyword\" : \"firstkey\","
43 " \"search_url\" : \"{invalid}/s?q={searchTerms}\","
44 " \"favicon_url\" : \"{invalid}/favicon.ico\","
45 " \"encoding\" : \"UTF-8\","
46 " \"is_default\" : true"
48 " \"startup_pages\" : [\"{invalid}\"]"
52 using extensions::api::manifest_types::ChromeSettingsOverrides
;
53 using extensions::Extension
;
54 using extensions::Manifest
;
55 using extensions::SettingsOverrides
;
56 namespace manifest_keys
= extensions::manifest_keys
;
58 class OverrideSettingsTest
: public testing::Test
{
62 TEST_F(OverrideSettingsTest
, ParseManifest
) {
63 extensions::ScopedCurrentChannel
channel(chrome::VersionInfo::CHANNEL_DEV
);
64 std::string
manifest(kManifest
);
65 JSONStringValueSerializer
json(&manifest
);
67 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
69 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
70 scoped_refptr
<Extension
> extension
= Extension::Create(
71 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
72 Manifest::INVALID_LOCATION
,
73 *static_cast<base::DictionaryValue
*>(root
.get()),
76 ASSERT_TRUE(extension
);
77 ASSERT_TRUE(extension
->manifest()->HasPath(manifest_keys::kSettingsOverride
));
79 SettingsOverrides
* settings_override
= static_cast<SettingsOverrides
*>(
80 extension
->GetManifestData(manifest_keys::kSettingsOverride
));
81 ASSERT_TRUE(settings_override
);
82 ASSERT_TRUE(settings_override
->search_engine
);
83 EXPECT_TRUE(settings_override
->search_engine
->is_default
);
84 const ChromeSettingsOverrides::Search_provider
* search_engine
=
85 settings_override
->search_engine
.get();
86 EXPECT_EQ("first", search_engine
->name
);
87 EXPECT_EQ("firstkey", search_engine
->keyword
);
88 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", search_engine
->search_url
);
89 EXPECT_EQ("http://www.foo.com/favicon.ico", search_engine
->favicon_url
);
90 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}",
91 *search_engine
->suggest_url
);
92 EXPECT_EQ("UTF-8", search_engine
->encoding
);
94 EXPECT_EQ(std::vector
<GURL
>(1, GURL("http://www.startup.com")),
95 settings_override
->startup_pages
);
97 ASSERT_TRUE(settings_override
->homepage
);
98 EXPECT_EQ(GURL("http://www.homepage.com"), *settings_override
->homepage
);
101 TEST_F(OverrideSettingsTest
, ParseBrokenManifest
) {
102 extensions::ScopedCurrentChannel
channel(chrome::VersionInfo::CHANNEL_DEV
);
103 std::string
manifest(kBrokenManifest
);
104 JSONStringValueSerializer
json(&manifest
);
106 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
108 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
109 scoped_refptr
<Extension
> extension
= Extension::Create(
110 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
111 Manifest::INVALID_LOCATION
,
112 *static_cast<base::DictionaryValue
*>(root
.get()),
115 EXPECT_FALSE(extension
);
117 std::string(extensions::manifest_errors::kInvalidEmptySettingsOverrides
),