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

Side by Side Diff: webkit/appcache/appcache_storage_impl_unittest.cc

Issue 7863009: Replace ancient crufty AppCacheThread with much improved MessageLoopProxy usage. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 3 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
« no previous file with comments | « webkit/appcache/appcache_storage_impl.cc ('k') | webkit/appcache/appcache_thread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stack> 5 #include <stack>
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/threading/thread.h" 8 #include "base/threading/thread.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webkit/appcache/appcache.h" 12 #include "webkit/appcache/appcache.h"
13 #include "webkit/appcache/appcache_database.h" 13 #include "webkit/appcache/appcache_database.h"
14 #include "webkit/appcache/appcache_entry.h" 14 #include "webkit/appcache/appcache_entry.h"
15 #include "webkit/appcache/appcache_group.h" 15 #include "webkit/appcache/appcache_group.h"
16 #include "webkit/appcache/appcache_service.h" 16 #include "webkit/appcache/appcache_service.h"
17 #include "webkit/appcache/appcache_storage_impl.h" 17 #include "webkit/appcache/appcache_storage_impl.h"
18 #include "webkit/quota/quota_manager.h" 18 #include "webkit/quota/quota_manager.h"
19 #include "webkit/tools/test_shell/simple_appcache_system.h"
20 19
21 namespace appcache { 20 namespace appcache {
22 21
23 namespace { 22 namespace {
24 23
25 const base::Time kZeroTime; 24 const base::Time kZeroTime;
26 const GURL kManifestUrl("http://blah/manifest"); 25 const GURL kManifestUrl("http://blah/manifest");
27 const GURL kManifestUrl2("http://blah/manifest2"); 26 const GURL kManifestUrl2("http://blah/manifest2");
28 const GURL kManifestUrl3("http://blah/manifest3"); 27 const GURL kManifestUrl3("http://blah/manifest3");
29 const GURL kEntryUrl("http://blah/entry"); 28 const GURL kEntryUrl("http://blah/entry");
30 const GURL kEntryUrl2("http://blah/entry2"); 29 const GURL kEntryUrl2("http://blah/entry2");
31 const GURL kFallbackNamespace("http://blah/fallback_namespace/"); 30 const GURL kFallbackNamespace("http://blah/fallback_namespace/");
32 const GURL kFallbackNamespace2("http://blah/fallback_namespace/longer"); 31 const GURL kFallbackNamespace2("http://blah/fallback_namespace/longer");
33 const GURL kFallbackTestUrl("http://blah/fallback_namespace/longer/test"); 32 const GURL kFallbackTestUrl("http://blah/fallback_namespace/longer/test");
34 const GURL kOnlineNamespace("http://blah/online_namespace"); 33 const GURL kOnlineNamespace("http://blah/online_namespace");
35 const GURL kOnlineNamespaceWithinFallback( 34 const GURL kOnlineNamespaceWithinFallback(
36 "http://blah/fallback_namespace/online/"); 35 "http://blah/fallback_namespace/online/");
37 const GURL kOrigin(kManifestUrl.GetOrigin()); 36 const GURL kOrigin(kManifestUrl.GetOrigin());
38 37
39 const int kManifestEntryIdOffset = 100; 38 const int kManifestEntryIdOffset = 100;
40 const int kFallbackEntryIdOffset = 1000; 39 const int kFallbackEntryIdOffset = 1000;
41 40
42 const GURL kDefaultEntryUrl("http://blah/makecacheandgroup_default_entry"); 41 const GURL kDefaultEntryUrl("http://blah/makecacheandgroup_default_entry");
43 const int kDefaultEntrySize = 10; 42 const int kDefaultEntrySize = 10;
44 const int kDefaultEntryIdOffset = 12345; 43 const int kDefaultEntryIdOffset = 12345;
45 44
46 const int kMockQuota = 5000; 45 const int kMockQuota = 5000;
47 46
48 // For the duration of this test case, we hijack the AppCacheThread API
49 // calls and implement them in terms of the io and db threads created here.
50
51 scoped_ptr<base::Thread> io_thread; 47 scoped_ptr<base::Thread> io_thread;
52 scoped_ptr<base::Thread> db_thread; 48 scoped_ptr<base::Thread> db_thread;
53 49
54 class TestThreadProvider : public SimpleAppCacheSystem::ThreadProvider {
55 public:
56 virtual bool PostTask(
57 int id,
58 const tracked_objects::Location& from_here,
59 Task* task) {
60 GetMessageLoop(id)->PostTask(from_here, task);
61 return true;
62 }
63
64 virtual bool CurrentlyOn(int id) {
65 return MessageLoop::current() == GetMessageLoop(id);
66 }
67
68 MessageLoop* GetMessageLoop(int id) {
69 DCHECK(io_thread.get() && db_thread.get());
70 if (id == SimpleAppCacheSystem::IO_THREAD_ID)
71 return io_thread->message_loop();
72 if (id == SimpleAppCacheSystem::DB_THREAD_ID)
73 return db_thread->message_loop();
74 NOTREACHED() << "Invalid AppCacheThreadID value";
75 return NULL;
76 }
77 };
78
79 TestThreadProvider thread_provider;
80
81 } // namespace 50 } // namespace
82 51
83 class AppCacheStorageImplTest : public testing::Test { 52 class AppCacheStorageImplTest : public testing::Test {
84 public: 53 public:
85 class MockStorageDelegate : public AppCacheStorage::Delegate { 54 class MockStorageDelegate : public AppCacheStorage::Delegate {
86 public: 55 public:
87 explicit MockStorageDelegate(AppCacheStorageImplTest* test) 56 explicit MockStorageDelegate(AppCacheStorageImplTest* test)
88 : loaded_cache_id_(0), stored_group_success_(false), 57 : loaded_cache_id_(0), stored_group_success_(false),
89 would_exceed_quota_(false), obsoleted_success_(false), 58 would_exceed_quota_(false), obsoleted_success_(false),
90 found_cache_id_(kNoCacheId), test_(test) { 59 found_cache_id_(kNoCacheId), test_(test) {
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 }; 226 };
258 227
259 228
260 static void SetUpTestCase() { 229 static void SetUpTestCase() {
261 io_thread.reset(new base::Thread("AppCacheTest.IOThread")); 230 io_thread.reset(new base::Thread("AppCacheTest.IOThread"));
262 base::Thread::Options options(MessageLoop::TYPE_IO, 0); 231 base::Thread::Options options(MessageLoop::TYPE_IO, 0);
263 ASSERT_TRUE(io_thread->StartWithOptions(options)); 232 ASSERT_TRUE(io_thread->StartWithOptions(options));
264 233
265 db_thread.reset(new base::Thread("AppCacheTest::DBThread")); 234 db_thread.reset(new base::Thread("AppCacheTest::DBThread"));
266 ASSERT_TRUE(db_thread->Start()); 235 ASSERT_TRUE(db_thread->Start());
267
268 SimpleAppCacheSystem::set_thread_provider(&thread_provider);
269 } 236 }
270 237
271 static void TearDownTestCase() { 238 static void TearDownTestCase() {
272 SimpleAppCacheSystem::set_thread_provider(NULL);
273 io_thread.reset(NULL); 239 io_thread.reset(NULL);
274 db_thread.reset(NULL); 240 db_thread.reset(NULL);
275 } 241 }
276 242
277 // Test harness -------------------------------------------------- 243 // Test harness --------------------------------------------------
278 244
279 AppCacheStorageImplTest() { 245 AppCacheStorageImplTest() {
280 } 246 }
281 247
282 template <class Method> 248 template <class Method>
283 void RunTestOnIOThread(Method method) { 249 void RunTestOnIOThread(Method method) {
284 test_finished_event_ .reset(new base::WaitableEvent(false, false)); 250 test_finished_event_ .reset(new base::WaitableEvent(false, false));
285 io_thread->message_loop()->PostTask( 251 io_thread->message_loop()->PostTask(
286 FROM_HERE, new WrapperTask<Method>(this, method)); 252 FROM_HERE, new WrapperTask<Method>(this, method));
287 test_finished_event_->Wait(); 253 test_finished_event_->Wait();
288 } 254 }
289 255
290 void SetUpTest() { 256 void SetUpTest() {
291 DCHECK(MessageLoop::current() == io_thread->message_loop()); 257 DCHECK(MessageLoop::current() == io_thread->message_loop());
292 service_.reset(new AppCacheService(NULL)); 258 service_.reset(new AppCacheService(NULL));
293 service_->Initialize(FilePath(), NULL); 259 service_->Initialize(FilePath(), db_thread->message_loop_proxy(), NULL);
294 mock_quota_manager_proxy_ = new MockQuotaManagerProxy(); 260 mock_quota_manager_proxy_ = new MockQuotaManagerProxy();
295 service_->quota_manager_proxy_ = mock_quota_manager_proxy_; 261 service_->quota_manager_proxy_ = mock_quota_manager_proxy_;
296 delegate_.reset(new MockStorageDelegate(this)); 262 delegate_.reset(new MockStorageDelegate(this));
297 } 263 }
298 264
299 void TearDownTest() { 265 void TearDownTest() {
300 DCHECK(MessageLoop::current() == io_thread->message_loop()); 266 DCHECK(MessageLoop::current() == io_thread->message_loop());
301 storage()->CancelDelegateCallbacks(delegate()); 267 storage()->CancelDelegateCallbacks(delegate());
302 group_ = NULL; 268 group_ = NULL;
303 cache_ = NULL; 269 cache_ = NULL;
(...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after
1349 &AppCacheStorageImplTest::FindMainResponseExclusionsInWorkingSet); 1315 &AppCacheStorageImplTest::FindMainResponseExclusionsInWorkingSet);
1350 } 1316 }
1351 1317
1352 // That's all folks! 1318 // That's all folks!
1353 1319
1354 } // namespace appcache 1320 } // namespace appcache
1355 1321
1356 // AppCacheStorageImplTest is expected to always live longer than the 1322 // AppCacheStorageImplTest is expected to always live longer than the
1357 // runnable methods. This lets us call NewRunnableMethod on its instances. 1323 // runnable methods. This lets us call NewRunnableMethod on its instances.
1358 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheStorageImplTest); 1324 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheStorageImplTest);
OLDNEW
« no previous file with comments | « webkit/appcache/appcache_storage_impl.cc ('k') | webkit/appcache/appcache_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698