view_model_unittest.cc 2.5 KB

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