Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(792)

Side by Side Diff: chrome/browser/extensions/extension_action_manager_unittest.cc

Issue 415813003: Improve extension icon prediction (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed unittest to add extensions to registry Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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/browser/extensions/extension_action_manager.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/extension_action.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "extensions/browser/extension_registry.h"
11 #include "extensions/common/extension_builder.h"
12 #include "extensions/common/manifest_handlers/icons_handler.h"
13 #include "extensions/common/value_builder.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace extensions {
17
18 namespace {
19
20 const char kBrowserAction[] = "browser_action";
21 const char kPageAction[] = "page_action";
22
23 } // namespace
24
25 class ExtensionActionManagerTest : public testing::Test {
26 public:
27 ExtensionActionManagerTest();
28
29 protected:
30 // Build an extension, populating |action_type| key with |action|, and
31 // "icons" key with |extension_icons|.
32 scoped_refptr<Extension> BuildExtension(DictionaryBuilder& extension_icons,
33 DictionaryBuilder& action,
34 const char* action_type);
35
36 // Returns true if |action|'s title matches |extension|'s name.
37 bool TitlesMatch(const Extension& extension, const ExtensionAction& action);
38
39 // Returns true if |action|'s icon for size |action_key| matches
40 // |extension|'s icon for size |extension_key|;
41 bool IconsMatch(const Extension& extension,
42 int extension_key,
43 const ExtensionAction& action,
44 int action_key);
45
46 // Returns the appropriate action for |extension| according to |action_type|.
47 ExtensionAction* GetAction(const char* action_type,
48 const Extension& extension);
49
50 // Tests that values that are missing from the |action_type| key are properly
51 // populated with values from the other keys in the manifest (e.g.
52 // "default_icon" key of |action_type| is populated with "icons" key).
53 void TestPopulateMissingValues(const char* action_type);
54
55 ExtensionActionManager* manager() { return manager_; }
56
57 private:
58 ExtensionRegistry* registry_;
59 int curr_id_;
60 ExtensionActionManager* manager_;
61 scoped_ptr<TestingProfile> profile_;
62 };
63
64 ExtensionActionManagerTest::ExtensionActionManagerTest()
65 : curr_id_(0),
66 profile_(new TestingProfile) {
67 registry_ = ExtensionRegistry::Get(profile_.get());
68 manager_ = ExtensionActionManager::Get(profile_.get());
69 }
70
71 scoped_refptr<Extension> ExtensionActionManagerTest::BuildExtension(
72 DictionaryBuilder& extension_icons,
73 DictionaryBuilder& action,
74 const char* action_type) {
75 std::string id = base::IntToString(curr_id_++);
76 return ExtensionBuilder()
77 .SetManifest(DictionaryBuilder().Set("version", "1")
78 .Set("manifest_version", 2)
79 .Set("icons", extension_icons)
80 .Set(action_type, action)
81 .Set("name",
82 std::string("Test Extension").append(id)))
83 .SetID(id)
84 .Build();
85 }
86
87 bool ExtensionActionManagerTest::TitlesMatch(const Extension& extension,
88 const ExtensionAction& action) {
89 return action.GetTitle(ExtensionAction::kDefaultTabId) == extension.name();
90 }
91
92 bool ExtensionActionManagerTest::IconsMatch(const Extension& extension,
93 int extension_key,
94 const ExtensionAction& action,
95 int action_key) {
96 return action.default_icon()->Get(action_key,
97 ExtensionIconSet::MATCH_EXACTLY) ==
98 IconsInfo::GetIcons(&extension).Get(extension_key,
99 ExtensionIconSet::MATCH_EXACTLY);
100 }
101
102 ExtensionAction* ExtensionActionManagerTest::GetAction(
103 const char* action_type,
104 const Extension& extension) {
105 return (action_type == kBrowserAction) ?
106 manager_->GetBrowserAction(extension) :
107 manager_->GetPageAction(extension);
108 }
109
110 void ExtensionActionManagerTest::TestPopulateMissingValues(
111 const char* action_type) {
112 // Test that the largest icon from the extension's "icons" key is chosen as a
113 // replacement for missing action default_icons keys. "19" should not be
114 // replaced because "38" can always be used in its place.
115 scoped_refptr<Extension> extension = BuildExtension(
116 DictionaryBuilder().Set("48", "icon48.png")
117 .Set("128", "icon128.png"),
118 DictionaryBuilder().Pass(),
119 action_type);
120
121 registry_->AddEnabled(extension);
122 ASSERT_TRUE(extension.get());
123 const ExtensionAction* action = GetAction(action_type, *extension);
124 ASSERT_TRUE(action);
125
126 ASSERT_TRUE(TitlesMatch(*extension, *action));
127 ASSERT_TRUE(IconsMatch(*extension, 128, *action, 38));
128
129 // Test that the action's missing default_icons are not replaced with smaller
130 // icons.
131 extension = BuildExtension(
132 DictionaryBuilder().Set("24", "icon24.png"),
133 DictionaryBuilder().Pass(),
134 action_type);
135
136 registry_->AddEnabled(extension);
137 ASSERT_TRUE(extension.get());
138 action = GetAction(action_type, *extension);
139 ASSERT_TRUE(action);
140
141 ASSERT_TRUE(IconsMatch(*extension, 24, *action, 19));
142 ASSERT_FALSE(IconsMatch(*extension, 24, *action, 38));
143
144 // Test that an action's 19px icon is not replaced if a 38px action icon
145 // exists.
146 extension = BuildExtension(
147 DictionaryBuilder().Set("128", "icon128.png"),
148 DictionaryBuilder().Set("default_icon", DictionaryBuilder()
149 .Set("38", "action38.png")),
150 action_type);
151
152 registry_->AddEnabled(extension);
153 ASSERT_TRUE(extension.get());
154 action = GetAction(action_type, *extension);
155 ASSERT_TRUE(action);
156
157 ASSERT_FALSE(IconsMatch(*extension, 128, *action, 19));
158
159 // Test that existing default_icons and default_title are not replaced.
160 extension = BuildExtension(
161 DictionaryBuilder().Set("128", "icon128.png"),
162 DictionaryBuilder().Set("default_title", "Action!")
163 .Set("default_icon", DictionaryBuilder()
164 .Set("19", "action19.png")
165 .Set("38", "action38.png")),
166 action_type);
167
168 registry_->AddEnabled(extension);
169 ASSERT_TRUE(extension.get());
170 action = GetAction(action_type, *extension);
171 ASSERT_TRUE(action);
172
173 ASSERT_FALSE(TitlesMatch(*extension, *action));
174 ASSERT_FALSE(IconsMatch(*extension, 128, *action, 19));
175 ASSERT_FALSE(IconsMatch(*extension, 128, *action, 38));
176 }
177
178 namespace {
179
180 TEST_F(ExtensionActionManagerTest, PopulateBrowserAction) {
181 TestPopulateMissingValues(kBrowserAction);
182 }
183
184 TEST_F(ExtensionActionManagerTest, PopulatePageAction) {
185 TestPopulateMissingValues(kPageAction);
186 }
187
188 TEST_F(ExtensionActionManagerTest, GetBestFitActionTest) {
189 // Create an extension with page action defaults.
190 scoped_refptr<Extension> extension = BuildExtension(
191 DictionaryBuilder().Set("48", "icon48.png"),
192 DictionaryBuilder().Set("default_title", "Action!")
193 .Set("default_icon", DictionaryBuilder()
194 .Set("38", "action38.png")),
195 kPageAction);
196 ASSERT_TRUE(extension.get());
197
198 // Get a "best fit" browser action for |extension|.
199 scoped_ptr<ExtensionAction> action =
200 manager()->GetBestFitAction(*extension, ActionInfo::TYPE_BROWSER);
201 ASSERT_TRUE(action.get());
202 ASSERT_EQ(action->action_type(), ActionInfo::TYPE_BROWSER);
203
204 // |action|'s title and default icon should match |extension|'s page action's.
205 ASSERT_EQ(action->GetTitle(ExtensionAction::kDefaultTabId), "Action!");
206 ASSERT_EQ(action->default_icon()->Get(38, ExtensionIconSet::MATCH_EXACTLY),
207 "action38.png");
208
209 // Create a new extension without page action defaults.
210 extension = BuildExtension(
211 DictionaryBuilder().Set("48", "icon48.png"),
212 DictionaryBuilder().Pass(),
213 kPageAction);
214 ASSERT_TRUE(extension.get());
215
216 action = manager()->GetBestFitAction(*extension, ActionInfo::TYPE_BROWSER);
217
218 // Now these values match because |extension| does not have page action
219 // defaults.
220 ASSERT_TRUE(TitlesMatch(*extension, *action));
221 ASSERT_TRUE(IconsMatch(*extension, 48, *action, 38));
222 }
223
224 } // namespace
225 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698