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/location_bar_controller.h" |
| 14 | #include "chrome/browser/extensions/tab_helper.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 15 | #include "chrome/browser/extensions/test_extension_system.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 16 | #include "chrome/browser/profiles/profile.h" |
| 17 | #include "chrome/browser/sessions/session_tab_helper.h" |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 18 | #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| 19 | #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 20 | #include "components/crx_file/id_util.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 21 | #include "content/public/browser/web_contents.h" |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 22 | #include "extensions/browser/extension_registry.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 23 | #include "extensions/common/extension.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 24 | #include "extensions/common/extension_builder.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 25 | #include "extensions/common/feature_switch.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 26 | #include "extensions/common/manifest_constants.h" |
| 27 | #include "extensions/common/value_builder.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 28 | |
| 29 | namespace extensions { |
| 30 | namespace extension_action_test_util { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | size_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 |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 41 | // ToolbarActionsModel), depending on whether or not the extension action |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 42 | // 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 { |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 57 | 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.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 62 | ExtensionActionManager* action_manager = |
| 63 | ExtensionActionManager::Get(web_contents->GetBrowserContext()); |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 64 | 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.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | return count; |
| 79 | } |
| 80 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 81 | // Creates a new ToolbarActionsModel for the given |context|. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame^] | 82 | std::unique_ptr<KeyedService> BuildToolbarModel( |
| 83 | content::BrowserContext* context) { |
| 84 | return base::WrapUnique( |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 85 | new ToolbarActionsModel(Profile::FromBrowserContext(context), |
| 86 | extensions::ExtensionPrefs::Get(context))); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 87 | } |
| 88 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 89 | // Creates a new ToolbarActionsModel for the given profile, optionally |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 90 | // triggering the extension system's ready signal. |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 91 | ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile, |
| 92 | bool wait_for_ready) { |
| 93 | ToolbarActionsModel* model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 94 | 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). |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 99 | ToolbarActionsModelFactory::GetInstance()->SetTestingFactory( |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 100 | profile, &BuildToolbarModel); |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 101 | model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 102 | 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.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 118 | } // namespace |
| 119 | |
| 120 | size_t GetVisiblePageActionCount(content::WebContents* web_contents) { |
| 121 | return GetPageActionCount(web_contents, true); |
| 122 | } |
| 123 | |
| 124 | size_t GetTotalPageActionCount(content::WebContents* web_contents) { |
| 125 | return GetPageActionCount(web_contents, false); |
| 126 | } |
| 127 | |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 128 | scoped_refptr<const Extension> CreateActionExtension(const std::string& name, |
| 129 | ActionType action_type) { |
rdevlin.cronin | 70fc605 | 2015-04-15 17:49:06 | [diff] [blame] | 130 | return CreateActionExtension(name, action_type, Manifest::INTERNAL); |
| 131 | } |
| 132 | |
| 133 | scoped_refptr<const Extension> CreateActionExtension( |
| 134 | const std::string& name, |
| 135 | ActionType action_type, |
| 136 | Manifest::Location location) { |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 137 | 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) |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 156 | manifest.Set(action_key, DictionaryBuilder().Build()); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 157 | |
limasdf | 21d67e6 | 2015-12-19 12:04:49 | [diff] [blame] | 158 | return ExtensionBuilder() |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 159 | .SetManifest(manifest.Build()) |
limasdf | 21d67e6 | 2015-12-19 12:04:49 | [diff] [blame] | 160 | .SetID(crx_file::id_util::GenerateId(name)) |
| 161 | .SetLocation(location) |
| 162 | .Build(); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 163 | } |
| 164 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 165 | ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) { |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 166 | return CreateToolbarModelImpl(profile, true); |
| 167 | } |
| 168 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 169 | ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady( |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 170 | Profile* profile) { |
| 171 | return CreateToolbarModelImpl(profile, false); |
| 172 | } |
| 173 | |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 174 | } // namespace extension_action_test_util |
| 175 | } // namespace extensions |