blob: 948709acc6d1b5ed67973fb99e1e4bcc26ddaae6 [file] [log] [blame]
[email protected]d9b14782010-04-15 08:08:071// Copyright (c) 2010 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]d9b14782010-04-15 08:08:075#include "base/message_loop.h"
[email protected]2cbac9e2010-04-29 03:31:346#include "base/message_loop_proxy.h"
[email protected]5097dc82010-07-15 17:23:237#include "base/scoped_ptr.h"
[email protected]ed7e6dd2010-10-12 02:02:458#include "chrome/browser/browser_thread.h"
initial.commit09911bf2008-07-26 23:55:299#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1510#include "testing/platform_test.h"
initial.commit09911bf2008-07-26 23:55:2911
[email protected]092b04e2010-10-12 23:23:4412class BrowserThreadTest : public testing::Test {
[email protected]f6710622009-11-02 06:10:3013 public:
[email protected]00ed48f2010-10-22 22:19:2414 void Release() const {
[email protected]092b04e2010-10-12 23:23:4415 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f6710622009-11-02 06:10:3016 loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
17 }
[email protected]64cd0d122008-10-17 21:16:1318
[email protected]f6710622009-11-02 06:10:3019 protected:
20 virtual void SetUp() {
[email protected]092b04e2010-10-12 23:23:4421 ui_thread_.reset(new BrowserThread(BrowserThread::UI));
22 file_thread_.reset(new BrowserThread(BrowserThread::FILE));
[email protected]2cbac9e2010-04-29 03:31:3423 ui_thread_->Start();
[email protected]f6710622009-11-02 06:10:3024 file_thread_->Start();
[email protected]f6710622009-11-02 06:10:3025 }
initial.commit09911bf2008-07-26 23:55:2926
[email protected]f6710622009-11-02 06:10:3027 virtual void TearDown() {
[email protected]2cbac9e2010-04-29 03:31:3428 ui_thread_->Stop();
[email protected]f6710622009-11-02 06:10:3029 file_thread_->Stop();
[email protected]f6710622009-11-02 06:10:3030 }
initial.commit09911bf2008-07-26 23:55:2931
[email protected]f6710622009-11-02 06:10:3032 static void BasicFunction(MessageLoop* message_loop) {
[email protected]092b04e2010-10-12 23:23:4433 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]f6710622009-11-02 06:10:3034 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask);
35 }
initial.commit09911bf2008-07-26 23:55:2936
[email protected]f6710622009-11-02 06:10:3037 class DummyTask : public Task {
38 public:
[email protected]4a3dab22009-11-11 17:36:5039 explicit DummyTask(bool* deleted) : deleted_(deleted) { }
[email protected]f6710622009-11-02 06:10:3040 ~DummyTask() {
41 *deleted_ = true;
42 }
initial.commit09911bf2008-07-26 23:55:2943
[email protected]f6710622009-11-02 06:10:3044 void Run() {
45 CHECK(false);
46 }
initial.commit09911bf2008-07-26 23:55:2947
[email protected]f6710622009-11-02 06:10:3048 private:
49 bool* deleted_;
50 };
initial.commit09911bf2008-07-26 23:55:2951
[email protected]2cbac9e2010-04-29 03:31:3452 class DeletedOnFile
[email protected]f6710622009-11-02 06:10:3053 : public base::RefCountedThreadSafe<
[email protected]092b04e2010-10-12 23:23:4454 DeletedOnFile, BrowserThread::DeleteOnFileThread> {
[email protected]f6710622009-11-02 06:10:3055 public:
[email protected]2cbac9e2010-04-29 03:31:3456 explicit DeletedOnFile(MessageLoop* message_loop)
[email protected]4a3dab22009-11-11 17:36:5057 : message_loop_(message_loop) { }
initial.commit09911bf2008-07-26 23:55:2958
[email protected]2cbac9e2010-04-29 03:31:3459 ~DeletedOnFile() {
[email protected]092b04e2010-10-12 23:23:4460 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]f6710622009-11-02 06:10:3061 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
62 }
initial.commit09911bf2008-07-26 23:55:2963
[email protected]f6710622009-11-02 06:10:3064 private:
65 MessageLoop* message_loop_;
66 };
initial.commit09911bf2008-07-26 23:55:2967
[email protected]f6710622009-11-02 06:10:3068 class NeverDeleted
69 : public base::RefCountedThreadSafe<
[email protected]092b04e2010-10-12 23:23:4470 NeverDeleted, BrowserThread::DeleteOnWebKitThread> {
[email protected]f6710622009-11-02 06:10:3071 public:
72 ~NeverDeleted() {
73 CHECK(false);
74 }
75 };
initial.commit09911bf2008-07-26 23:55:2976
[email protected]f6710622009-11-02 06:10:3077 private:
[email protected]092b04e2010-10-12 23:23:4478 scoped_ptr<BrowserThread> ui_thread_;
79 scoped_ptr<BrowserThread> file_thread_;
[email protected]00ed48f2010-10-22 22:19:2480 // It's kind of ugly to make this mutable - solely so we can post the Quit
81 // Task from Release(). This should be fixed.
82 mutable MessageLoop loop_;
[email protected]f6710622009-11-02 06:10:3083};
initial.commit09911bf2008-07-26 23:55:2984
[email protected]092b04e2010-10-12 23:23:4485TEST_F(BrowserThreadTest, PostTask) {
86 BrowserThread::PostTask(
87 BrowserThread::FILE, FROM_HERE,
[email protected]f6710622009-11-02 06:10:3088 NewRunnableFunction(&BasicFunction, MessageLoop::current()));
89 MessageLoop::current()->Run();
90}
initial.commit09911bf2008-07-26 23:55:2991
[email protected]092b04e2010-10-12 23:23:4492TEST_F(BrowserThreadTest, Release) {
93 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this);
[email protected]f6710622009-11-02 06:10:3094 MessageLoop::current()->Run();
95}
initial.commit09911bf2008-07-26 23:55:2996
[email protected]092b04e2010-10-12 23:23:4497TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeleted) {
[email protected]f6710622009-11-02 06:10:3098 bool deleted = false;
[email protected]092b04e2010-10-12 23:23:4499 BrowserThread::PostTask(
100 BrowserThread::WEBKIT, FROM_HERE,
[email protected]f6710622009-11-02 06:10:30101 new DummyTask(&deleted));
102 EXPECT_TRUE(deleted);
103}
initial.commit09911bf2008-07-26 23:55:29104
[email protected]092b04e2010-10-12 23:23:44105TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) {
[email protected]f6710622009-11-02 06:10:30106 {
[email protected]2cbac9e2010-04-29 03:31:34107 scoped_refptr<DeletedOnFile> test(
108 new DeletedOnFile(MessageLoop::current()));
[email protected]f6710622009-11-02 06:10:30109 }
110 MessageLoop::current()->Run();
111}
112
[email protected]092b04e2010-10-12 23:23:44113TEST_F(BrowserThreadTest, NotReleasedIfTargetThreadNonExistent) {
[email protected]f6710622009-11-02 06:10:30114 scoped_refptr<NeverDeleted> test(new NeverDeleted());
initial.commit09911bf2008-07-26 23:55:29115}
[email protected]2cbac9e2010-04-29 03:31:34116
[email protected]092b04e2010-10-12 23:23:44117TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxy) {
[email protected]656475d272010-05-06 18:34:24118 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44119 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
[email protected]2cbac9e2010-04-29 03:31:34120 message_loop_proxy->PostTask(FROM_HERE,
121 NewRunnableFunction(&BasicFunction,
122 MessageLoop::current()));
123 MessageLoop::current()->Run();
124}
125
[email protected]092b04e2010-10-12 23:23:44126TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) {
[email protected]656475d272010-05-06 18:34:24127 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
[email protected]2cbac9e2010-04-29 03:31:34129 message_loop_proxy->ReleaseSoon(FROM_HERE, this);
130 MessageLoop::current()->Run();
131}
132
[email protected]092b04e2010-10-12 23:23:44133TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) {
[email protected]2cbac9e2010-04-29 03:31:34134 bool deleted = false;
[email protected]656475d272010-05-06 18:34:24135 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44136 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT);
[email protected]2cbac9e2010-04-29 03:31:34137 message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
138 EXPECT_TRUE(deleted);
139}
140
[email protected]092b04e2010-10-12 23:23:44141TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) {
142 scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO));
[email protected]2cbac9e2010-04-29 03:31:34143 io_thread->Start();
144 io_thread->Stop();
145
146 bool deleted = false;
[email protected]656475d272010-05-06 18:34:24147 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44148 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]2cbac9e2010-04-29 03:31:34149 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
150 EXPECT_FALSE(ret);
151 EXPECT_TRUE(deleted);
152}
153
[email protected]092b04e2010-10-12 23:23:44154TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) {
[email protected]2cbac9e2010-04-29 03:31:34155 {
[email protected]092b04e2010-10-12 23:23:44156 scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO));
[email protected]2cbac9e2010-04-29 03:31:34157 io_thread->Start();
158 }
159 bool deleted = false;
[email protected]656475d272010-05-06 18:34:24160 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
[email protected]092b04e2010-10-12 23:23:44161 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]2cbac9e2010-04-29 03:31:34162 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
163 EXPECT_FALSE(ret);
164 EXPECT_TRUE(deleted);
165}
166