Home | History | Annotate | Download | only in test
      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/strings/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 
     13 namespace aura {
     14 namespace test {
     15 
     16 Window* CreateTestWindowWithId(int id, Window* parent) {
     17   return CreateTestWindowWithDelegate(NULL, id, gfx::Rect(), parent);
     18 }
     19 
     20 Window* CreateTestWindowWithBounds(const gfx::Rect& bounds, Window* parent) {
     21   return CreateTestWindowWithDelegate(NULL, 0, bounds, parent);
     22 }
     23 
     24 Window* 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 
     32 Window* CreateTestWindowWithDelegate(WindowDelegate* delegate,
     33                                      int id,
     34                                      const gfx::Rect& bounds,
     35                                      Window* parent) {
     36   return CreateTestWindowWithDelegateAndType(
     37       delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds, parent);
     38 }
     39 
     40 Window* CreateTestWindowWithDelegateAndType(WindowDelegate* delegate,
     41                                             ui::wm::WindowType type,
     42                                             int id,
     43                                             const gfx::Rect& bounds,
     44                                             Window* parent) {
     45   Window* window = new Window(delegate);
     46   window->set_id(id);
     47   window->SetType(type);
     48   window->Init(aura::WINDOW_LAYER_TEXTURED);
     49   window->SetBounds(bounds);
     50   window->Show();
     51   if (parent)
     52     parent->AddChild(window);
     53   window->SetProperty(aura::client::kCanMaximizeKey, true);
     54   return window;
     55 }
     56 
     57 template <typename T>
     58 bool ObjectIsAbove(T* upper, T* lower) {
     59   DCHECK_EQ(upper->parent(), lower->parent());
     60   DCHECK_NE(upper, lower);
     61   const std::vector<T*>& children = upper->parent()->children();
     62   const size_t upper_i =
     63       std::find(children.begin(), children.end(), upper) - children.begin();
     64   const size_t lower_i =
     65       std::find(children.begin(), children.end(), lower) - children.begin();
     66   return upper_i > lower_i;
     67 }
     68 
     69 bool WindowIsAbove(Window* upper, Window* lower) {
     70   return ObjectIsAbove<Window>(upper, lower);
     71 }
     72 
     73 bool LayerIsAbove(Window* upper, Window* lower) {
     74   return ObjectIsAbove<ui::Layer>(upper->layer(), lower->layer());
     75 }
     76 
     77 std::string ChildWindowIDsAsString(aura::Window* parent) {
     78   std::string result;
     79   for (Window::Windows::const_iterator i = parent->children().begin();
     80        i != parent->children().end(); ++i) {
     81     if (!result.empty())
     82       result += " ";
     83     result += base::IntToString((*i)->id());
     84   }
     85   return result;
     86 }
     87 
     88 }  // namespace test
     89 }  // namespace aura
     90