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

Side by Side Diff: ui/views/view_targeter_unittest.cc

Issue 286933014: Introduce the MaskedViewTargeter class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 months 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 | Annotate | Revision Log
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/views/view_targeter.h" 5 #include "ui/views/view_targeter.h"
6 6
7 #include "ui/events/event_targeter.h" 7 #include "ui/events/event_targeter.h"
8 #include "ui/events/event_utils.h" 8 #include "ui/events/event_utils.h"
9 #include "ui/gfx/path.h"
10 #include "ui/views/masked_view_targeter.h"
9 #include "ui/views/test/views_test_base.h" 11 #include "ui/views/test/views_test_base.h"
10 #include "ui/views/widget/root_view.h" 12 #include "ui/views/widget/root_view.h"
11 13
12 namespace views { 14 namespace views {
15
16 // A class used to define a triangular-shaped hit test mask on a View.
17 class TestMaskedViewTargeter : public MaskedViewTargeter {
18 public:
19 explicit TestMaskedViewTargeter(View* masked_view)
20 : MaskedViewTargeter(masked_view) {
21 }
22 virtual ~TestMaskedViewTargeter() {}
23
24 private:
25 virtual bool GetHitTestMask(View* view, gfx::Path* mask) const OVERRIDE {
26 SkScalar w = SkIntToScalar(view->width());
27 SkScalar h = SkIntToScalar(view->height());
28
29 // Create a triangular mask within the bounds of |view|.
30 mask->moveTo(w / 2, 0);
31 mask->lineTo(w, h);
32 mask->lineTo(0, h);
33 mask->close();
34
35 return true;
36 }
37
38 DISALLOW_COPY_AND_ASSIGN(TestMaskedViewTargeter);
39 };
40
13 namespace test { 41 namespace test {
14 42
15 typedef ViewsTestBase ViewTargeterTest; 43 typedef ViewsTestBase ViewTargeterTest;
16 44
17 // Verifies that the the functions ViewTargeter::FindTargetForEvent() 45 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
18 // and ViewTargeter::FindNextBestTarget() are implemented correctly 46 // and ViewTargeter::FindNextBestTarget() are implemented correctly
19 // for key events. 47 // for key events.
20 TEST_F(ViewTargeterTest, ViewTargeterForKeyEvents) { 48 TEST_F(ViewTargeterTest, ViewTargeterForKeyEvents) {
21 Widget widget; 49 Widget widget;
22 Widget::InitParams init_params = 50 Widget::InitParams init_params =
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 event.set_location(gfx::Point(400, 400)); 211 event.set_location(gfx::Point(400, 400));
184 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v1, event)); 212 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v1, event));
185 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v2, event)); 213 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v2, event));
186 v1.ConvertEventToTarget(&v2, &event); 214 v1.ConvertEventToTarget(&v2, &event);
187 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v3, event)); 215 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v3, event));
188 216
189 // TODO(tdanderson): Move the hit-testing unit tests out of view_unittest 217 // TODO(tdanderson): Move the hit-testing unit tests out of view_unittest
190 // and into here. See crbug.com/355425. 218 // and into here. See crbug.com/355425.
191 } 219 }
192 220
221 // Tests that FindTargetForEvent() returns the correct target when some
222 // views in the view tree have a MaskedViewTargeter installed, i.e.,
223 // they have a custom-shaped hit test mask.
224 TEST_F(ViewTargeterTest, MaskedViewTargeter) {
225 Widget widget;
226 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
227 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
228 params.bounds = gfx::Rect(0, 0, 650, 650);
229 widget.Init(params);
230
231 ui::EventTargeter* targeter = new ViewTargeter();
232 internal::RootView* root_view =
233 static_cast<internal::RootView*>(widget.GetRootView());
234 root_view->SetEventTargeter(make_scoped_ptr(targeter));
235
236 // The coordinates used for SetBounds() are in the parent coordinate space.
237 View masked_view, unmasked_view, masked_child;
238 masked_view.SetBounds(0, 0, 200, 200);
239 unmasked_view.SetBounds(300, 0, 300, 300);
240 masked_child.SetBounds(0, 0, 100, 100);
241 root_view->AddChildView(&masked_view);
242 root_view->AddChildView(&unmasked_view);
243 unmasked_view.AddChildView(&masked_child);
244
245 // Install event targeters of type TestMaskedViewTargeter on the two masked
246 // views to define their hit test masks.
247 ui::EventTargeter* masked_targeter = new TestMaskedViewTargeter(&masked_view);
248 masked_view.SetEventTargeter(make_scoped_ptr(masked_targeter));
249 masked_targeter = new TestMaskedViewTargeter(&masked_child);
250 masked_child.SetEventTargeter(make_scoped_ptr(masked_targeter));
251
252 // Note that the coordinates used below are in the coordinate space of
253 // the root view.
254
255 // Event located within the hit test mask of |masked_view|.
256 ui::ScrollEvent scroll(ui::ET_SCROLL, gfx::Point(100, 190),
257 ui::EventTimeForNow(), 0, 0, 3, 0, 3, 2);
258 ui::EventTarget* current_target = targeter->FindTargetForEvent(root_view,
259 &scroll);
260 EXPECT_EQ(&masked_view, static_cast<View*>(current_target));
261
262 // Event located outside the hit test mask of |masked_view|.
263 scroll.set_location(gfx::Point(10, 10));
264 current_target = targeter->FindTargetForEvent(root_view, &scroll);
265 EXPECT_EQ(root_view, static_cast<View*>(current_target));
266
267 // Event located within the hit test mask of |masked_child|.
268 scroll.set_location(gfx::Point(350, 3));
269 current_target = targeter->FindTargetForEvent(root_view, &scroll);
270 EXPECT_EQ(&masked_child, static_cast<View*>(current_target));
271
272 // Event located within the hit test mask of |masked_child|.
273 scroll.set_location(gfx::Point(300, 12));
274 current_target = targeter->FindTargetForEvent(root_view, &scroll);
275 EXPECT_EQ(&unmasked_view, static_cast<View*>(current_target));
276
277 // TODO(tdanderson): We should also test that targeting of masked views
278 // works correctly with gestures. See crbug.com/375822.
279 }
280
193 } // namespace test 281 } // namespace test
194 } // namespace views 282 } // namespace views
OLDNEW
« ui/views/view_targeter.cc ('K') | « ui/views/view_targeter.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698