Home | History | Annotate | Download | only in views
      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/views/view_model.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 #include "ui/views/view.h"
     10 
     11 namespace views {
     12 
     13 namespace {
     14 
     15 // Returns a string containing the x-coordinate of each of the views in |model|.
     16 std::string BoundsString(const ViewModel& model) {
     17   std::string result;
     18   for (int i = 0; i < model.view_size(); ++i) {
     19     if (i != 0)
     20       result += " ";
     21     result += base::IntToString(model.ideal_bounds(i).x());
     22   }
     23   return result;
     24 }
     25 
     26 // Returns a string containing the id of each of the views in |model|.
     27 std::string ViewIDsString(const ViewModel& model) {
     28   std::string result;
     29   for (int i = 0; i < model.view_size(); ++i) {
     30     if (i != 0)
     31       result += " ";
     32     result += base::IntToString(model.view_at(i)->id());
     33   }
     34   return result;
     35 }
     36 
     37 }  // namespace
     38 
     39 TEST(ViewModel, BasicAssertions) {
     40   View v1;
     41   ViewModel model;
     42   model.Add(&v1, 0);
     43   EXPECT_EQ(1, model.view_size());
     44   EXPECT_EQ(&v1, model.view_at(0));
     45   gfx::Rect v1_bounds(1, 2, 3, 4);
     46   model.set_ideal_bounds(0, v1_bounds);
     47   EXPECT_EQ(v1_bounds, model.ideal_bounds(0));
     48   EXPECT_EQ(0, model.GetIndexOfView(&v1));
     49 }
     50 
     51 TEST(ViewModel, Move) {
     52   View v1, v2, v3;
     53   v1.set_id(0);
     54   v2.set_id(1);
     55   v3.set_id(2);
     56   ViewModel model;
     57   model.Add(&v1, 0);
     58   model.Add(&v2, 1);
     59   model.Add(&v3, 2);
     60   model.Move(0, 2);
     61   EXPECT_EQ("1 2 0", ViewIDsString(model));
     62 
     63   model.Move(2, 0);
     64   EXPECT_EQ("0 1 2", ViewIDsString(model));
     65 }
     66 
     67 TEST(ViewModel, MoveViewOnly) {
     68   View v1, v2, v3;
     69   v1.set_id(0);
     70   v2.set_id(1);
     71   v3.set_id(2);
     72   ViewModel model;
     73   model.Add(&v1, 0);
     74   model.Add(&v2, 1);
     75   model.Add(&v3, 2);
     76   model.set_ideal_bounds(0, gfx::Rect(10, 0, 1, 2));
     77   model.set_ideal_bounds(1, gfx::Rect(11, 0, 1, 2));
     78   model.set_ideal_bounds(2, gfx::Rect(12, 0, 1, 2));
     79   model.MoveViewOnly(0, 2);
     80   EXPECT_EQ("1 2 0", ViewIDsString(model));
     81   EXPECT_EQ("10 11 12", BoundsString(model));
     82 
     83   model.MoveViewOnly(2, 0);
     84   EXPECT_EQ("0 1 2", ViewIDsString(model));
     85   EXPECT_EQ("10 11 12", BoundsString(model));
     86 
     87   model.MoveViewOnly(0, 1);
     88   EXPECT_EQ("1 0 2", ViewIDsString(model));
     89   EXPECT_EQ("10 11 12", BoundsString(model));
     90 
     91   model.MoveViewOnly(1, 0);
     92   EXPECT_EQ("0 1 2", ViewIDsString(model));
     93   EXPECT_EQ("10 11 12", BoundsString(model));
     94 }
     95 
     96 }  // namespace views
     97