| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/models/list_model.h" | 5 #include "ui/base/models/list_model.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_vector.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace ui { | 13 namespace ui { |
| 13 | 14 |
| 14 class FooItem { | 15 class FooItem { |
| 15 public: | 16 public: |
| 16 explicit FooItem(int id) : id_(id) {} | 17 explicit FooItem(int id) : id_(id) {} |
| 17 | 18 |
| 18 int id() const { return id_; } | 19 int id() const { return id_; } |
| 19 | 20 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 88 |
| 88 // Total 3 items in model. | 89 // Total 3 items in model. |
| 89 EXPECT_EQ(3U, model.item_count()); | 90 EXPECT_EQ(3U, model.item_count()); |
| 90 | 91 |
| 91 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) | 92 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) |
| 92 EXPECT_EQ(2, model.GetItemAt(0)->id()); | 93 EXPECT_EQ(2, model.GetItemAt(0)->id()); |
| 93 EXPECT_EQ(0, model.GetItemAt(1)->id()); | 94 EXPECT_EQ(0, model.GetItemAt(1)->id()); |
| 94 EXPECT_EQ(1, model.GetItemAt(2)->id()); | 95 EXPECT_EQ(1, model.GetItemAt(2)->id()); |
| 95 } | 96 } |
| 96 | 97 |
| 98 TEST_F(ListModelTest, AddAll) { |
| 99 ListModel<FooItem> model; |
| 100 model.AddObserver(this); |
| 101 |
| 102 // Append FooItem(0) |
| 103 model.Add(new FooItem(0)); |
| 104 ExpectCountsEqual(1, 0, 0, 0); |
| 105 |
| 106 ScopedVector<FooItem> items; |
| 107 items.push_back(new FooItem(1)); |
| 108 items.push_back(new FooItem(2)); |
| 109 |
| 110 // Append FooItem(1), FooItem(2) |
| 111 model.AddAll(items.Pass()); |
| 112 ExpectCountsEqual(3, 0, 0, 0); |
| 113 |
| 114 // Onwership of FooItems transferred. |
| 115 EXPECT_TRUE(items.empty()); |
| 116 |
| 117 // Total 3 items in model. |
| 118 EXPECT_EQ(3U, model.item_count()); |
| 119 |
| 120 // First one should be FooItem(0), followed by FooItem(1) and FooItem(2) |
| 121 EXPECT_EQ(0, model.GetItemAt(0)->id()); |
| 122 EXPECT_EQ(1, model.GetItemAt(1)->id()); |
| 123 EXPECT_EQ(2, model.GetItemAt(2)->id()); |
| 124 } |
| 125 |
| 97 TEST_F(ListModelTest, Remove) { | 126 TEST_F(ListModelTest, Remove) { |
| 98 ListModel<FooItem> model; | 127 ListModel<FooItem> model; |
| 99 model.AddObserver(this); | 128 model.AddObserver(this); |
| 100 | 129 |
| 101 model.Add(new FooItem(0)); | 130 model.Add(new FooItem(0)); |
| 102 model.Add(new FooItem(1)); | 131 model.Add(new FooItem(1)); |
| 103 model.Add(new FooItem(2)); | 132 model.Add(new FooItem(2)); |
| 104 | 133 |
| 105 ClearCounts(); | 134 ClearCounts(); |
| 106 | 135 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 ClearCounts(); | 194 ClearCounts(); |
| 166 | 195 |
| 167 model.NotifyItemsChanged(0, 1); | 196 model.NotifyItemsChanged(0, 1); |
| 168 ExpectCountsEqual(0, 0, 0, 1); | 197 ExpectCountsEqual(0, 0, 0, 1); |
| 169 | 198 |
| 170 model.NotifyItemsChanged(1, 2); | 199 model.NotifyItemsChanged(1, 2); |
| 171 ExpectCountsEqual(0, 0, 0, 3); | 200 ExpectCountsEqual(0, 0, 0, 3); |
| 172 } | 201 } |
| 173 | 202 |
| 174 } // namespace ui | 203 } // namespace ui |
| OLD | NEW |