This source file includes following definitions.
- TypeDefaultMessagePumpFactory
- TypeIOMessagePumpFactory
- TypeUIMessagePumpFactory
- Test1ConstRef
- test_count
- result
- SlowFunc
- RecordRunTimeFunc
- SubPumpFunc
- RunTest_PostDelayedTask_SharedTimer_SubPump
- RecordStart
- RecordEnd
- Size
- Get
- MessageBoxFunc
- EndDialogFunc
- RecursiveFunc
- QuitFunc
- RecursiveFuncWin
- RunTest_RecursiveDenial2
- RunTest_RecursiveSupport2
- PostNTasksThenQuit
- Dispatch
- MouseDownUp
- RunTest_Dispatcher
- MsgFilterProc
- RunTest_DispatcherWithMessageHook
- context
- size
- wait_
- Init
- OnIOCompleted
- WaitForIO
- RunTest_IOHandler
- RunTest_WaitForIO
- TEST
- TEST
- TEST
- num_tasks_
- WillProcessTask
- DidProcessTask
- num_tasks_started
- num_tasks_processed
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- OnFileCanWriteWithoutBlocking
- OnFileCanReadWithoutBlocking
- TEST
- TEST
- destruction_observer_called_
- Run
- task_destroyed_before_message_loop_
- WillDestroyCurrentMessageLoop
- task_destroyed_before_message_loop
- TEST
- TEST
- TEST
- EmptyFunction
- PostMultipleTasks
- PostWindowsMessage
- EndTest
- TestWndProcThunk
- TEST
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy_impl.h"
#include "base/message_loop/message_loop_test.h"
#include "base/message_loop/message_pump_dispatcher.h"
#include "base/pending_task.h"
#include "base/posix/eintr_wrapper.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
#include "base/message_loop/message_pump_win.h"
#include "base/process/memory.h"
#include "base/strings/string16.h"
#include "base/win/scoped_handle.h"
#endif
namespace base {
namespace {
scoped_ptr<MessagePump> TypeDefaultMessagePumpFactory() {
return MessageLoop::CreateMessagePumpForType(MessageLoop::TYPE_DEFAULT);
}
scoped_ptr<MessagePump> TypeIOMessagePumpFactory() {
return MessageLoop::CreateMessagePumpForType(MessageLoop::TYPE_IO);
}
scoped_ptr<MessagePump> TypeUIMessagePumpFactory() {
return MessageLoop::CreateMessagePumpForType(MessageLoop::TYPE_UI);
}
class Foo : public RefCounted<Foo> {
public:
Foo() : test_count_(0) {
}
void Test1ConstRef(const std::string& a) {
++test_count_;
result_.append(a);
}
int test_count() const { return test_count_; }
const std::string& result() const { return result_; }
private:
friend class RefCounted<Foo>;
~Foo() {}
int test_count_;
std::string result_;
};
#if defined(OS_WIN)
static void SlowFunc(TimeDelta pause, int* quit_counter) {
PlatformThread::Sleep(pause);
if (--(*quit_counter) == 0)
MessageLoop::current()->QuitWhenIdle();
}
static void RecordRunTimeFunc(Time* run_time, int* quit_counter) {
*run_time = Time::Now();
SlowFunc(TimeDelta::FromMilliseconds(10), quit_counter);
}
void SubPumpFunc() {
MessageLoop::current()->SetNestableTasksAllowed(true);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
MessageLoop::current()->QuitWhenIdle();
}
void RunTest_PostDelayedTask_SharedTimer_SubPump() {
MessageLoop loop(MessageLoop::TYPE_UI);
int num_tasks = 1;
Time run_time;
loop.PostTask(FROM_HERE, Bind(&SubPumpFunc));
loop.PostDelayedTask(
FROM_HERE,
Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
TimeDelta::FromSeconds(1000));
loop.PostDelayedTask(
FROM_HERE,
Bind(&PostQuitMessage, 0),
TimeDelta::FromMilliseconds(10));
Time start_time = Time::Now();
loop.Run();
EXPECT_EQ(1, num_tasks);
TimeDelta total_time = Time::Now() - start_time;
EXPECT_GT(5000, total_time.InMilliseconds());
PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
RunLoop().RunUntilIdle();
EXPECT_TRUE(run_time.is_null());
}
const wchar_t kMessageBoxTitle[] = L"MessageLoop Unit Test";
enum TaskType {
MESSAGEBOX,
ENDDIALOG,
RECURSIVE,
TIMEDMESSAGELOOP,
QUITMESSAGELOOP,
ORDERED,
PUMPS,
SLEEP,
RUNS,
};
struct TaskItem {
TaskItem(TaskType t, int c, bool s)
: type(t),
cookie(c),
start(s) {
}
TaskType type;
int cookie;
bool start;
bool operator == (const TaskItem& other) const {
return type == other.type && cookie == other.cookie && start == other.start;
}
};
std::ostream& operator <<(std::ostream& os, TaskType type) {
switch (type) {
case MESSAGEBOX: os << "MESSAGEBOX"; break;
case ENDDIALOG: os << "ENDDIALOG"; break;
case RECURSIVE: os << "RECURSIVE"; break;
case TIMEDMESSAGELOOP: os << "TIMEDMESSAGELOOP"; break;
case QUITMESSAGELOOP: os << "QUITMESSAGELOOP"; break;
case ORDERED: os << "ORDERED"; break;
case PUMPS: os << "PUMPS"; break;
case SLEEP: os << "SLEEP"; break;
default:
NOTREACHED();
os << "Unknown TaskType";
break;
}
return os;
}
std::ostream& operator <<(std::ostream& os, const TaskItem& item) {
if (item.start)
return os << item.type << " " << item.cookie << " starts";
else
return os << item.type << " " << item.cookie << " ends";
}
class TaskList {
public:
void RecordStart(TaskType type, int cookie) {
TaskItem item(type, cookie, true);
DVLOG(1) << item;
task_list_.push_back(item);
}
void RecordEnd(TaskType type, int cookie) {
TaskItem item(type, cookie, false);
DVLOG(1) << item;
task_list_.push_back(item);
}
size_t Size() {
return task_list_.size();
}
TaskItem Get(int n) {
return task_list_[n];
}
private:
std::vector<TaskItem> task_list_;
};
void MessageBoxFunc(TaskList* order, int cookie, bool is_reentrant) {
order->RecordStart(MESSAGEBOX, cookie);
if (is_reentrant)
MessageLoop::current()->SetNestableTasksAllowed(true);
MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK);
order->RecordEnd(MESSAGEBOX, cookie);
}
void EndDialogFunc(TaskList* order, int cookie) {
order->RecordStart(ENDDIALOG, cookie);
HWND window = GetActiveWindow();
if (window != NULL) {
EXPECT_NE(EndDialog(window, IDCONTINUE), 0);
order->RecordEnd(ENDDIALOG, cookie);
}
}
void RecursiveFunc(TaskList* order, int cookie, int depth,
bool is_reentrant) {
order->RecordStart(RECURSIVE, cookie);
if (depth > 0) {
if (is_reentrant)
MessageLoop::current()->SetNestableTasksAllowed(true);
MessageLoop::current()->PostTask(
FROM_HERE,
Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant));
}
order->RecordEnd(RECURSIVE, cookie);
}
void QuitFunc(TaskList* order, int cookie) {
order->RecordStart(QUITMESSAGELOOP, cookie);
MessageLoop::current()->QuitWhenIdle();
order->RecordEnd(QUITMESSAGELOOP, cookie);
}
void RecursiveFuncWin(MessageLoop* target,
HANDLE event,
bool expect_window,
TaskList* order,
bool is_reentrant) {
target->PostTask(FROM_HERE,
Bind(&RecursiveFunc, order, 1, 2, is_reentrant));
target->PostTask(FROM_HERE,
Bind(&MessageBoxFunc, order, 2, is_reentrant));
target->PostTask(FROM_HERE,
Bind(&RecursiveFunc, order, 3, 2, is_reentrant));
target->PostTask(FROM_HERE,
Bind(&EndDialogFunc, order, 4));
target->PostTask(FROM_HERE,
Bind(&QuitFunc, order, 5));
ASSERT_TRUE(SetEvent(event));
for (; expect_window;) {
HWND window = FindWindow(L"#32770", kMessageBoxTitle);
if (window) {
for (;;) {
HWND button = FindWindowEx(window, NULL, L"Button", NULL);
if (button != NULL) {
EXPECT_EQ(0, SendMessage(button, WM_LBUTTONDOWN, 0, 0));
EXPECT_EQ(0, SendMessage(button, WM_LBUTTONUP, 0, 0));
break;
}
}
break;
}
}
}
void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
Thread worker("RecursiveDenial2_worker");
Thread::Options options;
options.message_loop_type = message_loop_type;
ASSERT_EQ(true, worker.StartWithOptions(options));
TaskList order;
win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
worker.message_loop()->PostTask(FROM_HERE,
Bind(&RecursiveFuncWin,
MessageLoop::current(),
event.Get(),
true,
&order,
false));
WaitForSingleObject(event, INFINITE);
MessageLoop::current()->Run();
ASSERT_EQ(order.Size(), 17);
EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true));
EXPECT_EQ(order.Get(3), TaskItem(MESSAGEBOX, 2, false));
EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, true));
EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 3, false));
EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, true));
EXPECT_EQ(order.Get(7), TaskItem(QUITMESSAGELOOP, 5, true));
EXPECT_EQ(order.Get(8), TaskItem(QUITMESSAGELOOP, 5, false));
EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 1, true));
EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, false));
EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 3, true));
EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, false));
EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, true));
EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, false));
EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 3, true));
EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, false));
}
void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
Thread worker("RecursiveSupport2_worker");
Thread::Options options;
options.message_loop_type = message_loop_type;
ASSERT_EQ(true, worker.StartWithOptions(options));
TaskList order;
win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
worker.message_loop()->PostTask(FROM_HERE,
Bind(&RecursiveFuncWin,
MessageLoop::current(),
event.Get(),
false,
&order,
true));
WaitForSingleObject(event, INFINITE);
MessageLoop::current()->Run();
ASSERT_EQ(order.Size(), 18);
EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true));
EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 3, true));
EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, false));
EXPECT_EQ(order.Get(5), TaskItem(ENDDIALOG, 4, true));
EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, false));
EXPECT_EQ(order.Get(7), TaskItem(MESSAGEBOX, 2, false));
EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, true));
EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 3, false));
EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, true));
EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 1, false));
EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, true));
EXPECT_EQ(order.Get(17), TaskItem(RECURSIVE, 3, false));
}
#endif
void PostNTasksThenQuit(int posts_remaining) {
if (posts_remaining > 1) {
MessageLoop::current()->PostTask(
FROM_HERE,
Bind(&PostNTasksThenQuit, posts_remaining - 1));
} else {
MessageLoop::current()->QuitWhenIdle();
}
}
#if defined(OS_WIN)
class DispatcherImpl : public MessagePumpDispatcher {
public:
DispatcherImpl() : dispatch_count_(0) {}
virtual uint32_t Dispatch(const NativeEvent& msg) OVERRIDE {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if (msg.message != WM_TIMER)
++dispatch_count_;
return msg.message == WM_LBUTTONUP ? POST_DISPATCH_QUIT_LOOP
: POST_DISPATCH_NONE;
}
int dispatch_count_;
};
void MouseDownUp() {
PostMessage(NULL, WM_LBUTTONDOWN, 0, 0);
PostMessage(NULL, WM_LBUTTONUP, 'A', 0);
}
void RunTest_Dispatcher(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
Bind(&MouseDownUp),
TimeDelta::FromMilliseconds(100));
DispatcherImpl dispatcher;
RunLoop run_loop(&dispatcher);
run_loop.Run();
ASSERT_EQ(2, dispatcher.dispatch_count_);
}
LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) {
if (code == MessagePumpForUI::kMessageFilterCode) {
MSG* msg = reinterpret_cast<MSG*>(lparam);
if (msg->message == WM_LBUTTONDOWN)
return TRUE;
}
return FALSE;
}
void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
Bind(&MouseDownUp),
TimeDelta::FromMilliseconds(100));
HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER,
MsgFilterProc,
NULL,
GetCurrentThreadId());
DispatcherImpl dispatcher;
RunLoop run_loop(&dispatcher);
run_loop.Run();
ASSERT_EQ(1, dispatcher.dispatch_count_);
UnhookWindowsHookEx(msg_hook);
}
class TestIOHandler : public MessageLoopForIO::IOHandler {
public:
TestIOHandler(const wchar_t* name, HANDLE signal, bool wait);
virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
DWORD bytes_transfered, DWORD error);
void Init();
void WaitForIO();
OVERLAPPED* context() { return &context_.overlapped; }
DWORD size() { return sizeof(buffer_); }
private:
char buffer_[48];
MessageLoopForIO::IOContext context_;
HANDLE signal_;
win::ScopedHandle file_;
bool wait_;
};
TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait)
: signal_(signal), wait_(wait) {
memset(buffer_, 0, sizeof(buffer_));
memset(&context_, 0, sizeof(context_));
context_.handler = this;
file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL));
EXPECT_TRUE(file_.IsValid());
}
void TestIOHandler::Init() {
MessageLoopForIO::current()->RegisterIOHandler(file_, this);
DWORD read;
EXPECT_FALSE(ReadFile(file_, buffer_, size(), &read, context()));
EXPECT_EQ(ERROR_IO_PENDING, GetLastError());
if (wait_)
WaitForIO();
}
void TestIOHandler::OnIOCompleted(MessageLoopForIO::IOContext* context,
DWORD bytes_transfered, DWORD error) {
ASSERT_TRUE(context == &context_);
ASSERT_TRUE(SetEvent(signal_));
}
void TestIOHandler::WaitForIO() {
EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(300, this));
EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(400, this));
}
void RunTest_IOHandler() {
win::ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL));
ASSERT_TRUE(callback_called.IsValid());
const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe";
win::ScopedHandle server(
CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
ASSERT_TRUE(server.IsValid());
Thread thread("IOHandler test");
Thread::Options options;
options.message_loop_type = MessageLoop::TYPE_IO;
ASSERT_TRUE(thread.StartWithOptions(options));
MessageLoop* thread_loop = thread.message_loop();
ASSERT_TRUE(NULL != thread_loop);
TestIOHandler handler(kPipeName, callback_called, false);
thread_loop->PostTask(FROM_HERE, Bind(&TestIOHandler::Init,
Unretained(&handler)));
PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
const char buffer[] = "Hello there!";
DWORD written;
EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL));
DWORD result = WaitForSingleObject(callback_called, 1000);
EXPECT_EQ(WAIT_OBJECT_0, result);
thread.Stop();
}
void RunTest_WaitForIO() {
win::ScopedHandle callback1_called(
CreateEvent(NULL, TRUE, FALSE, NULL));
win::ScopedHandle callback2_called(
CreateEvent(NULL, TRUE, FALSE, NULL));
ASSERT_TRUE(callback1_called.IsValid());
ASSERT_TRUE(callback2_called.IsValid());
const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1";
const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2";
win::ScopedHandle server1(
CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
win::ScopedHandle server2(
CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
ASSERT_TRUE(server1.IsValid());
ASSERT_TRUE(server2.IsValid());
Thread thread("IOHandler test");
Thread::Options options;
options.message_loop_type = MessageLoop::TYPE_IO;
ASSERT_TRUE(thread.StartWithOptions(options));
MessageLoop* thread_loop = thread.message_loop();
ASSERT_TRUE(NULL != thread_loop);
TestIOHandler handler1(kPipeName1, callback1_called, false);
TestIOHandler handler2(kPipeName2, callback2_called, true);
thread_loop->PostTask(FROM_HERE, Bind(&TestIOHandler::Init,
Unretained(&handler1)));
TimeDelta delay = TimeDelta::FromMilliseconds(100);
PlatformThread::Sleep(delay);
thread_loop->PostTask(FROM_HERE, Bind(&TestIOHandler::Init,
Unretained(&handler2)));
PlatformThread::Sleep(delay);
const char buffer[] = "Hello there!";
DWORD written;
EXPECT_TRUE(WriteFile(server1, buffer, sizeof(buffer), &written, NULL));
PlatformThread::Sleep(2 * delay);
EXPECT_EQ(WAIT_TIMEOUT, WaitForSingleObject(callback1_called, 0)) <<
"handler1 has not been called";
EXPECT_TRUE(WriteFile(server2, buffer, sizeof(buffer), &written, NULL));
HANDLE objects[2] = { callback1_called.Get(), callback2_called.Get() };
DWORD result = WaitForMultipleObjects(2, objects, TRUE, 1000);
EXPECT_EQ(WAIT_OBJECT_0, result);
thread.Stop();
}
#endif
}
RUN_MESSAGE_LOOP_TESTS(Default, &TypeDefaultMessagePumpFactory);
RUN_MESSAGE_LOOP_TESTS(UI, &TypeUIMessagePumpFactory);
RUN_MESSAGE_LOOP_TESTS(IO, &TypeIOMessagePumpFactory);
#if defined(OS_WIN)
TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) {
RunTest_PostDelayedTask_SharedTimer_SubPump();
}
TEST(MessageLoopTest, DISABLED_RecursiveDenial2) {
RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT);
RunTest_RecursiveDenial2(MessageLoop::TYPE_UI);
RunTest_RecursiveDenial2(MessageLoop::TYPE_IO);
}
TEST(MessageLoopTest, RecursiveSupport2) {
RunTest_RecursiveSupport2(MessageLoop::TYPE_UI);
}
#endif
class DummyTaskObserver : public MessageLoop::TaskObserver {
public:
explicit DummyTaskObserver(int num_tasks)
: num_tasks_started_(0),
num_tasks_processed_(0),
num_tasks_(num_tasks) {}
virtual ~DummyTaskObserver() {}
virtual void WillProcessTask(const PendingTask& pending_task) OVERRIDE {
num_tasks_started_++;
EXPECT_TRUE(pending_task.time_posted != TimeTicks());
EXPECT_LE(num_tasks_started_, num_tasks_);
EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1);
}
virtual void DidProcessTask(const PendingTask& pending_task) OVERRIDE {
num_tasks_processed_++;
EXPECT_TRUE(pending_task.time_posted != TimeTicks());
EXPECT_LE(num_tasks_started_, num_tasks_);
EXPECT_EQ(num_tasks_started_, num_tasks_processed_);
}
int num_tasks_started() const { return num_tasks_started_; }
int num_tasks_processed() const { return num_tasks_processed_; }
private:
int num_tasks_started_;
int num_tasks_processed_;
const int num_tasks_;
DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver);
};
TEST(MessageLoopTest, TaskObserver) {
const int kNumPosts = 6;
DummyTaskObserver observer(kNumPosts);
MessageLoop loop;
loop.AddTaskObserver(&observer);
loop.PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumPosts));
loop.Run();
loop.RemoveTaskObserver(&observer);
EXPECT_EQ(kNumPosts, observer.num_tasks_started());
EXPECT_EQ(kNumPosts, observer.num_tasks_processed());
}
#if defined(OS_WIN)
TEST(MessageLoopTest, Dispatcher) {
RunTest_Dispatcher(MessageLoop::TYPE_UI);
}
TEST(MessageLoopTest, DispatcherWithMessageHook) {
RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI);
}
TEST(MessageLoopTest, IOHandler) {
RunTest_IOHandler();
}
TEST(MessageLoopTest, WaitForIO) {
RunTest_WaitForIO();
}
TEST(MessageLoopTest, HighResolutionTimer) {
MessageLoop loop;
const TimeDelta kFastTimer = TimeDelta::FromMilliseconds(5);
const TimeDelta kSlowTimer = TimeDelta::FromMilliseconds(100);
EXPECT_FALSE(loop.IsHighResolutionTimerEnabledForTesting());
loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1),
kFastTimer);
loop.Run();
EXPECT_TRUE(loop.IsHighResolutionTimerEnabledForTesting());
loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1),
kSlowTimer);
loop.Run();
EXPECT_TRUE(loop.IsHighResolutionTimerEnabledForTesting());
PlatformThread::Sleep(TimeDelta::FromMilliseconds(
MessageLoop::kHighResolutionTimerModeLeaseTimeMs));
loop.PostDelayedTask(FROM_HERE, Bind(&PostNTasksThenQuit, 1),
kSlowTimer);
loop.Run();
EXPECT_FALSE(loop.IsHighResolutionTimerEnabledForTesting());
}
#endif
#if defined(OS_POSIX) && !defined(OS_NACL)
namespace {
class QuitDelegate : public MessageLoopForIO::Watcher {
public:
virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
MessageLoop::current()->QuitWhenIdle();
}
virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
MessageLoop::current()->QuitWhenIdle();
}
};
TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) {
int pipefds[2];
int err = pipe(pipefds);
ASSERT_EQ(0, err);
int fd = pipefds[1];
{
MessageLoopForIO::FileDescriptorWatcher controller;
{
MessageLoopForIO message_loop;
QuitDelegate delegate;
message_loop.WatchFileDescriptor(fd,
true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
}
}
if (IGNORE_EINTR(close(pipefds[0])) < 0)
PLOG(ERROR) << "close";
if (IGNORE_EINTR(close(pipefds[1])) < 0)
PLOG(ERROR) << "close";
}
TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) {
int pipefds[2];
int err = pipe(pipefds);
ASSERT_EQ(0, err);
int fd = pipefds[1];
{
MessageLoopForIO message_loop;
{
MessageLoopForIO::FileDescriptorWatcher controller;
QuitDelegate delegate;
message_loop.WatchFileDescriptor(fd,
true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
controller.StopWatchingFileDescriptor();
}
}
if (IGNORE_EINTR(close(pipefds[0])) < 0)
PLOG(ERROR) << "close";
if (IGNORE_EINTR(close(pipefds[1])) < 0)
PLOG(ERROR) << "close";
}
}
#endif
namespace {
class DestructionObserverProbe :
public RefCounted<DestructionObserverProbe> {
public:
DestructionObserverProbe(bool* task_destroyed,
bool* destruction_observer_called)
: task_destroyed_(task_destroyed),
destruction_observer_called_(destruction_observer_called) {
}
virtual void Run() {
ADD_FAILURE();
}
private:
friend class RefCounted<DestructionObserverProbe>;
virtual ~DestructionObserverProbe() {
EXPECT_FALSE(*destruction_observer_called_);
*task_destroyed_ = true;
}
bool* task_destroyed_;
bool* destruction_observer_called_;
};
class MLDestructionObserver : public MessageLoop::DestructionObserver {
public:
MLDestructionObserver(bool* task_destroyed, bool* destruction_observer_called)
: task_destroyed_(task_destroyed),
destruction_observer_called_(destruction_observer_called),
task_destroyed_before_message_loop_(false) {
}
virtual void WillDestroyCurrentMessageLoop() OVERRIDE {
task_destroyed_before_message_loop_ = *task_destroyed_;
*destruction_observer_called_ = true;
}
bool task_destroyed_before_message_loop() const {
return task_destroyed_before_message_loop_;
}
private:
bool* task_destroyed_;
bool* destruction_observer_called_;
bool task_destroyed_before_message_loop_;
};
}
TEST(MessageLoopTest, DestructionObserverTest) {
MessageLoop* loop = new MessageLoop;
const TimeDelta kDelay = TimeDelta::FromMilliseconds(100);
bool task_destroyed = false;
bool destruction_observer_called = false;
MLDestructionObserver observer(&task_destroyed, &destruction_observer_called);
loop->AddDestructionObserver(&observer);
loop->PostDelayedTask(
FROM_HERE,
Bind(&DestructionObserverProbe::Run,
new DestructionObserverProbe(&task_destroyed,
&destruction_observer_called)),
kDelay);
delete loop;
EXPECT_TRUE(observer.task_destroyed_before_message_loop());
EXPECT_TRUE(task_destroyed);
EXPECT_TRUE(destruction_observer_called);
}
TEST(MessageLoopTest, ThreadMainTaskRunner) {
MessageLoop loop;
scoped_refptr<Foo> foo(new Foo());
std::string a("a");
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(
&Foo::Test1ConstRef, foo.get(), a));
MessageLoop::current()->PostTask(FROM_HERE, Bind(
&MessageLoop::Quit, Unretained(MessageLoop::current())));
MessageLoop::current()->Run();
EXPECT_EQ(foo->test_count(), 1);
EXPECT_EQ(foo->result(), "a");
}
TEST(MessageLoopTest, IsType) {
MessageLoop loop(MessageLoop::TYPE_UI);
EXPECT_TRUE(loop.IsType(MessageLoop::TYPE_UI));
EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_IO));
EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_DEFAULT));
}
#if defined(OS_WIN)
void EmptyFunction() {}
void PostMultipleTasks() {
MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&EmptyFunction));
MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&EmptyFunction));
}
static const int kSignalMsg = WM_USER + 2;
void PostWindowsMessage(HWND message_hwnd) {
PostMessage(message_hwnd, kSignalMsg, 0, 2);
}
void EndTest(bool* did_run, HWND hwnd) {
*did_run = true;
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
int kMyMessageFilterCode = 0x5002;
LRESULT CALLBACK TestWndProcThunk(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam) {
if (message == WM_CLOSE)
EXPECT_TRUE(DestroyWindow(hwnd));
if (message != kSignalMsg)
return DefWindowProc(hwnd, message, wparam, lparam);
switch (lparam) {
case 1:
MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&PostMultipleTasks));
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&PostWindowsMessage, hwnd));
break;
case 2:
MessageLoop::current()->SetNestableTasksAllowed(true);
bool did_run = false;
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&EndTest, &did_run, hwnd));
MSG msg;
while (GetMessage(&msg, 0, 0, 0)) {
if (!CallMsgFilter(&msg, kMyMessageFilterCode))
DispatchMessage(&msg);
if (msg.message == WM_CLOSE)
break;
}
EXPECT_TRUE(did_run);
MessageLoop::current()->Quit();
break;
}
return 0;
}
TEST(MessageLoopTest, AlwaysHaveUserMessageWhenNesting) {
MessageLoop loop(MessageLoop::TYPE_UI);
HINSTANCE instance = GetModuleFromAddress(&TestWndProcThunk);
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = TestWndProcThunk;
wc.hInstance = instance;
wc.lpszClassName = L"MessageLoopTest_HWND";
ATOM atom = RegisterClassEx(&wc);
ASSERT_TRUE(atom);
HWND message_hwnd = CreateWindow(MAKEINTATOM(atom), 0, 0, 0, 0, 0, 0,
HWND_MESSAGE, 0, instance, 0);
ASSERT_TRUE(message_hwnd) << GetLastError();
ASSERT_TRUE(PostMessage(message_hwnd, kSignalMsg, 0, 1));
loop.Run();
ASSERT_TRUE(UnregisterClass(MAKEINTATOM(atom), instance));
}
#endif
}