OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" |
| 6 |
| 7 #include "base/memory/scoped_callback_factory.h" |
| 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/scoped_temp_dir.h" |
| 10 #include "chrome/browser/browsing_data_quota_helper_impl.h" |
| 11 #include "webkit/quota/mock_storage_client.h" |
| 12 #include "webkit/quota/quota_manager.h" |
| 13 |
| 14 class BrowsingDataQuotaHelperTest : public testing::Test { |
| 15 public: |
| 16 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo; |
| 17 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray; |
| 18 |
| 19 BrowsingDataQuotaHelperTest() |
| 20 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 21 db_thread_(BrowserThread::DB, &message_loop_), |
| 22 io_thread_(BrowserThread::IO, &message_loop_), |
| 23 fetching_completed_(true), |
| 24 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
| 25 |
| 26 virtual ~BrowsingDataQuotaHelperTest() {} |
| 27 |
| 28 virtual void SetUp() OVERRIDE { |
| 29 EXPECT_TRUE(dir_.CreateUniqueTempDir()); |
| 30 quota_manager_ = new quota::QuotaManager( |
| 31 false, dir_.path(), |
| 32 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
| 33 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB), |
| 34 NULL); |
| 35 helper_ = new BrowsingDataQuotaHelperImpl( |
| 36 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
| 37 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
| 38 quota_manager_); |
| 39 } |
| 40 |
| 41 virtual void TearDown() OVERRIDE { |
| 42 helper_ = NULL; |
| 43 quota_manager_ = NULL; |
| 44 quota_info_.clear(); |
| 45 MessageLoop::current()->RunAllPending(); |
| 46 } |
| 47 |
| 48 protected: |
| 49 const QuotaInfoArray& quota_info() const { |
| 50 return quota_info_; |
| 51 } |
| 52 |
| 53 bool fetching_completed() const { |
| 54 return fetching_completed_; |
| 55 } |
| 56 |
| 57 void StartFetching() { |
| 58 fetching_completed_ = false; |
| 59 helper_->StartFetching( |
| 60 callback_factory_.NewCallback( |
| 61 &BrowsingDataQuotaHelperTest::FetchCompleted)); |
| 62 } |
| 63 |
| 64 void RegisterClient(const quota::MockOriginData* data, std::size_t data_len) { |
| 65 quota::MockStorageClient* client = |
| 66 new quota::MockStorageClient( |
| 67 quota_manager_->proxy(), data, data_len); |
| 68 quota_manager_->proxy()->RegisterClient(client); |
| 69 client->TouchAllOriginsAndNotify(); |
| 70 } |
| 71 |
| 72 private: |
| 73 void FetchCompleted(const QuotaInfoArray& quota_info) { |
| 74 quota_info_ = quota_info; |
| 75 fetching_completed_ = true; |
| 76 } |
| 77 |
| 78 MessageLoop message_loop_; |
| 79 BrowserThread ui_thread_; |
| 80 BrowserThread db_thread_; |
| 81 BrowserThread io_thread_; |
| 82 scoped_refptr<quota::QuotaManager> quota_manager_; |
| 83 |
| 84 ScopedTempDir dir_; |
| 85 scoped_refptr<BrowsingDataQuotaHelper> helper_; |
| 86 |
| 87 bool fetching_completed_; |
| 88 QuotaInfoArray quota_info_; |
| 89 base::ScopedCallbackFactory<BrowsingDataQuotaHelperTest> callback_factory_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest); |
| 92 }; |
| 93 |
| 94 TEST_F(BrowsingDataQuotaHelperTest, Empty) { |
| 95 StartFetching(); |
| 96 MessageLoop::current()->RunAllPending(); |
| 97 EXPECT_TRUE(fetching_completed()); |
| 98 EXPECT_TRUE(quota_info().empty()); |
| 99 } |
| 100 |
| 101 TEST_F(BrowsingDataQuotaHelperTest, FetchData) { |
| 102 const quota::MockOriginData kOrigins[] = { |
| 103 {"http://example.com/", quota::kStorageTypeTemporary, 1}, |
| 104 {"https://example.com/", quota::kStorageTypeTemporary, 10}, |
| 105 {"http://example.com/", quota::kStorageTypePersistent, 100}, |
| 106 {"http://example2.com/", quota::kStorageTypeTemporary, 1000}, |
| 107 }; |
| 108 |
| 109 RegisterClient(kOrigins, arraysize(kOrigins)); |
| 110 StartFetching(); |
| 111 MessageLoop::current()->RunAllPending(); |
| 112 EXPECT_TRUE(fetching_completed()); |
| 113 |
| 114 std::set<QuotaInfo> expected, actual; |
| 115 actual.insert(quota_info().begin(), quota_info().end()); |
| 116 expected.insert(QuotaInfo("example.com", 11, 100)); |
| 117 expected.insert(QuotaInfo("example2.com", 1000, 0)); |
| 118 EXPECT_TRUE(expected == actual); |
| 119 } |
OLD | NEW |