OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/sequenced_task_runner_helpers.h" | 10 #include "base/sequenced_task_runner_helpers.h" |
11 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 class BrowserThreadTest : public testing::Test { | 18 class BrowserThreadTest : public testing::Test { |
19 public: | 19 public: |
20 void Release() const { | 20 void Release() const { |
21 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 21 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
22 loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 22 loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
23 } | 23 } |
24 | 24 |
25 protected: | 25 protected: |
26 virtual void SetUp() { | 26 virtual void SetUp() { |
27 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); | 27 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); |
28 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); | 28 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
29 ui_thread_->Start(); | 29 ui_thread_->Start(); |
30 file_thread_->Start(); | 30 file_thread_->Start(); |
31 } | 31 } |
32 | 32 |
33 virtual void TearDown() { | 33 virtual void TearDown() { |
34 ui_thread_->Stop(); | 34 ui_thread_->Stop(); |
35 file_thread_->Stop(); | 35 file_thread_->Stop(); |
36 } | 36 } |
37 | 37 |
38 static void BasicFunction(MessageLoop* message_loop) { | 38 static void BasicFunction(base::MessageLoop* message_loop) { |
39 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 39 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
40 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 40 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
41 } | 41 } |
42 | 42 |
43 class DeletedOnFile | 43 class DeletedOnFile |
44 : public base::RefCountedThreadSafe< | 44 : public base::RefCountedThreadSafe< |
45 DeletedOnFile, BrowserThread::DeleteOnFileThread> { | 45 DeletedOnFile, BrowserThread::DeleteOnFileThread> { |
46 public: | 46 public: |
47 explicit DeletedOnFile(MessageLoop* message_loop) | 47 explicit DeletedOnFile(base::MessageLoop* message_loop) |
48 : message_loop_(message_loop) { } | 48 : message_loop_(message_loop) {} |
49 | 49 |
50 private: | 50 private: |
51 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | 51 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; |
52 friend class base::DeleteHelper<DeletedOnFile>; | 52 friend class base::DeleteHelper<DeletedOnFile>; |
53 | 53 |
54 ~DeletedOnFile() { | 54 ~DeletedOnFile() { |
55 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 55 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
56 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 56 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
57 } | 57 } |
58 | 58 |
59 MessageLoop* message_loop_; | 59 base::MessageLoop* message_loop_; |
60 }; | 60 }; |
61 | 61 |
62 private: | 62 private: |
63 scoped_ptr<BrowserThreadImpl> ui_thread_; | 63 scoped_ptr<BrowserThreadImpl> ui_thread_; |
64 scoped_ptr<BrowserThreadImpl> file_thread_; | 64 scoped_ptr<BrowserThreadImpl> file_thread_; |
65 // It's kind of ugly to make this mutable - solely so we can post the Quit | 65 // It's kind of ugly to make this mutable - solely so we can post the Quit |
66 // Task from Release(). This should be fixed. | 66 // Task from Release(). This should be fixed. |
67 mutable MessageLoop loop_; | 67 mutable base::MessageLoop loop_; |
68 }; | 68 }; |
69 | 69 |
70 TEST_F(BrowserThreadTest, PostTask) { | 70 TEST_F(BrowserThreadTest, PostTask) { |
71 BrowserThread::PostTask( | 71 BrowserThread::PostTask( |
72 BrowserThread::FILE, FROM_HERE, | 72 BrowserThread::FILE, |
73 base::Bind(&BasicFunction, MessageLoop::current())); | 73 FROM_HERE, |
74 MessageLoop::current()->Run(); | 74 base::Bind(&BasicFunction, base::MessageLoop::current())); |
| 75 base::MessageLoop::current()->Run(); |
75 } | 76 } |
76 | 77 |
77 TEST_F(BrowserThreadTest, Release) { | 78 TEST_F(BrowserThreadTest, Release) { |
78 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); | 79 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); |
79 MessageLoop::current()->Run(); | 80 base::MessageLoop::current()->Run(); |
80 } | 81 } |
81 | 82 |
82 TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) { | 83 TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) { |
83 { | 84 { |
84 scoped_refptr<DeletedOnFile> test( | 85 scoped_refptr<DeletedOnFile> test( |
85 new DeletedOnFile(MessageLoop::current())); | 86 new DeletedOnFile(base::MessageLoop::current())); |
86 } | 87 } |
87 MessageLoop::current()->Run(); | 88 base::MessageLoop::current()->Run(); |
88 } | 89 } |
89 | 90 |
90 TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxy) { | 91 TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxy) { |
91 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 92 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
92 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); | 93 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
93 message_loop_proxy->PostTask( | 94 message_loop_proxy->PostTask( |
94 FROM_HERE, base::Bind(&BasicFunction, MessageLoop::current())); | 95 FROM_HERE, base::Bind(&BasicFunction, base::MessageLoop::current())); |
95 MessageLoop::current()->Run(); | 96 base::MessageLoop::current()->Run(); |
96 } | 97 } |
97 | 98 |
98 TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) { | 99 TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) { |
99 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 100 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
100 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 101 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
101 message_loop_proxy->ReleaseSoon(FROM_HERE, this); | 102 message_loop_proxy->ReleaseSoon(FROM_HERE, this); |
102 MessageLoop::current()->Run(); | 103 base::MessageLoop::current()->Run(); |
103 } | 104 } |
104 | 105 |
105 TEST_F(BrowserThreadTest, PostTaskAndReply) { | 106 TEST_F(BrowserThreadTest, PostTaskAndReply) { |
106 // Most of the heavy testing for PostTaskAndReply() is done inside the | 107 // Most of the heavy testing for PostTaskAndReply() is done inside the |
107 // MessageLoopProxy test. This just makes sure we get piped through at all. | 108 // MessageLoopProxy test. This just makes sure we get piped through at all. |
108 ASSERT_TRUE(BrowserThread::PostTaskAndReply( | 109 ASSERT_TRUE(BrowserThread::PostTaskAndReply( |
109 BrowserThread::FILE, | 110 BrowserThread::FILE, |
110 FROM_HERE, | 111 FROM_HERE, |
111 base::Bind(&base::DoNothing), | 112 base::Bind(&base::DoNothing), |
112 base::Bind(&MessageLoop::Quit, | 113 base::Bind(&base::MessageLoop::Quit, |
113 base::Unretained(MessageLoop::current()->current())))); | 114 base::Unretained(base::MessageLoop::current()->current())))); |
114 MessageLoop::current()->Run(); | 115 base::MessageLoop::current()->Run(); |
115 } | 116 } |
116 | 117 |
117 } // namespace content | 118 } // namespace content |
OLD | NEW |