Move manifest_url_handler to //extensions
[chromium-blink-merge.git] / chrome / common / extensions / manifest_handlers / settings_overrides_handler_unittest.cc
blob3f09ca466e781151fe7b430233caa0f7076fea5e
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"
15 namespace {
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"
30 " },"
31 " \"startup_pages\" : [\"http://www.startup.com\"]"
32 " }"
33 "}";
35 const char kPrepopulatedManifest[] =
36 "{"
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"
44 " }"
45 " }"
46 "}";
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"
60 " },"
61 " \"startup_pages\" : [\"{invalid}\"]"
62 " }"
63 "}";
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);
78 std::string error;
79 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
80 ASSERT_TRUE(root);
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()),
86 Extension::NO_FLAGS,
87 &error);
88 ASSERT_TRUE(extension.get());
89 #if defined(OS_WIN)
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);
112 #else
113 EXPECT_FALSE(
114 extension->manifest()->HasPath(manifest_keys::kSettingsOverride));
115 #endif
118 TEST_F(OverrideSettingsTest, ParsePrepopulatedId) {
119 std::string manifest(kPrepopulatedManifest);
120 JSONStringValueSerializer json(&manifest);
121 std::string error;
122 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
123 ASSERT_TRUE(root);
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()),
129 Extension::NO_FLAGS,
130 &error);
131 ASSERT_TRUE(extension.get());
132 #if defined(OS_WIN)
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);
145 #else
146 EXPECT_FALSE(
147 extension->manifest()->HasPath(manifest_keys::kSettingsOverride));
148 #endif
151 TEST_F(OverrideSettingsTest, ParseBrokenManifest) {
152 std::string manifest(kBrokenManifest);
153 JSONStringValueSerializer json(&manifest);
154 std::string error;
155 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
156 ASSERT_TRUE(root);
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()),
162 Extension::NO_FLAGS,
163 &error);
164 #if defined(OS_WIN)
165 EXPECT_FALSE(extension);
166 EXPECT_EQ(
167 extensions::ErrorUtils::FormatErrorMessage(
168 extensions::manifest_errors::kInvalidEmptyDictionary,
169 extensions::manifest_keys::kSettingsOverride),
170 error);
171 #else
172 EXPECT_TRUE(extension.get());
173 EXPECT_FALSE(
174 extension->manifest()->HasPath(manifest_keys::kSettingsOverride));
175 #endif
178 } // namespace