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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// 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_selection_model.h"
#include <algorithm>
#include <string>
#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ui {
typedef testing::Test ListSelectionModelTest;
// Returns the state of the selection model as a string. The format is:
// 'active=X anchor=X selection=X X X...'.
static std::string StateAsString(const ListSelectionModel& model) {
std::string result = "active=" + base::IntToString(model.active()) +
" anchor=" + base::IntToString(model.anchor()) +
" selection=";
const ListSelectionModel::SelectedIndices& selection(
model.selected_indices());
for (size_t i = 0; i < selection.size(); ++i) {
if (i != 0)
result += " ";
result += base::IntToString(selection[i]);
}
return result;
}
TEST_F(ListSelectionModelTest, InitialState) {
ListSelectionModel model;
EXPECT_EQ("active=-1 anchor=-1 selection=", StateAsString(model));
EXPECT_TRUE(model.empty());
}
TEST_F(ListSelectionModelTest, SetSelectedIndex) {
ListSelectionModel model;
model.SetSelectedIndex(2);
EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
EXPECT_FALSE(model.empty());
}
TEST_F(ListSelectionModelTest, SetSelectedIndexToEmpty) {
ListSelectionModel model;
model.SetSelectedIndex(-1);
EXPECT_EQ("active=-1 anchor=-1 selection=", StateAsString(model));
EXPECT_TRUE(model.empty());
}
TEST_F(ListSelectionModelTest, IncrementFrom) {
ListSelectionModel model;
model.SetSelectedIndex(1);
model.IncrementFrom(1);
EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
// Increment from 4. This shouldn't effect the selection as its past the
// end of the selection.
model.IncrementFrom(4);
EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
}
TEST_F(ListSelectionModelTest, DecrementFrom) {
ListSelectionModel model;
model.SetSelectedIndex(2);
model.DecrementFrom(0);
EXPECT_EQ("active=1 anchor=1 selection=1", StateAsString(model));
// Shift down from 1. As the selection as the index being removed, this should
// clear the selection.
model.DecrementFrom(1);
EXPECT_EQ("active=-1 anchor=-1 selection=", StateAsString(model));
// Reset the selection to 2, and shift down from 4. This shouldn't do
// anything.
model.SetSelectedIndex(2);
model.DecrementFrom(4);
EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
}
TEST_F(ListSelectionModelTest, IsSelected) {
ListSelectionModel model;
model.SetSelectedIndex(2);
EXPECT_FALSE(model.IsSelected(0));
EXPECT_TRUE(model.IsSelected(2));
}
TEST_F(ListSelectionModelTest, AddIndexToSelected) {
ListSelectionModel model;
model.AddIndexToSelection(2);
EXPECT_EQ("active=-1 anchor=-1 selection=2", StateAsString(model));
model.AddIndexToSelection(4);
EXPECT_EQ("active=-1 anchor=-1 selection=2 4", StateAsString(model));
}
TEST_F(ListSelectionModelTest, RemoveIndexFromSelection) {
ListSelectionModel model;
model.SetSelectedIndex(2);
model.AddIndexToSelection(4);
EXPECT_EQ("active=2 anchor=2 selection=2 4", StateAsString(model));
model.RemoveIndexFromSelection(4);
EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
model.RemoveIndexFromSelection(2);
EXPECT_EQ("active=2 anchor=2 selection=", StateAsString(model));
}
TEST_F(ListSelectionModelTest, SetSelectionFromAnchorTo) {
ListSelectionModel model;
model.SetSelectedIndex(2);
model.SetSelectionFromAnchorTo(7);
EXPECT_EQ("active=7 anchor=2 selection=2 3 4 5 6 7", StateAsString(model));
model.Clear();
model.SetSelectedIndex(7);
model.SetSelectionFromAnchorTo(2);
EXPECT_EQ("active=2 anchor=7 selection=2 3 4 5 6 7", StateAsString(model));
model.Clear();
model.SetSelectionFromAnchorTo(7);
EXPECT_EQ("active=7 anchor=7 selection=7", StateAsString(model));
}
TEST_F(ListSelectionModelTest, Clear) {
ListSelectionModel model;
model.SetSelectedIndex(2);
model.Clear();
EXPECT_EQ("active=-1 anchor=-1 selection=", StateAsString(model));
}
TEST_F(ListSelectionModelTest, MoveToLeft) {
ListSelectionModel model;
model.SetSelectedIndex(0);
model.AddIndexToSelection(4);
model.AddIndexToSelection(10);
model.set_anchor(4);
model.set_active(4);
model.Move(4, 0);
EXPECT_EQ("active=0 anchor=0 selection=0 1 10", StateAsString(model));
}
TEST_F(ListSelectionModelTest, MoveToRight) {
ListSelectionModel model;
model.SetSelectedIndex(0);
model.AddIndexToSelection(4);
model.AddIndexToSelection(10);
model.set_anchor(0);
model.set_active(0);
model.Move(0, 3);
EXPECT_EQ("active=3 anchor=3 selection=3 4 10", StateAsString(model));
}
TEST_F(ListSelectionModelTest, Copy) {
ListSelectionModel model;
model.SetSelectedIndex(0);
model.AddIndexToSelection(4);
model.AddIndexToSelection(10);
EXPECT_EQ("active=0 anchor=0 selection=0 4 10", StateAsString(model));
ListSelectionModel model2;
model2.Copy(model);
EXPECT_EQ("active=0 anchor=0 selection=0 4 10", StateAsString(model2));
}
TEST_F(ListSelectionModelTest, AddSelectionFromAnchorTo) {
ListSelectionModel model;
model.SetSelectedIndex(2);
model.AddSelectionFromAnchorTo(4);
EXPECT_EQ("active=4 anchor=2 selection=2 3 4", StateAsString(model));
model.AddSelectionFromAnchorTo(0);
EXPECT_EQ("active=0 anchor=2 selection=0 1 2 3 4", StateAsString(model));
}
TEST_F(ListSelectionModelTest, Equals) {
ListSelectionModel model1;
model1.SetSelectedIndex(0);
model1.AddSelectionFromAnchorTo(4);
ListSelectionModel model2;
model2.SetSelectedIndex(0);
model2.AddSelectionFromAnchorTo(4);
EXPECT_TRUE(model1.Equals(model2));
EXPECT_TRUE(model2.Equals(model1));
model2.SetSelectedIndex(0);
EXPECT_FALSE(model1.Equals(model2));
EXPECT_FALSE(model2.Equals(model1));
}
} // namespace ui
|