blob: b5104ff2b0b58df5fdf3c875bc64350aa7d846b4 [file] [log] [blame]
rdevlin.cronin6e7e5edc2014-08-29 16:23:231// 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_test_util.h"
6
dchengc963c7142016-04-08 03:55:227#include <memory>
8
9#include "base/memory/ptr_util.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5510#include "base/run_loop.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2311#include "chrome/browser/extensions/extension_action.h"
12#include "chrome/browser/extensions/extension_action_manager.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2313#include "chrome/browser/extensions/location_bar_controller.h"
14#include "chrome/browser/extensions/tab_helper.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5515#include "chrome/browser/extensions/test_extension_system.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2316#include "chrome/browser/profiles/profile.h"
17#include "chrome/browser/sessions/session_tab_helper.h"
apaciblef9cfc4d2015-08-18 05:14:1418#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
19#include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5520#include "components/crx_file/id_util.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2321#include "content/public/browser/web_contents.h"
apaciblef9cfc4d2015-08-18 05:14:1422#include "extensions/browser/extension_registry.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2323#include "extensions/common/extension.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5524#include "extensions/common/extension_builder.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2325#include "extensions/common/feature_switch.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5526#include "extensions/common/manifest_constants.h"
27#include "extensions/common/value_builder.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2328
29namespace extensions {
30namespace extension_action_test_util {
31
32namespace {
33
34size_t GetPageActionCount(content::WebContents* web_contents,
35 bool only_count_visible) {
36 DCHECK(web_contents);
37 size_t count = 0u;
38 int tab_id = SessionTabHelper::IdForTab(web_contents);
39 // Page actions are either stored in the location bar (and provided by the
40 // LocationBarController), or in the main toolbar (and provided by the
apaciblef9cfc4d2015-08-18 05:14:1441 // ToolbarActionsModel), depending on whether or not the extension action
rdevlin.cronin6e7e5edc2014-08-29 16:23:2342 // redesign is enabled.
43 if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) {
44 std::vector<ExtensionAction*> page_actions =
45 TabHelper::FromWebContents(web_contents)->
46 location_bar_controller()->GetCurrentActions();
47 count = page_actions.size();
48 // Trim any invisible page actions, if necessary.
49 if (only_count_visible) {
50 for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin();
51 iter != page_actions.end(); ++iter) {
52 if (!(*iter)->GetIsVisible(tab_id))
53 --count;
54 }
55 }
56 } else {
apaciblef9cfc4d2015-08-18 05:14:1457 Profile* profile =
58 Profile::FromBrowserContext(web_contents->GetBrowserContext());
59 ToolbarActionsModel* toolbar_model = ToolbarActionsModel::Get(profile);
60 const std::vector<ToolbarActionsModel::ToolbarItem>& toolbar_items =
61 toolbar_model->toolbar_items();
rdevlin.cronin6e7e5edc2014-08-29 16:23:2362 ExtensionActionManager* action_manager =
63 ExtensionActionManager::Get(web_contents->GetBrowserContext());
apaciblef9cfc4d2015-08-18 05:14:1464 for (const ToolbarActionsModel::ToolbarItem& item : toolbar_items) {
65 if (item.type == ToolbarActionsModel::EXTENSION_ACTION) {
66 const Extension* extension =
67 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(
68 item.id);
69 ExtensionAction* extension_action =
70 action_manager->GetPageAction(*extension);
71 if (extension_action &&
72 (!only_count_visible || extension_action->GetIsVisible(tab_id)))
73 ++count;
74 }
rdevlin.cronin6e7e5edc2014-08-29 16:23:2375 }
76 }
77
78 return count;
79}
80
apaciblef9cfc4d2015-08-18 05:14:1481// Creates a new ToolbarActionsModel for the given |context|.
dchengc963c7142016-04-08 03:55:2282std::unique_ptr<KeyedService> BuildToolbarModel(
83 content::BrowserContext* context) {
84 return base::WrapUnique(
apaciblef9cfc4d2015-08-18 05:14:1485 new ToolbarActionsModel(Profile::FromBrowserContext(context),
86 extensions::ExtensionPrefs::Get(context)));
rdevlin.cronind2d87abc2014-12-01 19:23:5587}
88
apaciblef9cfc4d2015-08-18 05:14:1489// Creates a new ToolbarActionsModel for the given profile, optionally
rdevlin.cronind2d87abc2014-12-01 19:23:5590// triggering the extension system's ready signal.
apaciblef9cfc4d2015-08-18 05:14:1491ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile,
92 bool wait_for_ready) {
93 ToolbarActionsModel* model = ToolbarActionsModel::Get(profile);
rdevlin.cronind2d87abc2014-12-01 19:23:5594 if (model)
95 return model;
96
97 // No existing model means it's a new profile (since we, by default, don't
98 // create the ToolbarModel in testing).
apaciblef9cfc4d2015-08-18 05:14:1499 ToolbarActionsModelFactory::GetInstance()->SetTestingFactory(
rdevlin.cronind2d87abc2014-12-01 19:23:55100 profile, &BuildToolbarModel);
apaciblef9cfc4d2015-08-18 05:14:14101 model = ToolbarActionsModel::Get(profile);
rdevlin.cronind2d87abc2014-12-01 19:23:55102 if (wait_for_ready) {
103 // Fake the extension system ready signal.
104 // HACK ALERT! In production, the ready task on ExtensionSystem (and most
105 // everything else on it, too) is shared between incognito and normal
106 // profiles, but a TestExtensionSystem doesn't have the concept of "shared".
107 // Because of this, we have to set any new profile's TestExtensionSystem's
108 // ready task, too.
109 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))->
110 SetReady();
111 // Run tasks posted to TestExtensionSystem.
112 base::RunLoop().RunUntilIdle();
113 }
114
115 return model;
116}
117
rdevlin.cronin6e7e5edc2014-08-29 16:23:23118} // namespace
119
120size_t GetVisiblePageActionCount(content::WebContents* web_contents) {
121 return GetPageActionCount(web_contents, true);
122}
123
124size_t GetTotalPageActionCount(content::WebContents* web_contents) {
125 return GetPageActionCount(web_contents, false);
126}
127
rdevlin.cronind2d87abc2014-12-01 19:23:55128scoped_refptr<const Extension> CreateActionExtension(const std::string& name,
129 ActionType action_type) {
rdevlin.cronin70fc6052015-04-15 17:49:06130 return CreateActionExtension(name, action_type, Manifest::INTERNAL);
131}
132
133scoped_refptr<const Extension> CreateActionExtension(
134 const std::string& name,
135 ActionType action_type,
136 Manifest::Location location) {
rdevlin.cronind2d87abc2014-12-01 19:23:55137 DictionaryBuilder manifest;
138 manifest.Set("name", name)
139 .Set("description", "An extension")
140 .Set("manifest_version", 2)
141 .Set("version", "1.0.0");
142
143 const char* action_key = nullptr;
144 switch (action_type) {
145 case NO_ACTION:
146 break;
147 case PAGE_ACTION:
148 action_key = manifest_keys::kPageAction;
149 break;
150 case BROWSER_ACTION:
151 action_key = manifest_keys::kBrowserAction;
152 break;
153 }
154
155 if (action_key)
dcheng794d2bd2016-02-27 03:51:32156 manifest.Set(action_key, DictionaryBuilder().Build());
rdevlin.cronind2d87abc2014-12-01 19:23:55157
limasdf21d67e62015-12-19 12:04:49158 return ExtensionBuilder()
dcheng794d2bd2016-02-27 03:51:32159 .SetManifest(manifest.Build())
limasdf21d67e62015-12-19 12:04:49160 .SetID(crx_file::id_util::GenerateId(name))
161 .SetLocation(location)
162 .Build();
rdevlin.cronind2d87abc2014-12-01 19:23:55163}
164
apaciblef9cfc4d2015-08-18 05:14:14165ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) {
rdevlin.cronind2d87abc2014-12-01 19:23:55166 return CreateToolbarModelImpl(profile, true);
167}
168
apaciblef9cfc4d2015-08-18 05:14:14169ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady(
rdevlin.cronind2d87abc2014-12-01 19:23:55170 Profile* profile) {
171 return CreateToolbarModelImpl(profile, false);
172}
173
rdevlin.cronin6e7e5edc2014-08-29 16:23:23174} // namespace extension_action_test_util
175} // namespace extensions