OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/event_rewriter.h" | 5 #include "ui/events/event_rewriter.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "ui/events/test/test_event_processor.h" | 13 #include "ui/events/test/test_event_processor.h" |
14 | 14 |
15 namespace ui { | 15 namespace ui { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // To test the handling of |EventRewriter|s through |EventSource|, | 19 // To test the handling of |EventRewriter|s through |EventSource|, |
20 // we rewrite and test event types. | 20 // we rewrite and test event types. |
21 class TestEvent : public Event { | 21 class TestEvent : public Event { |
22 public: | 22 public: |
23 explicit TestEvent(EventType type) | 23 explicit TestEvent(EventType type) |
24 : Event(type, base::TimeDelta(), 0), unique_id_(next_unique_id_++) {} | 24 : Event(type, base::TimeDelta(), 0), unique_id_(next_unique_id_++) {} |
25 virtual ~TestEvent() {} | 25 ~TestEvent() override {} |
26 int unique_id() const { return unique_id_; } | 26 int unique_id() const { return unique_id_; } |
27 | 27 |
28 private: | 28 private: |
29 static int next_unique_id_; | 29 static int next_unique_id_; |
30 int unique_id_; | 30 int unique_id_; |
31 }; | 31 }; |
32 | 32 |
33 int TestEvent::next_unique_id_ = 0; | 33 int TestEvent::next_unique_id_ = 0; |
34 | 34 |
35 // TestEventRewriteProcessor is set up with a sequence of event types, | 35 // TestEventRewriteProcessor is set up with a sequence of event types, |
36 // and fails if the events received via OnEventFromSource() do not match | 36 // and fails if the events received via OnEventFromSource() do not match |
37 // this sequence. These expected event types are consumed on receipt. | 37 // this sequence. These expected event types are consumed on receipt. |
38 class TestEventRewriteProcessor : public test::TestEventProcessor { | 38 class TestEventRewriteProcessor : public test::TestEventProcessor { |
39 public: | 39 public: |
40 TestEventRewriteProcessor() {} | 40 TestEventRewriteProcessor() {} |
41 virtual ~TestEventRewriteProcessor() { CheckAllReceived(); } | 41 ~TestEventRewriteProcessor() override { CheckAllReceived(); } |
42 | 42 |
43 void AddExpectedEvent(EventType type) { expected_events_.push_back(type); } | 43 void AddExpectedEvent(EventType type) { expected_events_.push_back(type); } |
44 // Test that all expected events have been received. | 44 // Test that all expected events have been received. |
45 void CheckAllReceived() { EXPECT_TRUE(expected_events_.empty()); } | 45 void CheckAllReceived() { EXPECT_TRUE(expected_events_.empty()); } |
46 | 46 |
47 // EventProcessor: | 47 // EventProcessor: |
48 virtual EventDispatchDetails OnEventFromSource(Event* event) override { | 48 EventDispatchDetails OnEventFromSource(Event* event) override { |
49 EXPECT_FALSE(expected_events_.empty()); | 49 EXPECT_FALSE(expected_events_.empty()); |
50 EXPECT_EQ(expected_events_.front(), event->type()); | 50 EXPECT_EQ(expected_events_.front(), event->type()); |
51 expected_events_.pop_front(); | 51 expected_events_.pop_front(); |
52 return EventDispatchDetails(); | 52 return EventDispatchDetails(); |
53 } | 53 } |
54 | 54 |
55 private: | 55 private: |
56 std::list<EventType> expected_events_; | 56 std::list<EventType> expected_events_; |
57 DISALLOW_COPY_AND_ASSIGN(TestEventRewriteProcessor); | 57 DISALLOW_COPY_AND_ASSIGN(TestEventRewriteProcessor); |
58 }; | 58 }; |
59 | 59 |
60 // Trivial EventSource that does nothing but send events. | 60 // Trivial EventSource that does nothing but send events. |
61 class TestEventRewriteSource : public EventSource { | 61 class TestEventRewriteSource : public EventSource { |
62 public: | 62 public: |
63 explicit TestEventRewriteSource(EventProcessor* processor) | 63 explicit TestEventRewriteSource(EventProcessor* processor) |
64 : processor_(processor) {} | 64 : processor_(processor) {} |
65 virtual EventProcessor* GetEventProcessor() override { return processor_; } | 65 EventProcessor* GetEventProcessor() override { return processor_; } |
66 void Send(EventType type) { | 66 void Send(EventType type) { |
67 scoped_ptr<Event> event(new TestEvent(type)); | 67 scoped_ptr<Event> event(new TestEvent(type)); |
68 (void)SendEventToProcessor(event.get()); | 68 (void)SendEventToProcessor(event.get()); |
69 } | 69 } |
70 | 70 |
71 private: | 71 private: |
72 EventProcessor* processor_; | 72 EventProcessor* processor_; |
73 }; | 73 }; |
74 | 74 |
75 // This EventRewriter always returns the same status, and if rewriting, the | 75 // This EventRewriter always returns the same status, and if rewriting, the |
76 // same event type; it is used to test simple rewriting, and rewriter addition, | 76 // same event type; it is used to test simple rewriting, and rewriter addition, |
77 // removal, and sequencing. Consequently EVENT_REWRITE_DISPATCH_ANOTHER is not | 77 // removal, and sequencing. Consequently EVENT_REWRITE_DISPATCH_ANOTHER is not |
78 // supported here (calls to NextDispatchEvent() would continue indefinitely). | 78 // supported here (calls to NextDispatchEvent() would continue indefinitely). |
79 class TestConstantEventRewriter : public EventRewriter { | 79 class TestConstantEventRewriter : public EventRewriter { |
80 public: | 80 public: |
81 TestConstantEventRewriter(EventRewriteStatus status, EventType type) | 81 TestConstantEventRewriter(EventRewriteStatus status, EventType type) |
82 : status_(status), type_(type) { | 82 : status_(status), type_(type) { |
83 CHECK_NE(EVENT_REWRITE_DISPATCH_ANOTHER, status); | 83 CHECK_NE(EVENT_REWRITE_DISPATCH_ANOTHER, status); |
84 } | 84 } |
85 | 85 |
86 virtual EventRewriteStatus RewriteEvent(const Event& event, | 86 EventRewriteStatus RewriteEvent(const Event& event, |
87 scoped_ptr<Event>* rewritten_event) | 87 scoped_ptr<Event>* rewritten_event) |
88 override { | 88 override { |
89 if (status_ == EVENT_REWRITE_REWRITTEN) | 89 if (status_ == EVENT_REWRITE_REWRITTEN) |
90 rewritten_event->reset(new TestEvent(type_)); | 90 rewritten_event->reset(new TestEvent(type_)); |
91 return status_; | 91 return status_; |
92 } | 92 } |
93 virtual EventRewriteStatus NextDispatchEvent(const Event& last_event, | 93 EventRewriteStatus NextDispatchEvent(const Event& last_event, |
94 scoped_ptr<Event>* new_event) | 94 scoped_ptr<Event>* new_event) override { |
95 override { | |
96 NOTREACHED(); | 95 NOTREACHED(); |
97 return status_; | 96 return status_; |
98 } | 97 } |
99 | 98 |
100 private: | 99 private: |
101 EventRewriteStatus status_; | 100 EventRewriteStatus status_; |
102 EventType type_; | 101 EventType type_; |
103 }; | 102 }; |
104 | 103 |
105 // This EventRewriter runs a simple state machine; it is used to test | 104 // This EventRewriter runs a simple state machine; it is used to test |
106 // EVENT_REWRITE_DISPATCH_ANOTHER. | 105 // EVENT_REWRITE_DISPATCH_ANOTHER. |
107 class TestStateMachineEventRewriter : public EventRewriter { | 106 class TestStateMachineEventRewriter : public EventRewriter { |
108 public: | 107 public: |
109 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {} | 108 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {} |
110 void AddRule(int from_state, EventType from_type, | 109 void AddRule(int from_state, EventType from_type, |
111 int to_state, EventType to_type, EventRewriteStatus to_status) { | 110 int to_state, EventType to_type, EventRewriteStatus to_status) { |
112 RewriteResult r = {to_state, to_type, to_status}; | 111 RewriteResult r = {to_state, to_type, to_status}; |
113 rules_.insert(std::pair<RewriteCase, RewriteResult>( | 112 rules_.insert(std::pair<RewriteCase, RewriteResult>( |
114 RewriteCase(from_state, from_type), r)); | 113 RewriteCase(from_state, from_type), r)); |
115 } | 114 } |
116 virtual EventRewriteStatus RewriteEvent(const Event& event, | 115 EventRewriteStatus RewriteEvent(const Event& event, |
117 scoped_ptr<Event>* rewritten_event) | 116 scoped_ptr<Event>* rewritten_event) override { |
118 override { | |
119 RewriteRules::iterator find = | 117 RewriteRules::iterator find = |
120 rules_.find(RewriteCase(state_, event.type())); | 118 rules_.find(RewriteCase(state_, event.type())); |
121 if (find == rules_.end()) | 119 if (find == rules_.end()) |
122 return EVENT_REWRITE_CONTINUE; | 120 return EVENT_REWRITE_CONTINUE; |
123 if ((find->second.status == EVENT_REWRITE_REWRITTEN) || | 121 if ((find->second.status == EVENT_REWRITE_REWRITTEN) || |
124 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) { | 122 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) { |
125 last_rewritten_event_ = new TestEvent(find->second.type); | 123 last_rewritten_event_ = new TestEvent(find->second.type); |
126 rewritten_event->reset(last_rewritten_event_); | 124 rewritten_event->reset(last_rewritten_event_); |
127 } else { | 125 } else { |
128 last_rewritten_event_ = 0; | 126 last_rewritten_event_ = 0; |
129 } | 127 } |
130 state_ = find->second.state; | 128 state_ = find->second.state; |
131 return find->second.status; | 129 return find->second.status; |
132 } | 130 } |
133 virtual EventRewriteStatus NextDispatchEvent(const Event& last_event, | 131 EventRewriteStatus NextDispatchEvent(const Event& last_event, |
134 scoped_ptr<Event>* new_event) | 132 scoped_ptr<Event>* new_event) override { |
135 override { | |
136 EXPECT_TRUE(last_rewritten_event_); | 133 EXPECT_TRUE(last_rewritten_event_); |
137 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event); | 134 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event); |
138 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id()); | 135 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id()); |
139 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get()); | 136 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get()); |
140 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id()); | 137 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id()); |
141 return RewriteEvent(last_event, new_event); | 138 return RewriteEvent(last_event, new_event); |
142 } | 139 } |
143 | 140 |
144 private: | 141 private: |
145 typedef std::pair<int, EventType> RewriteCase; | 142 typedef std::pair<int, EventType> RewriteCase; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 s.RemoveEventRewriter(&r3); | 219 s.RemoveEventRewriter(&r3); |
223 | 220 |
224 // Continue with the state-based rewriting. | 221 // Continue with the state-based rewriting. |
225 p.AddExpectedEvent(ET_MOUSE_RELEASED); | 222 p.AddExpectedEvent(ET_MOUSE_RELEASED); |
226 p.AddExpectedEvent(ET_KEY_RELEASED); | 223 p.AddExpectedEvent(ET_KEY_RELEASED); |
227 s.Send(ET_MOUSE_RELEASED); | 224 s.Send(ET_MOUSE_RELEASED); |
228 p.CheckAllReceived(); | 225 p.CheckAllReceived(); |
229 } | 226 } |
230 | 227 |
231 } // namespace ui | 228 } // namespace ui |
OLD | NEW |