summaryrefslogtreecommitdiffstats
path: root/ui/views/view_model_unittest.cc
blob: fd4156e22c83db1e0bb564a9c40f6303857ac65c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/view_model.h"

#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/view.h"

namespace views {

namespace {

// Returns a string containing the x-coordinate of each of the views in |model|.
std::string BoundsString(const ViewModel& model) {
  std::string result;
  for (int i = 0; i < model.view_size(); ++i) {
    if (i != 0)
      result += " ";
    result += base::IntToString(model.ideal_bounds(i).x());
  }
  return result;
}

// Returns a string containing the id of each of the views in |model|.
std::string ViewIDsString(const ViewModel& model) {
  std::string result;
  for (int i = 0; i < model.view_size(); ++i) {
    if (i != 0)
      result += " ";
    result += base::IntToString(model.view_at(i)->id());
  }
  return result;
}

}  // namespace

TEST(ViewModel, BasicAssertions) {
  View v1;
  ViewModel model;
  model.Add(&v1, 0);
  EXPECT_EQ(1, model.view_size());
  EXPECT_EQ(&v1, model.view_at(0));
  gfx::Rect v1_bounds(1, 2, 3, 4);
  model.set_ideal_bounds(0, v1_bounds);
  EXPECT_EQ(v1_bounds, model.ideal_bounds(0));
  EXPECT_EQ(0, model.GetIndexOfView(&v1));
}

TEST(ViewModel, Move) {
  View v1, v2, v3;
  v1.set_id(0);
  v2.set_id(1);
  v3.set_id(2);
  ViewModel model;
  model.Add(&v1, 0);
  model.Add(&v2, 1);
  model.Add(&v3, 2);
  model.Move(0, 2);
  EXPECT_EQ("1 2 0", ViewIDsString(model));

  model.Move(2, 0);
  EXPECT_EQ("0 1 2", ViewIDsString(model));
}

TEST(ViewModel, MoveViewOnly) {
  View v1, v2, v3;
  v1.set_id(0);
  v2.set_id(1);
  v3.set_id(2);
  ViewModel model;
  model.Add(&v1, 0);
  model.Add(&v2, 1);
  model.Add(&v3, 2);
  model.set_ideal_bounds(0, gfx::Rect(10, 0, 1, 2));
  model.set_ideal_bounds(1, gfx::Rect(11, 0, 1, 2));
  model.set_ideal_bounds(2, gfx::Rect(12, 0, 1, 2));
  model.MoveViewOnly(0, 2);
  EXPECT_EQ("1 2 0", ViewIDsString(model));
  EXPECT_EQ("10 11 12", BoundsString(model));

  model.MoveViewOnly(2, 0);
  EXPECT_EQ("0 1 2", ViewIDsString(model));
  EXPECT_EQ("10 11 12", BoundsString(model));

  model.MoveViewOnly(0, 1);
  EXPECT_EQ("1 0 2", ViewIDsString(model));
  EXPECT_EQ("10 11 12", BoundsString(model));

  model.MoveViewOnly(1, 0);
  EXPECT_EQ("0 1 2", ViewIDsString(model));
  EXPECT_EQ("10 11 12", BoundsString(model));
}

}  // namespace views