| 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 "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 int id_; | 21 int id_; |
| 22 DISALLOW_COPY_AND_ASSIGN(FooItem); | 22 DISALLOW_COPY_AND_ASSIGN(FooItem); |
| 23 }; | 23 }; |
| 24 | 24 |
| 25 class ListModelTest : public testing::Test, | 25 class ListModelTest : public testing::Test, |
| 26 public ListModelObserver { | 26 public ListModelObserver { |
| 27 public: | 27 public: |
| 28 ListModelTest() | 28 ListModelTest() |
| 29 : added_count_(0), | 29 : added_count_(0), |
| 30 removed_count_(0), | 30 removed_count_(0), |
| 31 moved_count_(0), |
| 31 changed_count_(0) { | 32 changed_count_(0) { |
| 32 } | 33 } |
| 33 | 34 |
| 34 void ExpectCountsEqual(size_t added_count, | 35 void ExpectCountsEqual(size_t added_count, |
| 35 size_t removed_count, | 36 size_t removed_count, |
| 37 size_t moved_count, |
| 36 size_t changed_count) { | 38 size_t changed_count) { |
| 37 EXPECT_EQ(added_count, added_count_); | 39 EXPECT_EQ(added_count, added_count_); |
| 38 EXPECT_EQ(removed_count, removed_count_); | 40 EXPECT_EQ(removed_count, removed_count_); |
| 41 EXPECT_EQ(moved_count, moved_count_); |
| 39 EXPECT_EQ(changed_count, changed_count_); | 42 EXPECT_EQ(changed_count, changed_count_); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void ClearCounts() { | 45 void ClearCounts() { |
| 43 added_count_ = removed_count_ = changed_count_ = 0; | 46 added_count_ = removed_count_ = moved_count_ = changed_count_ = 0; |
| 44 } | 47 } |
| 45 | 48 |
| 46 // ListModelObserver implementation: | 49 // ListModelObserver implementation: |
| 47 virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE { | 50 virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE { |
| 48 added_count_ += count; | 51 added_count_ += count; |
| 49 } | 52 } |
| 50 virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE { | 53 virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE { |
| 51 removed_count_ += count; | 54 removed_count_ += count; |
| 52 } | 55 } |
| 56 virtual void ListItemMoved(size_t index, size_t target_index) OVERRIDE { |
| 57 ++moved_count_; |
| 58 } |
| 53 virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE { | 59 virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE { |
| 54 changed_count_ += count; | 60 changed_count_ += count; |
| 55 } | 61 } |
| 56 | 62 |
| 57 private: | 63 private: |
| 58 size_t added_count_; | 64 size_t added_count_; |
| 59 size_t removed_count_; | 65 size_t removed_count_; |
| 66 size_t moved_count_; |
| 60 size_t changed_count_; | 67 size_t changed_count_; |
| 61 | 68 |
| 62 DISALLOW_COPY_AND_ASSIGN(ListModelTest); | 69 DISALLOW_COPY_AND_ASSIGN(ListModelTest); |
| 63 }; | 70 }; |
| 64 | 71 |
| 65 TEST_F(ListModelTest, Add) { | 72 TEST_F(ListModelTest, Add) { |
| 66 ListModel<FooItem> model; | 73 ListModel<FooItem> model; |
| 67 model.AddObserver(this); | 74 model.AddObserver(this); |
| 68 | 75 |
| 69 // Append FooItem(0) | 76 // Append FooItem(0) |
| 70 model.Add(new FooItem(0)); | 77 model.Add(new FooItem(0)); |
| 71 ExpectCountsEqual(1, 0, 0); | 78 ExpectCountsEqual(1, 0, 0, 0); |
| 72 | 79 |
| 73 // Append FooItem(1) | 80 // Append FooItem(1) |
| 74 model.Add(new FooItem(1)); | 81 model.Add(new FooItem(1)); |
| 75 ExpectCountsEqual(2, 0, 0); | 82 ExpectCountsEqual(2, 0, 0, 0); |
| 76 | 83 |
| 77 // Insert FooItem(2) at position 0 | 84 // Insert FooItem(2) at position 0 |
| 78 model.AddAt(0, new FooItem(2)); | 85 model.AddAt(0, new FooItem(2)); |
| 79 ExpectCountsEqual(3, 0, 0); | 86 ExpectCountsEqual(3, 0, 0, 0); |
| 80 | 87 |
| 81 // Total 3 items in mode. | 88 // Total 3 items in model. |
| 82 EXPECT_EQ(3U, model.item_count()); | 89 EXPECT_EQ(3U, model.item_count()); |
| 83 | 90 |
| 84 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) | 91 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) |
| 85 EXPECT_EQ(2, model.GetItemAt(0)->id()); | 92 EXPECT_EQ(2, model.GetItemAt(0)->id()); |
| 86 EXPECT_EQ(0, model.GetItemAt(1)->id()); | 93 EXPECT_EQ(0, model.GetItemAt(1)->id()); |
| 87 EXPECT_EQ(1, model.GetItemAt(2)->id()); | 94 EXPECT_EQ(1, model.GetItemAt(2)->id()); |
| 88 } | 95 } |
| 89 | 96 |
| 90 TEST_F(ListModelTest, Remove) { | 97 TEST_F(ListModelTest, Remove) { |
| 91 ListModel<FooItem> model; | 98 ListModel<FooItem> model; |
| 92 model.AddObserver(this); | 99 model.AddObserver(this); |
| 93 | 100 |
| 94 model.Add(new FooItem(0)); | 101 model.Add(new FooItem(0)); |
| 95 model.Add(new FooItem(1)); | 102 model.Add(new FooItem(1)); |
| 96 model.Add(new FooItem(2)); | 103 model.Add(new FooItem(2)); |
| 97 | 104 |
| 98 ClearCounts(); | 105 ClearCounts(); |
| 99 | 106 |
| 100 // Remove item at index 1 from model and release memory. | 107 // Remove item at index 1 from model and release memory. |
| 101 model.DeleteAt(1); | 108 model.DeleteAt(1); |
| 102 ExpectCountsEqual(0, 1, 0); | 109 ExpectCountsEqual(0, 1, 0, 0); |
| 103 | 110 |
| 104 EXPECT_EQ(2U, model.item_count()); | 111 EXPECT_EQ(2U, model.item_count()); |
| 105 EXPECT_EQ(0, model.GetItemAt(0)->id()); | 112 EXPECT_EQ(0, model.GetItemAt(0)->id()); |
| 106 EXPECT_EQ(2, model.GetItemAt(1)->id()); | 113 EXPECT_EQ(2, model.GetItemAt(1)->id()); |
| 107 | 114 |
| 108 // Remove all items from model and delete them. | 115 // Remove all items from model and delete them. |
| 109 model.DeleteAll(); | 116 model.DeleteAll(); |
| 110 ExpectCountsEqual(0, 3, 0); | 117 ExpectCountsEqual(0, 3, 0, 0); |
| 111 } | 118 } |
| 112 | 119 |
| 113 TEST_F(ListModelTest, RemoveAll) { | 120 TEST_F(ListModelTest, RemoveAll) { |
| 114 ListModel<FooItem> model; | 121 ListModel<FooItem> model; |
| 115 model.AddObserver(this); | 122 model.AddObserver(this); |
| 116 | 123 |
| 117 scoped_ptr<FooItem> foo0(new FooItem(0)); | 124 scoped_ptr<FooItem> foo0(new FooItem(0)); |
| 118 scoped_ptr<FooItem> foo1(new FooItem(1)); | 125 scoped_ptr<FooItem> foo1(new FooItem(1)); |
| 119 scoped_ptr<FooItem> foo2(new FooItem(2)); | 126 scoped_ptr<FooItem> foo2(new FooItem(2)); |
| 120 | 127 |
| 121 model.Add(foo0.get()); | 128 model.Add(foo0.get()); |
| 122 model.Add(foo1.get()); | 129 model.Add(foo1.get()); |
| 123 model.Add(foo2.get()); | 130 model.Add(foo2.get()); |
| 124 | 131 |
| 125 ClearCounts(); | 132 ClearCounts(); |
| 126 | 133 |
| 127 // Remove all items and scoped_ptr above would release memory. | 134 // Remove all items and scoped_ptr above would release memory. |
| 128 model.RemoveAll(); | 135 model.RemoveAll(); |
| 129 ExpectCountsEqual(0, 3, 0); | 136 ExpectCountsEqual(0, 3, 0, 0); |
| 137 } |
| 138 |
| 139 TEST_F(ListModelTest, Move) { |
| 140 ListModel<FooItem> model; |
| 141 model.AddObserver(this); |
| 142 |
| 143 model.Add(new FooItem(0)); |
| 144 model.Add(new FooItem(1)); |
| 145 model.Add(new FooItem(2)); |
| 146 |
| 147 ClearCounts(); |
| 148 |
| 149 // Moves item at index 0 to index 2. |
| 150 model.Move(0, 2); |
| 151 ExpectCountsEqual(0, 0, 1, 0); |
| 152 EXPECT_EQ(1, model.GetItemAt(0)->id()); |
| 153 EXPECT_EQ(2, model.GetItemAt(1)->id()); |
| 154 EXPECT_EQ(0, model.GetItemAt(2)->id()); |
| 130 } | 155 } |
| 131 | 156 |
| 132 TEST_F(ListModelTest, FakeUpdate) { | 157 TEST_F(ListModelTest, FakeUpdate) { |
| 133 ListModel<FooItem> model; | 158 ListModel<FooItem> model; |
| 134 model.AddObserver(this); | 159 model.AddObserver(this); |
| 135 | 160 |
| 136 model.Add(new FooItem(0)); | 161 model.Add(new FooItem(0)); |
| 137 model.Add(new FooItem(1)); | 162 model.Add(new FooItem(1)); |
| 138 model.Add(new FooItem(2)); | 163 model.Add(new FooItem(2)); |
| 139 | 164 |
| 140 ClearCounts(); | 165 ClearCounts(); |
| 141 | 166 |
| 142 model.NotifyItemsChanged(0, 1); | 167 model.NotifyItemsChanged(0, 1); |
| 143 ExpectCountsEqual(0, 0, 1); | 168 ExpectCountsEqual(0, 0, 0, 1); |
| 144 | 169 |
| 145 model.NotifyItemsChanged(1, 2); | 170 model.NotifyItemsChanged(1, 2); |
| 146 ExpectCountsEqual(0, 0, 3); | 171 ExpectCountsEqual(0, 0, 0, 3); |
| 147 } | 172 } |
| 148 | 173 |
| 149 } // namespace ui | 174 } // namespace ui |
| OLD | NEW |