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