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.
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "content/public/test/mock_special_storage_policy.h"
11 #include "content/public/test/mock_storage_client.h"
12 #include "net/base/net_util.h"
13 #include "storage/browser/quota/quota_manager.h"
14 #include "storage/browser/quota/quota_manager_proxy.h"
15 #include "storage/browser/quota/storage_monitor.h"
16 #include "storage/browser/quota/storage_observer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using storage::HostStorageObservers
;
20 using storage::kQuotaErrorNotSupported
;
21 using storage::kQuotaStatusOk
;
22 using storage::kStorageTypePersistent
;
23 using storage::kStorageTypeTemporary
;
24 using storage::QuotaClient
;
25 using storage::QuotaManager
;
26 using storage::QuotaStatusCode
;
27 using storage::SpecialStoragePolicy
;
28 using storage::StorageMonitor
;
29 using storage::StorageObserver
;
30 using storage::StorageObserverList
;
31 using storage::StorageType
;
32 using storage::StorageTypeObservers
;
38 const char kDefaultOrigin
[] = "http://www.foo.com/";
39 const char kAlternativeOrigin
[] = "http://www.bar.com/";
41 class MockObserver
: public StorageObserver
{
43 const StorageObserver::Event
& LastEvent() const {
44 CHECK(!events_
.empty());
45 return events_
.back();
48 int EventCount() const {
49 return events_
.size();
52 // StorageObserver implementation:
53 void OnStorageEvent(const StorageObserver::Event
& event
) override
{
54 events_
.push_back(event
);
58 std::vector
<StorageObserver::Event
> events_
;
61 // A mock quota manager for overriding GetUsageAndQuotaForWebApps().
62 class UsageMockQuotaManager
: public QuotaManager
{
64 UsageMockQuotaManager(SpecialStoragePolicy
* special_storage_policy
)
67 base::ThreadTaskRunnerHandle::Get().get(),
68 base::ThreadTaskRunnerHandle::Get().get(),
69 special_storage_policy
),
72 callback_status_(kQuotaStatusOk
),
73 initialized_(false) {}
75 void SetCallbackParams(int64 usage
, int64 quota
, QuotaStatusCode status
) {
77 callback_quota_
= quota
;
78 callback_usage_
= usage
;
79 callback_status_
= status
;
82 void InvokeCallback() {
83 delayed_callback_
.Run(callback_status_
, callback_usage_
, callback_quota_
);
86 void GetUsageAndQuotaForWebApps(
89 const GetUsageAndQuotaCallback
& callback
) override
{
91 callback
.Run(callback_status_
, callback_usage_
, callback_quota_
);
93 delayed_callback_
= callback
;
97 ~UsageMockQuotaManager() override
{}
100 int64 callback_usage_
;
101 int64 callback_quota_
;
102 QuotaStatusCode callback_status_
;
104 GetUsageAndQuotaCallback delayed_callback_
;
109 class StorageMonitorTestBase
: public testing::Test
{
111 void DispatchPendingEvents(StorageObserverList
& observer_list
) {
112 observer_list
.DispatchPendingEvent();
115 const StorageObserver::Event
* GetPendingEvent(
116 const StorageObserverList
& observer_list
) {
117 return observer_list
.notification_timer_
.IsRunning()
118 ? &observer_list
.pending_event_
: NULL
;
121 const StorageObserver::Event
* GetPendingEvent(
122 const HostStorageObservers
& host_observers
) {
123 return GetPendingEvent(host_observers
.observers_
);
126 int GetRequiredUpdatesCount(const StorageObserverList
& observer_list
) {
128 for (StorageObserverList::StorageObserverStateMap::const_iterator it
=
129 observer_list
.observers_
.begin();
130 it
!= observer_list
.observers_
.end(); ++it
) {
131 if (it
->second
.requires_update
)
138 int GetRequiredUpdatesCount(const HostStorageObservers
& host_observers
) {
139 return GetRequiredUpdatesCount(host_observers
.observers_
);
142 void SetLastNotificationTime(StorageObserverList
& observer_list
,
143 StorageObserver
* observer
) {
144 ASSERT_TRUE(observer_list
.observers_
.find(observer
) !=
145 observer_list
.observers_
.end());
147 StorageObserverList::ObserverState
& state
=
148 observer_list
.observers_
[observer
];
149 state
.last_notification_time
= base::TimeTicks::Now() - state
.rate
;
152 void SetLastNotificationTime(HostStorageObservers
& host_observers
,
153 StorageObserver
* observer
) {
154 SetLastNotificationTime(host_observers
.observers_
, observer
);
157 int GetObserverCount(const HostStorageObservers
& host_observers
) {
158 return host_observers
.observers_
.ObserverCount();
162 class StorageTestWithManagerBase
: public StorageMonitorTestBase
{
164 void SetUp() override
{
165 storage_policy_
= new MockSpecialStoragePolicy();
166 quota_manager_
= new UsageMockQuotaManager(storage_policy_
.get());
169 void TearDown() override
{
170 // This ensures the quota manager is destroyed correctly.
171 quota_manager_
= NULL
;
172 base::RunLoop().RunUntilIdle();
176 base::MessageLoop message_loop_
;
177 scoped_refptr
<MockSpecialStoragePolicy
> storage_policy_
;
178 scoped_refptr
<UsageMockQuotaManager
> quota_manager_
;
181 // Tests for StorageObserverList:
183 typedef StorageMonitorTestBase StorageObserverListTest
;
185 // Test dispatching events to one observer.
186 TEST_F(StorageObserverListTest
, DispatchEventToSingleObserver
) {
187 // A message loop is required as StorageObserverList may schedule jobs.
188 base::MessageLoop
loop(base::MessageLoop::TYPE_DEFAULT
);
190 StorageObserver::MonitorParams
params(kStorageTypePersistent
,
191 GURL(kDefaultOrigin
),
192 base::TimeDelta::FromHours(1),
194 MockObserver mock_observer
;
195 StorageObserverList observer_list
;
196 observer_list
.AddObserver(&mock_observer
, params
);
198 StorageObserver::Event event
;
199 event
.filter
= params
.filter
;
201 // Verify that the first event is dispatched immediately.
204 observer_list
.OnStorageChange(event
);
205 EXPECT_EQ(1, mock_observer
.EventCount());
206 EXPECT_EQ(event
, mock_observer
.LastEvent());
207 EXPECT_EQ(NULL
, GetPendingEvent(observer_list
));
208 EXPECT_EQ(0, GetRequiredUpdatesCount(observer_list
));
210 // Verify that the next event is pending.
213 observer_list
.OnStorageChange(event
);
214 EXPECT_EQ(1, mock_observer
.EventCount());
215 ASSERT_TRUE(GetPendingEvent(observer_list
));
216 EXPECT_EQ(event
, *GetPendingEvent(observer_list
));
217 EXPECT_EQ(1, GetRequiredUpdatesCount(observer_list
));
219 // Fake the last notification time so that an event will be dispatched.
220 SetLastNotificationTime(observer_list
, &mock_observer
);
223 observer_list
.OnStorageChange(event
);
224 EXPECT_EQ(2, mock_observer
.EventCount());
225 EXPECT_EQ(event
, mock_observer
.LastEvent());
226 EXPECT_EQ(NULL
, GetPendingEvent(observer_list
));
227 EXPECT_EQ(0, GetRequiredUpdatesCount(observer_list
));
229 // Remove the observer.
232 observer_list
.RemoveObserver(&mock_observer
);
233 observer_list
.OnStorageChange(event
);
234 EXPECT_EQ(2, mock_observer
.EventCount());
235 EXPECT_EQ(NULL
, GetPendingEvent(observer_list
));
238 // Test dispatching events to multiple observers.
239 TEST_F(StorageObserverListTest
, DispatchEventToMultipleObservers
) {
240 // A message loop is required as StorageObserverList may schedule jobs.
241 base::MessageLoop
loop(base::MessageLoop::TYPE_DEFAULT
);
243 MockObserver mock_observer1
;
244 MockObserver mock_observer2
;
245 StorageObserverList observer_list
;
246 StorageObserver::Filter
filter(kStorageTypePersistent
,
247 GURL(kDefaultOrigin
));
248 observer_list
.AddObserver(
250 StorageObserver::MonitorParams(
251 filter
, base::TimeDelta::FromHours(1), false));
252 observer_list
.AddObserver(
254 StorageObserver::MonitorParams(
255 filter
, base::TimeDelta::FromHours(2), false));
257 StorageObserver::Event event
;
258 event
.filter
= filter
;
260 // Verify that the first event is dispatched immediately.
263 observer_list
.OnStorageChange(event
);
264 EXPECT_EQ(1, mock_observer1
.EventCount());
265 EXPECT_EQ(1, mock_observer2
.EventCount());
266 EXPECT_EQ(event
, mock_observer1
.LastEvent());
267 EXPECT_EQ(event
, mock_observer2
.LastEvent());
268 EXPECT_EQ(NULL
, GetPendingEvent(observer_list
));
269 EXPECT_EQ(0, GetRequiredUpdatesCount(observer_list
));
271 // Fake the last notification time so that observer1 will receive the next
272 // event, but it will be pending for observer2.
273 SetLastNotificationTime(observer_list
, &mock_observer1
);
276 observer_list
.OnStorageChange(event
);
277 EXPECT_EQ(2, mock_observer1
.EventCount());
278 EXPECT_EQ(1, mock_observer2
.EventCount());
279 EXPECT_EQ(event
, mock_observer1
.LastEvent());
280 ASSERT_TRUE(GetPendingEvent(observer_list
));
281 EXPECT_EQ(event
, *GetPendingEvent(observer_list
));
282 EXPECT_EQ(1, GetRequiredUpdatesCount(observer_list
));
284 // Now dispatch the pending event to observer2.
285 SetLastNotificationTime(observer_list
, &mock_observer2
);
286 DispatchPendingEvents(observer_list
);
287 EXPECT_EQ(2, mock_observer1
.EventCount());
288 EXPECT_EQ(2, mock_observer2
.EventCount());
289 EXPECT_EQ(event
, mock_observer1
.LastEvent());
290 EXPECT_EQ(event
, mock_observer2
.LastEvent());
291 EXPECT_EQ(NULL
, GetPendingEvent(observer_list
));
292 EXPECT_EQ(0, GetRequiredUpdatesCount(observer_list
));
295 // Ensure that the |origin| field in events match the origin specified by the
296 // observer on registration.
297 TEST_F(StorageObserverListTest
, ReplaceEventOrigin
) {
298 StorageObserver::MonitorParams
params(kStorageTypePersistent
,
299 GURL(kDefaultOrigin
),
300 base::TimeDelta::FromHours(1),
302 MockObserver mock_observer
;
303 StorageObserverList observer_list
;
304 observer_list
.AddObserver(&mock_observer
, params
);
306 StorageObserver::Event dispatched_event
;
307 dispatched_event
.filter
= params
.filter
;
308 dispatched_event
.filter
.origin
= GURL("https://www.foo.com/bar");
309 observer_list
.OnStorageChange(dispatched_event
);
311 EXPECT_EQ(params
.filter
.origin
, mock_observer
.LastEvent().filter
.origin
);
314 // Tests for HostStorageObservers:
316 typedef StorageTestWithManagerBase HostStorageObserversTest
;
318 // Verify that HostStorageObservers is initialized after the first usage change.
319 TEST_F(HostStorageObserversTest
, InitializeOnUsageChange
) {
320 StorageObserver::MonitorParams
params(kStorageTypePersistent
,
321 GURL(kDefaultOrigin
),
322 base::TimeDelta::FromHours(1),
324 const int64 kUsage
= 324554;
325 const int64 kQuota
= 234354354;
326 quota_manager_
->SetCallbackParams(kUsage
, kQuota
, kQuotaStatusOk
);
328 MockObserver mock_observer
;
329 HostStorageObservers
host_observers(quota_manager_
.get());
330 host_observers
.AddObserver(&mock_observer
, params
);
332 // Verify that HostStorageObservers dispatches the first event correctly.
333 StorageObserver::Event
expected_event(params
.filter
, kUsage
, kQuota
);
334 host_observers
.NotifyUsageChange(params
.filter
, 87324);
335 EXPECT_EQ(1, mock_observer
.EventCount());
336 EXPECT_EQ(expected_event
, mock_observer
.LastEvent());
337 EXPECT_TRUE(host_observers
.is_initialized());
339 // Verify that HostStorageObservers handles subsequent usage changes
341 const int64 kDelta
= 2345;
342 expected_event
.usage
+= kDelta
;
343 SetLastNotificationTime(host_observers
, &mock_observer
);
344 host_observers
.NotifyUsageChange(params
.filter
, kDelta
);
345 EXPECT_EQ(2, mock_observer
.EventCount());
346 EXPECT_EQ(expected_event
, mock_observer
.LastEvent());
349 // Verify that HostStorageObservers is initialized after the adding the first
350 // observer that elected to receive the initial state.
351 TEST_F(HostStorageObserversTest
, InitializeOnObserver
) {
352 const int64 kUsage
= 74387;
353 const int64 kQuota
= 92834743;
354 quota_manager_
->SetCallbackParams(kUsage
, kQuota
, kQuotaStatusOk
);
355 HostStorageObservers
host_observers(quota_manager_
.get());
357 // |host_observers| should not be initialized after the first observer is
358 // added because it did not elect to receive the initial state.
359 StorageObserver::MonitorParams
params(kStorageTypePersistent
,
360 GURL(kDefaultOrigin
),
361 base::TimeDelta::FromHours(1),
363 MockObserver mock_observer1
;
364 host_observers
.AddObserver(&mock_observer1
, params
);
365 EXPECT_FALSE(host_observers
.is_initialized());
366 EXPECT_EQ(0, mock_observer1
.EventCount());
368 // |host_observers| should be initialized after the second observer is
370 MockObserver mock_observer2
;
371 params
.dispatch_initial_state
= true;
372 host_observers
.AddObserver(&mock_observer2
, params
);
373 StorageObserver::Event
expected_event(params
.filter
, kUsage
, kQuota
);
374 EXPECT_EQ(0, mock_observer1
.EventCount());
375 EXPECT_EQ(1, mock_observer2
.EventCount());
376 EXPECT_EQ(expected_event
, mock_observer2
.LastEvent());
377 EXPECT_TRUE(host_observers
.is_initialized());
378 EXPECT_EQ(NULL
, GetPendingEvent(host_observers
));
379 EXPECT_EQ(0, GetRequiredUpdatesCount(host_observers
));
381 // Verify that both observers will receive events after a usage change.
382 const int64 kDelta
= 2345;
383 expected_event
.usage
+= kDelta
;
384 SetLastNotificationTime(host_observers
, &mock_observer2
);
385 host_observers
.NotifyUsageChange(params
.filter
, kDelta
);
386 EXPECT_EQ(1, mock_observer1
.EventCount());
387 EXPECT_EQ(2, mock_observer2
.EventCount());
388 EXPECT_EQ(expected_event
, mock_observer1
.LastEvent());
389 EXPECT_EQ(expected_event
, mock_observer2
.LastEvent());
390 EXPECT_EQ(NULL
, GetPendingEvent(host_observers
));
391 EXPECT_EQ(0, GetRequiredUpdatesCount(host_observers
));
393 // Verify that the addition of a third observer only causes an event to be
394 // dispatched to the new observer.
395 MockObserver mock_observer3
;
396 params
.dispatch_initial_state
= true;
397 host_observers
.AddObserver(&mock_observer3
, params
);
398 EXPECT_EQ(1, mock_observer1
.EventCount());
399 EXPECT_EQ(2, mock_observer2
.EventCount());
400 EXPECT_EQ(1, mock_observer3
.EventCount());
401 EXPECT_EQ(expected_event
, mock_observer3
.LastEvent());
404 // Verify that negative usage and quota is changed to zero.
405 TEST_F(HostStorageObserversTest
, NegativeUsageAndQuota
) {
406 StorageObserver::MonitorParams
params(kStorageTypePersistent
,
407 GURL(kDefaultOrigin
),
408 base::TimeDelta::FromHours(1),
410 const int64 kUsage
= -324554;
411 const int64 kQuota
= -234354354;
412 quota_manager_
->SetCallbackParams(kUsage
, kQuota
, kQuotaStatusOk
);
414 MockObserver mock_observer
;
415 HostStorageObservers
host_observers(quota_manager_
.get());
416 host_observers
.AddObserver(&mock_observer
, params
);
418 StorageObserver::Event
expected_event(params
.filter
, 0, 0);
419 host_observers
.NotifyUsageChange(params
.filter
, -87324);
420 EXPECT_EQ(expected_event
, mock_observer
.LastEvent());
423 // Verify that HostStorageObservers can recover from a bad initialization.
424 TEST_F(HostStorageObserversTest
, RecoverFromBadUsageInit
) {
425 StorageObserver::MonitorParams
params(kStorageTypePersistent
,
426 GURL(kDefaultOrigin
),
427 base::TimeDelta::FromHours(1),
429 MockObserver mock_observer
;
430 HostStorageObservers
host_observers(quota_manager_
.get());
431 host_observers
.AddObserver(&mock_observer
, params
);
433 // Set up the quota manager to return an error status.
434 const int64 kUsage
= 6656;
435 const int64 kQuota
= 99585556;
436 quota_manager_
->SetCallbackParams(kUsage
, kQuota
, kQuotaErrorNotSupported
);
438 // Verify that |host_observers| is not initialized and an event has not been
440 host_observers
.NotifyUsageChange(params
.filter
, 9438);
441 EXPECT_EQ(0, mock_observer
.EventCount());
442 EXPECT_FALSE(host_observers
.is_initialized());
443 EXPECT_EQ(NULL
, GetPendingEvent(host_observers
));
444 EXPECT_EQ(0, GetRequiredUpdatesCount(host_observers
));
446 // Now ensure that quota manager returns a good status.
447 quota_manager_
->SetCallbackParams(kUsage
, kQuota
, kQuotaStatusOk
);
448 host_observers
.NotifyUsageChange(params
.filter
, 9048543);
449 StorageObserver::Event
expected_event(params
.filter
, kUsage
, kQuota
);
450 EXPECT_EQ(1, mock_observer
.EventCount());
451 EXPECT_EQ(expected_event
, mock_observer
.LastEvent());
452 EXPECT_TRUE(host_observers
.is_initialized());
455 // Verify that HostStorageObservers handle initialization of the cached usage
456 // and quota correctly.
457 TEST_F(HostStorageObserversTest
, AsyncInitialization
) {
458 StorageObserver::MonitorParams
params(kStorageTypePersistent
,
459 GURL(kDefaultOrigin
),
460 base::TimeDelta::FromHours(1),
462 MockObserver mock_observer
;
463 HostStorageObservers
host_observers(quota_manager_
.get());
464 host_observers
.AddObserver(&mock_observer
, params
);
466 // Trigger initialization. Leave the mock quota manager uninitialized so that
467 // the callback is not invoked.
468 host_observers
.NotifyUsageChange(params
.filter
, 7645);
469 EXPECT_EQ(0, mock_observer
.EventCount());
470 EXPECT_FALSE(host_observers
.is_initialized());
471 EXPECT_EQ(NULL
, GetPendingEvent(host_observers
));
472 EXPECT_EQ(0, GetRequiredUpdatesCount(host_observers
));
474 // Simulate notifying |host_observers| of a usage change before initialization
476 const int64 kUsage
= 6656;
477 const int64 kQuota
= 99585556;
478 const int64 kDelta
= 327643;
479 host_observers
.NotifyUsageChange(params
.filter
, kDelta
);
480 EXPECT_EQ(0, mock_observer
.EventCount());
481 EXPECT_FALSE(host_observers
.is_initialized());
482 EXPECT_EQ(NULL
, GetPendingEvent(host_observers
));
483 EXPECT_EQ(0, GetRequiredUpdatesCount(host_observers
));
485 // Simulate an asynchronous callback from QuotaManager.
486 quota_manager_
->SetCallbackParams(kUsage
, kQuota
, kQuotaStatusOk
);
487 quota_manager_
->InvokeCallback();
488 StorageObserver::Event
expected_event(params
.filter
, kUsage
+ kDelta
, kQuota
);
489 EXPECT_EQ(1, mock_observer
.EventCount());
490 EXPECT_EQ(expected_event
, mock_observer
.LastEvent());
491 EXPECT_TRUE(host_observers
.is_initialized());
492 EXPECT_EQ(NULL
, GetPendingEvent(host_observers
));
493 EXPECT_EQ(0, GetRequiredUpdatesCount(host_observers
));
496 // Tests for StorageTypeObservers:
498 typedef StorageTestWithManagerBase StorageTypeObserversTest
;
500 // Test adding and removing observers.
501 TEST_F(StorageTypeObserversTest
, AddRemoveObservers
) {
502 StorageTypeObservers
type_observers(quota_manager_
.get());
504 StorageObserver::MonitorParams
params1(kStorageTypePersistent
,
505 GURL(kDefaultOrigin
),
506 base::TimeDelta::FromHours(1),
508 StorageObserver::MonitorParams
params2(kStorageTypePersistent
,
509 GURL(kAlternativeOrigin
),
510 base::TimeDelta::FromHours(1),
512 std::string host1
= net::GetHostOrSpecFromURL(params1
.filter
.origin
);
513 std::string host2
= net::GetHostOrSpecFromURL(params2
.filter
.origin
);
515 MockObserver mock_observer1
;
516 MockObserver mock_observer2
;
517 MockObserver mock_observer3
;
518 type_observers
.AddObserver(&mock_observer1
, params1
);
519 type_observers
.AddObserver(&mock_observer2
, params1
);
521 type_observers
.AddObserver(&mock_observer1
, params2
);
522 type_observers
.AddObserver(&mock_observer2
, params2
);
523 type_observers
.AddObserver(&mock_observer3
, params2
);
525 // Verify that the observers have been removed correctly.
526 ASSERT_TRUE(type_observers
.GetHostObservers(host1
));
527 ASSERT_TRUE(type_observers
.GetHostObservers(host2
));
528 EXPECT_EQ(2, GetObserverCount(*type_observers
.GetHostObservers(host1
)));
529 EXPECT_EQ(3, GetObserverCount(*type_observers
.GetHostObservers(host2
)));
531 // Remove an observer for a specific filter.
532 type_observers
.RemoveObserverForFilter(&mock_observer1
, params1
.filter
);
533 ASSERT_TRUE(type_observers
.GetHostObservers(host1
));
534 ASSERT_TRUE(type_observers
.GetHostObservers(host2
));
535 EXPECT_EQ(1, GetObserverCount(*type_observers
.GetHostObservers(host1
)));
536 EXPECT_EQ(3, GetObserverCount(*type_observers
.GetHostObservers(host2
)));
538 // Remove all instances of an observer.
539 type_observers
.RemoveObserver(&mock_observer2
);
540 ASSERT_TRUE(type_observers
.GetHostObservers(host2
));
541 EXPECT_EQ(2, GetObserverCount(*type_observers
.GetHostObservers(host2
)));
542 // Observers of host1 has been deleted as it is empty.
543 EXPECT_FALSE(type_observers
.GetHostObservers(host1
));
546 // Tests for StorageMonitor:
548 class StorageMonitorTest
: public StorageTestWithManagerBase
{
551 : storage_monitor_(NULL
),
552 params1_(kStorageTypeTemporary
,
553 GURL(kDefaultOrigin
),
554 base::TimeDelta::FromHours(1),
556 params2_(kStorageTypePersistent
,
557 GURL(kDefaultOrigin
),
558 base::TimeDelta::FromHours(1),
563 void SetUp() override
{
564 StorageTestWithManagerBase::SetUp();
566 storage_monitor_
= quota_manager_
->storage_monitor_
.get();
567 host_
= net::GetHostOrSpecFromURL(params1_
.filter
.origin
);
569 storage_monitor_
->AddObserver(&mock_observer1_
, params1_
);
570 storage_monitor_
->AddObserver(&mock_observer2_
, params1_
);
572 storage_monitor_
->AddObserver(&mock_observer1_
, params2_
);
573 storage_monitor_
->AddObserver(&mock_observer2_
, params2_
);
574 storage_monitor_
->AddObserver(&mock_observer3_
, params2_
);
577 int GetObserverCount(StorageType storage_type
) {
578 const StorageTypeObservers
* type_observers
=
579 storage_monitor_
->GetStorageTypeObservers(storage_type
);
580 return StorageMonitorTestBase::GetObserverCount(
581 *type_observers
->GetHostObservers(host_
));
584 void CheckObserverCount(int expected_temporary
, int expected_persistent
) {
585 ASSERT_TRUE(storage_monitor_
->GetStorageTypeObservers(
586 kStorageTypeTemporary
));
587 ASSERT_TRUE(storage_monitor_
->GetStorageTypeObservers(
588 kStorageTypeTemporary
)->GetHostObservers(host_
));
589 EXPECT_EQ(expected_temporary
, GetObserverCount(kStorageTypeTemporary
));
591 ASSERT_TRUE(storage_monitor_
->GetStorageTypeObservers(
592 kStorageTypePersistent
));
593 ASSERT_TRUE(storage_monitor_
->GetStorageTypeObservers(
594 kStorageTypePersistent
)->GetHostObservers(host_
));
595 EXPECT_EQ(expected_persistent
, GetObserverCount(kStorageTypePersistent
));
598 StorageMonitor
* storage_monitor_
;
599 StorageObserver::MonitorParams params1_
;
600 StorageObserver::MonitorParams params2_
;
601 MockObserver mock_observer1_
;
602 MockObserver mock_observer2_
;
603 MockObserver mock_observer3_
;
607 // Test adding storage observers.
608 TEST_F(StorageMonitorTest
, AddObservers
) {
609 // Verify that the observers are added correctly.
610 CheckObserverCount(2, 3);
613 // Test dispatching events to storage observers.
614 TEST_F(StorageMonitorTest
, EventDispatch
) {
615 // Verify dispatch of events.
616 const int64 kUsage
= 5325;
617 const int64 kQuota
= 903845;
618 quota_manager_
->SetCallbackParams(kUsage
, kQuota
, kQuotaStatusOk
);
619 storage_monitor_
->NotifyUsageChange(params1_
.filter
, 9048543);
621 StorageObserver::Event
expected_event(params1_
.filter
, kUsage
, kQuota
);
622 EXPECT_EQ(1, mock_observer1_
.EventCount());
623 EXPECT_EQ(1, mock_observer2_
.EventCount());
624 EXPECT_EQ(0, mock_observer3_
.EventCount());
625 EXPECT_EQ(expected_event
, mock_observer1_
.LastEvent());
626 EXPECT_EQ(expected_event
, mock_observer2_
.LastEvent());
629 // Test removing all instances of an observer.
630 TEST_F(StorageMonitorTest
, RemoveObserver
) {
631 storage_monitor_
->RemoveObserver(&mock_observer1_
);
632 CheckObserverCount(1, 2);
635 // Test removing an observer for a specific filter.
636 TEST_F(StorageMonitorTest
, RemoveObserverForFilter
) {
637 storage_monitor_
->RemoveObserverForFilter(&mock_observer1_
, params2_
.filter
);
638 CheckObserverCount(2, 2);
641 // Integration test for QuotaManager and StorageMonitor:
643 class StorageMonitorIntegrationTest
: public testing::Test
{
645 void SetUp() override
{
646 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
647 storage_policy_
= new MockSpecialStoragePolicy();
648 quota_manager_
= new QuotaManager(
649 false, data_dir_
.path(), base::ThreadTaskRunnerHandle::Get().get(),
650 base::ThreadTaskRunnerHandle::Get().get(), storage_policy_
.get());
652 client_
= new MockStorageClient(quota_manager_
->proxy(),
654 QuotaClient::kFileSystem
,
657 quota_manager_
->proxy()->RegisterClient(client_
);
660 void TearDown() override
{
661 // This ensures the quota manager is destroyed correctly.
662 quota_manager_
= NULL
;
663 base::RunLoop().RunUntilIdle();
667 base::MessageLoop message_loop_
;
668 base::ScopedTempDir data_dir_
;
669 scoped_refptr
<MockSpecialStoragePolicy
> storage_policy_
;
670 scoped_refptr
<QuotaManager
> quota_manager_
;
671 MockStorageClient
* client_
;
674 // This test simulates a usage change in a quota client and verifies that a
675 // storage observer will receive a storage event.
676 TEST_F(StorageMonitorIntegrationTest
, NotifyUsageEvent
) {
677 const StorageType kTestStorageType
= kStorageTypePersistent
;
678 const int64 kTestUsage
= 234743;
680 // Register the observer.
681 StorageObserver::MonitorParams
params(kTestStorageType
,
682 GURL(kDefaultOrigin
),
683 base::TimeDelta::FromHours(1),
685 MockObserver mock_observer
;
686 quota_manager_
->AddStorageObserver(&mock_observer
, params
);
688 // Fire a usage change.
689 client_
->AddOriginAndNotify(GURL(kDefaultOrigin
),
692 base::RunLoop().RunUntilIdle();
694 // Verify that the observer receives it.
695 ASSERT_EQ(1, mock_observer
.EventCount());
696 const StorageObserver::Event
& event
= mock_observer
.LastEvent();
697 EXPECT_EQ(params
.filter
, event
.filter
);
698 EXPECT_EQ(kTestUsage
, event
.usage
);
701 } // namespace content