| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace ui { | 16 namespace ui { |
| 16 | 17 |
| 17 class FooItem { | 18 class FooItem { |
| 18 public: | 19 public: |
| 19 explicit FooItem(int id) : id_(id) {} | 20 explicit FooItem(int id) : id_(id) {} |
| 20 | 21 |
| 21 int id() const { return id_; } | 22 int id() const { return id_; } |
| 22 | 23 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 size_t changed_count_; | 71 size_t changed_count_; |
| 71 | 72 |
| 72 DISALLOW_COPY_AND_ASSIGN(ListModelTest); | 73 DISALLOW_COPY_AND_ASSIGN(ListModelTest); |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 TEST_F(ListModelTest, Add) { | 76 TEST_F(ListModelTest, Add) { |
| 76 ListModel<FooItem> model; | 77 ListModel<FooItem> model; |
| 77 model.AddObserver(this); | 78 model.AddObserver(this); |
| 78 | 79 |
| 79 // Append FooItem(0) | 80 // Append FooItem(0) |
| 80 model.Add(new FooItem(0)); | 81 model.Add(base::MakeUnique<FooItem>(0)); |
| 81 ExpectCountsEqual(1, 0, 0, 0); | 82 ExpectCountsEqual(1, 0, 0, 0); |
| 82 | 83 |
| 83 // Append FooItem(1) | 84 // Append FooItem(1) |
| 84 model.Add(new FooItem(1)); | 85 model.Add(base::MakeUnique<FooItem>(1)); |
| 85 ExpectCountsEqual(2, 0, 0, 0); | 86 ExpectCountsEqual(2, 0, 0, 0); |
| 86 | 87 |
| 87 // Insert FooItem(2) at position 0 | 88 // Insert FooItem(2) at position 0 |
| 88 model.AddAt(0, new FooItem(2)); | 89 model.AddAt(0, base::MakeUnique<FooItem>(2)); |
| 89 ExpectCountsEqual(3, 0, 0, 0); | 90 ExpectCountsEqual(3, 0, 0, 0); |
| 90 | 91 |
| 91 // Total 3 items in model. | 92 // Total 3 items in model. |
| 92 EXPECT_EQ(3U, model.item_count()); | 93 EXPECT_EQ(3U, model.item_count()); |
| 93 | 94 |
| 94 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) | 95 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) |
| 95 EXPECT_EQ(2, model.GetItemAt(0)->id()); | 96 EXPECT_EQ(2, model.GetItemAt(0)->id()); |
| 96 EXPECT_EQ(0, model.GetItemAt(1)->id()); | 97 EXPECT_EQ(0, model.GetItemAt(1)->id()); |
| 97 EXPECT_EQ(1, model.GetItemAt(2)->id()); | 98 EXPECT_EQ(1, model.GetItemAt(2)->id()); |
| 98 } | 99 } |
| 99 | 100 |
| 100 TEST_F(ListModelTest, Remove) { | 101 TEST_F(ListModelTest, Remove) { |
| 101 ListModel<FooItem> model; | 102 ListModel<FooItem> model; |
| 102 model.AddObserver(this); | 103 model.AddObserver(this); |
| 103 | 104 |
| 104 model.Add(new FooItem(0)); | 105 model.Add(base::MakeUnique<FooItem>(0)); |
| 105 model.Add(new FooItem(1)); | 106 model.Add(base::MakeUnique<FooItem>(1)); |
| 106 model.Add(new FooItem(2)); | 107 model.Add(base::MakeUnique<FooItem>(2)); |
| 107 | 108 |
| 108 ClearCounts(); | 109 ClearCounts(); |
| 109 | 110 |
| 110 // Remove item at index 1 from model and release memory. | 111 // Remove item at index 1 from model and release memory. |
| 111 model.DeleteAt(1); | 112 model.DeleteAt(1); |
| 112 ExpectCountsEqual(0, 1, 0, 0); | 113 ExpectCountsEqual(0, 1, 0, 0); |
| 113 | 114 |
| 114 EXPECT_EQ(2U, model.item_count()); | 115 EXPECT_EQ(2U, model.item_count()); |
| 115 EXPECT_EQ(0, model.GetItemAt(0)->id()); | 116 EXPECT_EQ(0, model.GetItemAt(0)->id()); |
| 116 EXPECT_EQ(2, model.GetItemAt(1)->id()); | 117 EXPECT_EQ(2, model.GetItemAt(1)->id()); |
| 117 | 118 |
| 118 // Remove all items from model and delete them. | 119 // Remove all items from model and delete them. |
| 119 model.DeleteAll(); | 120 model.DeleteAll(); |
| 120 ExpectCountsEqual(0, 3, 0, 0); | 121 ExpectCountsEqual(0, 3, 0, 0); |
| 121 } | 122 } |
| 122 | 123 |
| 123 TEST_F(ListModelTest, RemoveAll) { | 124 TEST_F(ListModelTest, DeleteAll) { |
| 124 ListModel<FooItem> model; | 125 ListModel<FooItem> model; |
| 125 model.AddObserver(this); | 126 model.AddObserver(this); |
| 126 | 127 |
| 127 std::unique_ptr<FooItem> foo0(new FooItem(0)); | 128 model.Add(base::MakeUnique<FooItem>(0)); |
| 128 std::unique_ptr<FooItem> foo1(new FooItem(1)); | 129 model.Add(base::MakeUnique<FooItem>(1)); |
| 129 std::unique_ptr<FooItem> foo2(new FooItem(2)); | 130 model.Add(base::MakeUnique<FooItem>(2)); |
| 130 | |
| 131 model.Add(foo0.get()); | |
| 132 model.Add(foo1.get()); | |
| 133 model.Add(foo2.get()); | |
| 134 | 131 |
| 135 ClearCounts(); | 132 ClearCounts(); |
| 136 | 133 |
| 137 // Remove all items and scoped_ptr above would release memory. | 134 // Delete all items. |
| 138 model.RemoveAll(); | 135 model.DeleteAll(); |
| 139 ExpectCountsEqual(0, 3, 0, 0); | 136 ExpectCountsEqual(0, 3, 0, 0); |
| 140 } | 137 } |
| 141 | 138 |
| 142 TEST_F(ListModelTest, Move) { | 139 TEST_F(ListModelTest, Move) { |
| 143 ListModel<FooItem> model; | 140 ListModel<FooItem> model; |
| 144 model.AddObserver(this); | 141 model.AddObserver(this); |
| 145 | 142 |
| 146 model.Add(new FooItem(0)); | 143 model.Add(base::MakeUnique<FooItem>(0)); |
| 147 model.Add(new FooItem(1)); | 144 model.Add(base::MakeUnique<FooItem>(1)); |
| 148 model.Add(new FooItem(2)); | 145 model.Add(base::MakeUnique<FooItem>(2)); |
| 149 | 146 |
| 150 ClearCounts(); | 147 ClearCounts(); |
| 151 | 148 |
| 152 // Moves item at index 0 to index 2. | 149 // Moves item at index 0 to index 2. |
| 153 model.Move(0, 2); | 150 model.Move(0, 2); |
| 154 ExpectCountsEqual(0, 0, 1, 0); | 151 ExpectCountsEqual(0, 0, 1, 0); |
| 155 EXPECT_EQ(1, model.GetItemAt(0)->id()); | 152 EXPECT_EQ(1, model.GetItemAt(0)->id()); |
| 156 EXPECT_EQ(2, model.GetItemAt(1)->id()); | 153 EXPECT_EQ(2, model.GetItemAt(1)->id()); |
| 157 EXPECT_EQ(0, model.GetItemAt(2)->id()); | 154 EXPECT_EQ(0, model.GetItemAt(2)->id()); |
| 158 } | 155 } |
| 159 | 156 |
| 160 TEST_F(ListModelTest, FakeUpdate) { | 157 TEST_F(ListModelTest, FakeUpdate) { |
| 161 ListModel<FooItem> model; | 158 ListModel<FooItem> model; |
| 162 model.AddObserver(this); | 159 model.AddObserver(this); |
| 163 | 160 |
| 164 model.Add(new FooItem(0)); | 161 model.Add(base::MakeUnique<FooItem>(0)); |
| 165 model.Add(new FooItem(1)); | 162 model.Add(base::MakeUnique<FooItem>(1)); |
| 166 model.Add(new FooItem(2)); | 163 model.Add(base::MakeUnique<FooItem>(2)); |
| 167 | 164 |
| 168 ClearCounts(); | 165 ClearCounts(); |
| 169 | 166 |
| 170 model.NotifyItemsChanged(0, 1); | 167 model.NotifyItemsChanged(0, 1); |
| 171 ExpectCountsEqual(0, 0, 0, 1); | 168 ExpectCountsEqual(0, 0, 0, 1); |
| 172 | 169 |
| 173 model.NotifyItemsChanged(1, 2); | 170 model.NotifyItemsChanged(1, 2); |
| 174 ExpectCountsEqual(0, 0, 0, 3); | 171 ExpectCountsEqual(0, 0, 0, 3); |
| 175 } | 172 } |
| 176 | 173 |
| 177 } // namespace ui | 174 } // namespace ui |
| OLD | NEW |