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 "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"
19 const 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"
30 const char kBrokenManifest
[] = "{"
31 " \"version\" : \"1.0.0.0\","
32 " \"name\" : \"Test\","
33 " \"chrome_ui_overrides\" : {"
37 using extensions::api::manifest_types::ChromeUIOverrides
;
38 using extensions::Extension
;
39 using extensions::Manifest
;
40 using extensions::UIOverrides
;
41 namespace manifest_keys
= extensions::manifest_keys
;
43 class UIOverrideTest
: public testing::Test
{
47 TEST_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",
53 std::string
manifest(kManifest
);
54 JSONStringValueSerializer
json(&manifest
);
56 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
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()),
65 ASSERT_TRUE(extension
) << 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(chrome::VersionInfo::CHANNEL_DEV
);
78 // This functionality requires a feature flag.
79 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
80 "--enable-override-bookmarks-ui",
82 std::string
manifest(kBrokenManifest
);
83 JSONStringValueSerializer
json(&manifest
);
85 scoped_ptr
<base::Value
> root(json
.Deserialize(NULL
, &error
));
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()),
94 EXPECT_FALSE(extension
);
96 extensions::ErrorUtils::FormatErrorMessage(
97 extensions::manifest_errors::kInvalidEmptyDictionary
,
98 extensions::manifest_keys::kUIOverride
),