This source file includes following definitions.
- HandlePrepare
- HandleCheck
- HandleDispatch
- AddEvent
- AddDummyEvent
- AddEventAsTask
- Reset
- processed_events
- AddEventHelper
- Prepare
- Check
- Dispatch
- IncrementInt
- ExpectProcessedEvents
- PostMessageLoopTask
- injector_
- SetUp
- TearDown
- loop
- injector
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- task_count_
- FromTask
- FromEvent
- event_count
- task_count
- TEST_F
- AddEventsAndDrainGLib
- TEST_F
- AddEventsAndDrainGtk
- TEST_F
- RunGLib
- RunLoop
- Quit
- Reset
- TestGLibLoopInternal
- TestGtkLoopInternal
- TEST_F
- TEST_F
#include "base/message_loop/message_pump_glib.h"
#include <glib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(TOOLKIT_GTK)
#include <gtk/gtk.h>
#endif
namespace base {
namespace {
class EventInjector {
public:
EventInjector() : processed_events_(0) {
source_ = static_cast<Source*>(g_source_new(&SourceFuncs, sizeof(Source)));
source_->injector = this;
g_source_attach(source_, NULL);
g_source_set_can_recurse(source_, TRUE);
}
~EventInjector() {
g_source_destroy(source_);
g_source_unref(source_);
}
int HandlePrepare() {
if (events_.empty())
return -1;
TimeDelta delta = events_[0].time - Time::NowFromSystemTime();
return std::max(0, static_cast<int>(ceil(delta.InMillisecondsF())));
}
bool HandleCheck() {
if (events_.empty())
return false;
return events_[0].time <= Time::NowFromSystemTime();
}
void HandleDispatch() {
if (events_.empty())
return;
Event event = events_[0];
events_.erase(events_.begin());
++processed_events_;
if (!event.callback.is_null())
event.callback.Run();
else if (!event.task.is_null())
event.task.Run();
}
void AddEvent(int delay_ms, const Closure& callback) {
AddEventHelper(delay_ms, callback, Closure());
}
void AddDummyEvent(int delay_ms) {
AddEventHelper(delay_ms, Closure(), Closure());
}
void AddEventAsTask(int delay_ms, const Closure& task) {
AddEventHelper(delay_ms, Closure(), task);
}
void Reset() {
processed_events_ = 0;
events_.clear();
}
int processed_events() const { return processed_events_; }
private:
struct Event {
Time time;
Closure callback;
Closure task;
};
struct Source : public GSource {
EventInjector* injector;
};
void AddEventHelper(
int delay_ms, const Closure& callback, const Closure& task) {
Time last_time;
if (!events_.empty())
last_time = (events_.end()-1)->time;
else
last_time = Time::NowFromSystemTime();
Time future = last_time + TimeDelta::FromMilliseconds(delay_ms);
EventInjector::Event event = {future, callback, task};
events_.push_back(event);
}
static gboolean Prepare(GSource* source, gint* timeout_ms) {
*timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare();
return FALSE;
}
static gboolean Check(GSource* source) {
return static_cast<Source*>(source)->injector->HandleCheck();
}
static gboolean Dispatch(GSource* source,
GSourceFunc unused_func,
gpointer unused_data) {
static_cast<Source*>(source)->injector->HandleDispatch();
return TRUE;
}
Source* source_;
std::vector<Event> events_;
int processed_events_;
static GSourceFuncs SourceFuncs;
DISALLOW_COPY_AND_ASSIGN(EventInjector);
};
GSourceFuncs EventInjector::SourceFuncs = {
EventInjector::Prepare,
EventInjector::Check,
EventInjector::Dispatch,
NULL
};
void IncrementInt(int *value) {
++*value;
}
void ExpectProcessedEvents(EventInjector* injector, int count) {
EXPECT_EQ(injector->processed_events(), count);
}
void PostMessageLoopTask(const tracked_objects::Location& from_here,
const Closure& task) {
MessageLoop::current()->PostTask(from_here, task);
}
class MessagePumpGLibTest : public testing::Test {
public:
MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { }
virtual void SetUp() OVERRIDE {
loop_ = new MessageLoop(MessageLoop::TYPE_UI);
injector_ = new EventInjector();
}
virtual void TearDown() OVERRIDE {
delete injector_;
injector_ = NULL;
delete loop_;
loop_ = NULL;
}
MessageLoop* loop() const { return loop_; }
EventInjector* injector() const { return injector_; }
private:
MessageLoop* loop_;
EventInjector* injector_;
DISALLOW_COPY_AND_ASSIGN(MessagePumpGLibTest);
};
}
TEST_F(MessagePumpGLibTest, TestQuit) {
RunLoop().RunUntilIdle();
EXPECT_EQ(0, injector()->processed_events());
injector()->Reset();
injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
loop()->Run();
EXPECT_EQ(1, injector()->processed_events());
}
TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) {
injector()->AddEventAsTask(0, Bind(&DoNothing));
Closure check_task =
Bind(&ExpectProcessedEvents, Unretained(injector()), 2);
Closure posted_task =
Bind(&PostMessageLoopTask, FROM_HERE, check_task);
injector()->AddEventAsTask(0, posted_task);
injector()->AddEventAsTask(0, Bind(&DoNothing));
injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
loop()->Run();
EXPECT_EQ(4, injector()->processed_events());
injector()->Reset();
injector()->AddEventAsTask(0, Bind(&DoNothing));
check_task =
Bind(&ExpectProcessedEvents, Unretained(injector()), 2);
posted_task = Bind(&PostMessageLoopTask, FROM_HERE, check_task);
injector()->AddEventAsTask(0, posted_task);
injector()->AddEventAsTask(10, Bind(&DoNothing));
injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
loop()->Run();
EXPECT_EQ(4, injector()->processed_events());
}
TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) {
int task_count = 0;
for (int i = 0; i < 10; ++i) {
loop()->PostTask(FROM_HERE, Bind(&IncrementInt, &task_count));
}
loop()->PostTask(
FROM_HERE,
Bind(&EventInjector::AddEvent, Unretained(injector()), 0,
MessageLoop::QuitWhenIdleClosure()));
loop()->Run();
ASSERT_EQ(10, task_count);
EXPECT_EQ(1, injector()->processed_events());
injector()->Reset();
task_count = 0;
for (int i = 0; i < 10; ++i) {
loop()->PostDelayedTask(
FROM_HERE,
Bind(&IncrementInt, &task_count),
TimeDelta::FromMilliseconds(10*i));
}
loop()->PostDelayedTask(
FROM_HERE,
Bind(&EventInjector::AddEvent, Unretained(injector()), 10,
MessageLoop::QuitWhenIdleClosure()),
TimeDelta::FromMilliseconds(150));
loop()->Run();
ASSERT_EQ(10, task_count);
EXPECT_EQ(1, injector()->processed_events());
}
TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) {
for (int i = 0; i < 10; ++i) {
injector()->AddDummyEvent(0);
}
Closure check_task =
Bind(&ExpectProcessedEvents, Unretained(injector()), 11);
Closure posted_task =
Bind(&PostMessageLoopTask, FROM_HERE, check_task);
injector()->AddEventAsTask(10, posted_task);
injector()->AddEvent(10, MessageLoop::QuitWhenIdleClosure());
loop()->Run();
EXPECT_EQ(12, injector()->processed_events());
}
namespace {
class ConcurrentHelper : public RefCounted<ConcurrentHelper> {
public:
explicit ConcurrentHelper(EventInjector* injector)
: injector_(injector),
event_count_(kStartingEventCount),
task_count_(kStartingTaskCount) {
}
void FromTask() {
if (task_count_ > 0) {
--task_count_;
}
if (task_count_ == 0 && event_count_ == 0) {
MessageLoop::current()->QuitWhenIdle();
} else {
MessageLoop::current()->PostTask(
FROM_HERE, Bind(&ConcurrentHelper::FromTask, this));
}
}
void FromEvent() {
if (event_count_ > 0) {
--event_count_;
}
if (task_count_ == 0 && event_count_ == 0) {
MessageLoop::current()->QuitWhenIdle();
} else {
injector_->AddEventAsTask(
0, Bind(&ConcurrentHelper::FromEvent, this));
}
}
int event_count() const { return event_count_; }
int task_count() const { return task_count_; }
private:
friend class RefCounted<ConcurrentHelper>;
~ConcurrentHelper() {}
static const int kStartingEventCount = 20;
static const int kStartingTaskCount = 20;
EventInjector* injector_;
int event_count_;
int task_count_;
};
}
TEST_F(MessagePumpGLibTest, TestConcurrentEventPostedTask) {
scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector());
injector()->AddEventAsTask(
0, Bind(&ConcurrentHelper::FromEvent, helper.get()));
injector()->AddEventAsTask(
0, Bind(&ConcurrentHelper::FromEvent, helper.get()));
loop()->PostTask(
FROM_HERE, Bind(&ConcurrentHelper::FromTask, helper.get()));
loop()->PostTask(
FROM_HERE, Bind(&ConcurrentHelper::FromTask, helper.get()));
loop()->Run();
EXPECT_EQ(0, helper->event_count());
EXPECT_EQ(0, helper->task_count());
}
namespace {
void AddEventsAndDrainGLib(EventInjector* injector) {
injector->AddDummyEvent(0);
injector->AddDummyEvent(0);
injector->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
MessageLoop::current()->PostTask(FROM_HERE, Bind(&DoNothing));
MessageLoop::current()->PostTask(FROM_HERE, Bind(&DoNothing));
while (g_main_context_pending(NULL)) {
g_main_context_iteration(NULL, FALSE);
}
}
}
TEST_F(MessagePumpGLibTest, TestDrainingGLib) {
loop()->PostTask(
FROM_HERE,
Bind(&AddEventsAndDrainGLib, Unretained(injector())));
loop()->Run();
EXPECT_EQ(3, injector()->processed_events());
}
namespace {
#if defined(TOOLKIT_GTK)
void AddEventsAndDrainGtk(EventInjector* injector) {
injector->AddDummyEvent(0);
injector->AddDummyEvent(0);
injector->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
MessageLoop::current()->PostTask(FROM_HERE, Bind(&DoNothing));
MessageLoop::current()->PostTask(FROM_HERE, Bind(&DoNothing));
while (gtk_events_pending()) {
gtk_main_iteration();
}
}
#endif
}
#if defined(TOOLKIT_GTK)
TEST_F(MessagePumpGLibTest, TestDrainingGtk) {
loop()->PostTask(
FROM_HERE,
Bind(&AddEventsAndDrainGtk, Unretained(injector())));
loop()->Run();
EXPECT_EQ(3, injector()->processed_events());
}
#endif
namespace {
class GLibLoopRunner : public RefCounted<GLibLoopRunner> {
public:
GLibLoopRunner() : quit_(false) { }
void RunGLib() {
while (!quit_) {
g_main_context_iteration(NULL, TRUE);
}
}
void RunLoop() {
#if defined(TOOLKIT_GTK)
while (!quit_) {
gtk_main_iteration();
}
#else
while (!quit_) {
g_main_context_iteration(NULL, TRUE);
}
#endif
}
void Quit() {
quit_ = true;
}
void Reset() {
quit_ = false;
}
private:
friend class RefCounted<GLibLoopRunner>;
~GLibLoopRunner() {}
bool quit_;
};
void TestGLibLoopInternal(EventInjector* injector) {
MessageLoop::current()->SetNestableTasksAllowed(true);
scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner();
int task_count = 0;
injector->AddDummyEvent(0);
injector->AddDummyEvent(0);
MessageLoop::current()->PostTask(
FROM_HERE, Bind(&IncrementInt, &task_count));
MessageLoop::current()->PostTask(
FROM_HERE, Bind(&IncrementInt, &task_count));
injector->AddDummyEvent(10);
injector->AddDummyEvent(10);
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
Bind(&IncrementInt, &task_count),
TimeDelta::FromMilliseconds(30));
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
Bind(&GLibLoopRunner::Quit, runner.get()),
TimeDelta::FromMilliseconds(40));
runner->RunGLib();
ASSERT_EQ(3, task_count);
EXPECT_EQ(4, injector->processed_events());
MessageLoop::current()->QuitWhenIdle();
}
void TestGtkLoopInternal(EventInjector* injector) {
MessageLoop::current()->SetNestableTasksAllowed(true);
scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner();
int task_count = 0;
injector->AddDummyEvent(0);
injector->AddDummyEvent(0);
MessageLoop::current()->PostTask(
FROM_HERE, Bind(&IncrementInt, &task_count));
MessageLoop::current()->PostTask(
FROM_HERE, Bind(&IncrementInt, &task_count));
injector->AddDummyEvent(10);
injector->AddDummyEvent(10);
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
Bind(&IncrementInt, &task_count),
TimeDelta::FromMilliseconds(30));
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
Bind(&GLibLoopRunner::Quit, runner.get()),
TimeDelta::FromMilliseconds(40));
runner->RunLoop();
ASSERT_EQ(3, task_count);
EXPECT_EQ(4, injector->processed_events());
MessageLoop::current()->QuitWhenIdle();
}
}
TEST_F(MessagePumpGLibTest, TestGLibLoop) {
loop()->PostTask(
FROM_HERE,
Bind(&TestGLibLoopInternal, Unretained(injector())));
loop()->Run();
}
TEST_F(MessagePumpGLibTest, TestGtkLoop) {
loop()->PostTask(
FROM_HERE,
Bind(&TestGtkLoopInternal, Unretained(injector())));
loop()->Run();
}
}