
raw_scoped_refptr_mismatch_checker.h ref_counted.cc ref_counted.h ref_counted_memory.cc ref_counted_memory.h ref_counted_unittest.cc scoped_callback_factory.h scoped_comptr_win.h scoped_handle.h scoped_native_library.cc scoped_native_library.h scoped_native_library_unittest.cc scoped_nsobject.h scoped_open_process.h scoped_ptr.h scoped_ptr_unittest.cc scoped_temp_dir.cc scoped_temp_dir.h scoped_temp_dir_unittest.cc scoped_vector.h singleton.h singleton_objc.h singleton_unittest.cc linked_ptr.h linked_ptr_unittest.cc weak_ptr.cc weak_ptr.h weak_ptr_unittest.cc BUG=None TEST=Compile Review URL: http://codereview.chromium.org/6714032 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79524 0039d316-1c4b-4281-b951-d872f2087c98
133 lines
3.4 KiB
C++
133 lines
3.4 KiB
C++
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
#include "base/message_loop.h"
|
|
#include "base/message_loop_proxy_impl.h"
|
|
#include "base/threading/thread.h"
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
#include "testing/platform_test.h"
|
|
|
|
|
|
class MessageLoopProxyImplTest : public testing::Test {
|
|
public:
|
|
void Release() const {
|
|
AssertOnIOThread();
|
|
Quit();
|
|
}
|
|
|
|
void Quit() const {
|
|
loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
|
|
}
|
|
|
|
void AssertOnIOThread() const {
|
|
ASSERT_TRUE(io_thread_->message_loop_proxy()->BelongsToCurrentThread());
|
|
}
|
|
|
|
void AssertOnFileThread() const {
|
|
ASSERT_TRUE(file_thread_->message_loop_proxy()->BelongsToCurrentThread());
|
|
}
|
|
|
|
protected:
|
|
virtual void SetUp() {
|
|
io_thread_.reset(new base::Thread("MessageLoopProxyImplTest_IO"));
|
|
file_thread_.reset(new base::Thread("MessageLoopProxyImplTest_File"));
|
|
io_thread_->Start();
|
|
file_thread_->Start();
|
|
}
|
|
|
|
virtual void TearDown() {
|
|
io_thread_->Stop();
|
|
file_thread_->Stop();
|
|
}
|
|
|
|
static void BasicFunction(MessageLoopProxyImplTest* test) {
|
|
test->AssertOnFileThread();
|
|
test->Quit();
|
|
}
|
|
|
|
class DummyTask : public Task {
|
|
public:
|
|
explicit DummyTask(bool* deleted) : deleted_(deleted) { }
|
|
~DummyTask() {
|
|
*deleted_ = true;
|
|
}
|
|
|
|
void Run() {
|
|
FAIL();
|
|
}
|
|
|
|
private:
|
|
bool* deleted_;
|
|
};
|
|
|
|
class DeletedOnFile {
|
|
public:
|
|
explicit DeletedOnFile(MessageLoopProxyImplTest* test) : test_(test) {}
|
|
|
|
~DeletedOnFile() {
|
|
test_->AssertOnFileThread();
|
|
test_->Quit();
|
|
}
|
|
|
|
private:
|
|
MessageLoopProxyImplTest* test_;
|
|
};
|
|
|
|
scoped_ptr<base::Thread> io_thread_;
|
|
scoped_ptr<base::Thread> file_thread_;
|
|
|
|
private:
|
|
mutable MessageLoop loop_;
|
|
};
|
|
|
|
|
|
TEST_F(MessageLoopProxyImplTest, PostTask) {
|
|
EXPECT_TRUE(file_thread_->message_loop_proxy()->PostTask(
|
|
FROM_HERE, NewRunnableFunction(&BasicFunction, this)));
|
|
MessageLoop::current()->Run();
|
|
}
|
|
|
|
TEST_F(MessageLoopProxyImplTest, Release) {
|
|
EXPECT_TRUE(io_thread_->message_loop_proxy()->ReleaseSoon(FROM_HERE, this));
|
|
MessageLoop::current()->Run();
|
|
}
|
|
|
|
TEST_F(MessageLoopProxyImplTest, Delete) {
|
|
DeletedOnFile* deleted_on_file = new DeletedOnFile(this);
|
|
EXPECT_TRUE(file_thread_->message_loop_proxy()->DeleteSoon(
|
|
FROM_HERE, deleted_on_file));
|
|
MessageLoop::current()->Run();
|
|
}
|
|
|
|
TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadExits) {
|
|
scoped_ptr<base::Thread> test_thread(
|
|
new base::Thread("MessageLoopProxyImplTest_Dummy"));
|
|
test_thread->Start();
|
|
scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
|
|
test_thread->message_loop_proxy();
|
|
test_thread->Stop();
|
|
|
|
bool deleted = false;
|
|
bool ret = message_loop_proxy->PostTask(
|
|
FROM_HERE, new DummyTask(&deleted));
|
|
EXPECT_FALSE(ret);
|
|
EXPECT_TRUE(deleted);
|
|
}
|
|
|
|
TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadIsDeleted) {
|
|
scoped_refptr<base::MessageLoopProxy> message_loop_proxy;
|
|
{
|
|
scoped_ptr<base::Thread> test_thread(
|
|
new base::Thread("MessageLoopProxyImplTest_Dummy"));
|
|
test_thread->Start();
|
|
message_loop_proxy = test_thread->message_loop_proxy();
|
|
}
|
|
bool deleted = false;
|
|
bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
|
|
EXPECT_FALSE(ret);
|
|
EXPECT_TRUE(deleted);
|
|
}
|
|
|