rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 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_test_util.h" |
| 6 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Sylvain Defresne | 711ff6b | 2018-10-04 12:33:54 | [diff] [blame] | 9 | #include "base/bind.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 10 | #include "base/run_loop.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_action.h" |
| 12 | #include "chrome/browser/extensions/extension_action_manager.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 13 | #include "chrome/browser/extensions/tab_helper.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 14 | #include "chrome/browser/extensions/test_extension_system.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 15 | #include "chrome/browser/profiles/profile.h" |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 16 | #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| 17 | #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h" |
Scott Violet | 5655874f | 2020-01-14 21:03:21 | [diff] [blame] | 18 | #include "components/sessions/content/session_tab_helper.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 19 | #include "content/public/browser/web_contents.h" |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 20 | #include "extensions/browser/extension_registry.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 21 | #include "extensions/common/extension.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 22 | |
| 23 | namespace extensions { |
| 24 | namespace extension_action_test_util { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | size_t GetPageActionCount(content::WebContents* web_contents, |
| 29 | bool only_count_visible) { |
| 30 | DCHECK(web_contents); |
| 31 | size_t count = 0u; |
Scott Violet | 5655874f | 2020-01-14 21:03:21 | [diff] [blame] | 32 | SessionID tab_id = sessions::SessionTabHelper::IdForTab(web_contents); |
rdevlin.cronin | 6d71462 | 2017-04-11 00:50:28 | [diff] [blame] | 33 | Profile* profile = |
| 34 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 35 | ToolbarActionsModel* toolbar_model = ToolbarActionsModel::Get(profile); |
Takumi Fujimoto | b36836c | 2019-02-28 04:41:55 | [diff] [blame] | 36 | const std::vector<ToolbarActionsModel::ActionId>& toolbar_action_ids = |
| 37 | toolbar_model->action_ids(); |
rdevlin.cronin | 6d71462 | 2017-04-11 00:50:28 | [diff] [blame] | 38 | ExtensionActionManager* action_manager = |
| 39 | ExtensionActionManager::Get(web_contents->GetBrowserContext()); |
| 40 | const ExtensionSet& enabled_extensions = |
| 41 | ExtensionRegistry::Get(profile)->enabled_extensions(); |
Takumi Fujimoto | b36836c | 2019-02-28 04:41:55 | [diff] [blame] | 42 | for (const ToolbarActionsModel::ActionId& action_id : toolbar_action_ids) { |
| 43 | const Extension* extension = enabled_extensions.GetByID(action_id); |
| 44 | ExtensionAction* extension_action = |
Devlin Cronin | c7002c2 | 2019-05-15 00:46:16 | [diff] [blame] | 45 | action_manager->GetExtensionAction(*extension); |
Takumi Fujimoto | b36836c | 2019-02-28 04:41:55 | [diff] [blame] | 46 | if (extension_action && |
Devlin Cronin | c7002c2 | 2019-05-15 00:46:16 | [diff] [blame] | 47 | extension_action->action_type() == ActionInfo::TYPE_PAGE && |
Takumi Fujimoto | b36836c | 2019-02-28 04:41:55 | [diff] [blame] | 48 | (!only_count_visible || extension_action->GetIsVisible(tab_id.id()))) { |
| 49 | ++count; |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 50 | } |
| 51 | } |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 52 | return count; |
| 53 | } |
| 54 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 55 | // Creates a new ToolbarActionsModel for the given |context|. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 56 | std::unique_ptr<KeyedService> BuildToolbarModel( |
| 57 | content::BrowserContext* context) { |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 58 | return std::make_unique<ToolbarActionsModel>( |
ricea | 91d6fc12 | 2016-08-30 08:47:14 | [diff] [blame] | 59 | Profile::FromBrowserContext(context), |
| 60 | extensions::ExtensionPrefs::Get(context)); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 61 | } |
| 62 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 63 | // Creates a new ToolbarActionsModel for the given profile, optionally |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 64 | // triggering the extension system's ready signal. |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 65 | ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile, |
| 66 | bool wait_for_ready) { |
| 67 | ToolbarActionsModel* model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 68 | 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). |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 73 | ToolbarActionsModelFactory::GetInstance()->SetTestingFactory( |
Sylvain Defresne | 711ff6b | 2018-10-04 12:33:54 | [diff] [blame] | 74 | profile, base::BindRepeating(&BuildToolbarModel)); |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 75 | model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 76 | 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.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 92 | } // namespace |
| 93 | |
| 94 | size_t GetVisiblePageActionCount(content::WebContents* web_contents) { |
| 95 | return GetPageActionCount(web_contents, true); |
| 96 | } |
| 97 | |
| 98 | size_t GetTotalPageActionCount(content::WebContents* web_contents) { |
| 99 | return GetPageActionCount(web_contents, false); |
| 100 | } |
| 101 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 102 | ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) { |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 103 | return CreateToolbarModelImpl(profile, true); |
| 104 | } |
| 105 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 106 | ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady( |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 107 | Profile* profile) { |
| 108 | return CreateToolbarModelImpl(profile, false); |
| 109 | } |
| 110 | |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 111 | } // namespace extension_action_test_util |
| 112 | } // namespace extensions |