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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* Copyright 2014 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
import QtTest 1.0
import Lomiri.Test 1.0
import Lomiri.Components 1.0
import Lomiri.Components.ListItems 1.0
Item {
width: units.gu(40)
height: units.gu(60)
ListModel {
id: dummyModel
Component.onCompleted: {
for (var i = 0; i < 20; ++i) {
dummyModel.append({idx: i});
}
}
}
LomiriListView {
id: lomiriListView
anchors { left: parent.left; top: parent.top; right: parent.right }
height: units.gu(20)
clip: true
model: dummyModel
delegate: Expandable {
id: expandable
objectName: "expandable" + index
expandedHeight: contentColumn.height
onClicked: lomiriListView.expandedIndex = index
Column {
id: contentColumn
anchors { left: parent.left; right: parent.right; top: parent.top }
Rectangle {
anchors { left: parent.left; right: parent.right}
id: collapsedRect
color: index % 2 == 0 ? "khaki" : "blue"
height: expandable.collapsedHeight
}
Rectangle {
anchors { left: parent.left; right: parent.right }
height: units.gu(40)
color: "orange"
}
}
}
}
// FIXME: Failing unit tests with Qt 5.6. See bug #1624343.
LomiriTestCase {
name: "LomiriListView"
when: windowShown
function initTestCase() {
tryCompare(dummyModel, "count", 20);
}
function init() {
waitForRendering(lomiriListView);
}
function expandItem(item) {
var index = lomiriListView.indexAt(item.x, item.y);
lomiriListView.expandedIndex = index;
var targetHeight = Math.min(item.expandedHeight, lomiriListView.height - item.collapsedHeight);
tryCompare(item, "height", targetHeight);
waitForRendering(lomiriListView)
}
function collapse() {
if (lomiriListView.expandedIndex == -1) {
return;
}
var expandedItem = findChild(lomiriListView, "expandable" + lomiriListView.expandedIndex);
lomiriListView.expandedIndex = -1;
tryCompare(expandedItem, "height", expandedItem.collapsedHeight);
waitForRendering(lomiriListView);
}
function test_expandedItem() {
var item = findChild(lomiriListView, "expandable1");
expandItem(item);
// expandedItem needs to point to the expanded item
compare(lomiriListView.expandedIndex, 1);
// item must be expanded now
compare(item.expanded, true);
collapse();
// expandedItem must be unset after collapsing
compare(lomiriListView.expandedIndex, -1);
}
function test_noScrollingNeeded() {
var item = findChild(lomiriListView, "expandable1");
fuzzyCompare(lomiriListView.mapFromItem(item, 0, 0).y, item.collapsedHeight, .5);
expandItem(item);
waitForRendering(lomiriListView);
fuzzyCompare(lomiriListView.mapFromItem(item, 0, 0).y, item.collapsedHeight, .5);
}
function test_scrollToTop() {
lomiriListView.height = units.gu(30);
lomiriListView.positionViewAtIndex(0, ListView.Beginning)
var item = findChild(lomiriListView, "expandable1");
fuzzyCompare(lomiriListView.mapFromItem(item, 0, 0).y, item.collapsedHeight, 1);
expandItem(item);
fuzzyCompare(lomiriListView.mapFromItem(item, 0, 0).y, 0, .5);
}
function test_scrollIntoView() {
var item = findChild(lomiriListView, "expandable9");
expandItem(item);
waitForRendering(lomiriListView);
// The item must be scrolled upwards, leaving space for one other item at the bottom
fuzzyCompare(lomiriListView.mapFromItem(item, 0, 0).y, lomiriListView.height - item.collapsedHeight - item.expandedHeight, 1);
}
function test_collapseByClickingOutside() {
// expand item 0
var item = findChild(lomiriListView, "expandable0");
expandItem(item);
// click on item 1
var item1 = findChild(lomiriListView, "expandable1");
mouseClick(item1, item1.width / 2, item1.height / 2);
// make sure item1 is collapsed
tryCompare(item, "expanded", false);
}
function test_dimOthers() {
var item = findChild(lomiriListView, "expandable1");
expandItem(item);
for (var i = 0; i < lomiriListView.contentItem.children.length; ++i) {
var childItem = lomiriListView.contentItem.children[i];
if (childItem.hasOwnProperty("expanded")) {
compare(childItem.opacity, childItem.expanded ? 1 : .5)
}
}
}
function test_destroyAndRecreateExpanded() {
var item = findChild(lomiriListView, "expandable1");
expandItem(item);
// scroll the list to the bottom
lomiriListView.currentIndex = 0;
lomiriListView.positionViewAtIndex(lomiriListView.count -1, ListView.End);
// make sure the item is eventually destroyed
tryCompareFunction(function() { return findChild(lomiriListView, "expandable1") == null;}, true)
// scroll the list back up
lomiriListView.positionViewAtIndex(0, ListView.Beginning)
// wait until the item is recreated.
tryCompareFunction(function() { return findChild(lomiriListView, "expandable1") != null; }, true);
item = findChild(lomiriListView, "expandable1");
compare(item.expanded, true);
}
function test_collapseOnClick() {
var item = findChild(lomiriListView, "expandable1");
item.collapseOnClick = true;
expandItem(item);
compare(lomiriListView.expandedIndex, 1);
mouseClick(item, item.width / 2, item.collapsedHeight / 2);
tryCompare(lomiriListView, "expandedIndex", -1);
// restore stuff we've changed
item.collapseOnClick = false;
}
function cleanup() {
// Restore listview height
lomiriListView.height = units.gu(60);
collapse();
// scroll the ListView back to top
lomiriListView.positionViewAtIndex(0, ListView.Beginning);
}
}
}
|