on xenial help sign in
Tests
commit56f8ab53d6446375a40ba3224a44ce1517e97a50 authorAndrea Cimitan <andrea.cimitan@gmail.com> dateThu 13 Feb 2014, 17:00 (13 years ago) parent3e4f357
11 files changed+249−25
qml/Components/Rating.qml+1−1 qml/Components/RatingStyle.qml+17−1 qml/Components/graphics/icon_star_empty@20.png qml/Components/graphics/icon_star_full@20.png qml/Components/graphics/icon_star_half@20.png qml/Components/graphics/icon_star_off@20.png qml/Components/graphics/icon_star_on@20.png qml/Dash/Previews/PreviewRating.qml+82−17 tests/qmltests/CMakeLists.txt−1 tests/qmltests/Components/tst_Rating.qml+3−3 tests/qmltests/Dash/Previews/tst_PreviewRating.qml+146−2
qml/Components/Rating.qml+1−1
@@ -34,7 +34,7 @@ StyledItem {
3434 //! Current rating.
3535 property real value: -1
3636
37− readonly property int effectiveValue: MathUtils.clamp(size * value / maximumValue, 0, maximumValue)
37+ readonly property real effectiveValue: MathUtils.clamp(size * value / maximumValue, 0, maximumValue)
3838
3939 style: RatingStyle {}
4040
qml/Components/RatingStyle.qml+17−1
@@ -21,11 +21,27 @@ Row {
2121 anchors.fill: parent
2222
2323 Repeater {
24+ id: repeater
2425 model: styledItem.size
2526
27+ property int indexHalfValue: {
28+ var integerPart = Math.floor(styledItem.effectiveValue);
29+ var fractionalPart = styledItem.effectiveValue - integerPart;
30+
31+ if (fractionalPart < 0.5) return -1;
32+ else return integerPart;
33+ }
34+ property url urlIconEmpty: styledItem.urlIconEmpty || "graphics/icon_star_empty.png"
35+ property url urlIconFull: styledItem.urlIconFull || "graphics/icon_star_full.png"
36+ property url urlIconHalf: styledItem.urlIconHalf || "graphics/icon_star_half.png"
37+
2638 Image {
2739 opacity: styledItem.value < 0 ? 0.4 : 1 // Let's make the stars look inactive for a not-set value
28− source: index < styledItem.effectiveValue ? "graphics/icon_star_on.png" : "graphics/icon_star_off.png"
40+ source: {
41+ if (index === repeater.indexHalfValue) return repeater.urlIconHalf;
42+ else if (index < styledItem.effectiveValue) return repeater.urlIconFull;
43+ else return repeater.urlIconEmpty;
44+ }
2945 }
3046 }
3147}
qml/Components/graphics/icon_star_empty@20.pngnew file

Binary file not shown.

qml/Components/graphics/icon_star_full@20.pngnew file

Binary file not shown.

qml/Components/graphics/icon_star_half@20.pngnew file

Binary file not shown.

qml/Components/graphics/icon_star_off@20.pngdeleted

Binary file not shown.

qml/Components/graphics/icon_star_on@20.pngdeleted

Binary file not shown.

qml/Dash/Previews/PreviewRating.qml+82−17
@@ -25,15 +25,15 @@ PreviewWidget {
2525
2626 function submit() {
2727 // checks rating-input requirements
28− if ((widgetData["required"] === "both" ||
29− widgetData["required"] === "rating") &&
30− rating.value < 0) return;
31− if ((widgetData["required"] === "both" ||
32− widgetData["required"] === "review") &&
33− reviewTextArea.text === "") return;
34−
35− var data = [{"rating": rating.value, "review": reviewTextArea.text, "author": null}]
36− triggered(root.widgetId, null, data)
28+ if (((widgetData["required"] === "both" ||
29+ widgetData["required"] === "rating") &&
30+ rating.value < 0) ||
31+ ((widgetData["required"] === "both" ||
32+ widgetData["required"] === "review") &&
33+ reviewField.text === "")) return;
34+
35+ var data = {"rating": rating.value, "review": reviewField.text, "author": null};
36+ triggered(root.widgetId, widgetData["required"], data);
3737 }
3838
3939 Item {
@@ -41,41 +41,106 @@ PreviewWidget {
4141 anchors {
4242 left: parent.left
4343 right: parent.right
44− margins: units.gu(1)
4544 }
4645 height: childrenRect.height
4746 visible: widgetData["visible"] !== "review"
4847
4948 Label {
49+ objectName: "ratingLabel"
5050 anchors {
51− top: parent.top
51+ verticalCenter: parent.verticalCenter
5252 left: parent.left
5353 }
5454 color: Theme.palette.selected.backgroundText
5555 opacity: .8
56− text: widgetData["review-label"]
56+ text: widgetData["rating-label"] || i18n.tr("Rate this")
5757 }
5858
5959 Rating {
6060 id: rating
61+ objectName: "rating"
6162 anchors {
62− top: parent.top
63+ verticalCenter: parent.verticalCenter
6364 right: parent.right
6465 }
6566 size: 5
66− onValueChanged: submit()
67+ onValueChanged: {
68+ if (widgetData["visible"] === "rating") root.submit();
69+ }
70+
71+ property var urlIconEmpty: widgetData["rating-icon-empty"]
72+ property var urlIconFull: widgetData["rating-icon-full"]
6773 }
6874 }
6975
70− TextArea {
71− id: reviewTextArea
76+ Item {
77+ id: reviewContainer
78+
79+ readonly property real innerMargin: units.gu(1)
80+
7281 anchors {
7382 left: parent.left
7483 right: parent.right
7584 top: ratingLabelAndWidgetContainer.visible ? ratingLabelAndWidgetContainer.bottom : parent.top
7685 bottom: parent.bottom
86+ topMargin: ratingLabelAndWidgetContainer.visible ? reviewContainer.innerMargin : 0
7787 }
7888 visible: widgetData["visible"] !== "rating"
79− color: Theme.palette.selected.backgroundText
89+
90+ Label {
91+ objectName: "reviewLabel"
92+ id: reviewLabel
93+ anchors {
94+ top: parent.top
95+ left: parent.left
96+ right: parent.right
97+ }
98+ color: Theme.palette.selected.backgroundText
99+ opacity: .8
100+ text: widgetData["review-label"] || i18n.tr("Add a review")
101+ }
102+
103+ Item {
104+ anchors {
105+ top: reviewLabel.bottom
106+ left: parent.left
107+ right: parent.right
108+ topMargin: reviewContainer.innerMargin
109+ }
110+ implicitHeight: childrenRect.height
111+
112+ TextField {
113+ id: reviewField
114+ objectName: "reviewField"
115+ anchors {
116+ top: parent.top
117+ bottom: parent.bottom
118+ left: parent.left
119+ right: submitButton.left
120+ rightMargin: reviewContainer.innerMargin
121+ }
122+ }
123+
124+ Button {
125+ id: submitButton
126+ objectName: "submitButton"
127+
128+ readonly property bool readyToSubmit: {
129+ if (reviewField.text === "" ||
130+ (widgetData["visible"] === "both" && rating.value < 0)) return false;
131+ else return true;
132+ }
133+
134+ anchors {
135+ top: parent.top
136+ right: parent.right
137+ }
138+ color: readyToSubmit ? Theme.palette.selected.base : Theme.palette.normal.base
139+ text: widgetData["submit-label"] || i18n.tr("Send")
140+ onClicked: {
141+ if (readyToSubmit) root.submit()
142+ }
143+ }
144+ }
80145 }
81146}
tests/qmltests/CMakeLists.txt−1
@@ -60,7 +60,6 @@ add_qml_test(Dash/Previews PreviewActions IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_
6060add_qml_test(Dash/Previews PreviewAudioPlayback IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/tests/mocks)
6161add_qml_test(Dash/Previews PreviewHeader IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/tests/mocks)
6262add_qml_test(Dash/Previews PreviewImageGallery IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/tests/mocks)
63−add_qml_test(Dash/Previews PreviewHeader IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/tests/mocks)
6463add_qml_test(Dash/Previews PreviewProgress IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/tests/mocks)
6564add_qml_test(Dash/Previews PreviewRating IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/tests/mocks)
6665add_qml_test(Dash/Previews PreviewTextSummary IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/tests/mocks)
tests/qmltests/Components/tst_Rating.qml+3−3
@@ -24,7 +24,7 @@ Rectangle {
2424 id: root
2525 width: units.gu(40)
2626 height: units.gu(10)
27− color: "black"
27+ color: Theme.palette.selected.background
2828
2929 Rating {
3030 id: rating
@@ -73,13 +73,13 @@ Rectangle {
7373 {tag: "ranged value", value: 2, expectedValue: 2},
7474 {tag: "big", value: 200, expectedValue: rating.maximumValue},
7575 {tag: "min", value: 0, expectedValue: 0},
76− {tag: "max", value: rating.maximumValue, expectedValue: rating.maximumValue}
76+ {tag: "max", value: rating.maximumValue, expectedValue: rating.maximumValue},
77+ {tag: "half", value: 2.5, expectedValue: 2.5}
7778 ];
7879 }
7980
8081 function test_effectiveValue(data) {
8182 rating.value = data.value;
82− wait(1000)
8383 compare(rating.effectiveValue, data.expectedValue, "effectiveValue not calculated correctly")
8484 }
8585 }
tests/qmltests/Dash/Previews/tst_PreviewRating.qml+146−2
@@ -24,15 +24,159 @@ Rectangle {
2424 id: root
2525 width: units.gu(40)
2626 height: units.gu(80)
27− color: "black" //Theme.palette.selected.background
27+ color: Theme.palette.selected.background
28+
29+ property real validInputRating: 1
30+ property real invalidInputRating: -1
31+ property string validInputText: "Great!"
32+ property string invalidInputText: ""
33+
34+ property var triggeredDataBoth: { "rating": validInputRating, "review": validInputText, "author": null }
35+ property var triggeredDataRatingOnly: { "rating": validInputRating, "review": invalidInputText, "author": null }
36+ property var triggeredDataReviewOnly: { "rating": invalidInputRating, "review": validInputText, "author": null }
37+
38+ property var widgetDataBoth: { "visible": "both", "required": "both" }
39+ property var widgetDataRating: { "visible": "rating", "required": "rating" }
40+ property var widgetDataRatingBroken: { "visible": "rating", "required": "review" }
41+ property var widgetDataReview: { "visible": "review", "required": "review" }
42+ property var widgetDataReviewBroken: { "visible": "review", "required": "rating" }
43+
44+ property var widgetDataNewLabels: { "visible": "both", "required": "both", "rating-label": "TestRatingLabel", "review-label": "TestReviewLabel", "submit-label": "TestSubmitLabel" }
2845
2946 PreviewRating {
3047 id: previewRating
3148 anchors.fill: parent
49+ widgetData: widgetDataBoth
50+ widgetId: "previewRating"
51+ }
52+
53+ SignalSpy {
54+ id: spy
55+ signalName: "triggered"
3256 }
3357
3458 UT.UnityTestCase {
35− name: "PreviewTextSummaryTest"
59+ name: "PreviewRatingTest"
3660 when: windowShown
61+
62+ property var rating: findChild(previewRating, "rating")
63+ property var reviewField: findChild(previewRating, "reviewField")
64+ property var submitButton: findChild(previewRating, "submitButton")
65+
66+ function init() {
67+ rating.value = -1;
68+ reviewField.text = "";
69+ }
70+
71+ function test_labels() {
72+ var ratingLabel = findChild(previewRating, "ratingLabel");
73+ var reviewLabel = findChild(previewRating, "reviewLabel");
74+
75+ previewRating.widgetData = widgetDataNewLabels;
76+ compare(ratingLabel.text, widgetDataNewLabels["rating-label"]);
77+ compare(reviewLabel.text, widgetDataNewLabels["review-label"]);
78+ compare(submitButton.text, widgetDataNewLabels["submit-label"]);
79+ }
80+
81+ function test_submit_and_visibility_data() {
82+ return [
83+ {tag: "both, null review", widgetData: widgetDataBoth, inputRating: invalidInputRating, inputText: invalidInputText, emitted: false},
84+ {tag: "both, rating-only review", widgetData: widgetDataBoth, inputRating: validInputRating, inputText: invalidInputText, emitted: false},
85+ {tag: "both, review-only review", widgetData: widgetDataBoth, inputRating: invalidInputRating, inputText: validInputText, emitted: false},
86+ {tag: "both, complete review", widgetData: widgetDataBoth, inputRating: validInputRating, inputText: validInputText, emitted: true},
87+ {tag: "rating, null review", widgetData: widgetDataRating, inputRating: invalidInputRating, inputText: invalidInputText, emitted: false},
88+ {tag: "rating, rating-only review", widgetData: widgetDataRating, inputRating: validInputRating, inputText: invalidInputText, emitted: true},
89+ {tag: "rating, review-only review", widgetData: widgetDataRating, inputRating: invalidInputRating, inputText: validInputText, emitted: false},
90+ {tag: "rating, complete review", widgetData: widgetDataRating, inputRating: validInputRating, inputText: validInputText, emitted: true},
91+ {tag: "rating broken, null review", widgetData: widgetDataRatingBroken, inputRating: invalidInputRating, inputText: invalidInputText, emitted: false},
92+ {tag: "rating broken, rating-only review", widgetData: widgetDataRatingBroken, inputRating: validInputRating, inputText: invalidInputText, emitted: false},
93+ {tag: "rating broken, review-only review", widgetData: widgetDataRatingBroken, inputRating: invalidInputRating, inputText: validInputText, emitted: false},
94+ {tag: "rating broken, complete review", widgetData: widgetDataRatingBroken, inputRating: validInputRating, inputText: validInputText, emitted: false},
95+ {tag: "review, null review", widgetData: widgetDataReview, inputRating: invalidInputRating, inputText: invalidInputText, emitted: false},
96+ {tag: "review, rating-only review", widgetData: widgetDataReview, inputRating: validInputRating, inputText: invalidInputText, emitted: false},
97+ {tag: "review, review-only review", widgetData: widgetDataReview, inputRating: invalidInputRating, inputText: validInputText, emitted: true},
98+ {tag: "review, complete review", widgetData: widgetDataReview, inputRating: validInputRating, inputText: validInputText, emitted: true},
99+ {tag: "review broken, null review", widgetData: widgetDataReviewBroken, inputRating: invalidInputRating, inputText: invalidInputText, emitted: false},
100+ {tag: "review broken, rating-only review", widgetData: widgetDataReviewBroken, inputRating: validInputRating, inputText: invalidInputText, emitted: false},
101+ {tag: "review broken, review-only review", widgetData: widgetDataReviewBroken, inputRating: invalidInputRating, inputText: validInputText, emitted: false},
102+ {tag: "review broken, complete review", widgetData: widgetDataReviewBroken, inputRating: validInputRating, inputText: validInputText, emitted: false},
103+ ];
104+ }
105+
106+ function test_submit_and_visibility(data) {
107+ spy.clear();
108+ spy.target = previewRating;
109+
110+ previewRating.widgetData = data.widgetData;
111+
112+ if (data.widgetData["visible"] !== "review") {
113+ verify(rating.visible === true);
114+
115+ rating.value = data.inputRating;
116+ if (data.widgetData["required"] !== "rating" || data.inputRating < 0) {
117+ compare(spy.count, 0);
118+ } else {
119+ compare(spy.count, 1);
120+ }
121+ } else {
122+ verify(rating.visible === false);
123+ }
124+
125+ if (data.widgetData["visible"] !== "rating") {
126+ verify(reviewField.visible === true);
127+
128+ reviewField.text = data.inputText;
129+ mouseClick(submitButton, submitButton.width / 2, submitButton.height / 2);
130+ switch (data.widgetData["required"]) {
131+ case "rating": compare(spy.count, 0); break;
132+ case "both":
133+ default: {
134+ if (data.inputRating < 0 || data.inputText === "") {
135+ compare(spy.count, 0); break;
136+ }
137+ }
138+ case "review": {
139+ if (data.inputText === "") {
140+ compare(spy.count, 0); break;
141+ } else {
142+ compare(spy.count, 1); break;
143+ }
144+ }
145+ }
146+ } else {
147+ verify(reviewField.visible === false);
148+ }
149+
150+ compare(spy.count === 1, data.emitted);
151+ }
152+
153+ function test_triggered_data() {
154+ return [
155+ {tag: "complete review", widgetData: widgetDataBoth, inputRating: validInputRating, inputText: validInputText, triggeredData: triggeredDataBoth},
156+ {tag: "rating-only review", widgetData: widgetDataRating, inputRating: validInputRating, inputText: invalidInputText, triggeredData: triggeredDataRatingOnly},
157+ {tag: "review-only review", widgetData: widgetDataReview, inputRating: invalidInputRating, inputText: validInputText, triggeredData: triggeredDataReviewOnly}
158+ ];
159+ }
160+
161+ function test_triggered(data) {
162+ spy.clear();
163+ spy.target = previewRating;
164+
165+ previewRating.widgetData = data.widgetData;
166+
167+ if (data.inputRating > 0) rating.value = data.inputRating;
168+ if (data.inputText !== "") {
169+ reviewField.text = data.inputText;
170+ mouseClick(submitButton, submitButton.width / 2, submitButton.height / 2);
171+ }
172+
173+ compare(spy.count, 1);
174+ var args = spy.signalArguments[0];
175+ compare(args[0], previewRating.widgetId);
176+ compare(args[1], data.widgetData["required"]);
177+ compare(args[2]["rating"], data.triggeredData["rating"]);
178+ compare(args[2]["review"], data.triggeredData["review"]);
179+ verify(args[2]["author"]); // Just verifying it exists now
180+ }
37181 }
38182}