1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ui/events/test/test_event_target.h" 6 7 #include "ui/events/event.h" 8 #include "ui/events/event_target_iterator.h" 9 #include "ui/events/event_targeter.h" 10 11 namespace ui { 12 namespace test { 13 14 TestEventTarget::TestEventTarget() 15 : parent_(NULL) {} 16 TestEventTarget::~TestEventTarget() {} 17 18 void TestEventTarget::AddChild(scoped_ptr<TestEventTarget> child) { 19 TestEventTarget* child_r = child.get(); 20 if (child->parent()) { 21 AddChild(child->parent()->RemoveChild(child.release())); 22 } else { 23 children_.push_back(child.release()); 24 } 25 child_r->set_parent(this); 26 } 27 28 scoped_ptr<TestEventTarget> TestEventTarget::RemoveChild(TestEventTarget *c) { 29 ScopedVector<TestEventTarget>::iterator iter = std::find(children_.begin(), 30 children_.end(), 31 c); 32 if (iter != children_.end()) { 33 children_.weak_erase(iter); 34 c->set_parent(NULL); 35 return scoped_ptr<TestEventTarget>(c); 36 } 37 return scoped_ptr<TestEventTarget>(); 38 } 39 40 void TestEventTarget::SetEventTargeter(scoped_ptr<EventTargeter> targeter) { 41 targeter_ = targeter.Pass(); 42 } 43 44 bool TestEventTarget::DidReceiveEvent(ui::EventType type) const { 45 return received_.count(type) > 0; 46 } 47 48 void TestEventTarget::ResetReceivedEvents() { 49 received_.clear(); 50 } 51 52 //////////////////////////////////////////////////////////////////////////////// 53 // TestEventTarget, protected 54 55 bool TestEventTarget::CanAcceptEvent(const ui::Event& event) { 56 return true; 57 } 58 59 EventTarget* TestEventTarget::GetParentTarget() { 60 return parent_; 61 } 62 63 scoped_ptr<EventTargetIterator> TestEventTarget::GetChildIterator() const { 64 return scoped_ptr<EventTargetIterator>( 65 new EventTargetIteratorImpl<TestEventTarget>(children_.get())); 66 } 67 68 EventTargeter* TestEventTarget::GetEventTargeter() { 69 return targeter_.get(); 70 } 71 72 void TestEventTarget::OnEvent(Event* event) { 73 received_.insert(event->type()); 74 EventTarget::OnEvent(event); 75 } 76 77 //////////////////////////////////////////////////////////////////////////////// 78 // TestEventTarget, private 79 80 bool TestEventTarget::Contains(TestEventTarget* target) const { 81 while (target && target != this) 82 target = target->parent(); 83 return target == this; 84 } 85 86 } // namespace test 87 } // namespace ui 88