summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/script_badge_controller_unittest.cc
blob: 9600018fca86ef8e199cb046937e801c2ef6e6fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/stringprintf.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/script_badge_controller.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_builder.h"
#include "chrome/common/extensions/feature_switch.h"
#include "chrome/common/extensions/features/feature.h"
#include "chrome/common/extensions/value_builder.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#endif

using content::BrowserThread;

namespace extensions {
namespace {

class ScriptBadgeControllerTest : public ChromeRenderViewHostTestHarness {
 public:
  ScriptBadgeControllerTest()
      : feature_override_(FeatureSwitch::script_badges(), true),
        ui_thread_(BrowserThread::UI, MessageLoop::current()),
        file_thread_(BrowserThread::FILE, MessageLoop::current()),
        current_channel_(chrome::VersionInfo::CHANNEL_DEV) {}

  virtual void SetUp() OVERRIDE {
    // Note that this sets a PageActionController into the
    // extensions::TabHelper's location_bar_controller field.  Do
    // not use that for testing.
    ChromeRenderViewHostTestHarness::SetUp();

    Profile* profile =
        Profile::FromBrowserContext(web_contents()->GetBrowserContext());
    TestExtensionSystem* extension_system =
        static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile));

    // Create an ExtensionService so the ScriptBadgeController can find its
    // extensions.
    CommandLine command_line(CommandLine::NO_PROGRAM);
    extension_service_ = extension_system->CreateExtensionService(
        &command_line, base::FilePath(), false);

    TabHelper::CreateForWebContents(web_contents());
    script_badge_controller_ = static_cast<ScriptBadgeController*>(
        TabHelper::FromWebContents(web_contents())->location_bar_controller());
  }

 protected:
  // Creates a test extension and adds it to |extension_service_|.
  scoped_refptr<const Extension> AddTestExtension() {
    scoped_refptr<const Extension> extension = ExtensionBuilder()
        .SetManifest(DictionaryBuilder()
            .Set("name", "Extension with page action")
            .Set("version", "1.0.0")
            .Set("manifest_version", 2)
            .Set("permissions", ListBuilder()
                .Append("tabs"))
            .Set("page_action", DictionaryBuilder()
                .Set("default_title", "Hello")))
        .Build();
    extension_service_->AddExtension(extension);
    return extension;
  }

  ExtensionAction* GetScriptBadge(const Extension& extension) {
    return ExtensionActionManager::Get(profile())->GetScriptBadge(extension);
  }

  ExtensionService* extension_service_;
  ScriptBadgeController* script_badge_controller_;

 private:
  FeatureSwitch::ScopedOverride feature_override_;
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread file_thread_;
  Feature::ScopedCurrentChannel current_channel_;

#if defined OS_CHROMEOS
  chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
  chromeos::ScopedTestCrosSettings test_cros_settings_;
  chromeos::ScopedTestUserManager test_user_manager_;
#endif
};

struct CountingNotificationObserver : public content::NotificationObserver {
  CountingNotificationObserver() : events(0) {}

  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE {
    events++;
  }

  int events;
};

TEST_F(ScriptBadgeControllerTest, ExecutionMakesBadgeVisible) {
  content::NotificationRegistrar notification_registrar;

  EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
              testing::ElementsAre());

  scoped_refptr<const Extension> extension = AddTestExtension();

  // Establish a page id.
  NavigateAndCommit(GURL("http://www.google.com"));

  CountingNotificationObserver location_bar_updated;
  Profile* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
  notification_registrar.Add(
      &location_bar_updated,
      chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED,
      content::Source<Profile>(profile));

  // Initially, no script badges.
  EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
              testing::ElementsAre());

  TabHelper::ScriptExecutionObserver::ExecutingScriptsMap id_map;
  id_map[extension->id()] = std::set<std::string>();
  script_badge_controller_->OnScriptsExecuted(
      web_contents(),
      id_map,
      web_contents()->GetController().GetActiveEntry()->GetPageID(),
      GURL(std::string()));
  EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
              testing::ElementsAre(GetScriptBadge(*extension)));
  EXPECT_THAT(location_bar_updated.events, testing::Gt(0));
};

TEST_F(ScriptBadgeControllerTest, FragmentNavigation) {
  scoped_refptr<const Extension> extension = AddTestExtension();
  Profile* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());

  // Establish a page id.
  NavigateAndCommit(GURL("http://www.google.com"));

  // Run script. Should be a notification and a script badge.
  {
    content::NotificationRegistrar notification_registrar;
    CountingNotificationObserver location_bar_updated;
    notification_registrar.Add(
        &location_bar_updated,
        chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED,
        content::Source<Profile>(profile));

    TabHelper::ScriptExecutionObserver::ExecutingScriptsMap id_map;
    id_map[extension->id()] = std::set<std::string>();
    script_badge_controller_->OnScriptsExecuted(
        web_contents(),
        id_map,
        web_contents()->GetController().GetActiveEntry()->GetPageID(),
        GURL(std::string()));

    EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
                testing::ElementsAre(GetScriptBadge(*extension)));
    EXPECT_EQ(1, location_bar_updated.events);
  }

  // Navigate to a hash fragment. Shouldn't change.
  {
    content::NotificationRegistrar notification_registrar;
    CountingNotificationObserver location_bar_updated;
    notification_registrar.Add(
        &location_bar_updated,
        chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED,
        content::Source<Profile>(profile));

    NavigateAndCommit(GURL("http://www.google.com#hash"));

    EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
              testing::ElementsAre(GetScriptBadge(*extension)));
    EXPECT_EQ(0, location_bar_updated.events);
  }

  // Refreshing the page should reset the badges.
  {
    content::NotificationRegistrar notification_registrar;
    CountingNotificationObserver location_bar_updated;
    notification_registrar.Add(
        &location_bar_updated,
        chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED,
        content::Source<Profile>(profile));

    Reload();

    EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
                testing::ElementsAre());
  }
}

TEST_F(ScriptBadgeControllerTest, GetAttentionMakesBadgeVisible) {
  content::NotificationRegistrar notification_registrar;

  scoped_refptr<const Extension> extension =
      ExtensionBuilder()
      .SetManifest(DictionaryBuilder()
                   .Set("name", "Extension")
                   .Set("version", "1.0.0")
                   .Set("manifest_version", 2)
                   .Set("permissions", ListBuilder()
                        .Append("tabs")))
      .Build();
  extension_service_->AddExtension(extension);

  // Establish a page id.
  NavigateAndCommit(GURL("http://www.google.com"));

  CountingNotificationObserver initial_badge_display;
  Profile* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
  notification_registrar.Add(
      &initial_badge_display,
      chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED,
      content::Source<Profile>(profile));

  // Initially, no script badges.
  EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
              testing::ElementsAre());

  // Getting attention the first time should display the badge.
  script_badge_controller_->GetAttentionFor(extension->id());

  EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
              testing::ElementsAre(GetScriptBadge(*extension)));
  EXPECT_THAT(initial_badge_display.events, testing::Gt(0));

  CountingNotificationObserver subsequent_get_attention_call;
  notification_registrar.Add(
      &subsequent_get_attention_call,
      chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED,
      content::Source<Profile>(profile));

  // Getting attention a second time should have no effect.
  script_badge_controller_->GetAttentionFor(extension->id());

  EXPECT_THAT(script_badge_controller_->GetCurrentActions(),
              testing::ElementsAre(GetScriptBadge(*extension)));
  EXPECT_EQ(0, subsequent_get_attention_call.events);
};

}  // namespace
}  // namespace extensions