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

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

Issue 8070001: Use base::Callback in Quota related code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: choke lint Created 9 years, 2 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 | Annotate | Revision Log
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 <map> 5 #include <map>
6 6
7 #include "base/bind.h"
7 #include "base/file_path.h" 8 #include "base/file_path.h"
8 #include "base/file_util.h" 9 #include "base/file_util.h"
9 #include "base/memory/scoped_callback_factory.h" 10 #include "base/memory/scoped_callback_factory.h"
10 #include "base/message_loop.h" 11 #include "base/message_loop.h"
11 #include "base/message_loop_proxy.h" 12 #include "base/message_loop_proxy.h"
12 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
13 #include "content/browser/in_process_webkit/indexed_db_context.h" 14 #include "content/browser/in_process_webkit/indexed_db_context.h"
14 #include "content/browser/in_process_webkit/indexed_db_quota_client.h" 15 #include "content/browser/in_process_webkit/indexed_db_quota_client.h"
15 #include "content/browser/in_process_webkit/webkit_context.h" 16 #include "content/browser/in_process_webkit/webkit_context.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 10 matching lines...) Expand all
27 public: 28 public:
28 const GURL kOriginA; 29 const GURL kOriginA;
29 const GURL kOriginB; 30 const GURL kOriginB;
30 const GURL kOriginOther; 31 const GURL kOriginOther;
31 32
32 IndexedDBQuotaClientTest() 33 IndexedDBQuotaClientTest()
33 : kOriginA("http://host"), 34 : kOriginA("http://host"),
34 kOriginB("http://host:8000"), 35 kOriginB("http://host:8000"),
35 kOriginOther("http://other"), 36 kOriginOther("http://other"),
36 usage_(0), 37 usage_(0),
37 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 38 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
38 message_loop_(MessageLoop::TYPE_IO), 39 message_loop_(MessageLoop::TYPE_IO),
39 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), 40 webkit_thread_(BrowserThread::WEBKIT, &message_loop_),
40 io_thread_(BrowserThread::IO, &message_loop_) { 41 io_thread_(BrowserThread::IO, &message_loop_) {
41 TestingProfile profile; 42 TestingProfile profile;
42 idb_context_ = profile.GetWebKitContext()->indexed_db_context(); 43 idb_context_ = profile.GetWebKitContext()->indexed_db_context();
43 setup_temp_dir(); 44 setup_temp_dir();
44 } 45 }
45 void setup_temp_dir() { 46 void setup_temp_dir() {
46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
47 FilePath indexeddb_dir = temp_dir_.path().Append( 48 FilePath indexeddb_dir = temp_dir_.path().Append(
48 IndexedDBContext::kIndexedDBDirectory); 49 IndexedDBContext::kIndexedDBDirectory);
49 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); 50 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir));
50 idb_context()->set_data_path(indexeddb_dir); 51 idb_context()->set_data_path(indexeddb_dir);
51 } 52 }
52 53
53 ~IndexedDBQuotaClientTest() { 54 ~IndexedDBQuotaClientTest() {
54 // IndexedDBContext needs to be destructed on BrowserThread::WEBKIT, which 55 // IndexedDBContext needs to be destructed on BrowserThread::WEBKIT, which
55 // is also a member variable of this class. Cause IndexedDBContext's 56 // is also a member variable of this class. Cause IndexedDBContext's
56 // destruction now to ensure that it doesn't outlive BrowserThread::WEBKIT. 57 // destruction now to ensure that it doesn't outlive BrowserThread::WEBKIT.
57 idb_context_ = NULL; 58 idb_context_ = NULL;
58 MessageLoop::current()->RunAllPending(); 59 MessageLoop::current()->RunAllPending();
59 } 60 }
60 61
61 int64 GetOriginUsage( 62 int64 GetOriginUsage(
62 quota::QuotaClient* client, 63 quota::QuotaClient* client,
63 const GURL& origin, 64 const GURL& origin,
64 quota::StorageType type) { 65 quota::StorageType type) {
65 usage_ = -1; 66 usage_ = -1;
66 client->GetOriginUsage(origin, type, 67 client->GetOriginUsage(
67 callback_factory_.NewCallback( 68 origin, type,
68 &IndexedDBQuotaClientTest::OnGetOriginUsageComplete)); 69 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete,
70 weak_factory_.GetWeakPtr()));
69 MessageLoop::current()->RunAllPending(); 71 MessageLoop::current()->RunAllPending();
70 EXPECT_GT(usage_, -1); 72 EXPECT_GT(usage_, -1);
71 return usage_; 73 return usage_;
72 } 74 }
73 75
74 const std::set<GURL>& GetOriginsForType( 76 const std::set<GURL>& GetOriginsForType(
75 quota::QuotaClient* client, 77 quota::QuotaClient* client,
76 quota::StorageType type) { 78 quota::StorageType type) {
77 origins_.clear(); 79 origins_.clear();
78 type_ = quota::kStorageTypeTemporary; 80 type_ = quota::kStorageTypeTemporary;
79 client->GetOriginsForType(type, 81 client->GetOriginsForType(
80 callback_factory_.NewCallback( 82 type,
81 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); 83 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
84 weak_factory_.GetWeakPtr()));
82 MessageLoop::current()->RunAllPending(); 85 MessageLoop::current()->RunAllPending();
83 return origins_; 86 return origins_;
84 } 87 }
85 88
86 const std::set<GURL>& GetOriginsForHost( 89 const std::set<GURL>& GetOriginsForHost(
87 quota::QuotaClient* client, 90 quota::QuotaClient* client,
88 quota::StorageType type, 91 quota::StorageType type,
89 const std::string& host) { 92 const std::string& host) {
90 origins_.clear(); 93 origins_.clear();
91 type_ = quota::kStorageTypeTemporary; 94 type_ = quota::kStorageTypeTemporary;
92 client->GetOriginsForHost(type, host, 95 client->GetOriginsForHost(
93 callback_factory_.NewCallback( 96 type, host,
94 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); 97 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
98 weak_factory_.GetWeakPtr()));
95 MessageLoop::current()->RunAllPending(); 99 MessageLoop::current()->RunAllPending();
96 return origins_; 100 return origins_;
97 } 101 }
98 102
99 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, 103 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client,
100 const GURL& origin_url) { 104 const GURL& origin_url) {
101 delete_status_ = quota::kQuotaStatusUnknown; 105 delete_status_ = quota::kQuotaStatusUnknown;
102 client->DeleteOriginData(origin_url, kTemp, callback_factory_.NewCallback( 106 client->DeleteOriginData(
103 &IndexedDBQuotaClientTest::OnDeleteOriginComplete)); 107 origin_url, kTemp,
108 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete,
109 weak_factory_.GetWeakPtr()));
104 MessageLoop::current()->RunAllPending(); 110 MessageLoop::current()->RunAllPending();
105 return delete_status_; 111 return delete_status_;
106 } 112 }
107 113
108 IndexedDBContext* idb_context() { return idb_context_.get(); } 114 IndexedDBContext* idb_context() { return idb_context_.get(); }
109 115
110 void SetFileSizeTo(const FilePath& path, int size) { 116 void SetFileSizeTo(const FilePath& path, int size) {
111 std::string junk(size, 'a'); 117 std::string junk(size, 'a');
112 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); 118 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size));
113 } 119 }
(...skipping 23 matching lines...) Expand all
137 143
138 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { 144 void OnDeleteOriginComplete(quota::QuotaStatusCode code) {
139 delete_status_ = code; 145 delete_status_ = code;
140 } 146 }
141 147
142 ScopedTempDir temp_dir_; 148 ScopedTempDir temp_dir_;
143 int64 usage_; 149 int64 usage_;
144 std::set<GURL> origins_; 150 std::set<GURL> origins_;
145 quota::StorageType type_; 151 quota::StorageType type_;
146 scoped_refptr<IndexedDBContext> idb_context_; 152 scoped_refptr<IndexedDBContext> idb_context_;
147 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; 153 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_;
148 MessageLoop message_loop_; 154 MessageLoop message_loop_;
149 BrowserThread webkit_thread_; 155 BrowserThread webkit_thread_;
150 BrowserThread io_thread_; 156 BrowserThread io_thread_;
151 quota::QuotaStatusCode delete_status_; 157 quota::QuotaStatusCode delete_status_;
152 }; 158 };
153 159
154 160
155 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { 161 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) {
156 IndexedDBQuotaClient client( 162 IndexedDBQuotaClient client(
157 base::MessageLoopProxy::current(), 163 base::MessageLoopProxy::current(),
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 AddFakeIndexedDB(kOriginA, 1000); 227 AddFakeIndexedDB(kOriginA, 1000);
222 AddFakeIndexedDB(kOriginB, 50); 228 AddFakeIndexedDB(kOriginB, 50);
223 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); 229 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
224 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); 230 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
225 231
226 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); 232 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA);
227 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); 233 EXPECT_EQ(quota::kQuotaStatusOk, delete_status);
228 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); 234 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp));
229 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); 235 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
230 } 236 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698