OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "aura/desktop.h" | |
6 #include "aura/window_delegate.h" | |
7 #include "base/basictypes.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/message_loop.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "ui/gfx/canvas_skia.h" | |
12 | |
13 namespace aura { | |
14 namespace internal { | |
15 | |
16 namespace { | |
17 | |
18 // A simple WindowDelegate implementation for these tests. It owns itself | |
19 // (deletes itself when the Window it is attached to is destroyed). | |
20 class TestWindowDelegate : public WindowDelegate { | |
21 public: | |
22 TestWindowDelegate(SkColor color) : color_(color) {} | |
23 virtual ~TestWindowDelegate() {} | |
24 | |
25 // Overridden from WindowDelegate: | |
26 virtual bool OnMouseEvent(const MouseEvent& event) OVERRIDE { return false; } | |
27 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
28 canvas->AsCanvasSkia()->drawColor(color_, SkXfermode::kSrc_Mode); | |
29 } | |
30 virtual void OnWindowDestroyed() OVERRIDE { | |
31 delete this; | |
32 } | |
33 | |
34 private: | |
35 SkColor color_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(TestWindowDelegate); | |
38 }; | |
39 | |
40 class WindowTest : public testing::Test { | |
41 public: | |
42 WindowTest() { | |
43 aura::Desktop::GetInstance()->Show(); | |
44 aura::Desktop::GetInstance()->SetSize(gfx::Size(500, 500)); | |
45 } | |
46 virtual ~WindowTest() {} | |
47 | |
48 // Overridden from testing::Test: | |
49 virtual void SetUp() OVERRIDE { | |
50 } | |
51 | |
52 virtual void TearDown() OVERRIDE { | |
53 } | |
54 | |
55 Window* CreateTestWindow(SkColor color, | |
56 int id, | |
57 const gfx::Rect& bounds, | |
58 Window* parent) { | |
59 Window* window = new Window(new TestWindowDelegate(color)); | |
60 window->set_id(id); | |
61 window->Init(); | |
62 window->SetBounds(bounds, 0); | |
63 window->SetVisibility(Window::VISIBILITY_SHOWN); | |
64 window->SetParent(parent); | |
65 return window; | |
66 } | |
67 | |
68 void RunPendingMessages() { | |
69 MessageLoop main_message_loop(MessageLoop::TYPE_UI); | |
70 MessageLoopForUI::current()->Run(NULL); | |
71 } | |
72 | |
73 private: | |
74 DISALLOW_COPY_AND_ASSIGN(WindowTest); | |
75 }; | |
76 | |
77 } // namespace | |
78 | |
79 TEST_F(WindowTest, HitTest) { | |
80 Window w1(new TestWindowDelegate(SK_ColorWHITE)); | |
81 w1.set_id(1); | |
82 w1.Init(); | |
83 w1.SetBounds(gfx::Rect(10, 10, 50, 50), 0); | |
84 w1.SetVisibility(Window::VISIBILITY_SHOWN); | |
85 w1.SetParent(NULL); | |
86 | |
87 // Points are in the Window's coordinates. | |
88 EXPECT_TRUE(w1.HitTest(gfx::Point(1, 1))); | |
89 EXPECT_FALSE(w1.HitTest(gfx::Point(-1, -1))); | |
90 | |
91 // TODO(beng): clip Window to parent. | |
92 } | |
93 | |
94 TEST_F(WindowTest, GetEventHandlerForPoint) { | |
95 scoped_ptr<Window> w1( | |
96 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500), NULL)); | |
97 scoped_ptr<Window> w11( | |
98 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get())); | |
99 scoped_ptr<Window> w111( | |
100 CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get())); | |
101 scoped_ptr<Window> w1111( | |
102 CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get())); | |
103 scoped_ptr<Window> w12( | |
104 CreateTestWindow(SK_ColorMAGENTA, 12, gfx::Rect(10, 420, 25, 25), | |
105 w1.get())); | |
106 scoped_ptr<Window> w121( | |
107 CreateTestWindow(SK_ColorYELLOW, 121, gfx::Rect(5, 5, 5, 5), w12.get())); | |
108 scoped_ptr<Window> w13( | |
109 CreateTestWindow(SK_ColorGRAY, 13, gfx::Rect(5, 470, 50, 50), w1.get())); | |
110 | |
111 Window* desktop = Desktop::GetInstance()->window(); | |
112 EXPECT_EQ(desktop, desktop->GetEventHandlerForPoint(gfx::Point(5, 5))); | |
113 EXPECT_EQ(w1.get(), desktop->GetEventHandlerForPoint(gfx::Point(11, 11))); | |
114 EXPECT_EQ(w11.get(), desktop->GetEventHandlerForPoint(gfx::Point(16, 16))); | |
115 EXPECT_EQ(w111.get(), desktop->GetEventHandlerForPoint(gfx::Point(21, 21))); | |
116 EXPECT_EQ(w1111.get(), desktop->GetEventHandlerForPoint(gfx::Point(26, 26))); | |
117 EXPECT_EQ(w12.get(), desktop->GetEventHandlerForPoint(gfx::Point(21, 431))); | |
118 EXPECT_EQ(w121.get(), desktop->GetEventHandlerForPoint(gfx::Point(26, 436))); | |
119 EXPECT_EQ(w13.get(), desktop->GetEventHandlerForPoint(gfx::Point(26, 481))); | |
120 } | |
121 | |
122 } // namespace internal | |
123 } // namespace aura | |
124 | |
OLD | NEW |