Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / extension_action_test_util.cc
blob6a6eddcb33d8e4718a795818b3b8e817e2b921a2
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.
5 #include "chrome/browser/extensions/extension_action_test_util.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_action_manager.h"
11 #include "chrome/browser/extensions/extension_toolbar_model.h"
12 #include "chrome/browser/extensions/extension_toolbar_model_factory.h"
13 #include "chrome/browser/extensions/location_bar_controller.h"
14 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/extensions/test_extension_system.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sessions/session_tab_helper.h"
18 #include "components/crx_file/id_util.h"
19 #include "content/public/browser/web_contents.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_builder.h"
22 #include "extensions/common/feature_switch.h"
23 #include "extensions/common/manifest_constants.h"
24 #include "extensions/common/value_builder.h"
26 namespace extensions {
27 namespace extension_action_test_util {
29 namespace {
31 size_t GetPageActionCount(content::WebContents* web_contents,
32 bool only_count_visible) {
33 DCHECK(web_contents);
34 size_t count = 0u;
35 int tab_id = SessionTabHelper::IdForTab(web_contents);
36 // Page actions are either stored in the location bar (and provided by the
37 // LocationBarController), or in the main toolbar (and provided by the
38 // ExtensionToolbarModel), depending on whether or not the extension action
39 // redesign is enabled.
40 if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) {
41 std::vector<ExtensionAction*> page_actions =
42 TabHelper::FromWebContents(web_contents)->
43 location_bar_controller()->GetCurrentActions();
44 count = page_actions.size();
45 // Trim any invisible page actions, if necessary.
46 if (only_count_visible) {
47 for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin();
48 iter != page_actions.end(); ++iter) {
49 if (!(*iter)->GetIsVisible(tab_id))
50 --count;
53 } else {
54 ExtensionToolbarModel* toolbar_model =
55 ExtensionToolbarModel::Get(
56 Profile::FromBrowserContext(web_contents->GetBrowserContext()));
57 const ExtensionList& toolbar_extensions = toolbar_model->toolbar_items();
58 ExtensionActionManager* action_manager =
59 ExtensionActionManager::Get(web_contents->GetBrowserContext());
60 for (ExtensionList::const_iterator iter = toolbar_extensions.begin();
61 iter != toolbar_extensions.end(); ++iter) {
62 ExtensionAction* extension_action = action_manager->GetPageAction(**iter);
63 if (extension_action &&
64 (!only_count_visible || extension_action->GetIsVisible(tab_id)))
65 ++count;
69 return count;
72 // Creates a new ExtensionToolbarModel for the given |context|.
73 scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) {
74 return make_scoped_ptr(new extensions::ExtensionToolbarModel(
75 Profile::FromBrowserContext(context),
76 extensions::ExtensionPrefs::Get(context)));
79 // Creates a new ExtensionToolbarModel for the given profile, optionally
80 // triggering the extension system's ready signal.
81 ExtensionToolbarModel* CreateToolbarModelImpl(Profile* profile,
82 bool wait_for_ready) {
83 ExtensionToolbarModel* model = ExtensionToolbarModel::Get(profile);
84 if (model)
85 return model;
87 // No existing model means it's a new profile (since we, by default, don't
88 // create the ToolbarModel in testing).
89 ExtensionToolbarModelFactory::GetInstance()->SetTestingFactory(
90 profile, &BuildToolbarModel);
91 model = ExtensionToolbarModel::Get(profile);
92 if (wait_for_ready) {
93 // Fake the extension system ready signal.
94 // HACK ALERT! In production, the ready task on ExtensionSystem (and most
95 // everything else on it, too) is shared between incognito and normal
96 // profiles, but a TestExtensionSystem doesn't have the concept of "shared".
97 // Because of this, we have to set any new profile's TestExtensionSystem's
98 // ready task, too.
99 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))->
100 SetReady();
101 // Run tasks posted to TestExtensionSystem.
102 base::RunLoop().RunUntilIdle();
105 return model;
108 } // namespace
110 size_t GetVisiblePageActionCount(content::WebContents* web_contents) {
111 return GetPageActionCount(web_contents, true);
114 size_t GetTotalPageActionCount(content::WebContents* web_contents) {
115 return GetPageActionCount(web_contents, false);
118 scoped_refptr<const Extension> CreateActionExtension(const std::string& name,
119 ActionType action_type) {
120 return CreateActionExtension(name, action_type, Manifest::INTERNAL);
123 scoped_refptr<const Extension> CreateActionExtension(
124 const std::string& name,
125 ActionType action_type,
126 Manifest::Location location) {
127 DictionaryBuilder manifest;
128 manifest.Set("name", name)
129 .Set("description", "An extension")
130 .Set("manifest_version", 2)
131 .Set("version", "1.0.0");
133 const char* action_key = nullptr;
134 switch (action_type) {
135 case NO_ACTION:
136 break;
137 case PAGE_ACTION:
138 action_key = manifest_keys::kPageAction;
139 break;
140 case BROWSER_ACTION:
141 action_key = manifest_keys::kBrowserAction;
142 break;
145 if (action_key)
146 manifest.Set(action_key, DictionaryBuilder().Pass());
148 return ExtensionBuilder().SetManifest(manifest.Pass()).
149 SetID(crx_file::id_util::GenerateId(name)).
150 SetLocation(location).
151 Build();
154 ExtensionToolbarModel* CreateToolbarModelForProfile(Profile* profile) {
155 return CreateToolbarModelImpl(profile, true);
158 ExtensionToolbarModel* CreateToolbarModelForProfileWithoutWaitingForReady(
159 Profile* profile) {
160 return CreateToolbarModelImpl(profile, false);
163 } // namespace extension_action_test_util
164 } // namespace extensions