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"
26 #include "base/message_loop/message_pump_x11.h"
31 // Trivial WindowDelegate implementation that draws a colored background.
32 class DemoWindowDelegate : public aura::WindowDelegate {
34 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
36 // Overridden from WindowDelegate:
37 virtual gfx::Size GetMinimumSize() const OVERRIDE {
41 virtual gfx::Size GetMaximumSize() const OVERRIDE {
45 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
46 const gfx::Rect& new_bounds) OVERRIDE {}
47 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
48 return gfx::kNullCursor;
50 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
53 virtual bool ShouldDescendIntoChildForEventHandling(
55 const gfx::Point& location) OVERRIDE {
58 virtual bool CanFocus() OVERRIDE { return true; }
59 virtual void OnCaptureLost() OVERRIDE {}
60 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
61 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
63 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
64 virtual void OnWindowDestroying() OVERRIDE {}
65 virtual void OnWindowDestroyed() OVERRIDE {}
66 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
67 virtual bool HasHitTestMask() const OVERRIDE { return false; }
68 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
69 virtual void DidRecreateLayer(ui::Layer* old_layer,
70 ui::Layer* new_layer) OVERRIDE {}
75 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
78 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
80 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
81 aura::client::SetWindowTreeClient(window_, this);
84 virtual ~DemoWindowTreeClient() {
85 aura::client::SetWindowTreeClient(window_, NULL);
88 // Overridden from aura::client::WindowTreeClient:
89 virtual aura::Window* GetDefaultParent(aura::Window* context,
91 const gfx::Rect& bounds) OVERRIDE {
92 if (!capture_client_) {
93 capture_client_.reset(
94 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
100 aura::Window* window_;
102 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
104 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
108 // Create the message-loop here before creating the root window.
109 base::MessageLoopForUI message_loop;
111 // The ContextFactory must exist before any Compositors are created.
112 bool allow_test_contexts = false;
113 ui::InitializeContextFactoryForTests(allow_test_contexts);
115 aura::Env::CreateInstance();
116 scoped_ptr<aura::TestScreen> test_screen(aura::TestScreen::Create());
117 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
118 scoped_ptr<aura::RootWindow> root_window(
119 test_screen->CreateRootWindowForPrimaryDisplay());
120 scoped_ptr<DemoWindowTreeClient> window_tree_client(new DemoWindowTreeClient(
121 root_window->window()));
122 aura::test::TestFocusClient focus_client;
123 aura::client::SetFocusClient(root_window->window(), &focus_client);
125 // Create a hierarchy of test windows.
126 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
127 aura::Window window1(&window_delegate1);
129 window1.Init(aura::WINDOW_LAYER_TEXTURED);
130 window1.SetBounds(gfx::Rect(100, 100, 400, 400));
132 aura::client::ParentWindowWithContext(
133 &window1, root_window->window(), gfx::Rect());
135 DemoWindowDelegate window_delegate2(SK_ColorRED);
136 aura::Window window2(&window_delegate2);
138 window2.Init(aura::WINDOW_LAYER_TEXTURED);
139 window2.SetBounds(gfx::Rect(200, 200, 350, 350));
141 aura::client::ParentWindowWithContext(
142 &window2, root_window->window(), gfx::Rect());
144 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
145 aura::Window window3(&window_delegate3);
147 window3.Init(aura::WINDOW_LAYER_TEXTURED);
148 window3.SetBounds(gfx::Rect(10, 10, 50, 50));
150 window2.AddChild(&window3);
152 root_window->host()->Show();
153 base::MessageLoopForUI::current()->Run();
160 int main(int argc, char** argv) {
161 CommandLine::Init(argc, argv);
163 // The exit manager is in charge of calling the dtors of singleton objects.
164 base::AtExitManager exit_manager;
166 base::i18n::InitializeICU();