| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/scoped_target_handler.h" | 5 #include "ui/events/scoped_target_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 140 |
| 141 // Tests that a ScopedTargetHandler invokes both the target and a delegate. | 141 // Tests that a ScopedTargetHandler invokes both the target and a delegate. |
| 142 TEST(ScopedTargetHandlerTest, HandlerInvoked) { | 142 TEST(ScopedTargetHandlerTest, HandlerInvoked) { |
| 143 int count = 0; | 143 int count = 0; |
| 144 TestEventTarget* target = new TestEventTarget; | 144 TestEventTarget* target = new TestEventTarget; |
| 145 std::unique_ptr<NestedEventHandler> target_handler( | 145 std::unique_ptr<NestedEventHandler> target_handler( |
| 146 new NestedEventHandler(target, 1)); | 146 new NestedEventHandler(target, 1)); |
| 147 std::unique_ptr<EventCountingEventHandler> delegate( | 147 std::unique_ptr<EventCountingEventHandler> delegate( |
| 148 new EventCountingEventHandler(target, &count)); | 148 new EventCountingEventHandler(target, &count)); |
| 149 target->SetHandler(std::move(target_handler), std::move(delegate)); | 149 target->SetHandler(std::move(target_handler), std::move(delegate)); |
| 150 MouseEvent event(ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 150 MouseEvent event( |
| 151 EventTimeForNow(), EF_LEFT_MOUSE_BUTTON, | 151 ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), EventTimeForNow(), |
| 152 EF_LEFT_MOUSE_BUTTON); | 152 EF_LEFT_MOUSE_BUTTON, EF_LEFT_MOUSE_BUTTON, |
| 153 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 153 target->DispatchEvent(&event); | 154 target->DispatchEvent(&event); |
| 154 EXPECT_EQ(1, count); | 155 EXPECT_EQ(1, count); |
| 155 delete target; | 156 delete target; |
| 156 } | 157 } |
| 157 | 158 |
| 158 // Tests that a ScopedTargetHandler invokes both the target and a delegate when | 159 // Tests that a ScopedTargetHandler invokes both the target and a delegate when |
| 159 // an Event is dispatched recursively such as with synthetic events. | 160 // an Event is dispatched recursively such as with synthetic events. |
| 160 TEST(ScopedTargetHandlerTest, HandlerInvokedNested) { | 161 TEST(ScopedTargetHandlerTest, HandlerInvokedNested) { |
| 161 int count = 0; | 162 int count = 0; |
| 162 TestEventTarget* target = new TestEventTarget; | 163 TestEventTarget* target = new TestEventTarget; |
| 163 std::unique_ptr<NestedEventHandler> target_handler( | 164 std::unique_ptr<NestedEventHandler> target_handler( |
| 164 new NestedEventHandler(target, 2)); | 165 new NestedEventHandler(target, 2)); |
| 165 std::unique_ptr<EventCountingEventHandler> delegate( | 166 std::unique_ptr<EventCountingEventHandler> delegate( |
| 166 new EventCountingEventHandler(target, &count)); | 167 new EventCountingEventHandler(target, &count)); |
| 167 target->SetHandler(std::move(target_handler), std::move(delegate)); | 168 target->SetHandler(std::move(target_handler), std::move(delegate)); |
| 168 MouseEvent event(ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 169 MouseEvent event( |
| 169 EventTimeForNow(), EF_LEFT_MOUSE_BUTTON, | 170 ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), EventTimeForNow(), |
| 170 EF_LEFT_MOUSE_BUTTON); | 171 EF_LEFT_MOUSE_BUTTON, EF_LEFT_MOUSE_BUTTON, |
| 172 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 171 target->DispatchEvent(&event); | 173 target->DispatchEvent(&event); |
| 172 EXPECT_EQ(2, count); | 174 EXPECT_EQ(2, count); |
| 173 delete target; | 175 delete target; |
| 174 } | 176 } |
| 175 | 177 |
| 176 // Tests that a it is safe to delete a ScopedTargetHandler while handling an | 178 // Tests that a it is safe to delete a ScopedTargetHandler while handling an |
| 177 // event. | 179 // event. |
| 178 TEST(ScopedTargetHandlerTest, SafeToDestroy) { | 180 TEST(ScopedTargetHandlerTest, SafeToDestroy) { |
| 179 int count = 0; | 181 int count = 0; |
| 180 TestEventTarget* target = new TestEventTarget; | 182 TestEventTarget* target = new TestEventTarget; |
| 181 std::unique_ptr<TargetDestroyingEventHandler> target_handler( | 183 std::unique_ptr<TargetDestroyingEventHandler> target_handler( |
| 182 new TargetDestroyingEventHandler(target, 1)); | 184 new TargetDestroyingEventHandler(target, 1)); |
| 183 std::unique_ptr<EventCountingEventHandler> delegate( | 185 std::unique_ptr<EventCountingEventHandler> delegate( |
| 184 new EventCountingEventHandler(target, &count)); | 186 new EventCountingEventHandler(target, &count)); |
| 185 target->SetHandler(std::move(target_handler), std::move(delegate)); | 187 target->SetHandler(std::move(target_handler), std::move(delegate)); |
| 186 MouseEvent event(ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 188 MouseEvent event( |
| 187 EventTimeForNow(), EF_LEFT_MOUSE_BUTTON, | 189 ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), EventTimeForNow(), |
| 188 EF_LEFT_MOUSE_BUTTON); | 190 EF_LEFT_MOUSE_BUTTON, EF_LEFT_MOUSE_BUTTON, |
| 191 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 189 target->DispatchEvent(&event); | 192 target->DispatchEvent(&event); |
| 190 EXPECT_EQ(0, count); | 193 EXPECT_EQ(0, count); |
| 191 } | 194 } |
| 192 | 195 |
| 193 // Tests that a it is safe to delete a ScopedTargetHandler while handling an | 196 // Tests that a it is safe to delete a ScopedTargetHandler while handling an |
| 194 // event recursively. | 197 // event recursively. |
| 195 TEST(ScopedTargetHandlerTest, SafeToDestroyNested) { | 198 TEST(ScopedTargetHandlerTest, SafeToDestroyNested) { |
| 196 int count = 0; | 199 int count = 0; |
| 197 TestEventTarget* target = new TestEventTarget; | 200 TestEventTarget* target = new TestEventTarget; |
| 198 std::unique_ptr<TargetDestroyingEventHandler> target_handler( | 201 std::unique_ptr<TargetDestroyingEventHandler> target_handler( |
| 199 new TargetDestroyingEventHandler(target, 2)); | 202 new TargetDestroyingEventHandler(target, 2)); |
| 200 std::unique_ptr<EventCountingEventHandler> delegate( | 203 std::unique_ptr<EventCountingEventHandler> delegate( |
| 201 new EventCountingEventHandler(target, &count)); | 204 new EventCountingEventHandler(target, &count)); |
| 202 target->SetHandler(std::move(target_handler), std::move(delegate)); | 205 target->SetHandler(std::move(target_handler), std::move(delegate)); |
| 203 MouseEvent event(ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 206 MouseEvent event( |
| 204 EventTimeForNow(), EF_LEFT_MOUSE_BUTTON, | 207 ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), EventTimeForNow(), |
| 205 EF_LEFT_MOUSE_BUTTON); | 208 EF_LEFT_MOUSE_BUTTON, EF_LEFT_MOUSE_BUTTON, |
| 209 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 206 target->DispatchEvent(&event); | 210 target->DispatchEvent(&event); |
| 207 EXPECT_EQ(0, count); | 211 EXPECT_EQ(0, count); |
| 208 } | 212 } |
| 209 | 213 |
| 210 } // namespace ui | 214 } // namespace ui |
| OLD | NEW |