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

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: Added tests, modified GetBestFitAction 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 "chrome/browser/extensions/extension_action.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_service_test_base.h"
10 #include "chrome/test/base/testing_profile.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 class ExtensionActionManagerTest : public ExtensionServiceTestBase {
19 public:
20 // Build an extension, populating "name" and "icons" keys for the extension,
21 // and "default_title" and "default_icon" keys for the extension's page
22 // action.
23 scoped_refptr<Extension> BuildExtension(const char* extension_name,
24 const char* extension_icon,
25 const char* action_title,
26 const char* action_icon);
27
28 // Returns true if |action|'s title matches |extension|'s name.
29 bool TitlesMatch(const Extension& extension, const ExtensionAction& action);
30
31 // Returns true if |action|'s default icon path matches |extension|'s.
32 bool IconsMatch(const Extension& extension, const ExtensionAction& action);
33 };
34
35 scoped_refptr<Extension> ExtensionActionManagerTest::BuildExtension(
36 const char* extension_name,
37 const char* extension_icon,
38 const char* action_title,
39 const char* action_icon) {
40 DictionaryBuilder action;
41 if (action_title)
42 action.Set("default_title", action_title);
43 if (action_icon) {
44 action.Set("default_icon", DictionaryBuilder()
45 .Set("38", action_icon));
46 }
47
48 DictionaryBuilder manifest;
49 manifest.Set("version", "1")
50 .Set("manifest_version", 2)
51 .Set("page_action", action);
52 if (extension_name)
53 manifest.Set("name", extension_name);
54 else
not at google - send to devlin 2014/08/05 00:40:09 why this? seems odd, and you never pass in NULL an
gpdavis 2014/08/05 18:44:15 Well, I figured I'd add a default name because it'
55 manifest.Set("name", "Test Extension");
56 if (extension_icon) {
57 manifest.Set("icons", DictionaryBuilder()
58 .Set("48", extension_icon));
59 }
60
61 return ExtensionBuilder().SetManifest(manifest).Build();
62 }
63
64 bool ExtensionActionManagerTest::TitlesMatch(const Extension& extension,
65 const ExtensionAction& action) {
66 return action.GetTitle(ExtensionAction::kDefaultTabId) == extension.name();
67 }
68
69 bool ExtensionActionManagerTest::IconsMatch(const Extension& extension,
70 const ExtensionAction& action) {
71 return action.default_icon()->Get(38, ExtensionIconSet::MATCH_EXACTLY) ==
72 IconsInfo::GetIcons(&extension).Get(48, ExtensionIconSet::MATCH_EXACTLY);
73 }
74
75 namespace {
76
77 TEST_F(ExtensionActionManagerTest, PopulateMissingValuesTest) {
78 InitializeEmptyExtensionService();
not at google - send to devlin 2014/08/05 00:40:09 ok let's fix this. there's no good reason for Exte
gpdavis 2014/08/05 18:44:15 I already changed the code you're referring to, si
79 ExtensionActionManager* manager = ExtensionActionManager::Get(profile_.get());
80 ASSERT_TRUE(manager);
81
82 // Create an extension without page action defaults.
83 scoped_refptr<Extension> extension = BuildExtension("Test Extension 1",
84 "test_icon.png",
85 NULL,
86 NULL);
87 ASSERT_TRUE(extension.get());
88 service_->AddExtension(extension.get());
89
90 const ExtensionAction* action = manager->GetPageAction(*extension);
not at google - send to devlin 2014/08/05 00:40:09 and it would be nice to test both page and browser
gpdavis 2014/08/05 18:44:15 I'm having trouble getting the callback to work.
not at google - send to devlin 2014/08/05 18:48:53 are you using base::Unretained()?
gpdavis 2014/08/05 18:50:57 Yep. I was doing it just as you said, except that
gpdavis 2014/08/05 19:07:59 Ah, I got it. GetPageAction doesn't return a cons
gpdavis 2014/08/05 19:33:50 Double nevermind. Sorry for the repeated comments
91 ASSERT_TRUE(action);
92
93 // Ensure that |action|'s title and default icon match |extension|'s name and
94 // icon.
95 ASSERT_TRUE(TitlesMatch(*extension, *action));
96 ASSERT_TRUE(IconsMatch(*extension, *action));
97
98 // Create a new extension with page action defaults.
99 extension = BuildExtension("Test Extension 2",
100 "Test Action",
101 "test_icon.png",
102 "test_action_icon.png");
103 ASSERT_TRUE(extension);
104 service_->AddExtension(extension.get());
105
106 action = manager->GetPageAction(*extension);
107 ASSERT_TRUE(action);
108
109 // The titles and icons should no longer match since the page action has
110 // explicitly set default values.
111 ASSERT_FALSE(TitlesMatch(*extension, *action));
112 ASSERT_FALSE(IconsMatch(*extension, *action));
113 }
114
115 TEST_F(ExtensionActionManagerTest, GetBestFitActionTest) {
116 InitializeEmptyExtensionService();
117 ExtensionActionManager* manager = ExtensionActionManager::Get(profile_.get());
118 ASSERT_TRUE(manager);
119
120 // Create an extension with page action defaults.
121 scoped_refptr<Extension> extension = BuildExtension("Test Extension",
122 "test_icon.png",
123 "Test Action",
124 "test_action_icon.png");
125 ASSERT_TRUE(extension.get());
126 service_->AddExtension(extension.get());
127
128 // Get a "best fit" browser action for |extension|.
129 scoped_ptr<ExtensionAction> action =
130 manager->GetBestFitAction(*extension, ActionInfo::TYPE_BROWSER);
131 ASSERT_TRUE(action.get());
132
133 // |action|'s title and default icon should not match |extension|'s because
134 // the data should be pulled from |extension|'s page action manifest key.
135 ASSERT_FALSE(TitlesMatch(*extension, *action));
136 ASSERT_FALSE(IconsMatch(*extension, *action));
137
138 // Create a new extension without page action defaults.
139 extension = BuildExtension("Test Extension 2",
140 "Test Action",
141 NULL,
142 NULL);
143 ASSERT_TRUE(extension);
144 service_->AddExtension(extension.get());
145
146 action = manager->GetBestFitAction(*extension, ActionInfo::TYPE_BROWSER);
147
148 // Now these values match because |extension| does not have page action
149 // defaults.
150 ASSERT_TRUE(TitlesMatch(*extension, *action));
151 ASSERT_TRUE(IconsMatch(*extension, *action));
152 }
153
154 } // namespace
155 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698