blob: 59ec5f7057a078bed8b931c0f4a4be15f3cb3c26 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]c31af70db22011-08-18 23:13:015#include "base/bind.h"
[email protected]3b63f8f42011-03-28 01:54:156#include "base/memory/scoped_ptr.h"
[email protected]d9b14782010-04-15 08:08:077#include "base/message_loop.h"
[email protected]2cbac9e2010-04-29 03:31:348#include "base/message_loop_proxy.h"
[email protected]c38831a12011-10-28 12:44:499#include "content/browser/browser_thread_impl.h"
10#include "content/test/test_browser_thread.h"
initial.commit09911bf2008-07-26 23:55:2911#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1512#include "testing/platform_test.h"
initial.commit09911bf2008-07-26 23:55:2913
[email protected]c38831a12011-10-28 12:44:4914namespace content {
15
[email protected]092b04e2010-10-12 23:23:4416class BrowserThreadTest : public testing::Test {
[email protected]f6710622009-11-02 06:10:3017 public:
[email protected]00ed48f2010-10-22 22:19:2418 void Release() const {
[email protected]092b04e2010-10-12 23:23:4419 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d111e932011-12-10 00:22:5920 loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure());
[email protected]f6710622009-11-02 06:10:3021 }
[email protected]64cd0d122008-10-17 21:16:1322
[email protected]f6710622009-11-02 06:10:3023 protected:
24 virtual void SetUp() {
[email protected]c38831a12011-10-28 12:44:4925 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI));
26 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE));
[email protected]2cbac9e2010-04-29 03:31:3427 ui_thread_->Start();
[email protected]f6710622009-11-02 06:10:3028 file_thread_->Start();
[email protected]f6710622009-11-02 06:10:3029 }
initial.commit09911bf2008-07-26 23:55:2930
[email protected]f6710622009-11-02 06:10:3031 virtual void TearDown() {
[email protected]2cbac9e2010-04-29 03:31:3432 ui_thread_->Stop();
[email protected]f6710622009-11-02 06:10:3033 file_thread_->Stop();
[email protected]f6710622009-11-02 06:10:3034 }
initial.commit09911bf2008-07-26 23:55:2935
[email protected]f6710622009-11-02 06:10:3036 static void BasicFunction(MessageLoop* message_loop) {
[email protected]092b04e2010-10-12 23:23:4437 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]d111e932011-12-10 00:22:5938 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure());
[email protected]f6710622009-11-02 06:10:3039 }
initial.commit09911bf2008-07-26 23:55:2940
[email protected]c31af70db22011-08-18 23:13:0141 static void DoNothing() {
42 }
43
[email protected]f6710622009-11-02 06:10:3044 class DummyTask : public Task {
45 public:
[email protected]4a3dab22009-11-11 17:36:5046 explicit DummyTask(bool* deleted) : deleted_(deleted) { }
[email protected]f6710622009-11-02 06:10:3047 ~DummyTask() {
48 *deleted_ = true;
49 }
initial.commit09911bf2008-07-26 23:55:2950
[email protected]f6710622009-11-02 06:10:3051 void Run() {
52 CHECK(false);
53 }
initial.commit09911bf2008-07-26 23:55:2954
[email protected]f6710622009-11-02 06:10:3055 private:
56 bool* deleted_;
57 };
initial.commit09911bf2008-07-26 23:55:2958
[email protected]2cbac9e2010-04-29 03:31:3459 class DeletedOnFile
[email protected]f6710622009-11-02 06:10:3060 : public base::RefCountedThreadSafe<
[email protected]092b04e2010-10-12 23:23:4461 DeletedOnFile, BrowserThread::DeleteOnFileThread> {
[email protected]f6710622009-11-02 06:10:3062 public:
[email protected]2cbac9e2010-04-29 03:31:3463 explicit DeletedOnFile(MessageLoop* message_loop)
[email protected]4a3dab22009-11-11 17:36:5064 : message_loop_(message_loop) { }
initial.commit09911bf2008-07-26 23:55:2965
[email protected]2cbac9e2010-04-29 03:31:3466 ~DeletedOnFile() {
[email protected]092b04e2010-10-12 23:23:4467 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]d111e932011-12-10 00:22:5968 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
[email protected]f6710622009-11-02 06:10:3069 }
initial.commit09911bf2008-07-26 23:55:2970
[email protected]f6710622009-11-02 06:10:3071 private:
72 MessageLoop* message_loop_;
73 };
initial.commit09911bf2008-07-26 23:55:2974
[email protected]f6710622009-11-02 06:10:3075 class NeverDeleted
76 : public base::RefCountedThreadSafe<
[email protected]092b04e2010-10-12 23:23:4477 NeverDeleted, BrowserThread::DeleteOnWebKitThread> {
[email protected]f6710622009-11-02 06:10:3078 public:
79 ~NeverDeleted() {
80 CHECK(false);
81 }
82 };
initial.commit09911bf2008-07-26 23:55:2983
[email protected]f6710622009-11-02 06:10:3084 private:
[email protected]c38831a12011-10-28 12:44:4985 scoped_ptr<BrowserThreadImpl> ui_thread_;
86 scoped_ptr<BrowserThreadImpl> file_thread_;
[email protected]00ed48f2010-10-22 22:19:2487 // It's kind of ugly to make this mutable - solely so we can post the Quit
88 // Task from Release(). This should be fixed.
89 mutable MessageLoop loop_;
[email protected]f6710622009-11-02 06:10:3090};
initial.commit09911bf2008-07-26 23:55:2991
[email protected]092b04e2010-10-12 23:23:4492TEST_F(BrowserThreadTest, PostTask) {
93 BrowserThread::PostTask(
94 BrowserThread::FILE, FROM_HERE,
[email protected]80751052011-11-12 17:10:5895 base::Bind(&BasicFunction, MessageLoop::current()));
[email protected]f6710622009-11-02 06:10:3096 MessageLoop::current()->Run();
97}
initial.commit09911bf2008-07-26 23:55:2998
[email protected]092b04e2010-10-12 23:23:4499TEST_F(BrowserThreadTest, Release) {
100 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this);
[email protected]f6710622009-11-02 06:10:30101 MessageLoop::current()->Run();
102}
initial.commit09911bf2008-07-26 23:55:29103
[email protected]092b04e2010-10-12 23:23:44104TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeleted) {
[email protected]f6710622009-11-02 06:10:30105 bool deleted = false;
[email protected]092b04e2010-10-12 23:23:44106 BrowserThread::PostTask(
[email protected]e1dd5622011-12-20 12:28:58107 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
[email protected]f6710622009-11-02 06:10:30108 new DummyTask(&deleted));
109 EXPECT_TRUE(deleted);
110}
initial.commit09911bf2008-07-26 23:55:29111
[email protected]092b04e2010-10-12 23:23:44112TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) {
[email protected]f6710622009-11-02 06:10:30113 {
[email protected]2cbac9e2010-04-29 03:31:34114 scoped_refptr<DeletedOnFile> test(
115 new DeletedOnFile(MessageLoop::current()));
[email protected]f6710622009-11-02 06:10:30116 }
117 MessageLoop::current()->Run();
118}
119
[email protected]092b04e2010-10-12 23:23:44120TEST_F(BrowserThreadTest, NotReleasedIfTargetThreadNonExistent) {
[email protected]f6710622009-11-02 06:10:30121 scoped_refptr<NeverDeleted> test(new NeverDeleted());
initial.commit09911bf2008-07-26 23:55:29122}
[email protected]2cbac9e2010-04-29 03:31:34123
[email protected]092b04e2010-10-12 23:23:44124TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxy) {
[email protected]656475d2010-05-06 18:34:24125 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44126 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
[email protected]80751052011-11-12 17:10:58127 message_loop_proxy->PostTask(
128 FROM_HERE, base::Bind(&BasicFunction, MessageLoop::current()));
[email protected]2cbac9e2010-04-29 03:31:34129 MessageLoop::current()->Run();
130}
131
[email protected]092b04e2010-10-12 23:23:44132TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) {
[email protected]656475d2010-05-06 18:34:24133 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44134 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
[email protected]2cbac9e2010-04-29 03:31:34135 message_loop_proxy->ReleaseSoon(FROM_HERE, this);
136 MessageLoop::current()->Run();
137}
138
[email protected]c31af70db22011-08-18 23:13:01139TEST_F(BrowserThreadTest, PostTaskAndReply) {
140 // Most of the heavy testing for PostTaskAndReply() is done inside the
141 // MessageLoopProxy test. This just makes sure we get piped through at all.
142 ASSERT_TRUE(BrowserThread::PostTaskAndReply(
143 BrowserThread::FILE,
144 FROM_HERE,
145 base::Bind(&BrowserThreadTest::DoNothing),
146 base::Bind(&MessageLoop::Quit,
147 base::Unretained(MessageLoop::current()->current()))));
148 MessageLoop::current()->Run();
149}
150
151
[email protected]092b04e2010-10-12 23:23:44152TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) {
[email protected]2cbac9e2010-04-29 03:31:34153 bool deleted = false;
[email protected]656475d2010-05-06 18:34:24154 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]e1dd5622011-12-20 12:28:58155 BrowserThread::GetMessageLoopProxyForThread(
156 BrowserThread::WEBKIT_DEPRECATED);
[email protected]2cbac9e2010-04-29 03:31:34157 message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
158 EXPECT_TRUE(deleted);
159}
160
[email protected]092b04e2010-10-12 23:23:44161TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) {
[email protected]c38831a12011-10-28 12:44:49162 scoped_ptr<BrowserThreadImpl> io_thread(
163 new BrowserThreadImpl(BrowserThread::IO));
[email protected]2cbac9e2010-04-29 03:31:34164 io_thread->Start();
165 io_thread->Stop();
166
167 bool deleted = false;
[email protected]656475d2010-05-06 18:34:24168 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44169 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]2cbac9e2010-04-29 03:31:34170 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
171 EXPECT_FALSE(ret);
172 EXPECT_TRUE(deleted);
173}
174
[email protected]092b04e2010-10-12 23:23:44175TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) {
[email protected]2cbac9e2010-04-29 03:31:34176 {
[email protected]c38831a12011-10-28 12:44:49177 scoped_ptr<BrowserThreadImpl> io_thread(
178 new BrowserThreadImpl(BrowserThread::IO));
[email protected]2cbac9e2010-04-29 03:31:34179 io_thread->Start();
180 }
181 bool deleted = false;
[email protected]656475d2010-05-06 18:34:24182 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44183 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]2cbac9e2010-04-29 03:31:34184 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
185 EXPECT_FALSE(ret);
186 EXPECT_TRUE(deleted);
187}
[email protected]c38831a12011-10-28 12:44:49188
189}