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.
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/compositor/test/context_factories_for_test.h"
21 #include "ui/events/event.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/rect.h"
24 #include "ui/gl/gl_surface.h"
27 #include "base/message_loop/message_pump_x11.h"
32 // Trivial WindowDelegate implementation that draws a colored background.
33 class DemoWindowDelegate
: public aura::WindowDelegate
{
35 explicit DemoWindowDelegate(SkColor color
) : color_(color
) {}
37 // Overridden from WindowDelegate:
38 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
42 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{
46 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
47 const gfx::Rect
& new_bounds
) OVERRIDE
{}
48 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
49 return gfx::kNullCursor
;
51 virtual int GetNonClientComponent(const gfx::Point
& point
) const OVERRIDE
{
54 virtual bool ShouldDescendIntoChildForEventHandling(
56 const gfx::Point
& location
) OVERRIDE
{
59 virtual bool CanFocus() OVERRIDE
{ return true; }
60 virtual void OnCaptureLost() OVERRIDE
{}
61 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
62 canvas
->DrawColor(color_
, SkXfermode::kSrc_Mode
);
64 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
{}
65 virtual void OnWindowDestroying() OVERRIDE
{}
66 virtual void OnWindowDestroyed() OVERRIDE
{}
67 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{}
68 virtual bool HasHitTestMask() const OVERRIDE
{ return false; }
69 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
70 virtual void DidRecreateLayer(ui::Layer
* old_layer
,
71 ui::Layer
* new_layer
) OVERRIDE
{}
76 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate
);
79 class DemoWindowTreeClient
: public aura::client::WindowTreeClient
{
81 explicit DemoWindowTreeClient(aura::Window
* window
) : window_(window
) {
82 aura::client::SetWindowTreeClient(window_
, this);
85 virtual ~DemoWindowTreeClient() {
86 aura::client::SetWindowTreeClient(window_
, NULL
);
89 // Overridden from aura::client::WindowTreeClient:
90 virtual aura::Window
* GetDefaultParent(aura::Window
* context
,
92 const gfx::Rect
& bounds
) OVERRIDE
{
93 if (!capture_client_
) {
94 capture_client_
.reset(
95 new aura::client::DefaultCaptureClient(window_
->GetRootWindow()));
101 aura::Window
* window_
;
103 scoped_ptr
<aura::client::DefaultCaptureClient
> capture_client_
;
105 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient
);
109 // Create the message-loop here before creating the root window.
110 base::MessageLoopForUI message_loop
;
112 gfx::GLSurface::InitializeOneOff();
114 // The ContextFactory must exist before any Compositors are created.
115 bool allow_test_contexts
= false;
116 ui::InitializeContextFactoryForTests(allow_test_contexts
);
118 aura::Env::CreateInstance();
119 scoped_ptr
<aura::TestScreen
> test_screen(aura::TestScreen::Create());
120 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, test_screen
.get());
121 scoped_ptr
<aura::RootWindow
> root_window(
122 test_screen
->CreateRootWindowForPrimaryDisplay());
123 scoped_ptr
<DemoWindowTreeClient
> window_tree_client(new DemoWindowTreeClient(
124 root_window
->window()));
125 aura::test::TestFocusClient focus_client
;
126 aura::client::SetFocusClient(root_window
->window(), &focus_client
);
128 // Create a hierarchy of test windows.
129 DemoWindowDelegate
window_delegate1(SK_ColorBLUE
);
130 aura::Window
window1(&window_delegate1
);
132 window1
.Init(aura::WINDOW_LAYER_TEXTURED
);
133 window1
.SetBounds(gfx::Rect(100, 100, 400, 400));
135 aura::client::ParentWindowWithContext(
136 &window1
, root_window
->window(), gfx::Rect());
138 DemoWindowDelegate
window_delegate2(SK_ColorRED
);
139 aura::Window
window2(&window_delegate2
);
141 window2
.Init(aura::WINDOW_LAYER_TEXTURED
);
142 window2
.SetBounds(gfx::Rect(200, 200, 350, 350));
144 aura::client::ParentWindowWithContext(
145 &window2
, root_window
->window(), gfx::Rect());
147 DemoWindowDelegate
window_delegate3(SK_ColorGREEN
);
148 aura::Window
window3(&window_delegate3
);
150 window3
.Init(aura::WINDOW_LAYER_TEXTURED
);
151 window3
.SetBounds(gfx::Rect(10, 10, 50, 50));
153 window2
.AddChild(&window3
);
155 root_window
->host()->Show();
156 base::MessageLoopForUI::current()->Run();
163 int main(int argc
, char** argv
) {
164 CommandLine::Init(argc
, argv
);
166 // The exit manager is in charge of calling the dtors of singleton objects.
167 base::AtExitManager exit_manager
;
169 base::i18n::InitializeICU();