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

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

Issue 1241063003: Support Component Actions in the toolbar. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix tests and indentation changes. Created 5 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_action_test_util.h" 5 #include "chrome/browser/extensions/extension_action_test_util.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "chrome/browser/extensions/extension_action.h" 9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_action_manager.h" 10 #include "chrome/browser/extensions/extension_action_manager.h"
11 #include "chrome/browser/extensions/extension_toolbar_model.h"
12 #include "chrome/browser/extensions/extension_toolbar_model_factory.h"
13 #include "chrome/browser/extensions/location_bar_controller.h" 11 #include "chrome/browser/extensions/location_bar_controller.h"
14 #include "chrome/browser/extensions/tab_helper.h" 12 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/extensions/test_extension_system.h" 13 #include "chrome/browser/extensions/test_extension_system.h"
16 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sessions/session_tab_helper.h" 15 #include "chrome/browser/sessions/session_tab_helper.h"
16 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
17 #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h"
18 #include "components/crx_file/id_util.h" 18 #include "components/crx_file/id_util.h"
19 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
20 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_builder.h" 22 #include "extensions/common/extension_builder.h"
22 #include "extensions/common/feature_switch.h" 23 #include "extensions/common/feature_switch.h"
23 #include "extensions/common/manifest_constants.h" 24 #include "extensions/common/manifest_constants.h"
24 #include "extensions/common/value_builder.h" 25 #include "extensions/common/value_builder.h"
25 26
26 namespace extensions { 27 namespace extensions {
27 namespace extension_action_test_util { 28 namespace extension_action_test_util {
28 29
29 namespace { 30 namespace {
30 31
31 size_t GetPageActionCount(content::WebContents* web_contents, 32 size_t GetPageActionCount(content::WebContents* web_contents,
32 bool only_count_visible) { 33 bool only_count_visible) {
33 DCHECK(web_contents); 34 DCHECK(web_contents);
34 size_t count = 0u; 35 size_t count = 0u;
35 int tab_id = SessionTabHelper::IdForTab(web_contents); 36 int tab_id = SessionTabHelper::IdForTab(web_contents);
36 // Page actions are either stored in the location bar (and provided by the 37 // Page actions are either stored in the location bar (and provided by the
37 // LocationBarController), or in the main toolbar (and provided by the 38 // LocationBarController), or in the main toolbar (and provided by the
38 // ExtensionToolbarModel), depending on whether or not the extension action 39 // ToolbarActionsModel), depending on whether or not the extension action
39 // redesign is enabled. 40 // redesign is enabled.
40 if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) { 41 if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) {
41 std::vector<ExtensionAction*> page_actions = 42 std::vector<ExtensionAction*> page_actions =
42 TabHelper::FromWebContents(web_contents)-> 43 TabHelper::FromWebContents(web_contents)->
43 location_bar_controller()->GetCurrentActions(); 44 location_bar_controller()->GetCurrentActions();
44 count = page_actions.size(); 45 count = page_actions.size();
45 // Trim any invisible page actions, if necessary. 46 // Trim any invisible page actions, if necessary.
46 if (only_count_visible) { 47 if (only_count_visible) {
47 for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin(); 48 for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin();
48 iter != page_actions.end(); ++iter) { 49 iter != page_actions.end(); ++iter) {
49 if (!(*iter)->GetIsVisible(tab_id)) 50 if (!(*iter)->GetIsVisible(tab_id))
50 --count; 51 --count;
51 } 52 }
52 } 53 }
53 } else { 54 } else {
54 ExtensionToolbarModel* toolbar_model = 55 Profile* profile = Profile::FromBrowserContext(
55 ExtensionToolbarModel::Get( 56 web_contents->GetBrowserContext());
56 Profile::FromBrowserContext(web_contents->GetBrowserContext())); 57 ToolbarActionsModel* toolbar_model = ToolbarActionsModel::Get(profile);
57 const ExtensionList& toolbar_extensions = toolbar_model->toolbar_items(); 58 const ExtensionIdList& toolbar_extensions = toolbar_model->toolbar_items();
58 ExtensionActionManager* action_manager = 59 ExtensionActionManager* action_manager =
59 ExtensionActionManager::Get(web_contents->GetBrowserContext()); 60 ExtensionActionManager::Get(web_contents->GetBrowserContext());
60 for (ExtensionList::const_iterator iter = toolbar_extensions.begin(); 61 for (std::string id : toolbar_extensions) {
Devlin 2015/08/13 21:21:10 nit: const &
apacible 2015/08/15 08:46:40 Done.
61 iter != toolbar_extensions.end(); ++iter) { 62 const Extension* extension =
62 ExtensionAction* extension_action = action_manager->GetPageAction(**iter); 63 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(id);
Devlin 2015/08/13 21:21:10 I can see this causing problems when cast is a com
apacible 2015/08/15 08:46:40 Acknowledged. Added a check for extension action t
64 ExtensionAction* extension_action =
65 action_manager->GetPageAction(*extension);
63 if (extension_action && 66 if (extension_action &&
64 (!only_count_visible || extension_action->GetIsVisible(tab_id))) 67 (!only_count_visible || extension_action->GetIsVisible(tab_id)))
65 ++count; 68 ++count;
66 } 69 }
67 } 70 }
68 71
69 return count; 72 return count;
70 } 73 }
71 74
72 // Creates a new ExtensionToolbarModel for the given |context|. 75 // Creates a new ToolbarActionsModel for the given |context|.
73 scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) { 76 scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) {
74 return make_scoped_ptr(new extensions::ExtensionToolbarModel( 77 return make_scoped_ptr(new ToolbarActionsModel(
75 Profile::FromBrowserContext(context), 78 Profile::FromBrowserContext(context),
76 extensions::ExtensionPrefs::Get(context))); 79 extensions::ExtensionPrefs::Get(context)));
77 } 80 }
78 81
79 // Creates a new ExtensionToolbarModel for the given profile, optionally 82 // Creates a new ToolbarActionsModel for the given profile, optionally
80 // triggering the extension system's ready signal. 83 // triggering the extension system's ready signal.
81 ExtensionToolbarModel* CreateToolbarModelImpl(Profile* profile, 84 ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile,
82 bool wait_for_ready) { 85 bool wait_for_ready) {
83 ExtensionToolbarModel* model = ExtensionToolbarModel::Get(profile); 86 ToolbarActionsModel* model = ToolbarActionsModel::Get(profile);
84 if (model) 87 if (model)
85 return model; 88 return model;
86 89
87 // No existing model means it's a new profile (since we, by default, don't 90 // No existing model means it's a new profile (since we, by default, don't
88 // create the ToolbarModel in testing). 91 // create the ToolbarModel in testing).
89 ExtensionToolbarModelFactory::GetInstance()->SetTestingFactory( 92 ToolbarActionsModelFactory::GetInstance()->SetTestingFactory(
90 profile, &BuildToolbarModel); 93 profile, &BuildToolbarModel);
91 model = ExtensionToolbarModel::Get(profile); 94 model = ToolbarActionsModel::Get(profile);
92 if (wait_for_ready) { 95 if (wait_for_ready) {
93 // Fake the extension system ready signal. 96 // Fake the extension system ready signal.
94 // HACK ALERT! In production, the ready task on ExtensionSystem (and most 97 // HACK ALERT! In production, the ready task on ExtensionSystem (and most
95 // everything else on it, too) is shared between incognito and normal 98 // everything else on it, too) is shared between incognito and normal
96 // profiles, but a TestExtensionSystem doesn't have the concept of "shared". 99 // profiles, but a TestExtensionSystem doesn't have the concept of "shared".
97 // Because of this, we have to set any new profile's TestExtensionSystem's 100 // Because of this, we have to set any new profile's TestExtensionSystem's
98 // ready task, too. 101 // ready task, too.
99 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))-> 102 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))->
100 SetReady(); 103 SetReady();
101 // Run tasks posted to TestExtensionSystem. 104 // Run tasks posted to TestExtensionSystem.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 147
145 if (action_key) 148 if (action_key)
146 manifest.Set(action_key, DictionaryBuilder().Pass()); 149 manifest.Set(action_key, DictionaryBuilder().Pass());
147 150
148 return ExtensionBuilder().SetManifest(manifest.Pass()). 151 return ExtensionBuilder().SetManifest(manifest.Pass()).
149 SetID(crx_file::id_util::GenerateId(name)). 152 SetID(crx_file::id_util::GenerateId(name)).
150 SetLocation(location). 153 SetLocation(location).
151 Build(); 154 Build();
152 } 155 }
153 156
154 ExtensionToolbarModel* CreateToolbarModelForProfile(Profile* profile) { 157 ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) {
155 return CreateToolbarModelImpl(profile, true); 158 return CreateToolbarModelImpl(profile, true);
156 } 159 }
157 160
158 ExtensionToolbarModel* CreateToolbarModelForProfileWithoutWaitingForReady( 161 ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady(
159 Profile* profile) { 162 Profile* profile) {
160 return CreateToolbarModelImpl(profile, false); 163 return CreateToolbarModelImpl(profile, false);
161 } 164 }
162 165
163 } // namespace extension_action_test_util 166 } // namespace extension_action_test_util
164 } // namespace extensions 167 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698