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
|
/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gtest/gtest.h"
#include "test_utils.h"
#include "core/mergedproxymodel.h"
#include <QStandardItemModel>
#include <QSignalSpy>
class MergedProxyModelTest : public ::testing::Test {
protected:
void SetUp() {
merged_.setSourceModel(&source_);
}
QStandardItemModel source_;
MergedProxyModel merged_;
};
TEST_F(MergedProxyModelTest, Flat) {
source_.appendRow(new QStandardItem("one"));
source_.appendRow(new QStandardItem("two"));
ASSERT_EQ(2, merged_.rowCount(QModelIndex()));
QModelIndex one_i = merged_.index(0, 0, QModelIndex());
QModelIndex two_i = merged_.index(1, 0, QModelIndex());
EXPECT_EQ("one", one_i.data().toString());
EXPECT_EQ("two", two_i.data().toString());
EXPECT_FALSE(merged_.parent(one_i).isValid());
EXPECT_FALSE(merged_.hasChildren(one_i));
}
TEST_F(MergedProxyModelTest, Tree) {
QStandardItem* one = new QStandardItem("one");
QStandardItem* two = new QStandardItem("two");
source_.appendRow(one);
one->appendRow(two);
ASSERT_EQ(1, merged_.rowCount(QModelIndex()));
QModelIndex one_i = merged_.index(0, 0, QModelIndex());
ASSERT_EQ(1, merged_.rowCount(one_i));
QModelIndex two_i = merged_.index(0, 0, one_i);
EXPECT_EQ("one", one_i.data().toString());
EXPECT_EQ("two", two_i.data().toString());
EXPECT_EQ("one", two_i.parent().data().toString());
}
TEST_F(MergedProxyModelTest, Merged) {
source_.appendRow(new QStandardItem("one"));
QStandardItemModel submodel;
submodel.appendRow(new QStandardItem("two"));
merged_.AddSubModel(source_.index(0, 0, QModelIndex()), &submodel);
ASSERT_EQ(1, merged_.rowCount(QModelIndex()));
QModelIndex one_i = merged_.index(0, 0, QModelIndex());
EXPECT_EQ("one", merged_.data(one_i).toString());
EXPECT_TRUE(merged_.hasChildren(one_i));
ASSERT_EQ(1, merged_.rowCount(one_i));
QModelIndex two_i = merged_.index(0, 0, one_i);
EXPECT_EQ("two", merged_.data(two_i).toString());
EXPECT_EQ(0, merged_.rowCount(two_i));
EXPECT_FALSE(merged_.hasChildren(two_i));
}
TEST_F(MergedProxyModelTest, SourceInsert) {
QSignalSpy before_spy(&merged_, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)));
QSignalSpy after_spy(&merged_, SIGNAL(rowsInserted(QModelIndex,int,int)));
source_.appendRow(new QStandardItem("one"));
ASSERT_EQ(1, before_spy.count());
ASSERT_EQ(1, after_spy.count());
EXPECT_FALSE(before_spy[0][0].value<QModelIndex>().isValid());
EXPECT_EQ(0, before_spy[0][1].toInt());
EXPECT_EQ(0, before_spy[0][2].toInt());
EXPECT_FALSE(after_spy[0][0].value<QModelIndex>().isValid());
EXPECT_EQ(0, after_spy[0][1].toInt());
EXPECT_EQ(0, after_spy[0][2].toInt());
}
TEST_F(MergedProxyModelTest, SourceRemove) {
source_.appendRow(new QStandardItem("one"));
QSignalSpy before_spy(&merged_, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)));
QSignalSpy after_spy(&merged_, SIGNAL(rowsRemoved(QModelIndex,int,int)));
source_.removeRow(0, QModelIndex());
ASSERT_EQ(1, before_spy.count());
ASSERT_EQ(1, after_spy.count());
EXPECT_FALSE(before_spy[0][0].value<QModelIndex>().isValid());
EXPECT_EQ(0, before_spy[0][1].toInt());
EXPECT_EQ(0, before_spy[0][2].toInt());
EXPECT_FALSE(after_spy[0][0].value<QModelIndex>().isValid());
EXPECT_EQ(0, after_spy[0][1].toInt());
EXPECT_EQ(0, after_spy[0][2].toInt());
}
TEST_F(MergedProxyModelTest, SubInsert) {
source_.appendRow(new QStandardItem("one"));
QStandardItemModel submodel;
merged_.AddSubModel(source_.index(0, 0, QModelIndex()), &submodel);
QSignalSpy before_spy(&merged_, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)));
QSignalSpy after_spy(&merged_, SIGNAL(rowsInserted(QModelIndex,int,int)));
submodel.appendRow(new QStandardItem("two"));
ASSERT_EQ(1, before_spy.count());
ASSERT_EQ(1, after_spy.count());
EXPECT_EQ("one", before_spy[0][0].value<QModelIndex>().data());
EXPECT_EQ(0, before_spy[0][1].toInt());
EXPECT_EQ(0, before_spy[0][2].toInt());
EXPECT_EQ("one", after_spy[0][0].value<QModelIndex>().data());
EXPECT_EQ(0, after_spy[0][1].toInt());
EXPECT_EQ(0, after_spy[0][2].toInt());
}
TEST_F(MergedProxyModelTest, SubRemove) {
source_.appendRow(new QStandardItem("one"));
QStandardItemModel submodel;
merged_.AddSubModel(source_.index(0, 0, QModelIndex()), &submodel);
submodel.appendRow(new QStandardItem("two"));
QSignalSpy before_spy(&merged_, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)));
QSignalSpy after_spy(&merged_, SIGNAL(rowsRemoved(QModelIndex,int,int)));
submodel.removeRow(0, QModelIndex());
ASSERT_EQ(1, before_spy.count());
ASSERT_EQ(1, after_spy.count());
EXPECT_EQ("one", before_spy[0][0].value<QModelIndex>().data());
EXPECT_EQ(0, before_spy[0][1].toInt());
EXPECT_EQ(0, before_spy[0][2].toInt());
EXPECT_EQ("one", after_spy[0][0].value<QModelIndex>().data());
EXPECT_EQ(0, after_spy[0][1].toInt());
EXPECT_EQ(0, after_spy[0][2].toInt());
}
|