blob: 9ce1a10093747d11c8d83f81cafc3af7081257e8 [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
Sylvain Defresne711ff6b2018-10-04 12:33:549#include "base/bind.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/tab_helper.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5514#include "chrome/browser/extensions/test_extension_system.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2315#include "chrome/browser/profiles/profile.h"
apaciblef9cfc4d2015-08-18 05:14:1416#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
17#include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h"
Scott Violet5655874f2020-01-14 21:03:2118#include "components/sessions/content/session_tab_helper.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2319#include "content/public/browser/web_contents.h"
apaciblef9cfc4d2015-08-18 05:14:1420#include "extensions/browser/extension_registry.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2321#include "extensions/common/extension.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2322
23namespace extensions {
24namespace extension_action_test_util {
25
26namespace {
27
28size_t GetPageActionCount(content::WebContents* web_contents,
29 bool only_count_visible) {
30 DCHECK(web_contents);
31 size_t count = 0u;
Scott Violet5655874f2020-01-14 21:03:2132 SessionID tab_id = sessions::SessionTabHelper::IdForTab(web_contents);
rdevlin.cronin6d714622017-04-11 00:50:2833 Profile* profile =
34 Profile::FromBrowserContext(web_contents->GetBrowserContext());
35 ToolbarActionsModel* toolbar_model = ToolbarActionsModel::Get(profile);
Takumi Fujimotob36836c2019-02-28 04:41:5536 const std::vector<ToolbarActionsModel::ActionId>& toolbar_action_ids =
37 toolbar_model->action_ids();
rdevlin.cronin6d714622017-04-11 00:50:2838 ExtensionActionManager* action_manager =
39 ExtensionActionManager::Get(web_contents->GetBrowserContext());
40 const ExtensionSet& enabled_extensions =
41 ExtensionRegistry::Get(profile)->enabled_extensions();
Takumi Fujimotob36836c2019-02-28 04:41:5542 for (const ToolbarActionsModel::ActionId& action_id : toolbar_action_ids) {
43 const Extension* extension = enabled_extensions.GetByID(action_id);
44 ExtensionAction* extension_action =
Devlin Croninc7002c22019-05-15 00:46:1645 action_manager->GetExtensionAction(*extension);
Takumi Fujimotob36836c2019-02-28 04:41:5546 if (extension_action &&
Devlin Croninc7002c22019-05-15 00:46:1647 extension_action->action_type() == ActionInfo::TYPE_PAGE &&
Takumi Fujimotob36836c2019-02-28 04:41:5548 (!only_count_visible || extension_action->GetIsVisible(tab_id.id()))) {
49 ++count;
rdevlin.cronin6e7e5edc2014-08-29 16:23:2350 }
51 }
rdevlin.cronin6e7e5edc2014-08-29 16:23:2352 return count;
53}
54
apaciblef9cfc4d2015-08-18 05:14:1455// Creates a new ToolbarActionsModel for the given |context|.
dchengc963c7142016-04-08 03:55:2256std::unique_ptr<KeyedService> BuildToolbarModel(
57 content::BrowserContext* context) {
Jinho Bangb5216cec2018-01-17 19:43:1158 return std::make_unique<ToolbarActionsModel>(
ricea91d6fc122016-08-30 08:47:1459 Profile::FromBrowserContext(context),
60 extensions::ExtensionPrefs::Get(context));
rdevlin.cronind2d87abc2014-12-01 19:23:5561}
62
apaciblef9cfc4d2015-08-18 05:14:1463// Creates a new ToolbarActionsModel for the given profile, optionally
rdevlin.cronind2d87abc2014-12-01 19:23:5564// triggering the extension system's ready signal.
apaciblef9cfc4d2015-08-18 05:14:1465ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile,
66 bool wait_for_ready) {
67 ToolbarActionsModel* model = ToolbarActionsModel::Get(profile);
rdevlin.cronind2d87abc2014-12-01 19:23:5568 if (model)
69 return model;
70
71 // No existing model means it's a new profile (since we, by default, don't
72 // create the ToolbarModel in testing).
apaciblef9cfc4d2015-08-18 05:14:1473 ToolbarActionsModelFactory::GetInstance()->SetTestingFactory(
Sylvain Defresne711ff6b2018-10-04 12:33:5474 profile, base::BindRepeating(&BuildToolbarModel));
apaciblef9cfc4d2015-08-18 05:14:1475 model = ToolbarActionsModel::Get(profile);
rdevlin.cronind2d87abc2014-12-01 19:23:5576 if (wait_for_ready) {
77 // Fake the extension system ready signal.
78 // HACK ALERT! In production, the ready task on ExtensionSystem (and most
79 // everything else on it, too) is shared between incognito and normal
80 // profiles, but a TestExtensionSystem doesn't have the concept of "shared".
81 // Because of this, we have to set any new profile's TestExtensionSystem's
82 // ready task, too.
83 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))->
84 SetReady();
85 // Run tasks posted to TestExtensionSystem.
86 base::RunLoop().RunUntilIdle();
87 }
88
89 return model;
90}
91
rdevlin.cronin6e7e5edc2014-08-29 16:23:2392} // namespace
93
94size_t GetVisiblePageActionCount(content::WebContents* web_contents) {
95 return GetPageActionCount(web_contents, true);
96}
97
98size_t GetTotalPageActionCount(content::WebContents* web_contents) {
99 return GetPageActionCount(web_contents, false);
100}
101
apaciblef9cfc4d2015-08-18 05:14:14102ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) {
rdevlin.cronind2d87abc2014-12-01 19:23:55103 return CreateToolbarModelImpl(profile, true);
104}
105
apaciblef9cfc4d2015-08-18 05:14:14106ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady(
rdevlin.cronind2d87abc2014-12-01 19:23:55107 Profile* profile) {
108 return CreateToolbarModelImpl(profile, false);
109}
110
rdevlin.cronin6e7e5edc2014-08-29 16:23:23111} // namespace extension_action_test_util
112} // namespace extensions