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 "extensions/common/error_utils.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "extensions/common/manifest_url_handlers.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 kPrepopulatedManifest
[] =
37 " \"version\" : \"1.0.0.0\","
38 " \"name\" : \"Test\","
39 " \"chrome_settings_overrides\" : {"
40 " \"search_provider\" : {"
41 " \"search_url\" : \"http://www.foo.com/s?q={searchTerms}\","
42 " \"prepopulated_id\" : 3,"
43 " \"is_default\" : true"
48 const char kBrokenManifest
[] = "{"
49 " \"version\" : \"1.0.0.0\","
50 " \"name\" : \"Test\","
51 " \"chrome_settings_overrides\" : {"
52 " \"homepage\" : \"{invalid}\","
53 " \"search_provider\" : {"
54 " \"name\" : \"first\","
55 " \"keyword\" : \"firstkey\","
56 " \"search_url\" : \"{invalid}/s?q={searchTerms}\","
57 " \"favicon_url\" : \"{invalid}/favicon.ico\","
58 " \"encoding\" : \"UTF-8\","
59 " \"is_default\" : true"
61 " \"startup_pages\" : [\"{invalid}\"]"
65 using extensions::api::manifest_types::ChromeSettingsOverrides
;
66 using extensions::Extension
;
67 using extensions::Manifest
;
68 using extensions::SettingsOverrides
;
69 namespace manifest_keys
= extensions::manifest_keys
;
71 class OverrideSettingsTest
: public testing::Test
{
75 TEST_F(OverrideSettingsTest
, ParseManifest
) {
76 std::string
manifest(kManifest
);
77 JSONStringValueSerializer
json(&manifest
);
79 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
81 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
82 scoped_refptr
<Extension
> extension
= Extension::Create(
83 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
84 Manifest::INVALID_LOCATION
,
85 *static_cast<base::DictionaryValue
*>(root
.get()),
88 ASSERT_TRUE(extension
.get());
90 ASSERT_TRUE(extension
->manifest()->HasPath(manifest_keys::kSettingsOverride
));
92 SettingsOverrides
* settings_override
= static_cast<SettingsOverrides
*>(
93 extension
->GetManifestData(manifest_keys::kSettingsOverride
));
94 ASSERT_TRUE(settings_override
);
95 ASSERT_TRUE(settings_override
->search_engine
);
96 EXPECT_TRUE(settings_override
->search_engine
->is_default
);
97 const ChromeSettingsOverrides::Search_provider
* search_engine
=
98 settings_override
->search_engine
.get();
99 EXPECT_EQ("first", *search_engine
->name
);
100 EXPECT_EQ("firstkey", *search_engine
->keyword
);
101 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", search_engine
->search_url
);
102 EXPECT_EQ("http://www.foo.com/favicon.ico", *search_engine
->favicon_url
);
103 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}",
104 *search_engine
->suggest_url
);
105 EXPECT_EQ("UTF-8", *search_engine
->encoding
);
107 EXPECT_EQ(std::vector
<GURL
>(1, GURL("http://www.startup.com")),
108 settings_override
->startup_pages
);
110 ASSERT_TRUE(settings_override
->homepage
);
111 EXPECT_EQ(GURL("http://www.homepage.com"), *settings_override
->homepage
);
114 extension
->manifest()->HasPath(manifest_keys::kSettingsOverride
));
118 TEST_F(OverrideSettingsTest
, ParsePrepopulatedId
) {
119 std::string
manifest(kPrepopulatedManifest
);
120 JSONStringValueSerializer
json(&manifest
);
122 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
124 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
125 scoped_refptr
<Extension
> extension
=
126 Extension::Create(base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
127 Manifest::INVALID_LOCATION
,
128 *static_cast<base::DictionaryValue
*>(root
.get()),
131 ASSERT_TRUE(extension
.get());
133 ASSERT_TRUE(extension
->manifest()->HasPath(manifest_keys::kSettingsOverride
));
135 SettingsOverrides
* settings_override
= static_cast<SettingsOverrides
*>(
136 extension
->GetManifestData(manifest_keys::kSettingsOverride
));
137 ASSERT_TRUE(settings_override
);
138 ASSERT_TRUE(settings_override
->search_engine
);
139 EXPECT_TRUE(settings_override
->search_engine
->is_default
);
140 const ChromeSettingsOverrides::Search_provider
* search_engine
=
141 settings_override
->search_engine
.get();
142 ASSERT_TRUE(search_engine
->prepopulated_id
);
143 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", search_engine
->search_url
);
144 EXPECT_EQ(3, *search_engine
->prepopulated_id
);
147 extension
->manifest()->HasPath(manifest_keys::kSettingsOverride
));
151 TEST_F(OverrideSettingsTest
, ParseBrokenManifest
) {
152 std::string
manifest(kBrokenManifest
);
153 JSONStringValueSerializer
json(&manifest
);
155 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
157 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
158 scoped_refptr
<Extension
> extension
= Extension::Create(
159 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
160 Manifest::INVALID_LOCATION
,
161 *static_cast<base::DictionaryValue
*>(root
.get()),
165 EXPECT_FALSE(extension
.get());
167 extensions::ErrorUtils::FormatErrorMessage(
168 extensions::manifest_errors::kInvalidEmptyDictionary
,
169 extensions::manifest_keys::kSettingsOverride
),
172 EXPECT_TRUE(extension
.get());
174 extension
->manifest()->HasPath(manifest_keys::kSettingsOverride
));