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 | |
isherman | 30fa851a | 2015-06-09 23:32:10 | [diff] [blame] | 7 | #include "base/memory/scoped_ptr.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 8 | #include "base/run_loop.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_action.h" |
| 10 | #include "chrome/browser/extensions/extension_action_manager.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 11 | #include "chrome/browser/extensions/location_bar_controller.h" |
| 12 | #include "chrome/browser/extensions/tab_helper.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 13 | #include "chrome/browser/extensions/test_extension_system.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
| 15 | #include "chrome/browser/sessions/session_tab_helper.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" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 18 | #include "components/crx_file/id_util.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 | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 22 | #include "extensions/common/extension_builder.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 23 | #include "extensions/common/feature_switch.h" |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 24 | #include "extensions/common/manifest_constants.h" |
| 25 | #include "extensions/common/value_builder.h" |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 26 | |
| 27 | namespace extensions { |
| 28 | namespace extension_action_test_util { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | size_t GetPageActionCount(content::WebContents* web_contents, |
| 33 | bool only_count_visible) { |
| 34 | DCHECK(web_contents); |
| 35 | size_t count = 0u; |
| 36 | int tab_id = SessionTabHelper::IdForTab(web_contents); |
| 37 | // Page actions are either stored in the location bar (and provided by the |
| 38 | // LocationBarController), or in the main toolbar (and provided by the |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 39 | // ToolbarActionsModel), depending on whether or not the extension action |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 40 | // redesign is enabled. |
| 41 | if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) { |
| 42 | std::vector<ExtensionAction*> page_actions = |
| 43 | TabHelper::FromWebContents(web_contents)-> |
| 44 | location_bar_controller()->GetCurrentActions(); |
| 45 | count = page_actions.size(); |
| 46 | // Trim any invisible page actions, if necessary. |
| 47 | if (only_count_visible) { |
| 48 | for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin(); |
| 49 | iter != page_actions.end(); ++iter) { |
| 50 | if (!(*iter)->GetIsVisible(tab_id)) |
| 51 | --count; |
| 52 | } |
| 53 | } |
| 54 | } else { |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 55 | Profile* profile = |
| 56 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 57 | ToolbarActionsModel* toolbar_model = ToolbarActionsModel::Get(profile); |
| 58 | const std::vector<ToolbarActionsModel::ToolbarItem>& toolbar_items = |
| 59 | toolbar_model->toolbar_items(); |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 60 | ExtensionActionManager* action_manager = |
| 61 | ExtensionActionManager::Get(web_contents->GetBrowserContext()); |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 62 | for (const ToolbarActionsModel::ToolbarItem& item : toolbar_items) { |
| 63 | if (item.type == ToolbarActionsModel::EXTENSION_ACTION) { |
| 64 | const Extension* extension = |
| 65 | ExtensionRegistry::Get(profile)->enabled_extensions().GetByID( |
| 66 | item.id); |
| 67 | ExtensionAction* extension_action = |
| 68 | action_manager->GetPageAction(*extension); |
| 69 | if (extension_action && |
| 70 | (!only_count_visible || extension_action->GetIsVisible(tab_id))) |
| 71 | ++count; |
| 72 | } |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | return count; |
| 77 | } |
| 78 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 79 | // Creates a new ToolbarActionsModel for the given |context|. |
isherman | 30fa851a | 2015-06-09 23:32:10 | [diff] [blame] | 80 | scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) { |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 81 | return make_scoped_ptr( |
| 82 | new ToolbarActionsModel(Profile::FromBrowserContext(context), |
| 83 | extensions::ExtensionPrefs::Get(context))); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 84 | } |
| 85 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 86 | // Creates a new ToolbarActionsModel for the given profile, optionally |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 87 | // triggering the extension system's ready signal. |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 88 | ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile, |
| 89 | bool wait_for_ready) { |
| 90 | ToolbarActionsModel* model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 91 | if (model) |
| 92 | return model; |
| 93 | |
| 94 | // No existing model means it's a new profile (since we, by default, don't |
| 95 | // create the ToolbarModel in testing). |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 96 | ToolbarActionsModelFactory::GetInstance()->SetTestingFactory( |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 97 | profile, &BuildToolbarModel); |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 98 | model = ToolbarActionsModel::Get(profile); |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 99 | if (wait_for_ready) { |
| 100 | // Fake the extension system ready signal. |
| 101 | // HACK ALERT! In production, the ready task on ExtensionSystem (and most |
| 102 | // everything else on it, too) is shared between incognito and normal |
| 103 | // profiles, but a TestExtensionSystem doesn't have the concept of "shared". |
| 104 | // Because of this, we have to set any new profile's TestExtensionSystem's |
| 105 | // ready task, too. |
| 106 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))-> |
| 107 | SetReady(); |
| 108 | // Run tasks posted to TestExtensionSystem. |
| 109 | base::RunLoop().RunUntilIdle(); |
| 110 | } |
| 111 | |
| 112 | return model; |
| 113 | } |
| 114 | |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 115 | } // namespace |
| 116 | |
| 117 | size_t GetVisiblePageActionCount(content::WebContents* web_contents) { |
| 118 | return GetPageActionCount(web_contents, true); |
| 119 | } |
| 120 | |
| 121 | size_t GetTotalPageActionCount(content::WebContents* web_contents) { |
| 122 | return GetPageActionCount(web_contents, false); |
| 123 | } |
| 124 | |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 125 | scoped_refptr<const Extension> CreateActionExtension(const std::string& name, |
| 126 | ActionType action_type) { |
rdevlin.cronin | 70fc605 | 2015-04-15 17:49:06 | [diff] [blame] | 127 | return CreateActionExtension(name, action_type, Manifest::INTERNAL); |
| 128 | } |
| 129 | |
| 130 | scoped_refptr<const Extension> CreateActionExtension( |
| 131 | const std::string& name, |
| 132 | ActionType action_type, |
| 133 | Manifest::Location location) { |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 134 | DictionaryBuilder manifest; |
| 135 | manifest.Set("name", name) |
| 136 | .Set("description", "An extension") |
| 137 | .Set("manifest_version", 2) |
| 138 | .Set("version", "1.0.0"); |
| 139 | |
| 140 | const char* action_key = nullptr; |
| 141 | switch (action_type) { |
| 142 | case NO_ACTION: |
| 143 | break; |
| 144 | case PAGE_ACTION: |
| 145 | action_key = manifest_keys::kPageAction; |
| 146 | break; |
| 147 | case BROWSER_ACTION: |
| 148 | action_key = manifest_keys::kBrowserAction; |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | if (action_key) |
| 153 | manifest.Set(action_key, DictionaryBuilder().Pass()); |
| 154 | |
| 155 | return ExtensionBuilder().SetManifest(manifest.Pass()). |
| 156 | SetID(crx_file::id_util::GenerateId(name)). |
rdevlin.cronin | 70fc605 | 2015-04-15 17:49:06 | [diff] [blame] | 157 | SetLocation(location). |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 158 | Build(); |
| 159 | } |
| 160 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 161 | ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) { |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 162 | return CreateToolbarModelImpl(profile, true); |
| 163 | } |
| 164 | |
apacible | f9cfc4d | 2015-08-18 05:14:14 | [diff] [blame] | 165 | ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady( |
rdevlin.cronin | d2d87abc | 2014-12-01 19:23:55 | [diff] [blame] | 166 | Profile* profile) { |
| 167 | return CreateToolbarModelImpl(profile, false); |
| 168 | } |
| 169 | |
rdevlin.cronin | 6e7e5edc | 2014-08-29 16:23:23 | [diff] [blame] | 170 | } // namespace extension_action_test_util |
| 171 | } // namespace extensions |