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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 DISALLOW_COPY_AND_ASSIGN(TestEventRewriteProcessor); | 58 DISALLOW_COPY_AND_ASSIGN(TestEventRewriteProcessor); |
59 }; | 59 }; |
60 | 60 |
61 // Trivial EventSource that does nothing but send events. | 61 // Trivial EventSource that does nothing but send events. |
62 class TestEventRewriteSource : public EventSource { | 62 class TestEventRewriteSource : public EventSource { |
63 public: | 63 public: |
64 explicit TestEventRewriteSource(EventProcessor* processor) | 64 explicit TestEventRewriteSource(EventProcessor* processor) |
65 : processor_(processor) {} | 65 : processor_(processor) {} |
66 EventProcessor* GetEventProcessor() override { return processor_; } | 66 EventProcessor* GetEventProcessor() override { return processor_; } |
67 void Send(EventType type) { | 67 void Send(EventType type) { |
68 scoped_ptr<Event> event(new TestEvent(type)); | 68 std::unique_ptr<Event> event(new TestEvent(type)); |
69 (void)SendEventToProcessor(event.get()); | 69 (void)SendEventToProcessor(event.get()); |
70 } | 70 } |
71 | 71 |
72 private: | 72 private: |
73 EventProcessor* processor_; | 73 EventProcessor* processor_; |
74 }; | 74 }; |
75 | 75 |
76 // This EventRewriter always returns the same status, and if rewriting, the | 76 // This EventRewriter always returns the same status, and if rewriting, the |
77 // same event type; it is used to test simple rewriting, and rewriter addition, | 77 // same event type; it is used to test simple rewriting, and rewriter addition, |
78 // removal, and sequencing. Consequently EVENT_REWRITE_DISPATCH_ANOTHER is not | 78 // removal, and sequencing. Consequently EVENT_REWRITE_DISPATCH_ANOTHER is not |
79 // supported here (calls to NextDispatchEvent() would continue indefinitely). | 79 // supported here (calls to NextDispatchEvent() would continue indefinitely). |
80 class TestConstantEventRewriter : public EventRewriter { | 80 class TestConstantEventRewriter : public EventRewriter { |
81 public: | 81 public: |
82 TestConstantEventRewriter(EventRewriteStatus status, EventType type) | 82 TestConstantEventRewriter(EventRewriteStatus status, EventType type) |
83 : status_(status), type_(type) { | 83 : status_(status), type_(type) { |
84 CHECK_NE(EVENT_REWRITE_DISPATCH_ANOTHER, status); | 84 CHECK_NE(EVENT_REWRITE_DISPATCH_ANOTHER, status); |
85 } | 85 } |
86 | 86 |
87 EventRewriteStatus RewriteEvent(const Event& event, | 87 EventRewriteStatus RewriteEvent( |
88 scoped_ptr<Event>* rewritten_event) override { | 88 const Event& event, |
| 89 std::unique_ptr<Event>* rewritten_event) override { |
89 if (status_ == EVENT_REWRITE_REWRITTEN) | 90 if (status_ == EVENT_REWRITE_REWRITTEN) |
90 rewritten_event->reset(new TestEvent(type_)); | 91 rewritten_event->reset(new TestEvent(type_)); |
91 return status_; | 92 return status_; |
92 } | 93 } |
93 EventRewriteStatus NextDispatchEvent(const Event& last_event, | 94 EventRewriteStatus NextDispatchEvent( |
94 scoped_ptr<Event>* new_event) override { | 95 const Event& last_event, |
| 96 std::unique_ptr<Event>* new_event) override { |
95 NOTREACHED(); | 97 NOTREACHED(); |
96 return status_; | 98 return status_; |
97 } | 99 } |
98 | 100 |
99 private: | 101 private: |
100 EventRewriteStatus status_; | 102 EventRewriteStatus status_; |
101 EventType type_; | 103 EventType type_; |
102 }; | 104 }; |
103 | 105 |
104 // This EventRewriter runs a simple state machine; it is used to test | 106 // This EventRewriter runs a simple state machine; it is used to test |
105 // EVENT_REWRITE_DISPATCH_ANOTHER. | 107 // EVENT_REWRITE_DISPATCH_ANOTHER. |
106 class TestStateMachineEventRewriter : public EventRewriter { | 108 class TestStateMachineEventRewriter : public EventRewriter { |
107 public: | 109 public: |
108 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {} | 110 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {} |
109 void AddRule(int from_state, EventType from_type, | 111 void AddRule(int from_state, EventType from_type, |
110 int to_state, EventType to_type, EventRewriteStatus to_status) { | 112 int to_state, EventType to_type, EventRewriteStatus to_status) { |
111 RewriteResult r = {to_state, to_type, to_status}; | 113 RewriteResult r = {to_state, to_type, to_status}; |
112 rules_.insert(std::pair<RewriteCase, RewriteResult>( | 114 rules_.insert(std::pair<RewriteCase, RewriteResult>( |
113 RewriteCase(from_state, from_type), r)); | 115 RewriteCase(from_state, from_type), r)); |
114 } | 116 } |
115 EventRewriteStatus RewriteEvent(const Event& event, | 117 EventRewriteStatus RewriteEvent( |
116 scoped_ptr<Event>* rewritten_event) override { | 118 const Event& event, |
| 119 std::unique_ptr<Event>* rewritten_event) override { |
117 RewriteRules::iterator find = | 120 RewriteRules::iterator find = |
118 rules_.find(RewriteCase(state_, event.type())); | 121 rules_.find(RewriteCase(state_, event.type())); |
119 if (find == rules_.end()) | 122 if (find == rules_.end()) |
120 return EVENT_REWRITE_CONTINUE; | 123 return EVENT_REWRITE_CONTINUE; |
121 if ((find->second.status == EVENT_REWRITE_REWRITTEN) || | 124 if ((find->second.status == EVENT_REWRITE_REWRITTEN) || |
122 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) { | 125 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) { |
123 last_rewritten_event_ = new TestEvent(find->second.type); | 126 last_rewritten_event_ = new TestEvent(find->second.type); |
124 rewritten_event->reset(last_rewritten_event_); | 127 rewritten_event->reset(last_rewritten_event_); |
125 } else { | 128 } else { |
126 last_rewritten_event_ = 0; | 129 last_rewritten_event_ = 0; |
127 } | 130 } |
128 state_ = find->second.state; | 131 state_ = find->second.state; |
129 return find->second.status; | 132 return find->second.status; |
130 } | 133 } |
131 EventRewriteStatus NextDispatchEvent(const Event& last_event, | 134 EventRewriteStatus NextDispatchEvent( |
132 scoped_ptr<Event>* new_event) override { | 135 const Event& last_event, |
| 136 std::unique_ptr<Event>* new_event) override { |
133 EXPECT_TRUE(last_rewritten_event_); | 137 EXPECT_TRUE(last_rewritten_event_); |
134 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event); | 138 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event); |
135 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id()); | 139 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id()); |
136 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get()); | 140 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get()); |
137 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id()); | 141 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id()); |
138 return RewriteEvent(last_event, new_event); | 142 return RewriteEvent(last_event, new_event); |
139 } | 143 } |
140 | 144 |
141 private: | 145 private: |
142 typedef std::pair<int, EventType> RewriteCase; | 146 typedef std::pair<int, EventType> RewriteCase; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 s.RemoveEventRewriter(&r3); | 223 s.RemoveEventRewriter(&r3); |
220 | 224 |
221 // Continue with the state-based rewriting. | 225 // Continue with the state-based rewriting. |
222 p.AddExpectedEvent(ET_MOUSE_RELEASED); | 226 p.AddExpectedEvent(ET_MOUSE_RELEASED); |
223 p.AddExpectedEvent(ET_KEY_RELEASED); | 227 p.AddExpectedEvent(ET_KEY_RELEASED); |
224 s.Send(ET_MOUSE_RELEASED); | 228 s.Send(ET_MOUSE_RELEASED); |
225 p.CheckAllReceived(); | 229 p.CheckAllReceived(); |
226 } | 230 } |
227 | 231 |
228 } // namespace ui | 232 } // namespace ui |
OLD | NEW |