ui_overrides_handler_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h"
6
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 "chrome/common/extensions/manifest_url_handler.h"
12#include "extensions/common/error_utils.h"
13#include "extensions/common/extension.h"
14#include "extensions/common/manifest_constants.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace {
18
19const char kManifest[] = "{"
20    " \"version\" : \"1.0.0.0\","
21    " \"name\" : \"Test\","
22    " \"chrome_ui_overrides\" : {"
23    "   \"bookmarks_ui\" : {"
24    "        \"remove_button\" : true,"
25    "        \"remove_bookmark_shortcut\" : true"
26    "    }"
27    "  }"
28    "}";
29
30const char kBrokenManifest[] = "{"
31    " \"version\" : \"1.0.0.0\","
32    " \"name\" : \"Test\","
33    " \"chrome_ui_overrides\" : {"
34    "  }"
35    "}";
36
37using extensions::api::manifest_types::ChromeUIOverrides;
38using extensions::Extension;
39using extensions::Manifest;
40using extensions::UIOverrides;
41namespace manifest_keys = extensions::manifest_keys;
42
43class UIOverrideTest : public testing::Test {
44};
45
46
47TEST_F(UIOverrideTest, ParseManifest) {
48  extensions::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV);
49  // This functionality requires a feature flag.
50  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
51      "--enable-override-bookmarks-ui",
52      "1");
53  std::string manifest(kManifest);
54  JSONStringValueSerializer 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));
67
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);
74}
75
76TEST_F(UIOverrideTest, ParseBrokenManifest) {
77  extensions::ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV);
78  // This functionality requires a feature flag.
79  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
80      "--enable-override-bookmarks-ui",
81      "1");
82  std::string manifest(kBrokenManifest);
83  JSONStringValueSerializer json(&manifest);
84  std::string error;
85  scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
86  ASSERT_TRUE(root);
87  ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY));
88  scoped_refptr<Extension> extension = Extension::Create(
89      base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
90      Manifest::INVALID_LOCATION,
91      *static_cast<base::DictionaryValue*>(root.get()),
92      Extension::NO_FLAGS,
93      &error);
94  EXPECT_FALSE(extension.get());
95  EXPECT_EQ(
96      extensions::ErrorUtils::FormatErrorMessage(
97          extensions::manifest_errors::kInvalidEmptyDictionary,
98          extensions::manifest_keys::kUIOverride),
99      error);
100}
101
102}  // namespace
103