Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / extensions / extension_storage_monitor_browsertest.cc
blob295331d60e4fe34c9215220e510f2f484edfa395
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 <set>
7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_storage_monitor.h"
12 #include "chrome/browser/ui/extensions/app_launch_params.h"
13 #include "chrome/browser/ui/extensions/application_launch.h"
14 #include "content/public/test/test_utils.h"
15 #include "extensions/browser/extension_dialog_auto_confirm.h"
16 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/test_extension_registry_observer.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/test/extension_test_message_listener.h"
22 #include "ui/message_center/message_center.h"
23 #include "ui/message_center/message_center_observer.h"
25 namespace extensions {
27 namespace {
29 const int kInitialUsageThreshold = 500;
31 const char kWriteDataApp[] = "storage_monitor/write_data";
33 class NotificationObserver : public message_center::MessageCenterObserver {
34 public:
35 explicit NotificationObserver(const std::string& target_notification)
36 : message_center_(message_center::MessageCenter::Get()),
37 target_notification_id_(target_notification),
38 waiting_(false) {
39 message_center_->AddObserver(this);
42 ~NotificationObserver() override { message_center_->RemoveObserver(this); }
44 bool HasReceivedNotification() const {
45 return received_notifications_.find(target_notification_id_) !=
46 received_notifications_.end();
49 // Runs the message loop and returns true if a notification is received.
50 // Immediately returns true if a notification has already been received.
51 bool WaitForNotification() {
52 if (HasReceivedNotification())
53 return true;
55 waiting_ = true;
56 content::RunMessageLoop();
57 waiting_ = false;
58 return HasReceivedNotification();
61 private:
62 // MessageCenterObserver implementation:
63 void OnNotificationAdded(const std::string& notification_id) override {
64 received_notifications_.insert(notification_id);
66 if (waiting_ && HasReceivedNotification())
67 base::MessageLoopForUI::current()->Quit();
70 message_center::MessageCenter* message_center_;
71 std::set<std::string> received_notifications_;
72 std::string target_notification_id_;
73 bool waiting_;
76 } // namespace
78 class ExtensionStorageMonitorTest : public ExtensionBrowserTest {
79 public:
80 ExtensionStorageMonitorTest() : storage_monitor_(NULL) {}
82 protected:
83 // ExtensionBrowserTest overrides:
84 void SetUpOnMainThread() override {
85 ExtensionBrowserTest::SetUpOnMainThread();
87 InitStorageMonitor();
90 ExtensionStorageMonitor* monitor() {
91 CHECK(storage_monitor_);
92 return storage_monitor_;
95 int64 GetInitialExtensionThreshold() {
96 CHECK(storage_monitor_);
97 return storage_monitor_->initial_extension_threshold_;
100 int64 GetInitialEphemeralThreshold() {
101 CHECK(storage_monitor_);
102 return storage_monitor_->initial_ephemeral_threshold_;
105 void DisableForInstalledExtensions() {
106 CHECK(storage_monitor_);
107 storage_monitor_->enable_for_all_extensions_ = false;
110 const Extension* InitWriteDataApp() {
111 base::FilePath path = test_data_dir_.AppendASCII(kWriteDataApp);
112 const Extension* extension = InstallExtension(path, 1);
113 EXPECT_TRUE(extension);
114 return extension;
117 const Extension* InitWriteDataEphemeralApp() {
118 // The threshold for installed extensions should be higher than ephemeral
119 // apps.
120 storage_monitor_->initial_extension_threshold_ =
121 storage_monitor_->initial_ephemeral_threshold_ * 4;
123 base::FilePath path = test_data_dir_.AppendASCII(kWriteDataApp);
124 const Extension* extension = InstallEphemeralAppWithSourceAndFlags(
125 path, 1, Manifest::INTERNAL, Extension::NO_FLAGS);
126 EXPECT_TRUE(extension);
127 return extension;
130 std::string GetNotificationId(const std::string& extension_id) {
131 return monitor()->GetNotificationId(extension_id);
134 bool IsStorageNotificationEnabled(const std::string& extension_id) {
135 return monitor()->IsStorageNotificationEnabled(extension_id);
138 int64 GetNextStorageThreshold(const std::string& extension_id) {
139 return monitor()->GetNextStorageThreshold(extension_id);
142 void WriteBytesExpectingNotification(const Extension* extension,
143 int num_bytes) {
144 int64 previous_threshold = GetNextStorageThreshold(extension->id());
145 WriteBytes(extension, num_bytes, true);
146 EXPECT_GT(GetNextStorageThreshold(extension->id()), previous_threshold);
149 void WriteBytesNotExpectingNotification(const Extension* extension,
150 int num_bytes) {
151 WriteBytes(extension, num_bytes, false);
154 private:
155 void InitStorageMonitor() {
156 storage_monitor_ = ExtensionStorageMonitor::Get(profile());
157 ASSERT_TRUE(storage_monitor_);
159 // Override thresholds so that we don't have to write a huge amount of data
160 // to trigger notifications in these tests.
161 storage_monitor_->enable_for_all_extensions_ = true;
162 storage_monitor_->initial_extension_threshold_ = kInitialUsageThreshold;
163 storage_monitor_->initial_ephemeral_threshold_ = kInitialUsageThreshold;
165 // To ensure storage events are dispatched from QuotaManager immediately.
166 storage_monitor_->observer_rate_ = base::TimeDelta();
169 // Write a number of bytes to persistent storage.
170 void WriteBytes(const Extension* extension,
171 int num_bytes,
172 bool expected_notification) {
173 ExtensionTestMessageListener launched_listener("launched", true);
174 ExtensionTestMessageListener write_complete_listener(
175 "write_complete", false);
176 NotificationObserver notification_observer(
177 GetNotificationId(extension->id()));
179 OpenApplication(AppLaunchParams(profile(), extension, LAUNCH_CONTAINER_NONE,
180 NEW_WINDOW, extensions::SOURCE_TEST));
181 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
183 // Instruct the app to write |num_bytes| of data.
184 launched_listener.Reply(base::IntToString(num_bytes));
185 ASSERT_TRUE(write_complete_listener.WaitUntilSatisfied());
187 if (expected_notification) {
188 EXPECT_TRUE(notification_observer.WaitForNotification());
189 } else {
190 base::RunLoop().RunUntilIdle();
191 EXPECT_FALSE(notification_observer.HasReceivedNotification());
195 ExtensionStorageMonitor* storage_monitor_;
198 // Control - No notifications should be shown if usage remains under the
199 // threshold.
200 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, UnderThreshold) {
201 const Extension* extension = InitWriteDataApp();
202 ASSERT_TRUE(extension);
203 WriteBytesNotExpectingNotification(extension, 1);
206 // Ensure a notification is shown when usage reaches the first threshold.
207 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, ExceedInitialThreshold) {
208 const Extension* extension = InitWriteDataApp();
209 ASSERT_TRUE(extension);
210 WriteBytesExpectingNotification(extension, GetInitialExtensionThreshold());
213 // Ensure a notification is shown when usage immediately exceeds double the
214 // first threshold.
215 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, DoubleInitialThreshold) {
216 const Extension* extension = InitWriteDataApp();
217 ASSERT_TRUE(extension);
218 WriteBytesExpectingNotification(extension,
219 GetInitialExtensionThreshold() * 2);
222 // Ensure that notifications are not fired if the next threshold has not been
223 // reached.
224 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, ThrottleNotifications) {
225 const Extension* extension = InitWriteDataApp();
226 ASSERT_TRUE(extension);
228 // Exceed the first threshold.
229 WriteBytesExpectingNotification(extension, GetInitialExtensionThreshold());
231 // Stay within the next threshold.
232 WriteBytesNotExpectingNotification(extension, 1);
235 // Verify that notifications are disabled when the user clicks the action button
236 // in the notification.
237 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, UserDisabledNotifications) {
238 const Extension* extension = InitWriteDataApp();
239 ASSERT_TRUE(extension);
240 WriteBytesExpectingNotification(extension, GetInitialExtensionThreshold());
242 EXPECT_TRUE(IsStorageNotificationEnabled(extension->id()));
244 // Fake clicking the notification button to disable notifications.
245 message_center::MessageCenter::Get()->ClickOnNotificationButton(
246 GetNotificationId(extension->id()),
247 ExtensionStorageMonitor::BUTTON_DISABLE_NOTIFICATION);
249 EXPECT_FALSE(IsStorageNotificationEnabled(extension->id()));
251 // Expect to receive no further notifications when usage continues to
252 // increase.
253 int64 next_threshold = GetNextStorageThreshold(extension->id());
254 int64 next_data_size = next_threshold - GetInitialExtensionThreshold();
255 ASSERT_GT(next_data_size, 0);
257 WriteBytesNotExpectingNotification(extension, next_data_size);
260 // Verify that thresholds for ephemeral apps are reset when they are
261 // promoted to regular installed apps.
262 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, EphemeralAppLowUsage) {
263 const Extension* extension = InitWriteDataEphemeralApp();
264 ASSERT_TRUE(extension);
265 WriteBytesExpectingNotification(extension, GetInitialEphemeralThreshold());
267 // Store the number of bytes until the next threshold is reached.
268 int64 next_threshold = GetNextStorageThreshold(extension->id());
269 int64 next_data_size = next_threshold - GetInitialEphemeralThreshold();
270 ASSERT_GT(next_data_size, 0);
271 EXPECT_GE(GetInitialExtensionThreshold(), next_threshold);
273 // Promote the ephemeral app.
274 ExtensionService* service =
275 ExtensionSystem::Get(profile())->extension_service();
276 service->PromoteEphemeralApp(extension, false);
278 // The next threshold should now be equal to the initial threshold for
279 // extensions (which is higher than the initial threshold for ephemeral apps).
280 EXPECT_EQ(GetInitialExtensionThreshold(),
281 GetNextStorageThreshold(extension->id()));
283 // Since the threshold was increased, a notification should not be
284 // triggered.
285 WriteBytesNotExpectingNotification(extension, next_data_size);
288 // Verify that thresholds for ephemeral apps are not reset when they are
289 // promoted to regular installed apps if their usage is higher than the initial
290 // threshold for installed extensions.
291 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, EphemeralAppWithHighUsage) {
292 const Extension* extension = InitWriteDataEphemeralApp();
293 ASSERT_TRUE(extension);
294 WriteBytesExpectingNotification(extension, GetInitialExtensionThreshold());
295 int64 saved_next_threshold = GetNextStorageThreshold(extension->id());
297 // Promote the ephemeral app.
298 ExtensionService* service =
299 ExtensionSystem::Get(profile())->extension_service();
300 service->PromoteEphemeralApp(extension, false);
302 // The next threshold should not have changed.
303 EXPECT_EQ(saved_next_threshold, GetNextStorageThreshold(extension->id()));
306 // Ensure that monitoring is disabled for installed extensions if
307 // |enable_for_all_extensions_| is false. This test can be removed if monitoring
308 // is eventually enabled for all extensions.
309 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest,
310 DisableForInstalledExtensions) {
311 DisableForInstalledExtensions();
313 const Extension* extension = InitWriteDataApp();
314 ASSERT_TRUE(extension);
315 WriteBytesNotExpectingNotification(extension, GetInitialExtensionThreshold());
318 // Verify that notifications are disabled when the user clicks the action button
319 // in the notification.
320 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, UninstallExtension) {
321 const Extension* extension = InitWriteDataApp();
322 ASSERT_TRUE(extension);
323 WriteBytesExpectingNotification(extension, GetInitialExtensionThreshold());
325 // Fake clicking the notification button to uninstall and accepting the
326 // uninstall.
327 ScopedTestDialogAutoConfirm scoped_autoconfirm(
328 ScopedTestDialogAutoConfirm::ACCEPT);
329 TestExtensionRegistryObserver observer(ExtensionRegistry::Get(profile()),
330 extension->id());
331 message_center::MessageCenter::Get()->ClickOnNotificationButton(
332 GetNotificationId(extension->id()),
333 ExtensionStorageMonitor::BUTTON_UNINSTALL);
334 observer.WaitForExtensionUninstalled();
337 } // namespace extensions