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> |
(...skipping 27 matching lines...) Expand all Loading... |
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 virtual ~TestEventRewriteProcessor() { 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 virtual 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 virtual 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 virtual 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 virtual EventRewriteStatus NextDispatchEvent(const Event& last_event, |
94 scoped_ptr<Event>* new_event) | 94 scoped_ptr<Event>* new_event) |
95 OVERRIDE { | 95 override { |
96 NOTREACHED(); | 96 NOTREACHED(); |
97 return status_; | 97 return status_; |
98 } | 98 } |
99 | 99 |
100 private: | 100 private: |
101 EventRewriteStatus status_; | 101 EventRewriteStatus status_; |
102 EventType type_; | 102 EventType type_; |
103 }; | 103 }; |
104 | 104 |
105 // This EventRewriter runs a simple state machine; it is used to test | 105 // This EventRewriter runs a simple state machine; it is used to test |
106 // EVENT_REWRITE_DISPATCH_ANOTHER. | 106 // EVENT_REWRITE_DISPATCH_ANOTHER. |
107 class TestStateMachineEventRewriter : public EventRewriter { | 107 class TestStateMachineEventRewriter : public EventRewriter { |
108 public: | 108 public: |
109 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {} | 109 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {} |
110 void AddRule(int from_state, EventType from_type, | 110 void AddRule(int from_state, EventType from_type, |
111 int to_state, EventType to_type, EventRewriteStatus to_status) { | 111 int to_state, EventType to_type, EventRewriteStatus to_status) { |
112 RewriteResult r = {to_state, to_type, to_status}; | 112 RewriteResult r = {to_state, to_type, to_status}; |
113 rules_.insert(std::pair<RewriteCase, RewriteResult>( | 113 rules_.insert(std::pair<RewriteCase, RewriteResult>( |
114 RewriteCase(from_state, from_type), r)); | 114 RewriteCase(from_state, from_type), r)); |
115 } | 115 } |
116 virtual EventRewriteStatus RewriteEvent(const Event& event, | 116 virtual EventRewriteStatus RewriteEvent(const Event& event, |
117 scoped_ptr<Event>* rewritten_event) | 117 scoped_ptr<Event>* rewritten_event) |
118 OVERRIDE { | 118 override { |
119 RewriteRules::iterator find = | 119 RewriteRules::iterator find = |
120 rules_.find(RewriteCase(state_, event.type())); | 120 rules_.find(RewriteCase(state_, event.type())); |
121 if (find == rules_.end()) | 121 if (find == rules_.end()) |
122 return EVENT_REWRITE_CONTINUE; | 122 return EVENT_REWRITE_CONTINUE; |
123 if ((find->second.status == EVENT_REWRITE_REWRITTEN) || | 123 if ((find->second.status == EVENT_REWRITE_REWRITTEN) || |
124 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) { | 124 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) { |
125 last_rewritten_event_ = new TestEvent(find->second.type); | 125 last_rewritten_event_ = new TestEvent(find->second.type); |
126 rewritten_event->reset(last_rewritten_event_); | 126 rewritten_event->reset(last_rewritten_event_); |
127 } else { | 127 } else { |
128 last_rewritten_event_ = 0; | 128 last_rewritten_event_ = 0; |
129 } | 129 } |
130 state_ = find->second.state; | 130 state_ = find->second.state; |
131 return find->second.status; | 131 return find->second.status; |
132 } | 132 } |
133 virtual EventRewriteStatus NextDispatchEvent(const Event& last_event, | 133 virtual EventRewriteStatus NextDispatchEvent(const Event& last_event, |
134 scoped_ptr<Event>* new_event) | 134 scoped_ptr<Event>* new_event) |
135 OVERRIDE { | 135 override { |
136 EXPECT_TRUE(last_rewritten_event_); | 136 EXPECT_TRUE(last_rewritten_event_); |
137 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event); | 137 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event); |
138 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id()); | 138 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id()); |
139 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get()); | 139 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get()); |
140 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id()); | 140 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id()); |
141 return RewriteEvent(last_event, new_event); | 141 return RewriteEvent(last_event, new_event); |
142 } | 142 } |
143 | 143 |
144 private: | 144 private: |
145 typedef std::pair<int, EventType> RewriteCase; | 145 typedef std::pair<int, EventType> RewriteCase; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 s.RemoveEventRewriter(&r3); | 222 s.RemoveEventRewriter(&r3); |
223 | 223 |
224 // Continue with the state-based rewriting. | 224 // Continue with the state-based rewriting. |
225 p.AddExpectedEvent(ET_MOUSE_RELEASED); | 225 p.AddExpectedEvent(ET_MOUSE_RELEASED); |
226 p.AddExpectedEvent(ET_KEY_RELEASED); | 226 p.AddExpectedEvent(ET_KEY_RELEASED); |
227 s.Send(ET_MOUSE_RELEASED); | 227 s.Send(ET_MOUSE_RELEASED); |
228 p.CheckAllReceived(); | 228 p.CheckAllReceived(); |
229 } | 229 } |
230 | 230 |
231 } // namespace ui | 231 } // namespace ui |
OLD | NEW |