Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(680)

Side by Side Diff: chrome/browser/extensions/extension_storage_monitor_browsertest.cc

Issue 221933013: Show a notification when an ephemeral app consumes excessive disk space (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webkit_storage_monitor
Patch Set: Fix unit tests Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include <set>
6
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_storage_monitor.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/ui/extensions/application_launch.h"
13 #include "content/public/test/test_utils.h"
14 #include "extensions/browser/extension_prefs.h"
15 #include "ui/message_center/message_center.h"
16 #include "ui/message_center/message_center_observer.h"
17
18 namespace extensions {
19
20 namespace {
21
22 const int kInitialUsageThreshold = 500;
23
24 const char kWriteDataApp[] = "storage_monitor/write_data";
25
26 class NotificationObserver : public message_center::MessageCenterObserver {
27 public:
28 explicit NotificationObserver(const std::string& target_notification)
29 : message_center_(message_center::MessageCenter::Get()),
30 target_notification_id_(target_notification),
31 waiting_(false) {
32 message_center_->AddObserver(this);
33 }
34
35 virtual ~NotificationObserver() {
36 message_center_->RemoveObserver(this);
37 }
38
39 bool HasReceivedNotification() const {
40 return received_notifications_.find(target_notification_id_) !=
41 received_notifications_.end();
koz (OOO until 15th September) 2014/04/07 05:24:32 nit: I think this should be indented like return
tmdiep 2014/04/07 08:45:01 Done.
42 }
43
44 // Runs the message loop and returns true if a notification is received.
45 // Immediately returns true if a notification has already been received.
46 bool WaitForNotification() {
47 if (HasReceivedNotification())
48 return true;
49
50 waiting_ = true;
51 content::RunMessageLoop();
52 waiting_ = false;
53 return HasReceivedNotification();
54 }
55
56 private:
57 // MessageCenterObserver implementation:
58 virtual void OnNotificationAdded(
59 const std::string& notification_id) OVERRIDE {
60 received_notifications_.insert(notification_id);
61
62 if (waiting_ && HasReceivedNotification())
63 base::MessageLoopForUI::current()->Quit();
64 }
65
66 message_center::MessageCenter* message_center_;
67 std::set<std::string> received_notifications_;
68 std::string target_notification_id_;
69 bool waiting_;
70 };
71
72 } // namespace
73
74 class ExtensionStorageMonitorTest : public ExtensionBrowserTest {
75 protected:
76 void InitStorageMonitor() {
77 ExtensionStorageMonitor* monitor = ExtensionStorageMonitor::Get(profile());
78 ASSERT_TRUE(monitor);
79
80 // Override thresholds so that we don't have to write a huge amount of data
81 // to trigger notifications in these tests.
82 monitor->enable_for_all_extensions_ = true;
83 monitor->initial_extension_threshold_ = kInitialUsageThreshold;
84
85 // To ensure storage events are dispatched from QuotaManager immediately.
86 monitor->observer_rate_ = 0;
87 }
88
89 const Extension* InitWriteDataApp() {
90 InitStorageMonitor();
91
92 base::FilePath path = test_data_dir_.AppendASCII(kWriteDataApp);
93 const Extension* extension = InstallExtension(path, 1);
94 EXPECT_TRUE(extension);
95 return extension;
96 }
97
98 std::string GetNotificationId(const std::string& extension_id) {
99 return ExtensionStorageMonitor::GetNotificationId(extension_id);
100 }
101
102 // Write a number of characters to persistent storage.
103 void WriteDataPacket(const Extension* extension,
104 int num_chars,
105 bool expected_notification,
106 bool expected_notifications_enabled) {
107 ExtensionTestMessageListener launched_listener("launched", true);
108 ExtensionTestMessageListener write_complete_listener(
109 "write_complete", false);
110 NotificationObserver notification_observer(
111 GetNotificationId(extension->id()));
112
113 OpenApplication(AppLaunchParams(
114 profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
115 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
116
117 // Instruct the app to write |num_chars| of data.
118 launched_listener.Reply(base::IntToString(num_chars));
119 ASSERT_TRUE(write_complete_listener.WaitUntilSatisfied());
120
121 if (expected_notification) {
122 EXPECT_TRUE(notification_observer.WaitForNotification());
123 } else {
124 base::RunLoop().RunUntilIdle();
125 EXPECT_FALSE(notification_observer.HasReceivedNotification());
126 }
127
128 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
129 ASSERT_TRUE(prefs);
130 EXPECT_EQ(expected_notifications_enabled,
131 prefs->StorageNotificationsEnabled(extension->id()));
koz (OOO until 15th September) 2014/04/07 05:24:32 StorageNotificationsEnabled -> GetStorageNotificat
tmdiep 2014/04/07 08:45:01 Changed to IsStorageNotificationEnabled().
132 }
133 };
134
135 // Control - No notifications should be shown if usage remains under the
136 // threshold.
137 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, UnderThreshold) {
138 const Extension* extension = InitWriteDataApp();
139 ASSERT_TRUE(extension);
140 WriteDataPacket(extension, 1, false, true);
koz (OOO until 15th September) 2014/04/07 05:24:32 These calls might be clearer if you removed the la
tmdiep 2014/04/07 08:45:01 Good suggestion. Done.
141 }
142
143 // Ensure a notification is shown when usage reaches the first threshold.
144 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, ExceedInitialThreshold) {
145 const Extension* extension = InitWriteDataApp();
146 ASSERT_TRUE(extension);
147 WriteDataPacket(extension, kInitialUsageThreshold, true, true);
148 }
149
150 // Ensure a notification is shown when usage immediately exceeds double the
151 // first threshold.
152 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, DoubleInitialThreshold) {
153 const Extension* extension = InitWriteDataApp();
154 ASSERT_TRUE(extension);
155 WriteDataPacket(extension, kInitialUsageThreshold*2, true, true);
156 }
157
158 // Ensure that notifications are not fired if the next threshold has not been
159 // reached.
160 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, ThrottleNotifications) {
161 const Extension* extension = InitWriteDataApp();
162 ASSERT_TRUE(extension);
163
164 // Exceed the first threshold.
165 WriteDataPacket(extension, kInitialUsageThreshold, true, true);
166
167 // Stay within the next threshold.
168 WriteDataPacket(extension, 1, false, true);
169 }
170
171 // Verify that notifications are disabled when the user clicks the action button
172 // in the notification.
173 IN_PROC_BROWSER_TEST_F(ExtensionStorageMonitorTest, UserDisabledNotifications) {
174 const Extension* extension = InitWriteDataApp();
175 ASSERT_TRUE(extension);
176 WriteDataPacket(extension, kInitialUsageThreshold, true, true);
177
178 // Fake clicking the notification button.
179 message_center::MessageCenter::Get()->ClickOnNotificationButton(
180 GetNotificationId(extension->id()),
181 ExtensionStorageMonitor::BUTTON_DISABLE_NOTIFICATION);
182
183 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
184 ASSERT_TRUE(prefs);
185 EXPECT_FALSE(prefs->StorageNotificationsEnabled(extension->id()));
186
187 // Expect to receive no further notifications when usage continues to
188 // increase.
189 int64 next_threshold = prefs->GetNextStorageThreshold(extension->id());
190 int64 next_data_size = next_threshold - kInitialUsageThreshold;
191 ASSERT_GE(next_data_size, 0);
192
193 WriteDataPacket(extension, next_data_size, false, false);
194 }
195
196 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698