summaryrefslogtreecommitdiffstats
path: root/ui/base/models/list_model_unittest.cc
blob: 2dcaf4d39888f26b01d37d3c91ace0571b07fa03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/models/list_model.h"

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ui {

class FooItem {
 public:
  explicit FooItem(int id) : id_(id) {}

  int id() const { return id_; }

 private:
  int id_;
  DISALLOW_COPY_AND_ASSIGN(FooItem);
};

class ListModelTest : public testing::Test,
                      public ListModelObserver {
 public:
  ListModelTest()
      : added_count_(0),
        removed_count_(0),
        changed_count_(0) {
  }

  void ExpectCountsEqual(int added_count,
                         int removed_count,
                         int changed_count) {
    EXPECT_EQ(added_count, added_count_);
    EXPECT_EQ(removed_count, removed_count_);
    EXPECT_EQ(changed_count, changed_count_);
  }

  void ClearCounts() {
    added_count_ = removed_count_ = changed_count_ = 0;
  }

  // ListModelObserver implementation:
  virtual void ListItemsAdded(int start, int count) OVERRIDE {
    added_count_ += count;
  }
  virtual void ListItemsRemoved(int start, int count) OVERRIDE {
    removed_count_ += count;
  }
  virtual void ListItemsChanged(int start, int count) OVERRIDE {
    changed_count_ += count;
  }

 private:
  int added_count_;
  int removed_count_;
  int changed_count_;

  DISALLOW_COPY_AND_ASSIGN(ListModelTest);
};

TEST_F(ListModelTest, Add) {
  ListModel<FooItem> model;
  model.AddObserver(this);

  // Append FooItem(0)
  model.Add(new FooItem(0));
  ExpectCountsEqual(1, 0, 0);

  // Append FooItem(1)
  model.Add(new FooItem(1));
  ExpectCountsEqual(2, 0, 0);

  // Insert FooItem(2) at position 0
  model.AddAt(0, new FooItem(2));
  ExpectCountsEqual(3, 0, 0);

  // Total 3 items in mode.
  EXPECT_EQ(3, model.item_count());

  // First one should be FooItem(2), followed by FooItem(0) and FooItem(1)
  EXPECT_EQ(2, model.GetItemAt(0)->id());
  EXPECT_EQ(0, model.GetItemAt(1)->id());
  EXPECT_EQ(1, model.GetItemAt(2)->id());
}

TEST_F(ListModelTest, Remove) {
  ListModel<FooItem> model;
  model.AddObserver(this);

  model.Add(new FooItem(0));
  model.Add(new FooItem(1));
  model.Add(new FooItem(2));

  ClearCounts();

  // Remove item at index 1 from model and release memory.
  model.DeleteAt(1);
  ExpectCountsEqual(0, 1, 0);

  EXPECT_EQ(2, model.item_count());
  EXPECT_EQ(0, model.GetItemAt(0)->id());
  EXPECT_EQ(2, model.GetItemAt(1)->id());

  // Remove all items from model and delete them.
  model.DeleteAll();
  ExpectCountsEqual(0, 3, 0);
}

TEST_F(ListModelTest, RemoveAll) {
  ListModel<FooItem> model;
  model.AddObserver(this);

  scoped_ptr<FooItem> foo0(new FooItem(0));
  scoped_ptr<FooItem> foo1(new FooItem(1));
  scoped_ptr<FooItem> foo2(new FooItem(2));

  model.Add(foo0.get());
  model.Add(foo1.get());
  model.Add(foo2.get());

  ClearCounts();

  // Remove all items and scoped_ptr above would release memory.
  model.RemoveAll();
  ExpectCountsEqual(0, 3, 0);
}

TEST_F(ListModelTest, FakeUpdate) {
  ListModel<FooItem> model;
  model.AddObserver(this);

  model.Add(new FooItem(0));
  model.Add(new FooItem(1));
  model.Add(new FooItem(2));

  ClearCounts();

  model.NotifyItemsChanged(0, 1);
  ExpectCountsEqual(0, 0, 1);

  model.NotifyItemsChanged(1, 2);
  ExpectCountsEqual(0, 0, 3);
}

}  // namespace ui