Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / manifest_handlers / ui_overrides_handler_unittest.cc
blobe8e18a148831be2cfd049c3fd8d48bcae679f34a
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/ui_overrides_handler.h"
7 #include "base/command_line.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/common/extensions/features/feature_channel.h"
11 #include "components/version_info/version_info.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_url_handlers.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace {
20 const char kManifest[] = "{"
21 " \"version\" : \"1.0.0.0\","
22 " \"name\" : \"Test\","
23 " \"chrome_ui_overrides\" : {"
24 " \"bookmarks_ui\" : {"
25 " \"remove_button\" : true,"
26 " \"remove_bookmark_shortcut\" : true"
27 " }"
28 " }"
29 "}";
31 const char kBrokenManifest[] = "{"
32 " \"version\" : \"1.0.0.0\","
33 " \"name\" : \"Test\","
34 " \"chrome_ui_overrides\" : {"
35 " }"
36 "}";
38 using extensions::api::manifest_types::ChromeUIOverrides;
39 using extensions::Extension;
40 using extensions::Manifest;
41 using extensions::UIOverrides;
42 namespace manifest_keys = extensions::manifest_keys;
44 class UIOverrideTest : public testing::Test {
48 TEST_F(UIOverrideTest, ParseManifest) {
49 extensions::ScopedCurrentChannel channel(version_info::Channel::DEV);
50 // This functionality requires a feature flag.
51 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
52 "--enable-override-bookmarks-ui", "1");
53 std::string manifest(kManifest);
54 JSONStringValueDeserializer json(manifest);
55 std::string error;
56 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
57 ASSERT_TRUE(root);
58 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY));
59 scoped_refptr<Extension> extension = Extension::Create(
60 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
61 Manifest::INVALID_LOCATION,
62 *static_cast<base::DictionaryValue*>(root.get()),
63 Extension::NO_FLAGS,
64 &error);
65 ASSERT_TRUE(extension.get()) << error;
66 ASSERT_TRUE(extension->manifest()->HasPath(manifest_keys::kUIOverride));
68 UIOverrides* ui_override = static_cast<UIOverrides*>(
69 extension->GetManifestData(manifest_keys::kUIOverride));
70 ASSERT_TRUE(ui_override);
71 ASSERT_TRUE(ui_override->bookmarks_ui);
72 EXPECT_TRUE(ui_override->bookmarks_ui->remove_button);
73 EXPECT_TRUE(ui_override->bookmarks_ui->remove_bookmark_shortcut);
76 TEST_F(UIOverrideTest, ParseBrokenManifest) {
77 extensions::ScopedCurrentChannel channel(version_info::Channel::DEV);
78 // This functionality requires a feature flag.
79 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
80 "--enable-override-bookmarks-ui", "1");
81 std::string manifest(kBrokenManifest);
82 JSONStringValueDeserializer json(manifest);
83 std::string error;
84 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
85 ASSERT_TRUE(root);
86 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY));
87 scoped_refptr<Extension> extension = Extension::Create(
88 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
89 Manifest::INVALID_LOCATION,
90 *static_cast<base::DictionaryValue*>(root.get()),
91 Extension::NO_FLAGS,
92 &error);
93 EXPECT_FALSE(extension.get());
94 EXPECT_EQ(
95 extensions::ErrorUtils::FormatErrorMessage(
96 extensions::manifest_errors::kInvalidEmptyDictionary,
97 extensions::manifest_keys::kUIOverride),
98 error);
101 } // namespace