OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <vector> | |
6 | |
7 #include "chrome/browser/chrome_thread.h" | 5 #include "chrome/browser/chrome_thread.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "testing/platform_test.h" | 7 #include "testing/platform_test.h" |
10 | 8 |
11 typedef PlatformTest ChromeThreadTest; | 9 class ChromeThreadTest : public testing::Test { |
| 10 public: |
| 11 void Release() { |
| 12 CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 13 loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 14 } |
12 | 15 |
13 TEST_F(ChromeThreadTest, Get) { | 16 protected: |
14 /* | 17 virtual void SetUp() { |
15 // TODO(jabdelmalek): rewrite this test when the change to delete objects on | 18 file_thread_.reset(new ChromeThread(ChromeThread::FILE)); |
16 // a specific thread lands. | 19 io_thread_.reset(new ChromeThread(ChromeThread::IO)); |
17 scoped_ptr<ChromeThread> io_thread; | 20 file_thread_->Start(); |
18 scoped_ptr<ChromeThread> file_thread; | 21 io_thread_->Start(); |
19 scoped_ptr<ChromeThread> db_thread; | 22 } |
20 | 23 |
21 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL); | 24 virtual void TearDown() { |
22 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL); | 25 file_thread_->Stop(); |
23 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL); | 26 io_thread_->Stop(); |
| 27 } |
24 | 28 |
25 // Phase 1: Create threads. | 29 static void BasicFunction(MessageLoop* message_loop) { |
| 30 CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 31 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 32 } |
26 | 33 |
27 io_thread.reset(new ChromeThread(ChromeThread::IO)); | 34 class DummyTask : public Task { |
28 file_thread.reset(new ChromeThread(ChromeThread::FILE)); | 35 public: |
29 db_thread.reset(new ChromeThread(ChromeThread::DB)); | 36 DummyTask(bool* deleted) : deleted_(deleted) { } |
| 37 ~DummyTask() { |
| 38 *deleted_ = true; |
| 39 } |
30 | 40 |
31 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL); | 41 void Run() { |
32 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL); | 42 CHECK(false); |
33 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL); | 43 } |
34 | 44 |
35 // Phase 2: Start the threads. | 45 private: |
| 46 bool* deleted_; |
| 47 }; |
36 | 48 |
37 io_thread->Start(); | 49 class DeletedOnIO |
38 file_thread->Start(); | 50 : public base::RefCountedThreadSafe< |
39 db_thread->Start(); | 51 DeletedOnIO, ChromeThread::DeleteOnIOThread> { |
| 52 public: |
| 53 DeletedOnIO(MessageLoop* message_loop) : message_loop_(message_loop) { } |
40 | 54 |
41 EXPECT_TRUE(io_thread->message_loop() != NULL); | 55 ~DeletedOnIO() { |
42 EXPECT_TRUE(file_thread->message_loop() != NULL); | 56 CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
43 EXPECT_TRUE(db_thread->message_loop() != NULL); | 57 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 58 } |
44 | 59 |
45 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == | 60 private: |
46 io_thread->message_loop()); | 61 MessageLoop* message_loop_; |
47 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == | 62 }; |
48 file_thread->message_loop()); | |
49 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == | |
50 db_thread->message_loop()); | |
51 | 63 |
52 // Phase 3: Stop the threads. | 64 class NeverDeleted |
| 65 : public base::RefCountedThreadSafe< |
| 66 NeverDeleted, ChromeThread::DeleteOnWebKitThread> { |
| 67 public: |
| 68 ~NeverDeleted() { |
| 69 CHECK(false); |
| 70 } |
| 71 }; |
53 | 72 |
54 io_thread->Stop(); | 73 private: |
55 file_thread->Stop(); | 74 scoped_ptr<ChromeThread> file_thread_; |
56 db_thread->Stop(); | 75 scoped_ptr<ChromeThread> io_thread_; |
| 76 MessageLoop loop_; |
| 77 }; |
57 | 78 |
58 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL); | 79 TEST_F(ChromeThreadTest, PostTask) { |
59 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL); | 80 ChromeThread::PostTask( |
60 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL); | 81 ChromeThread::IO, FROM_HERE, |
| 82 NewRunnableFunction(&BasicFunction, MessageLoop::current())); |
| 83 MessageLoop::current()->Run(); |
| 84 } |
61 | 85 |
62 // Phase 4: Destroy the threads. | 86 TEST_F(ChromeThreadTest, Release) { |
| 87 ChromeThread::ReleaseSoon(ChromeThread::FILE, FROM_HERE, this); |
| 88 MessageLoop::current()->Run(); |
| 89 } |
63 | 90 |
64 io_thread.reset(); | 91 TEST_F(ChromeThreadTest, TaskToNonExistentThreadIsDeleted) { |
65 file_thread.reset(); | 92 bool deleted = false; |
66 db_thread.reset(); | 93 ChromeThread::PostTask( |
| 94 ChromeThread::WEBKIT, FROM_HERE, |
| 95 new DummyTask(&deleted)); |
| 96 EXPECT_TRUE(deleted); |
| 97 } |
67 | 98 |
68 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL); | 99 TEST_F(ChromeThreadTest, ReleasedOnCorrectThread) { |
69 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL); | 100 { |
70 EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL); | 101 scoped_refptr<DeletedOnIO> test(new DeletedOnIO(MessageLoop::current())); |
71 */ | 102 } |
| 103 MessageLoop::current()->Run(); |
72 } | 104 } |
| 105 |
| 106 TEST_F(ChromeThreadTest, NotReleasedIfTargetThreadNonExistent) { |
| 107 scoped_refptr<NeverDeleted> test(new NeverDeleted()); |
| 108 } |
OLD | NEW |