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 13 matching lines...) Expand all Loading... |
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 changed_count_(0) { | 31 changed_count_(0) { |
32 } | 32 } |
33 | 33 |
34 void ExpectCountsEqual(int added_count, | 34 void ExpectCountsEqual(size_t added_count, |
35 int removed_count, | 35 size_t removed_count, |
36 int changed_count) { | 36 size_t changed_count) { |
37 EXPECT_EQ(added_count, added_count_); | 37 EXPECT_EQ(added_count, added_count_); |
38 EXPECT_EQ(removed_count, removed_count_); | 38 EXPECT_EQ(removed_count, removed_count_); |
39 EXPECT_EQ(changed_count, changed_count_); | 39 EXPECT_EQ(changed_count, changed_count_); |
40 } | 40 } |
41 | 41 |
42 void ClearCounts() { | 42 void ClearCounts() { |
43 added_count_ = removed_count_ = changed_count_ = 0; | 43 added_count_ = removed_count_ = changed_count_ = 0; |
44 } | 44 } |
45 | 45 |
46 // ListModelObserver implementation: | 46 // ListModelObserver implementation: |
47 virtual void ListItemsAdded(int start, int count) OVERRIDE { | 47 virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE { |
48 added_count_ += count; | 48 added_count_ += count; |
49 } | 49 } |
50 virtual void ListItemsRemoved(int start, int count) OVERRIDE { | 50 virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE { |
51 removed_count_ += count; | 51 removed_count_ += count; |
52 } | 52 } |
53 virtual void ListItemsChanged(int start, int count) OVERRIDE { | 53 virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE { |
54 changed_count_ += count; | 54 changed_count_ += count; |
55 } | 55 } |
56 | 56 |
57 private: | 57 private: |
58 int added_count_; | 58 size_t added_count_; |
59 int removed_count_; | 59 size_t removed_count_; |
60 int changed_count_; | 60 size_t changed_count_; |
61 | 61 |
62 DISALLOW_COPY_AND_ASSIGN(ListModelTest); | 62 DISALLOW_COPY_AND_ASSIGN(ListModelTest); |
63 }; | 63 }; |
64 | 64 |
65 TEST_F(ListModelTest, Add) { | 65 TEST_F(ListModelTest, Add) { |
66 ListModel<FooItem> model; | 66 ListModel<FooItem> model; |
67 model.AddObserver(this); | 67 model.AddObserver(this); |
68 | 68 |
69 // Append FooItem(0) | 69 // Append FooItem(0) |
70 model.Add(new FooItem(0)); | 70 model.Add(new FooItem(0)); |
71 ExpectCountsEqual(1, 0, 0); | 71 ExpectCountsEqual(1, 0, 0); |
72 | 72 |
73 // Append FooItem(1) | 73 // Append FooItem(1) |
74 model.Add(new FooItem(1)); | 74 model.Add(new FooItem(1)); |
75 ExpectCountsEqual(2, 0, 0); | 75 ExpectCountsEqual(2, 0, 0); |
76 | 76 |
77 // Insert FooItem(2) at position 0 | 77 // Insert FooItem(2) at position 0 |
78 model.AddAt(0, new FooItem(2)); | 78 model.AddAt(0, new FooItem(2)); |
79 ExpectCountsEqual(3, 0, 0); | 79 ExpectCountsEqual(3, 0, 0); |
80 | 80 |
81 // Total 3 items in mode. | 81 // Total 3 items in mode. |
82 EXPECT_EQ(3, model.item_count()); | 82 EXPECT_EQ(3U, model.item_count()); |
83 | 83 |
84 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) | 84 // First one should be FooItem(2), followed by FooItem(0) and FooItem(1) |
85 EXPECT_EQ(2, model.GetItemAt(0)->id()); | 85 EXPECT_EQ(2, model.GetItemAt(0)->id()); |
86 EXPECT_EQ(0, model.GetItemAt(1)->id()); | 86 EXPECT_EQ(0, model.GetItemAt(1)->id()); |
87 EXPECT_EQ(1, model.GetItemAt(2)->id()); | 87 EXPECT_EQ(1, model.GetItemAt(2)->id()); |
88 } | 88 } |
89 | 89 |
90 TEST_F(ListModelTest, Remove) { | 90 TEST_F(ListModelTest, Remove) { |
91 ListModel<FooItem> model; | 91 ListModel<FooItem> model; |
92 model.AddObserver(this); | 92 model.AddObserver(this); |
93 | 93 |
94 model.Add(new FooItem(0)); | 94 model.Add(new FooItem(0)); |
95 model.Add(new FooItem(1)); | 95 model.Add(new FooItem(1)); |
96 model.Add(new FooItem(2)); | 96 model.Add(new FooItem(2)); |
97 | 97 |
98 ClearCounts(); | 98 ClearCounts(); |
99 | 99 |
100 // Remove item at index 1 from model and release memory. | 100 // Remove item at index 1 from model and release memory. |
101 model.DeleteAt(1); | 101 model.DeleteAt(1); |
102 ExpectCountsEqual(0, 1, 0); | 102 ExpectCountsEqual(0, 1, 0); |
103 | 103 |
104 EXPECT_EQ(2, model.item_count()); | 104 EXPECT_EQ(2U, model.item_count()); |
105 EXPECT_EQ(0, model.GetItemAt(0)->id()); | 105 EXPECT_EQ(0, model.GetItemAt(0)->id()); |
106 EXPECT_EQ(2, model.GetItemAt(1)->id()); | 106 EXPECT_EQ(2, model.GetItemAt(1)->id()); |
107 | 107 |
108 // Remove all items from model and delete them. | 108 // Remove all items from model and delete them. |
109 model.DeleteAll(); | 109 model.DeleteAll(); |
110 ExpectCountsEqual(0, 3, 0); | 110 ExpectCountsEqual(0, 3, 0); |
111 } | 111 } |
112 | 112 |
113 TEST_F(ListModelTest, RemoveAll) { | 113 TEST_F(ListModelTest, RemoveAll) { |
114 ListModel<FooItem> model; | 114 ListModel<FooItem> model; |
(...skipping 25 matching lines...) Expand all Loading... |
140 ClearCounts(); | 140 ClearCounts(); |
141 | 141 |
142 model.NotifyItemsChanged(0, 1); | 142 model.NotifyItemsChanged(0, 1); |
143 ExpectCountsEqual(0, 0, 1); | 143 ExpectCountsEqual(0, 0, 1); |
144 | 144 |
145 model.NotifyItemsChanged(1, 2); | 145 model.NotifyItemsChanged(1, 2); |
146 ExpectCountsEqual(0, 0, 3); | 146 ExpectCountsEqual(0, 0, 3); |
147 } | 147 } |
148 | 148 |
149 } // namespace ui | 149 } // namespace ui |
OLD | NEW |