@@ -0,0 +1,293 @@
1+import QtQuick 2.0
2+import QtTest 1.0
3+import Ubuntu.Components 0.1
4+import "../../../Dash"
5+import "CardHelpers.js" as Helpers
6+
7+Rectangle {
8+ id: root
9+ width: units.gu(80)
10+ height: units.gu(72)
11+ color: "#88FFFFFF"
12+
13+ property string defaultLayout: '
14+ {
15+ "schema-version": 1,
16+ "template": {
17+ "category-layout": "grid",
18+ "card-layout": "vertical",
19+ "card-size": "medium",
20+ "overlay-mode": null,
21+ "collapsed-rows": 2
22+ },
23+ "components": {
24+ "title": null,
25+ "art": {
26+ "aspect-ratio": 1.0,
27+ "fill-mode": "crop"
28+ },
29+ "subtitle": null,
30+ "mascot": null,
31+ "emblem": null,
32+ "old-price": null,
33+ "price": null,
34+ "alt-price": null,
35+ "rating": {
36+ "type": "stars",
37+ "range": [0, 5],
38+ "full": "image://theme/rating-star-full",
39+ "half": "image://theme/rating-star-half",
40+ "empty": "image://theme/rating-star-empty"
41+ },
42+ "alt-rating": null,
43+ "summary": null
44+ },
45+ "resources": {}
46+ }'
47+
48+ property string cardData: '
49+ {
50+ "art": "../tests/qmltests/Dash/artwork/music-player-design.png",
51+ "mascot": "../tests/qmltests/Dash/artwork/avatar.png",
52+ "title": "foo",
53+ "subtitle": "bar",
54+ "summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
55+ }'
56+
57+ property string fullMapping: '
58+ {
59+ "title": "title",
60+ "art": "art",
61+ "subtitle": "subtitle",
62+ "mascot": "mascot",
63+ "summary": "summary"
64+ }'
65+
66+ property var cardsModel: [
67+ {
68+ "name": "Art, header, summary - vertical",
69+ "layout": { "components": JSON.parse(fullMapping) }
70+ },
71+ {
72+ "name": "Art, header, summary - vertical, small",
73+ "layout": { "template": { "card-size": "small" }, "components": JSON.parse(fullMapping) }
74+ },
75+ {
76+ "name": "Art, header, summary - vertical, large",
77+ "layout": { "template": { "card-size": "large" }, "components": JSON.parse(fullMapping) }
78+ },
79+ {
80+ "name": "Art, header, summary - vertical, wide",
81+ "layout": { "components": Helpers.update(JSON.parse(root.fullMapping), { "art": { "aspect-ratio": 2 } }) }
82+ },
83+ {
84+ "name": "Art, title - vertical, fitted",
85+ "layout": { "components": Helpers.update(JSON.parse(root.fullMapping), { "art": { "fill-mode": "fit" } }) }
86+ }
87+ ]
88+
89+ Card {
90+ id: card
91+ anchors { top: parent.top; left: parent.left; margins: units.gu(1) }
92+
93+ template: Helpers.update(JSON.parse(root.defaultLayout), Helpers.tryParse(layoutArea.text, layoutError))['template'];
94+ components: Helpers.update(JSON.parse(root.defaultLayout), Helpers.tryParse(layoutArea.text, layoutError))['components'];
95+ cardData: Helpers.mapData(dataArea.text, components, dataError)
96+ }
97+
98+ Rectangle {
99+ anchors { top: parent.top; bottom: parent.bottom; right: parent.right}
100+ width: units.gu(40)
101+ color: "lightgrey"
102+
103+ Column {
104+ anchors { fill: parent; margins: units.gu(1) }
105+ spacing: units.gu(1)
106+
107+ OptionSelector {
108+ id: selector
109+ model: cardsModel
110+ delegate: OptionSelectorDelegate { text: modelData.name }
111+ onSelectedIndexChanged: updateAreas()
112+ Component.onCompleted: updateAreas()
113+
114+ function updateAreas() {
115+ var element = cardsModel[selectedIndex];
116+ if (element) {
117+ layoutArea.text = JSON.stringify(element.layout, undefined, 2) || "{}";
118+ // FIXME: don't overwrite data
119+ var data = JSON.parse(root.cardData);
120+ Helpers.update(data, element.data);
121+ dataArea.text = JSON.stringify(data, undefined, 2) || "{}";
122+ } else {
123+ layoutArea.text = "";
124+ dataArea.text = "";
125+ }
126+
127+ }
128+ }
129+
130+ TextArea {
131+ id: layoutArea
132+ anchors { left: parent.left; right: parent.right }
133+ height: units.gu(25)
134+ }
135+
136+ Label {
137+ id: layoutError
138+ anchors { left: parent.left; right: parent.right }
139+ height: units.gu(4)
140+ color: "orange"
141+ }
142+
143+ TextArea {
144+ id: dataArea
145+ anchors { left: parent.left; right: parent.right }
146+ height: units.gu(25)
147+ }
148+
149+ Label {
150+ id: dataError
151+ anchors { left: parent.left; right: parent.right }
152+ height: units.gu(4)
153+ color: "orange"
154+ }
155+ }
156+ }
157+
158+ TestCase {
159+ id: testCase
160+
161+ when: windowShown
162+
163+ property Item header: findChild(card, "cardHeader")
164+ property Item art: findChild(card, "artShape")
165+ property Item artImage: findChild(card, "artImage")
166+ property Item summary: findChild(card, "summaryLabel")
167+
168+ // Find an object with the given name in the children tree of "obj"
169+ function findChild(obj,objectName) {
170+ var childs = new Array(0);
171+ childs.push(obj)
172+ while (childs.length > 0) {
173+ if (childs[0].objectName == objectName) {
174+ return childs[0]
175+ }
176+ for (var i in childs[0].children) {
177+ childs.push(childs[0].children[i])
178+ }
179+ childs.splice(0, 1);
180+ }
181+ return null;
182+ }
183+
184+ function initTestCase() {
185+ verify(testCase.header !== undefined, "Couldn't find header object.");
186+ }
187+
188+ function cleanup() {
189+ selector.selectedIndex = -1;
190+ }
191+
192+
193+ function test_header_binding_data() {
194+ return [
195+ { tag: "Mascot", property: "mascot", value: Qt.resolvedUrl("artwork/avatar.png"), index: 0 },
196+ { tag: "Title", property: "title", value: "foo", index: 0 },
197+ { tag: "Subtitle", property: "subtitle", value: "bar", index: 0 },
198+ ];
199+ }
200+
201+ function test_header_binding(data) {
202+ selector.selectedIndex = data.index;
203+ tryCompare(testCase.header, data.property, data.value);
204+ }
205+
206+ function test_card_binding_data() {
207+ return [
208+ { tag: "Art", object: artImage, property: "source", value: Qt.resolvedUrl("artwork/music-player-design.png"), index: 0 },
209+ { tag: "Summary", object: summary, property: "text", field: "summary", index: 0 },
210+ { tag: "Fit", object: art, fill: Image.PreserveAspectFit, index: 4 },
211+ ];
212+ }
213+
214+ function test_card_binding(data) {
215+ selector.selectedIndex = data.index;
216+
217+ if (data.hasOwnProperty('value')) {
218+ tryCompare(data.object, data.property, data.value);
219+ }
220+
221+ if (data.hasOwnProperty('field')) {
222+ tryCompare(data.object, data.property, card.cardData[data.field]);
223+ }
224+ }
225+
226+ function test_card_size_data() {
227+ return [
228+ { tag: "Medium", width: units.gu(18.5), index: 0 },
229+ { tag: "Small", width: units.gu(12), size: "small", index: 0 },
230+ { tag: "Large", width: units.gu(38), size: "large", index: 0 },
231+ { tag: "Wide", width: units.gu(18.5), aspect: 0.5, index: 0 },
232+ ]
233+ }
234+
235+ function test_card_size(data) {
236+ selector.selectedIndex = data.index;
237+
238+ if (data.hasOwnProperty("size")) {
239+ card.template['card-size'] = data.size;
240+ card.templateChanged();
241+ }
242+
243+ if (data.hasOwnProperty("aspect")) {
244+ card.components['art']['aspect-ratio'] = data.aspect;
245+ card.componentsChanged();
246+ }
247+
248+ if (data.hasOwnProperty("width")) {
249+ tryCompare(card, "width", data.width);
250+ }
251+
252+ if (data.hasOwnProperty("height")) {
253+ tryCompare(card, "height", data.height);
254+ }
255+ }
256+
257+ function test_art_size_data() {
258+ return [
259+ { tag: "Medium", width: units.gu(18.5), fill: Image.PreserveAspectCrop, index: 0 },
260+ { tag: "Small", width: units.gu(12), index: 1 },
261+ { tag: "Large", width: units.gu(38), index: 2 },
262+ { tag: "Wide", height: units.gu(9.25), index: 3 },
263+ { tag: "Fit", height: units.gu(18.5), width: units.gu(9.25), index: 4 },
264+ ]
265+ }
266+
267+ function test_art_size(data) {
268+ selector.selectedIndex = data.index;
269+
270+ if (data.hasOwnProperty("size")) {
271+ card.template['card-size'] = data.size;
272+ card.templateChanged();
273+ }
274+
275+ if (data.hasOwnProperty("aspect")) {
276+ card.components['art']['aspect-ratio'] = data.aspect;
277+ card.componentsChanged();
278+ }
279+
280+ if (data.hasOwnProperty("width")) {
281+ tryCompare(art, "width", data.width);
282+ }
283+
284+ if (data.hasOwnProperty("height")) {
285+ tryCompare(art, "height", data.height);
286+ }
287+
288+ if (data.hasOwnProperty("fill")) {
289+ tryCompare(artImage, "fillMode", data.fill);
290+ }
291+ }
292+ }
293+}