This source file includes following definitions.
- unique_id_
- unique_id
- AddExpectedEvent
- CheckAllReceived
- OnEventFromSource
- GetEventProcessor
- Send
- type_
- RewriteEvent
- NextDispatchEvent
- state_
- AddRule
- RewriteEvent
- NextDispatchEvent
- TEST
#include "ui/events/event_rewriter.h"
#include <list>
#include <map>
#include <set>
#include <utility>
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/test/test_event_processor.h"
namespace ui {
namespace {
class TestEvent : public Event {
public:
explicit TestEvent(EventType type)
: Event(type, base::TimeDelta(), 0), unique_id_(next_unique_id_++) {}
virtual ~TestEvent() {}
int unique_id() const { return unique_id_; }
private:
static int next_unique_id_;
int unique_id_;
};
int TestEvent::next_unique_id_ = 0;
class TestEventRewriteProcessor : public test::TestEventProcessor {
public:
TestEventRewriteProcessor() {}
virtual ~TestEventRewriteProcessor() { CheckAllReceived(); }
void AddExpectedEvent(EventType type) { expected_events_.push_back(type); }
void CheckAllReceived() { EXPECT_TRUE(expected_events_.empty()); }
virtual EventDispatchDetails OnEventFromSource(Event* event) OVERRIDE {
EXPECT_FALSE(expected_events_.empty());
EXPECT_EQ(expected_events_.front(), event->type());
expected_events_.pop_front();
return EventDispatchDetails();
}
private:
std::list<EventType> expected_events_;
DISALLOW_COPY_AND_ASSIGN(TestEventRewriteProcessor);
};
class TestEventRewriteSource : public EventSource {
public:
explicit TestEventRewriteSource(EventProcessor* processor)
: processor_(processor) {}
virtual EventProcessor* GetEventProcessor() OVERRIDE { return processor_; }
void Send(EventType type) {
scoped_ptr<Event> event(new TestEvent(type));
(void)SendEventToProcessor(event.get());
}
private:
EventProcessor* processor_;
};
class TestConstantEventRewriter : public EventRewriter {
public:
TestConstantEventRewriter(EventRewriteStatus status, EventType type)
: status_(status), type_(type) {
CHECK_NE(EVENT_REWRITE_DISPATCH_ANOTHER, status);
}
virtual EventRewriteStatus RewriteEvent(const Event& event,
scoped_ptr<Event>* rewritten_event)
OVERRIDE {
if (status_ == EVENT_REWRITE_REWRITTEN)
rewritten_event->reset(new TestEvent(type_));
return status_;
}
virtual EventRewriteStatus NextDispatchEvent(const Event& last_event,
scoped_ptr<Event>* new_event)
OVERRIDE {
NOTREACHED();
return status_;
}
private:
EventRewriteStatus status_;
EventType type_;
};
class TestStateMachineEventRewriter : public EventRewriter {
public:
TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {}
void AddRule(int from_state, EventType from_type,
int to_state, EventType to_type, EventRewriteStatus to_status) {
RewriteResult r = {to_state, to_type, to_status};
rules_.insert(std::pair<RewriteCase, RewriteResult>(
RewriteCase(from_state, from_type), r));
}
virtual EventRewriteStatus RewriteEvent(const Event& event,
scoped_ptr<Event>* rewritten_event)
OVERRIDE {
RewriteRules::iterator find =
rules_.find(RewriteCase(state_, event.type()));
if (find == rules_.end())
return EVENT_REWRITE_CONTINUE;
if ((find->second.status == EVENT_REWRITE_REWRITTEN) ||
(find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) {
last_rewritten_event_ = new TestEvent(find->second.type);
rewritten_event->reset(last_rewritten_event_);
} else {
last_rewritten_event_ = 0;
}
state_ = find->second.state;
return find->second.status;
}
virtual EventRewriteStatus NextDispatchEvent(const Event& last_event,
scoped_ptr<Event>* new_event)
OVERRIDE {
EXPECT_TRUE(last_rewritten_event_);
const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event);
EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id());
const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get());
EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id());
return RewriteEvent(last_event, new_event);
}
private:
typedef std::pair<int, EventType> RewriteCase;
struct RewriteResult {
int state;
EventType type;
EventRewriteStatus status;
};
typedef std::map<RewriteCase, RewriteResult> RewriteRules;
RewriteRules rules_;
TestEvent* last_rewritten_event_;
int state_;
};
}
TEST(EventRewriterTest, EventRewriting) {
TestConstantEventRewriter r0(EVENT_REWRITE_REWRITTEN, ET_CANCEL_MODE);
TestConstantEventRewriter r1(EVENT_REWRITE_CONTINUE, ET_UNKNOWN);
TestStateMachineEventRewriter r2;
TestConstantEventRewriter r3(EVENT_REWRITE_REWRITTEN, ET_CANCEL_MODE);
TestEventRewriteProcessor p;
TestEventRewriteSource s(&p);
s.AddEventRewriter(&r0);
s.AddEventRewriter(&r1);
s.AddEventRewriter(&r2);
p.AddExpectedEvent(ET_CANCEL_MODE);
s.Send(ET_MOUSE_DRAGGED);
p.AddExpectedEvent(ET_CANCEL_MODE);
s.Send(ET_MOUSE_PRESSED);
p.CheckAllReceived();
s.AddEventRewriter(&r3);
s.RemoveEventRewriter(&r0);
r2.AddRule(0, ET_SCROLL_FLING_START,
0, ET_SCROLL_FLING_CANCEL, EVENT_REWRITE_REWRITTEN);
p.AddExpectedEvent(ET_SCROLL_FLING_CANCEL);
s.Send(ET_SCROLL_FLING_START);
p.CheckAllReceived();
s.RemoveEventRewriter(&r3);
r2.AddRule(0, ET_KEY_PRESSED,
1, ET_KEY_PRESSED, EVENT_REWRITE_CONTINUE);
r2.AddRule(1, ET_MOUSE_PRESSED,
0, ET_MOUSE_PRESSED, EVENT_REWRITE_CONTINUE);
r2.AddRule(1, ET_KEY_RELEASED,
2, ET_KEY_RELEASED, EVENT_REWRITE_DISCARD);
r2.AddRule(2, ET_MOUSE_RELEASED,
3, ET_MOUSE_RELEASED, EVENT_REWRITE_DISPATCH_ANOTHER);
r2.AddRule(3, ET_MOUSE_RELEASED,
0, ET_KEY_RELEASED, EVENT_REWRITE_REWRITTEN);
p.AddExpectedEvent(ET_KEY_PRESSED);
s.Send(ET_KEY_PRESSED);
s.Send(ET_KEY_RELEASED);
p.AddExpectedEvent(ET_MOUSE_PRESSED);
s.Send(ET_MOUSE_PRESSED);
s.RemoveEventRewriter(&r1);
s.RemoveEventRewriter(&r3);
p.AddExpectedEvent(ET_MOUSE_RELEASED);
p.AddExpectedEvent(ET_KEY_RELEASED);
s.Send(ET_MOUSE_RELEASED);
p.CheckAllReceived();
}
}