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 | |
| 9 | #include "base/memory/ptr_util.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" |
| 16 | #include "chrome/browser/sessions/session_tab_helper.h" |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 17 | #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| 18 | #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.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; |
| 32 | int tab_id = 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); |
| 36 | const std::vector<ToolbarActionsModel::ToolbarItem>& toolbar_items = |
| 37 | toolbar_model->toolbar_items(); |
| 38 | ExtensionActionManager* action_manager = |
| 39 | ExtensionActionManager::Get(web_contents->GetBrowserContext()); |
| 40 | const ExtensionSet& enabled_extensions = |
| 41 | ExtensionRegistry::Get(profile)->enabled_extensions(); |
| 42 | for (const ToolbarActionsModel::ToolbarItem& item : toolbar_items) { |
| 43 | if (item.type == ToolbarActionsModel::EXTENSION_ACTION) { |
| 44 | const Extension* extension = enabled_extensions.GetByID(item.id); |
| 45 | ExtensionAction* extension_action = |
| 46 | action_manager->GetPageAction(*extension); |
| 47 | if (extension_action && |
| 48 | (!only_count_visible || extension_action->GetIsVisible(tab_id))) |
| 49 | ++count; |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | return count; |
| 54 | } |
| 55 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 56 | // Creates a new ToolbarActionsModel for the given |context|. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 57 | std::unique_ptr<KeyedService> BuildToolbarModel( |
| 58 | content::BrowserContext* context) { |
ricea | 91d6fc12 | 2016-08-30 08:47:14 | [diff] [blame] | 59 | return base::MakeUnique<ToolbarActionsModel>( |
| 60 | Profile::FromBrowserContext(context), |
| 61 | extensions::ExtensionPrefs::Get(context)); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 62 | } |
| 63 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 64 | // Creates a new ToolbarActionsModel for the given profile, optionally |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 65 | // triggering the extension system's ready signal. |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 66 | ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile, |
| 67 | bool wait_for_ready) { |
| 68 | ToolbarActionsModel* model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 69 | if (model) |
| 70 | return model; |
| 71 | |
| 72 | // No existing model means it's a new profile (since we, by default, don't |
| 73 | // create the ToolbarModel in testing). |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 74 | ToolbarActionsModelFactory::GetInstance()->SetTestingFactory( |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 75 | profile, &BuildToolbarModel); |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 76 | model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 77 | if (wait_for_ready) { |
| 78 | // Fake the extension system ready signal. |
| 79 | // HACK ALERT! In production, the ready task on ExtensionSystem (and most |
| 80 | // everything else on it, too) is shared between incognito and normal |
| 81 | // profiles, but a TestExtensionSystem doesn't have the concept of "shared". |
| 82 | // Because of this, we have to set any new profile's TestExtensionSystem's |
| 83 | // ready task, too. |
| 84 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))-> |
| 85 | SetReady(); |
| 86 | // Run tasks posted to TestExtensionSystem. |
| 87 | base::RunLoop().RunUntilIdle(); |
| 88 | } |
| 89 | |
| 90 | return model; |
| 91 | } |
| 92 | |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 93 | } // namespace |
| 94 | |
| 95 | size_t GetVisiblePageActionCount(content::WebContents* web_contents) { |
| 96 | return GetPageActionCount(web_contents, true); |
| 97 | } |
| 98 | |
| 99 | size_t GetTotalPageActionCount(content::WebContents* web_contents) { |
| 100 | return GetPageActionCount(web_contents, false); |
| 101 | } |
| 102 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 103 | ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) { |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 104 | return CreateToolbarModelImpl(profile, true); |
| 105 | } |
| 106 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 107 | ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady( |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 108 | Profile* profile) { |
| 109 | return CreateToolbarModelImpl(profile, false); |
| 110 | } |
| 111 | |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 112 | } // namespace extension_action_test_util |
| 113 | } // namespace extensions |