OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 |
| 5 #include "ui/base/models/list_model.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace ui { |
| 13 |
| 14 class FooItem { |
| 15 public: |
| 16 explicit FooItem(int id) : id_(id) {} |
| 17 |
| 18 int id() const { return id_; } |
| 19 |
| 20 private: |
| 21 int id_; |
| 22 DISALLOW_COPY_AND_ASSIGN(FooItem); |
| 23 }; |
| 24 |
| 25 class ListModelTest : public testing::Test, |
| 26 public ListModelObserver { |
| 27 public: |
| 28 ListModelTest() |
| 29 : added_count_(0), |
| 30 removed_count_(0), |
| 31 changed_count_(0) { |
| 32 } |
| 33 |
| 34 void ExpectCountsEqual(int added_count, |
| 35 int removed_count, |
| 36 int changed_count) { |
| 37 EXPECT_EQ(added_count, added_count_); |
| 38 EXPECT_EQ(removed_count, removed_count_); |
| 39 EXPECT_EQ(changed_count, changed_count_); |
| 40 } |
| 41 |
| 42 void ClearCounts() { |
| 43 added_count_ = removed_count_ = changed_count_ = 0; |
| 44 } |
| 45 |
| 46 // ListModelObserver implementation: |
| 47 virtual void ListItemsAdded(int start, int count) OVERRIDE { |
| 48 added_count_ += count; |
| 49 } |
| 50 virtual void ListItemsRemoved(int start, int count) OVERRIDE { |
| 51 removed_count_ += count; |
| 52 } |
| 53 virtual void ListItemsChanged(int start, int count) OVERRIDE { |
| 54 changed_count_ += count; |
| 55 } |
| 56 |
| 57 private: |
| 58 int added_count_; |
| 59 int removed_count_; |
| 60 int changed_count_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(ListModelTest); |
| 63 }; |
| 64 |
| 65 TEST_F(ListModelTest, Add) { |
| 66 ListModel<FooItem> model; |
| 67 model.AddObserver(this); |
| 68 |
| 69 // Append FooItem(0) |
| 70 model.Add(new FooItem(0)); |
| 71 ExpectCountsEqual(1, 0, 0); |
| 72 |
| 73 // Append FooItem(1) |
| 74 model.Add(new FooItem(1)); |
| 75 ExpectCountsEqual(2, 0, 0); |
| 76 |
| 77 // Insert FooItem(2) at position 0 |
| 78 model.AddAt(0, new FooItem(2)); |
| 79 ExpectCountsEqual(3, 0, 0); |
| 80 |
| 81 // Total 3 items in mode. |
| 82 EXPECT_EQ(3, model.item_count()); |
| 83 |
| 84 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) |
| 85 EXPECT_EQ(2, model.item_at(0)->id()); |
| 86 EXPECT_EQ(0, model.item_at(1)->id()); |
| 87 EXPECT_EQ(1, model.item_at(2)->id()); |
| 88 } |
| 89 |
| 90 TEST_F(ListModelTest, Remove) { |
| 91 ListModel<FooItem> model; |
| 92 model.AddObserver(this); |
| 93 |
| 94 model.Add(new FooItem(0)); |
| 95 model.Add(new FooItem(1)); |
| 96 model.Add(new FooItem(2)); |
| 97 |
| 98 ClearCounts(); |
| 99 |
| 100 // Remove item at index 1 from model and release memory. |
| 101 model.DeleteAt(1); |
| 102 ExpectCountsEqual(0, 1, 0); |
| 103 |
| 104 EXPECT_EQ(2, model.item_count()); |
| 105 EXPECT_EQ(0, model.item_at(0)->id()); |
| 106 EXPECT_EQ(2, model.item_at(1)->id()); |
| 107 |
| 108 // Remove all items from model and delete them. |
| 109 model.DeleteAll(); |
| 110 ExpectCountsEqual(0, 3, 0); |
| 111 } |
| 112 |
| 113 TEST_F(ListModelTest, RemoveAll) { |
| 114 ListModel<FooItem> model; |
| 115 model.AddObserver(this); |
| 116 |
| 117 scoped_ptr<FooItem> foo0(new FooItem(0)); |
| 118 scoped_ptr<FooItem> foo1(new FooItem(1)); |
| 119 scoped_ptr<FooItem> foo2(new FooItem(2)); |
| 120 |
| 121 model.Add(foo0.get()); |
| 122 model.Add(foo1.get()); |
| 123 model.Add(foo2.get()); |
| 124 |
| 125 ClearCounts(); |
| 126 |
| 127 // Remove all items and scoped_ptr above would release memory. |
| 128 model.RemoveAll(); |
| 129 ExpectCountsEqual(0, 3, 0); |
| 130 } |
| 131 |
| 132 TEST_F(ListModelTest, FakeUpdate) { |
| 133 ListModel<FooItem> model; |
| 134 model.AddObserver(this); |
| 135 |
| 136 model.Add(new FooItem(0)); |
| 137 model.Add(new FooItem(1)); |
| 138 model.Add(new FooItem(2)); |
| 139 |
| 140 ClearCounts(); |
| 141 |
| 142 model.NotifyItemsChanged(0, 1); |
| 143 ExpectCountsEqual(0, 0, 1); |
| 144 |
| 145 model.NotifyItemsChanged(1, 2); |
| 146 ExpectCountsEqual(0, 0, 3); |
| 147 } |
| 148 |
| 149 } // namespace ui |
OLD | NEW |