blob: ec606192521f7ec7f33a51ee9e55c7182e72a660 [file] [log] [blame]
rdevlin.cronin6e7e5edc2014-08-29 16:23:231// 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
dchengc963c7142016-04-08 03:55:227#include <memory>
8
9#include "base/memory/ptr_util.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5510#include "base/run_loop.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2311#include "chrome/browser/extensions/extension_action.h"
12#include "chrome/browser/extensions/extension_action_manager.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2313#include "chrome/browser/extensions/tab_helper.h"
rdevlin.cronind2d87abc2014-12-01 19:23:5514#include "chrome/browser/extensions/test_extension_system.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2315#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/sessions/session_tab_helper.h"
apaciblef9cfc4d2015-08-18 05:14:1417#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
18#include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2319#include "content/public/browser/web_contents.h"
apaciblef9cfc4d2015-08-18 05:14:1420#include "extensions/browser/extension_registry.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2321#include "extensions/common/extension.h"
rdevlin.cronin6e7e5edc2014-08-29 16:23:2322
23namespace extensions {
24namespace extension_action_test_util {
25
26namespace {
27
28size_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.cronin6d714622017-04-11 00:50:2833 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.cronin6e7e5edc2014-08-29 16:23:2350 }
51 }
52
53 return count;
54}
55
apaciblef9cfc4d2015-08-18 05:14:1456// Creates a new ToolbarActionsModel for the given |context|.
dchengc963c7142016-04-08 03:55:2257std::unique_ptr<KeyedService> BuildToolbarModel(
58 content::BrowserContext* context) {
ricea91d6fc122016-08-30 08:47:1459 return base::MakeUnique<ToolbarActionsModel>(
60 Profile::FromBrowserContext(context),
61 extensions::ExtensionPrefs::Get(context));
rdevlin.cronind2d87abc2014-12-01 19:23:5562}
63
apaciblef9cfc4d2015-08-18 05:14:1464// Creates a new ToolbarActionsModel for the given profile, optionally
rdevlin.cronind2d87abc2014-12-01 19:23:5565// triggering the extension system's ready signal.
apaciblef9cfc4d2015-08-18 05:14:1466ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile,
67 bool wait_for_ready) {
68 ToolbarActionsModel* model = ToolbarActionsModel::Get(profile);
rdevlin.cronind2d87abc2014-12-01 19:23:5569 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).
apaciblef9cfc4d2015-08-18 05:14:1474 ToolbarActionsModelFactory::GetInstance()->SetTestingFactory(
rdevlin.cronind2d87abc2014-12-01 19:23:5575 profile, &BuildToolbarModel);
apaciblef9cfc4d2015-08-18 05:14:1476 model = ToolbarActionsModel::Get(profile);
rdevlin.cronind2d87abc2014-12-01 19:23:5577 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.cronin6e7e5edc2014-08-29 16:23:2393} // namespace
94
95size_t GetVisiblePageActionCount(content::WebContents* web_contents) {
96 return GetPageActionCount(web_contents, true);
97}
98
99size_t GetTotalPageActionCount(content::WebContents* web_contents) {
100 return GetPageActionCount(web_contents, false);
101}
102
apaciblef9cfc4d2015-08-18 05:14:14103ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) {
rdevlin.cronind2d87abc2014-12-01 19:23:55104 return CreateToolbarModelImpl(profile, true);
105}
106
apaciblef9cfc4d2015-08-18 05:14:14107ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady(
rdevlin.cronind2d87abc2014-12-01 19:23:55108 Profile* profile) {
109 return CreateToolbarModelImpl(profile, false);
110}
111
rdevlin.cronin6e7e5edc2014-08-29 16:23:23112} // namespace extension_action_test_util
113} // namespace extensions