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