Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: ui/events/event_rewriter_unittest.cc

Issue 683553002: Standardize usage of virtual/override/final specifiers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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) override {
88 override {
89 if (status_ == EVENT_REWRITE_REWRITTEN) 88 if (status_ == EVENT_REWRITE_REWRITTEN)
90 rewritten_event->reset(new TestEvent(type_)); 89 rewritten_event->reset(new TestEvent(type_));
91 return status_; 90 return status_;
92 } 91 }
93 virtual EventRewriteStatus NextDispatchEvent(const Event& last_event, 92 EventRewriteStatus NextDispatchEvent(const Event& last_event,
94 scoped_ptr<Event>* new_event) 93 scoped_ptr<Event>* new_event) override {
95 override {
96 NOTREACHED(); 94 NOTREACHED();
97 return status_; 95 return status_;
98 } 96 }
99 97
100 private: 98 private:
101 EventRewriteStatus status_; 99 EventRewriteStatus status_;
102 EventType type_; 100 EventType type_;
103 }; 101 };
104 102
105 // This EventRewriter runs a simple state machine; it is used to test 103 // This EventRewriter runs a simple state machine; it is used to test
106 // EVENT_REWRITE_DISPATCH_ANOTHER. 104 // EVENT_REWRITE_DISPATCH_ANOTHER.
107 class TestStateMachineEventRewriter : public EventRewriter { 105 class TestStateMachineEventRewriter : public EventRewriter {
108 public: 106 public:
109 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {} 107 TestStateMachineEventRewriter() : last_rewritten_event_(0), state_(0) {}
110 void AddRule(int from_state, EventType from_type, 108 void AddRule(int from_state, EventType from_type,
111 int to_state, EventType to_type, EventRewriteStatus to_status) { 109 int to_state, EventType to_type, EventRewriteStatus to_status) {
112 RewriteResult r = {to_state, to_type, to_status}; 110 RewriteResult r = {to_state, to_type, to_status};
113 rules_.insert(std::pair<RewriteCase, RewriteResult>( 111 rules_.insert(std::pair<RewriteCase, RewriteResult>(
114 RewriteCase(from_state, from_type), r)); 112 RewriteCase(from_state, from_type), r));
115 } 113 }
116 virtual EventRewriteStatus RewriteEvent(const Event& event, 114 EventRewriteStatus RewriteEvent(const Event& event,
117 scoped_ptr<Event>* rewritten_event) 115 scoped_ptr<Event>* rewritten_event) override {
118 override {
119 RewriteRules::iterator find = 116 RewriteRules::iterator find =
120 rules_.find(RewriteCase(state_, event.type())); 117 rules_.find(RewriteCase(state_, event.type()));
121 if (find == rules_.end()) 118 if (find == rules_.end())
122 return EVENT_REWRITE_CONTINUE; 119 return EVENT_REWRITE_CONTINUE;
123 if ((find->second.status == EVENT_REWRITE_REWRITTEN) || 120 if ((find->second.status == EVENT_REWRITE_REWRITTEN) ||
124 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) { 121 (find->second.status == EVENT_REWRITE_DISPATCH_ANOTHER)) {
125 last_rewritten_event_ = new TestEvent(find->second.type); 122 last_rewritten_event_ = new TestEvent(find->second.type);
126 rewritten_event->reset(last_rewritten_event_); 123 rewritten_event->reset(last_rewritten_event_);
127 } else { 124 } else {
128 last_rewritten_event_ = 0; 125 last_rewritten_event_ = 0;
129 } 126 }
130 state_ = find->second.state; 127 state_ = find->second.state;
131 return find->second.status; 128 return find->second.status;
132 } 129 }
133 virtual EventRewriteStatus NextDispatchEvent(const Event& last_event, 130 EventRewriteStatus NextDispatchEvent(const Event& last_event,
134 scoped_ptr<Event>* new_event) 131 scoped_ptr<Event>* new_event) override {
135 override {
136 EXPECT_TRUE(last_rewritten_event_); 132 EXPECT_TRUE(last_rewritten_event_);
137 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event); 133 const TestEvent* arg_last = static_cast<const TestEvent*>(&last_event);
138 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id()); 134 EXPECT_EQ(last_rewritten_event_->unique_id(), arg_last->unique_id());
139 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get()); 135 const TestEvent* arg_new = static_cast<const TestEvent*>(new_event->get());
140 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id()); 136 EXPECT_FALSE(arg_new && arg_last->unique_id() == arg_new->unique_id());
141 return RewriteEvent(last_event, new_event); 137 return RewriteEvent(last_event, new_event);
142 } 138 }
143 139
144 private: 140 private:
145 typedef std::pair<int, EventType> RewriteCase; 141 typedef std::pair<int, EventType> RewriteCase;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 s.RemoveEventRewriter(&r3); 218 s.RemoveEventRewriter(&r3);
223 219
224 // Continue with the state-based rewriting. 220 // Continue with the state-based rewriting.
225 p.AddExpectedEvent(ET_MOUSE_RELEASED); 221 p.AddExpectedEvent(ET_MOUSE_RELEASED);
226 p.AddExpectedEvent(ET_KEY_RELEASED); 222 p.AddExpectedEvent(ET_KEY_RELEASED);
227 s.Send(ET_MOUSE_RELEASED); 223 s.Send(ET_MOUSE_RELEASED);
228 p.CheckAllReceived(); 224 p.CheckAllReceived();
229 } 225 }
230 226
231 } // namespace ui 227 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/event_processor_unittest.cc ('k') | ui/events/gesture_detection/gesture_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698