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 "ui/aura/test/test_windows.h"
6 
7 #include <stddef.h>
8 
9 #include "base/strings/string_number_conversions.h"
10 #include "ui/aura/client/aura_constants.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/compositor/layer.h"
14 #include "ui/gfx/geometry/rect.h"
15 
16 namespace aura {
17 namespace test {
18 
CreateTestWindowWithId(int id,Window * parent)19 Window* CreateTestWindowWithId(int id, Window* parent) {
20   return CreateTestWindowWithDelegate(NULL, id, gfx::Rect(), parent);
21 }
22 
CreateTestWindowWithBounds(const gfx::Rect & bounds,Window * parent)23 Window* CreateTestWindowWithBounds(const gfx::Rect& bounds, Window* parent) {
24   return CreateTestWindowWithDelegate(NULL, 0, bounds, parent);
25 }
26 
CreateTestWindow(SkColor color,int id,const gfx::Rect & bounds,Window * parent)27 Window* CreateTestWindow(SkColor color,
28                          int id,
29                          const gfx::Rect& bounds,
30                          Window* parent) {
31   return CreateTestWindowWithDelegate(new ColorTestWindowDelegate(color), id,
32                                       bounds, parent);
33 }
34 
CreateTestWindowWithDelegate(WindowDelegate * delegate,int id,const gfx::Rect & bounds,Window * parent)35 Window* CreateTestWindowWithDelegate(WindowDelegate* delegate,
36                                      int id,
37                                      const gfx::Rect& bounds,
38                                      Window* parent) {
39   return CreateTestWindowWithDelegateAndType(
40       delegate, client::WINDOW_TYPE_NORMAL, id, bounds, parent, true);
41 }
42 
CreateTestWindowWithDelegateAndType(WindowDelegate * delegate,client::WindowType type,int id,const gfx::Rect & bounds,Window * parent,bool show_on_creation)43 Window* CreateTestWindowWithDelegateAndType(WindowDelegate* delegate,
44                                             client::WindowType type,
45                                             int id,
46                                             const gfx::Rect& bounds,
47                                             Window* parent,
48                                             bool show_on_creation) {
49   Window* window = new Window(delegate, type);
50   window->set_id(id);
51   window->Init(ui::LAYER_TEXTURED);
52   window->SetProperty(
53       client::kResizeBehaviorKey,
54       client::kResizeBehaviorCanResize | client::kResizeBehaviorCanMaximize);
55   window->SetBounds(bounds);
56   if (show_on_creation)
57     window->Show();
58   if (parent)
59     parent->AddChild(window);
60   return window;
61 }
62 
63 template <typename T>
ObjectIsAbove(T * upper,T * lower)64 bool ObjectIsAbove(T* upper, T* lower) {
65   DCHECK_EQ(upper->parent(), lower->parent());
66   DCHECK_NE(upper, lower);
67   const std::vector<T*>& children = upper->parent()->children();
68   const size_t upper_i =
69       std::find(children.begin(), children.end(), upper) - children.begin();
70   const size_t lower_i =
71       std::find(children.begin(), children.end(), lower) - children.begin();
72   return upper_i > lower_i;
73 }
74 
WindowIsAbove(Window * upper,Window * lower)75 bool WindowIsAbove(Window* upper, Window* lower) {
76   return ObjectIsAbove<Window>(upper, lower);
77 }
78 
LayerIsAbove(Window * upper,Window * lower)79 bool LayerIsAbove(Window* upper, Window* lower) {
80   return ObjectIsAbove<ui::Layer>(upper->layer(), lower->layer());
81 }
82 
ChildWindowIDsAsString(aura::Window * parent)83 std::string ChildWindowIDsAsString(aura::Window* parent) {
84   std::string result;
85   for (auto i = parent->children().begin(); i != parent->children().end();
86        ++i) {
87     if (!result.empty())
88       result += " ";
89     result += base::NumberToString((*i)->id());
90   }
91   return result;
92 }
93 
94 }  // namespace test
95 }  // namespace aura
96