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/location.h" | 7 #include "base/location.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/sequenced_task_runner_helpers.h" | 9 #include "base/sequenced_task_runner_helpers.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.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_.task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 22 loop_.task_runner()->PostTask(FROM_HERE, |
| 23 base::MessageLoop::QuitWhenIdleClosure()); |
23 } | 24 } |
24 | 25 |
25 protected: | 26 protected: |
26 void SetUp() override { | 27 void SetUp() override { |
27 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); | 28 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); |
28 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); | 29 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
29 ui_thread_->Start(); | 30 ui_thread_->Start(); |
30 file_thread_->Start(); | 31 file_thread_->Start(); |
31 } | 32 } |
32 | 33 |
33 void TearDown() override { | 34 void TearDown() override { |
34 ui_thread_->Stop(); | 35 ui_thread_->Stop(); |
35 file_thread_->Stop(); | 36 file_thread_->Stop(); |
36 } | 37 } |
37 | 38 |
38 static void BasicFunction(base::MessageLoop* message_loop) { | 39 static void BasicFunction(base::MessageLoop* message_loop) { |
39 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 40 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
40 message_loop->task_runner()->PostTask(FROM_HERE, | 41 message_loop->task_runner()->PostTask( |
41 base::MessageLoop::QuitClosure()); | 42 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
42 } | 43 } |
43 | 44 |
44 class DeletedOnFile | 45 class DeletedOnFile |
45 : public base::RefCountedThreadSafe< | 46 : public base::RefCountedThreadSafe< |
46 DeletedOnFile, BrowserThread::DeleteOnFileThread> { | 47 DeletedOnFile, BrowserThread::DeleteOnFileThread> { |
47 public: | 48 public: |
48 explicit DeletedOnFile(base::MessageLoop* message_loop) | 49 explicit DeletedOnFile(base::MessageLoop* message_loop) |
49 : message_loop_(message_loop) {} | 50 : message_loop_(message_loop) {} |
50 | 51 |
51 private: | 52 private: |
52 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | 53 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; |
53 friend class base::DeleteHelper<DeletedOnFile>; | 54 friend class base::DeleteHelper<DeletedOnFile>; |
54 | 55 |
55 ~DeletedOnFile() { | 56 ~DeletedOnFile() { |
56 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 57 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
57 message_loop_->task_runner()->PostTask(FROM_HERE, | 58 message_loop_->task_runner()->PostTask( |
58 base::MessageLoop::QuitClosure()); | 59 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
59 } | 60 } |
60 | 61 |
61 base::MessageLoop* message_loop_; | 62 base::MessageLoop* message_loop_; |
62 }; | 63 }; |
63 | 64 |
64 private: | 65 private: |
65 scoped_ptr<BrowserThreadImpl> ui_thread_; | 66 scoped_ptr<BrowserThreadImpl> ui_thread_; |
66 scoped_ptr<BrowserThreadImpl> file_thread_; | 67 scoped_ptr<BrowserThreadImpl> file_thread_; |
67 // It's kind of ugly to make this mutable - solely so we can post the Quit | 68 // It's kind of ugly to make this mutable - solely so we can post the Quit |
68 // Task from Release(). This should be fixed. | 69 // Task from Release(). This should be fixed. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 103 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
103 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 104 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
104 task_runner->ReleaseSoon(FROM_HERE, this); | 105 task_runner->ReleaseSoon(FROM_HERE, this); |
105 base::MessageLoop::current()->Run(); | 106 base::MessageLoop::current()->Run(); |
106 } | 107 } |
107 | 108 |
108 TEST_F(BrowserThreadTest, PostTaskAndReply) { | 109 TEST_F(BrowserThreadTest, PostTaskAndReply) { |
109 // Most of the heavy testing for PostTaskAndReply() is done inside the | 110 // Most of the heavy testing for PostTaskAndReply() is done inside the |
110 // task runner test. This just makes sure we get piped through at all. | 111 // task runner test. This just makes sure we get piped through at all. |
111 ASSERT_TRUE(BrowserThread::PostTaskAndReply( | 112 ASSERT_TRUE(BrowserThread::PostTaskAndReply( |
112 BrowserThread::FILE, | 113 BrowserThread::FILE, FROM_HERE, base::Bind(&base::DoNothing), |
113 FROM_HERE, | 114 base::Bind(&base::MessageLoop::QuitWhenIdle, |
114 base::Bind(&base::DoNothing), | |
115 base::Bind(&base::MessageLoop::Quit, | |
116 base::Unretained(base::MessageLoop::current()->current())))); | 115 base::Unretained(base::MessageLoop::current()->current())))); |
117 base::MessageLoop::current()->Run(); | 116 base::MessageLoop::current()->Run(); |
118 } | 117 } |
119 | 118 |
120 } // namespace content | 119 } // namespace content |
OLD | NEW |