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

Side by Side Diff: content/browser/indexed_db/indexed_db_quota_client_unittest.cc

Issue 2930183002: Let IndexedDBContextImpl create its own task runner (Closed)
Patch Set: Use ScopedTaskEnvironment for most tests Created 3 years, 6 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/test/test_simple_task_runner.h" 15 #include "base/test/scoped_task_environment.h"
16 #include "base/threading/sequenced_task_runner_handle.h"
16 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
17 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
18 #include "content/browser/browser_thread_impl.h" 19 #include "content/browser/browser_thread_impl.h"
19 #include "content/browser/indexed_db/indexed_db_context_impl.h" 20 #include "content/browser/indexed_db/indexed_db_context_impl.h"
20 #include "content/browser/indexed_db/indexed_db_quota_client.h" 21 #include "content/browser/indexed_db/indexed_db_quota_client.h"
21 #include "content/public/browser/storage_partition.h" 22 #include "content/public/browser/storage_partition.h"
22 #include "content/public/test/test_browser_context.h" 23 #include "content/public/test/test_browser_context.h"
23 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "storage/browser/test/mock_quota_manager.h" 25 #include "storage/browser/test/mock_quota_manager.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 // Declared to shorten the line lengths. 28 // Declared to shorten the line lengths.
28 static const storage::StorageType kTemp = storage::kStorageTypeTemporary; 29 static const storage::StorageType kTemp = storage::kStorageTypeTemporary;
29 static const storage::StorageType kPerm = storage::kStorageTypePersistent; 30 static const storage::StorageType kPerm = storage::kStorageTypePersistent;
30 31
31 namespace content { 32 namespace content {
32 33
33 // Base class for our test fixtures. 34 // Base class for our test fixtures.
34 class IndexedDBQuotaClientTest : public testing::Test { 35 class IndexedDBQuotaClientTest : public testing::Test {
35 public: 36 public:
36 const GURL kOriginA; 37 const GURL kOriginA;
37 const GURL kOriginB; 38 const GURL kOriginB;
38 const GURL kOriginOther; 39 const GURL kOriginOther;
39 40
40 IndexedDBQuotaClientTest() 41 IndexedDBQuotaClientTest()
41 : kOriginA("http://host"), 42 : kOriginA("http://host"),
42 kOriginB("http://host:8000"), 43 kOriginB("http://host:8000"),
43 kOriginOther("http://other"), 44 kOriginOther("http://other"),
44 usage_(0), 45 usage_(0),
45 task_runner_(new base::TestSimpleTaskRunner), 46 scoped_task_environment_(
47 base::test::ScopedTaskEnvironment::MainThreadType::UI),
46 weak_factory_(this) { 48 weak_factory_(this) {
47 browser_context_.reset(new TestBrowserContext()); 49 browser_context_.reset(new TestBrowserContext());
48 50
49 scoped_refptr<storage::QuotaManager> quota_manager = 51 scoped_refptr<storage::QuotaManager> quota_manager =
50 new MockQuotaManager(false /*in_memory*/, browser_context_->GetPath(), 52 new MockQuotaManager(false /*in_memory*/, browser_context_->GetPath(),
51 base::ThreadTaskRunnerHandle::Get(), 53 base::ThreadTaskRunnerHandle::Get(),
52 base::ThreadTaskRunnerHandle::Get(), 54 base::SequencedTaskRunnerHandle::Get(),
53 browser_context_->GetSpecialStoragePolicy()); 55 browser_context_->GetSpecialStoragePolicy());
54 56
55 idb_context_ = 57 idb_context_ = new IndexedDBContextImpl(
56 new IndexedDBContextImpl(browser_context_->GetPath(), 58 browser_context_->GetPath(),
57 browser_context_->GetSpecialStoragePolicy(), 59 browser_context_->GetSpecialStoragePolicy(), quota_manager->proxy());
58 quota_manager->proxy(),
59 task_runner_.get());
60 base::RunLoop().RunUntilIdle(); 60 base::RunLoop().RunUntilIdle();
61 setup_temp_dir(); 61 setup_temp_dir();
62 } 62 }
63 63
64 void FlushIndexedDBTaskRunner() { task_runner_->RunUntilIdle(); }
65
66 void setup_temp_dir() { 64 void setup_temp_dir() {
67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 65 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
68 base::FilePath indexeddb_dir = 66 base::FilePath indexeddb_dir =
69 temp_dir_.GetPath().Append(IndexedDBContextImpl::kIndexedDBDirectory); 67 temp_dir_.GetPath().Append(IndexedDBContextImpl::kIndexedDBDirectory);
70 ASSERT_TRUE(base::CreateDirectory(indexeddb_dir)); 68 ASSERT_TRUE(base::CreateDirectory(indexeddb_dir));
71 idb_context()->set_data_path_for_testing(indexeddb_dir); 69 idb_context()->set_data_path_for_testing(indexeddb_dir);
72 } 70 }
73 71
74 ~IndexedDBQuotaClientTest() override { 72 ~IndexedDBQuotaClientTest() override {
75 FlushIndexedDBTaskRunner(); 73 scoped_task_environment_.RunUntilIdle();
76 idb_context_ = NULL; 74 idb_context_ = NULL;
77 browser_context_.reset(); 75 browser_context_.reset();
78 base::RunLoop().RunUntilIdle(); 76 base::RunLoop().RunUntilIdle();
79 } 77 }
80 78
81 int64_t GetOriginUsage(storage::QuotaClient* client, 79 int64_t GetOriginUsage(storage::QuotaClient* client,
82 const GURL& origin, 80 const GURL& origin,
83 storage::StorageType type) { 81 storage::StorageType type) {
84 usage_ = -1; 82 usage_ = -1;
85 client->GetOriginUsage( 83 client->GetOriginUsage(
86 origin, 84 origin,
87 type, 85 type,
88 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, 86 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete,
89 weak_factory_.GetWeakPtr())); 87 weak_factory_.GetWeakPtr()));
90 FlushIndexedDBTaskRunner(); 88 scoped_task_environment_.RunUntilIdle();
91 base::RunLoop().RunUntilIdle();
92 EXPECT_GT(usage_, -1); 89 EXPECT_GT(usage_, -1);
93 return usage_; 90 return usage_;
94 } 91 }
95 92
96 const std::set<GURL>& GetOriginsForType(storage::QuotaClient* client, 93 const std::set<GURL>& GetOriginsForType(storage::QuotaClient* client,
97 storage::StorageType type) { 94 storage::StorageType type) {
98 origins_.clear(); 95 origins_.clear();
99 client->GetOriginsForType( 96 client->GetOriginsForType(
100 type, 97 type,
101 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, 98 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
102 weak_factory_.GetWeakPtr())); 99 weak_factory_.GetWeakPtr()));
103 FlushIndexedDBTaskRunner(); 100 scoped_task_environment_.RunUntilIdle();
104 base::RunLoop().RunUntilIdle();
105 return origins_; 101 return origins_;
106 } 102 }
107 103
108 const std::set<GURL>& GetOriginsForHost(storage::QuotaClient* client, 104 const std::set<GURL>& GetOriginsForHost(storage::QuotaClient* client,
109 storage::StorageType type, 105 storage::StorageType type,
110 const std::string& host) { 106 const std::string& host) {
111 origins_.clear(); 107 origins_.clear();
112 client->GetOriginsForHost( 108 client->GetOriginsForHost(
113 type, 109 type,
114 host, 110 host,
115 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, 111 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
116 weak_factory_.GetWeakPtr())); 112 weak_factory_.GetWeakPtr()));
117 FlushIndexedDBTaskRunner(); 113 scoped_task_environment_.RunUntilIdle();
118 base::RunLoop().RunUntilIdle();
119 return origins_; 114 return origins_;
120 } 115 }
121 116
122 storage::QuotaStatusCode DeleteOrigin(storage::QuotaClient* client, 117 storage::QuotaStatusCode DeleteOrigin(storage::QuotaClient* client,
123 const GURL& origin_url) { 118 const GURL& origin_url) {
124 delete_status_ = storage::kQuotaStatusUnknown; 119 delete_status_ = storage::kQuotaStatusUnknown;
125 client->DeleteOriginData( 120 client->DeleteOriginData(
126 origin_url, 121 origin_url,
127 kTemp, 122 kTemp,
128 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, 123 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete,
129 weak_factory_.GetWeakPtr())); 124 weak_factory_.GetWeakPtr()));
130 FlushIndexedDBTaskRunner(); 125 scoped_task_environment_.RunUntilIdle();
131 base::RunLoop().RunUntilIdle();
132 return delete_status_; 126 return delete_status_;
133 } 127 }
134 128
135 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } 129 IndexedDBContextImpl* idb_context() { return idb_context_.get(); }
136 130
137 void SetFileSizeTo(const base::FilePath& path, int size) { 131 void SetFileSizeTo(const base::FilePath& path, int size) {
138 std::string junk(size, 'a'); 132 std::string junk(size, 'a');
139 ASSERT_EQ(size, base::WriteFile(path, junk.c_str(), size)); 133 ASSERT_EQ(size, base::WriteFile(path, junk.c_str(), size));
140 } 134 }
141 135
(...skipping 16 matching lines...) Expand all
158 origins_ = origins; 152 origins_ = origins;
159 } 153 }
160 154
161 void OnDeleteOriginComplete(storage::QuotaStatusCode code) { 155 void OnDeleteOriginComplete(storage::QuotaStatusCode code) {
162 delete_status_ = code; 156 delete_status_ = code;
163 } 157 }
164 158
165 base::ScopedTempDir temp_dir_; 159 base::ScopedTempDir temp_dir_;
166 int64_t usage_; 160 int64_t usage_;
167 std::set<GURL> origins_; 161 std::set<GURL> origins_;
168 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 162 base::test::ScopedTaskEnvironment scoped_task_environment_;
169 scoped_refptr<IndexedDBContextImpl> idb_context_; 163 scoped_refptr<IndexedDBContextImpl> idb_context_;
170 content::TestBrowserThreadBundle thread_bundle_; 164 content::TestBrowserThreadBundle thread_bundle_;
171 std::unique_ptr<TestBrowserContext> browser_context_; 165 std::unique_ptr<TestBrowserContext> browser_context_;
172 storage::QuotaStatusCode delete_status_; 166 storage::QuotaStatusCode delete_status_;
173 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; 167 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_;
174 168
175 DISALLOW_COPY_AND_ASSIGN(IndexedDBQuotaClientTest); 169 DISALLOW_COPY_AND_ASSIGN(IndexedDBQuotaClientTest);
176 }; 170 };
177 171
178 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { 172 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); 232 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
239 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); 233 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
240 234
241 storage::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); 235 storage::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA);
242 EXPECT_EQ(storage::kQuotaStatusOk, delete_status); 236 EXPECT_EQ(storage::kQuotaStatusOk, delete_status);
243 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); 237 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp));
244 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); 238 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
245 } 239 }
246 240
247 } // namespace content 241 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698