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