Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/test/base/testing_profile.h" | |
| 9 #include "extensions/common/extension_builder.h" | |
| 10 #include "extensions/common/manifest_handlers/icons_handler.h" | |
| 11 #include "extensions/common/value_builder.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace extensions { | |
| 15 | |
| 16 class ExtensionActionManagerTest : public testing::Test { | |
| 17 public: | |
| 18 ExtensionActionManagerTest(); | |
|
not at google - send to devlin
2014/08/05 21:20:41
everything from here down can be protected: I thin
gpdavis
2014/08/05 21:41:46
Done.
| |
| 19 // Build an extension, populating "name" and "icons" keys for the extension, | |
| 20 // and "default_title" and "default_icon" keys for the extension's | |
| 21 // |action_type| key. | |
| 22 scoped_refptr<Extension> BuildExtension(const char* extension_name, | |
| 23 const char* extension_icon, | |
| 24 const char* action_type, | |
| 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 // Tests that values that are missing from the |action_type| key are | |
| 35 // populated with values from the other keys in the manifest (e.g. | |
| 36 // "default_icon" key of |action_type| is populated with "icons" key). | |
| 37 // Uses |getter| to retrieve the action. | |
| 38 void TestPopulateMissingValues( | |
| 39 const base::Callback<ExtensionAction*(const Extension&)>& getter, | |
| 40 const char* action_type); | |
| 41 | |
| 42 ExtensionActionManager* manager() { return manager_; } | |
| 43 | |
| 44 private: | |
| 45 ExtensionActionManager* manager_; | |
| 46 scoped_ptr<TestingProfile> profile_; | |
| 47 }; | |
| 48 | |
| 49 ExtensionActionManagerTest::ExtensionActionManagerTest() | |
| 50 : profile_(new TestingProfile) { | |
| 51 manager_ = ExtensionActionManager::Get(profile_.get()); | |
| 52 } | |
| 53 | |
| 54 scoped_refptr<Extension> ExtensionActionManagerTest::BuildExtension( | |
| 55 const char* extension_name, | |
| 56 const char* extension_icon, | |
| 57 const char* action_type, | |
| 58 const char* action_title, | |
| 59 const char* action_icon) { | |
| 60 DictionaryBuilder action; | |
| 61 if (action_title) | |
| 62 action.Set("default_title", action_title); | |
| 63 if (action_icon) { | |
| 64 action.Set("default_icon", DictionaryBuilder() | |
| 65 .Set("38", action_icon)); | |
| 66 } | |
| 67 | |
| 68 DictionaryBuilder manifest; | |
| 69 manifest.Set("version", "1") | |
| 70 .Set("manifest_version", 2) | |
| 71 .Set(action_type, action); | |
| 72 if (extension_name) | |
| 73 manifest.Set("name", extension_name); | |
| 74 if (extension_icon) { | |
| 75 manifest.Set("icons", DictionaryBuilder() | |
| 76 .Set("48", extension_icon)); | |
| 77 } | |
| 78 return ExtensionBuilder().SetManifest(manifest).Build(); | |
| 79 } | |
| 80 | |
| 81 bool ExtensionActionManagerTest::TitlesMatch(const Extension& extension, | |
| 82 const ExtensionAction& action) { | |
| 83 return action.GetTitle(ExtensionAction::kDefaultTabId) == extension.name(); | |
| 84 } | |
| 85 | |
| 86 bool ExtensionActionManagerTest::IconsMatch(const Extension& extension, | |
| 87 const ExtensionAction& action) { | |
| 88 return action.default_icon()->Get(38, ExtensionIconSet::MATCH_EXACTLY) == | |
| 89 IconsInfo::GetIcons(&extension).Get(48, ExtensionIconSet::MATCH_EXACTLY); | |
| 90 } | |
| 91 | |
|
not at google - send to devlin
2014/08/05 21:20:40
somewhere in here we should also test having multi
gpdavis
2014/08/05 21:41:46
Currently, I have it grabbing the largest icon it
not at google - send to devlin
2014/08/05 21:45:31
Yep, and we should test that it does pick the larg
gpdavis
2014/08/06 00:01:40
So we prefer the default puzzle piece icon to a 16
| |
| 92 void ExtensionActionManagerTest::TestPopulateMissingValues( | |
| 93 const base::Callback<ExtensionAction*(const Extension&)>& getter, | |
| 94 const char* action_type) { | |
| 95 // Create an extension without action defaults. | |
| 96 scoped_refptr<Extension> extension = BuildExtension("Test Extension 1", | |
| 97 "test_icon.png", | |
| 98 action_type, | |
| 99 NULL, | |
| 100 NULL); | |
| 101 ASSERT_TRUE(extension.get()); | |
|
not at google - send to devlin
2014/08/05 21:20:40
it's better if you make this assertion inside the
gpdavis
2014/08/05 21:41:46
Done.
gpdavis
2014/08/06 00:01:40
Shouldn't have given you a premature done here-- I
| |
| 102 const ExtensionAction* action = getter.Run(*extension); | |
| 103 ASSERT_TRUE(action); | |
| 104 | |
| 105 // Ensure that |action|'s title and default icon match |extension|'s name and | |
| 106 // icon. | |
| 107 ASSERT_TRUE(TitlesMatch(*extension, *action)); | |
| 108 ASSERT_TRUE(IconsMatch(*extension, *action)); | |
| 109 | |
| 110 // Create a new extension with action defaults. | |
| 111 extension = BuildExtension("Test Extension 2", | |
| 112 "test_icon.png", | |
| 113 action_type, | |
| 114 "Test Action", | |
| 115 "test_action_icon.png"); | |
| 116 ASSERT_TRUE(extension); | |
| 117 action = getter.Run(*extension); | |
| 118 ASSERT_TRUE(action); | |
| 119 | |
| 120 // The titles and icons should no longer match since the page action has | |
| 121 // explicitly set default values. | |
| 122 ASSERT_FALSE(TitlesMatch(*extension, *action)); | |
| 123 ASSERT_FALSE(IconsMatch(*extension, *action)); | |
| 124 } | |
| 125 | |
| 126 namespace { | |
| 127 | |
| 128 TEST_F(ExtensionActionManagerTest, PopulatePageAction) { | |
| 129 TestPopulateMissingValues(base::Bind( | |
| 130 &ExtensionActionManager::GetPageAction, | |
| 131 base::Unretained(manager())), | |
| 132 "page_action"); | |
| 133 } | |
| 134 | |
| 135 TEST_F(ExtensionActionManagerTest, PopulateBrowserAction) { | |
| 136 TestPopulateMissingValues(base::Bind( | |
| 137 &ExtensionActionManager::GetBrowserAction, | |
| 138 base::Unretained(manager())), | |
| 139 "browser_action"); | |
| 140 } | |
| 141 | |
| 142 | |
| 143 TEST_F(ExtensionActionManagerTest, GetBestFitActionTest) { | |
| 144 // Create an extension with page action defaults. | |
| 145 scoped_refptr<Extension> extension = BuildExtension("Test Extension", | |
| 146 "test_icon.png", | |
| 147 "page_action", | |
| 148 "Test Action", | |
| 149 "test_action_icon.png"); | |
| 150 ASSERT_TRUE(extension.get()); | |
| 151 | |
| 152 // Get a "best fit" browser action for |extension|. | |
| 153 scoped_ptr<ExtensionAction> action = | |
| 154 manager()->GetBestFitAction(*extension, ActionInfo::TYPE_BROWSER); | |
| 155 ASSERT_TRUE(action.get()); | |
| 156 | |
| 157 // |action|'s title and default icon should not match |extension|'s because | |
| 158 // the data should be pulled from |extension|'s page action manifest key. | |
| 159 ASSERT_FALSE(TitlesMatch(*extension, *action)); | |
| 160 ASSERT_FALSE(IconsMatch(*extension, *action)); | |
| 161 | |
|
not at google - send to devlin
2014/08/05 21:20:41
assert that it matches the page action?
gpdavis
2014/08/05 21:41:46
Is this a documentation suggestion, or are you sug
| |
| 162 // Create a new extension without page action defaults. | |
| 163 extension = BuildExtension("Test Extension 2", | |
| 164 "test_icon.png", | |
| 165 "page_action", | |
| 166 NULL, | |
| 167 NULL); | |
| 168 ASSERT_TRUE(extension); | |
| 169 action = manager()->GetBestFitAction(*extension, ActionInfo::TYPE_BROWSER); | |
| 170 | |
| 171 // Now these values match because |extension| does not have page action | |
| 172 // defaults. | |
| 173 ASSERT_TRUE(TitlesMatch(*extension, *action)); | |
| 174 ASSERT_TRUE(IconsMatch(*extension, *action)); | |
|
not at google - send to devlin
2014/08/05 21:20:40
assert that it matches the page action?
| |
| 175 } | |
| 176 | |
| 177 } // namespace | |
| 178 } // namespace extensions | |
| OLD | NEW |