1 // Copyright (c) 2012 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 "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/i18n/icu_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "third_party/skia/include/core/SkXfermode.h"
11 #include "ui/aura/client/default_capture_client.h"
12 #include "ui/aura/client/window_tree_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/root_window.h"
15 #include "ui/aura/test/test_focus_client.h"
16 #include "ui/aura/test/test_screen.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_delegate.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/base/ui_base_paths.h"
22 #include "ui/compositor/test/context_factories_for_test.h"
23 #include "ui/events/event.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/rect.h"
26
27 #if defined(USE_X11)
28 #include "base/message_loop/message_pump_x11.h"
29 #endif
30
31 namespace {
32
33 // Trivial WindowDelegate implementation that draws a colored background.
34 class DemoWindowDelegate : public aura::WindowDelegate {
35 public:
DemoWindowDelegate(SkColor color)36 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
37
38 // Overridden from WindowDelegate:
GetMinimumSize() const39 virtual gfx::Size GetMinimumSize() const OVERRIDE {
40 return gfx::Size();
41 }
42
GetMaximumSize() const43 virtual gfx::Size GetMaximumSize() const OVERRIDE {
44 return gfx::Size();
45 }
46
OnBoundsChanged(const gfx::Rect & old_bounds,const gfx::Rect & new_bounds)47 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
48 const gfx::Rect& new_bounds) OVERRIDE {}
GetCursor(const gfx::Point & point)49 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
50 return gfx::kNullCursor;
51 }
GetNonClientComponent(const gfx::Point & point) const52 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
53 return HTCAPTION;
54 }
ShouldDescendIntoChildForEventHandling(aura::Window * child,const gfx::Point & location)55 virtual bool ShouldDescendIntoChildForEventHandling(
56 aura::Window* child,
57 const gfx::Point& location) OVERRIDE {
58 return true;
59 }
CanFocus()60 virtual bool CanFocus() OVERRIDE { return true; }
OnCaptureLost()61 virtual void OnCaptureLost() OVERRIDE {}
OnPaint(gfx::Canvas * canvas)62 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
63 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
64 }
OnDeviceScaleFactorChanged(float device_scale_factor)65 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
OnWindowDestroying()66 virtual void OnWindowDestroying() OVERRIDE {}
OnWindowDestroyed()67 virtual void OnWindowDestroyed() OVERRIDE {}
OnWindowTargetVisibilityChanged(bool visible)68 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
HasHitTestMask() const69 virtual bool HasHitTestMask() const OVERRIDE { return false; }
GetHitTestMask(gfx::Path * mask) const70 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
DidRecreateLayer(ui::Layer * old_layer,ui::Layer * new_layer)71 virtual void DidRecreateLayer(ui::Layer* old_layer,
72 ui::Layer* new_layer) OVERRIDE {}
73
74 private:
75 SkColor color_;
76
77 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
78 };
79
80 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
81 public:
DemoWindowTreeClient(aura::Window * window)82 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
83 aura::client::SetWindowTreeClient(window_, this);
84 }
85
~DemoWindowTreeClient()86 virtual ~DemoWindowTreeClient() {
87 aura::client::SetWindowTreeClient(window_, NULL);
88 }
89
90 // Overridden from aura::client::WindowTreeClient:
GetDefaultParent(aura::Window * context,aura::Window * window,const gfx::Rect & bounds)91 virtual aura::Window* GetDefaultParent(aura::Window* context,
92 aura::Window* window,
93 const gfx::Rect& bounds) OVERRIDE {
94 if (!capture_client_) {
95 capture_client_.reset(
96 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
97 }
98 return window_;
99 }
100
101 private:
102 aura::Window* window_;
103
104 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
105
106 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
107 };
108
DemoMain()109 int DemoMain() {
110 // Create the message-loop here before creating the root window.
111 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI);
112
113 // The ContextFactory must exist before any Compositors are created.
114 bool allow_test_contexts = false;
115 ui::InitializeContextFactoryForTests(allow_test_contexts);
116
117 aura::Env::CreateInstance();
118 scoped_ptr<aura::TestScreen> test_screen(aura::TestScreen::Create());
119 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
120 scoped_ptr<aura::RootWindow> root_window(
121 test_screen->CreateRootWindowForPrimaryDisplay());
122 scoped_ptr<DemoWindowTreeClient> window_tree_client(new DemoWindowTreeClient(
123 root_window->window()));
124 aura::test::TestFocusClient focus_client;
125 aura::client::SetFocusClient(root_window->window(), &focus_client);
126
127 // Create a hierarchy of test windows.
128 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
129 aura::Window window1(&window_delegate1);
130 window1.set_id(1);
131 window1.Init(ui::LAYER_TEXTURED);
132 window1.SetBounds(gfx::Rect(100, 100, 400, 400));
133 window1.Show();
134 aura::client::ParentWindowWithContext(
135 &window1, root_window->window(), gfx::Rect());
136
137 DemoWindowDelegate window_delegate2(SK_ColorRED);
138 aura::Window window2(&window_delegate2);
139 window2.set_id(2);
140 window2.Init(ui::LAYER_TEXTURED);
141 window2.SetBounds(gfx::Rect(200, 200, 350, 350));
142 window2.Show();
143 aura::client::ParentWindowWithContext(
144 &window2, root_window->window(), gfx::Rect());
145
146 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
147 aura::Window window3(&window_delegate3);
148 window3.set_id(3);
149 window3.Init(ui::LAYER_TEXTURED);
150 window3.SetBounds(gfx::Rect(10, 10, 50, 50));
151 window3.Show();
152 window2.AddChild(&window3);
153
154 root_window->host()->Show();
155 base::MessageLoopForUI::current()->Run();
156
157 return 0;
158 }
159
160 } // namespace
161
main(int argc,char ** argv)162 int main(int argc, char** argv) {
163 CommandLine::Init(argc, argv);
164
165 // The exit manager is in charge of calling the dtors of singleton objects.
166 base::AtExitManager exit_manager;
167
168 ui::RegisterPathProvider();
169 base::i18n::InitializeICU();
170 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
171
172 return DemoMain();
173 }
174