on xenial help sign in
Use PreviewSingleton for PreviewRatingInput and PreviewCommentInput (LP: #1595235)

Approved by: Albert Astals Cid

commit478e4d1a8b83915f08f4483a58c3fde55d5504d3 authorAndrea Cimitan <andrea.cimitan@gmail.com> dateThu 22 Sep 2016, 07:32 (10 years ago) parent385fda68562118
5 files changed+102−3
qml/Dash/Previews/PreviewCommentInput.qml+15 qml/Dash/Previews/PreviewRatingInput.qml+27−1 tests/qmltests/Dash/Previews/tst_PreviewCommentInput.qml+28 tests/qmltests/Dash/Previews/tst_PreviewInlineVideo.qml−2 tests/qmltests/Dash/Previews/tst_PreviewRatingInput.qml+32
qml/Dash/Previews/PreviewCommentInput.qml+15
@@ -17,6 +17,7 @@
1717import QtQuick 2.4
1818import Ubuntu.Components 1.3
1919import "../../Components"
20+import "PreviewSingleton"
2021
2122/*! \brief Preview widget for commenting.
2223
@@ -35,6 +36,20 @@ PreviewWidget {
3536 triggered(root.widgetId, "commented", data);
3637 }
3738
39+ property alias commentText: commentTextArea.text
40+
41+ onCommentTextChanged: storeCommentState()
42+ onWidgetIdChanged: restoreCommentState()
43+
44+ function storeCommentState() {
45+ PreviewSingleton.widgetExtraData[widgetId] = commentText;
46+ }
47+
48+ function restoreCommentState() {
49+ if (!PreviewSingleton.widgetExtraData[widgetId]) return;
50+ if (PreviewSingleton.widgetExtraData[widgetId] != "") commentText = PreviewSingleton.widgetExtraData[widgetId];
51+ }
52+
3853 TextArea {
3954 id: commentTextArea
4055 objectName: "commentTextArea"
qml/Dash/Previews/PreviewRatingInput.qml+27−1
@@ -17,6 +17,7 @@
1717import QtQuick 2.4
1818import Ubuntu.Components 1.3
1919import "../../Components"
20+import "PreviewSingleton"
2021
2122/*! \brief Preview widget for rating.
2223
@@ -53,6 +54,31 @@ PreviewWidget {
5354 property alias ratingValue: rating.value
5455 property alias reviewText: reviewTextArea.text
5556
57+ onRatingValueChanged: storeRatingState()
58+ onReviewTextChanged: storeTextState()
59+ onWidgetIdChanged: restoreReviewState()
60+
61+ function initializeWidgetExtraData() {
62+ if (typeof(PreviewSingleton.widgetExtraData[widgetId]) == "undefined") PreviewSingleton.widgetExtraData[widgetId] = [];
63+ }
64+
65+ function storeRatingState() {
66+ initializeWidgetExtraData();
67+ PreviewSingleton.widgetExtraData[widgetId][0] = ratingValue;
68+ }
69+
70+ function storeTextState() {
71+ initializeWidgetExtraData();
72+ PreviewSingleton.widgetExtraData[widgetId][1] = reviewText;
73+ }
74+
75+ function restoreReviewState() {
76+ if (!PreviewSingleton.widgetExtraData[widgetId]) return;
77+ if (PreviewSingleton.widgetExtraData[widgetId][0] > 0) ratingValue = PreviewSingleton.widgetExtraData[widgetId][0];
78+ if (typeof(PreviewSingleton.widgetExtraData[widgetId][1]) != "undefined" &&
79+ PreviewSingleton.widgetExtraData[widgetId][1] != "") reviewText = PreviewSingleton.widgetExtraData[widgetId][1];
80+ }
81+
5682 function submit() {
5783 // checks rating-input requirements
5884 if (((widgetData["required"] === "both" ||
@@ -89,7 +115,7 @@ PreviewWidget {
89115 size: 5
90116 height: units.gu(4)
91117 onValueChanged: {
92− if (widgetData["visible"] === "rating") root.submit();
118+ if (widgetData && widgetData["visible"] === "rating") root.submit();
93119 }
94120
95121 property var urlIconEmpty: widgetData["rating-icon-empty"] || "image://theme/non-starred"
tests/qmltests/Dash/Previews/tst_PreviewCommentInput.qml+28
@@ -39,6 +39,21 @@ Rectangle {
3939 widgetId: "previewCommentInput"
4040 }
4141
42+ Loader {
43+ id: previewCommentInputLoader
44+ anchors {
45+ left: parent.left
46+ right: parent.right
47+ top: previewCommentInput.bottom
48+ topMargin: units.gu(2)
49+ }
50+
51+ sourceComponent: PreviewCommentInput {
52+ widgetData: widgetDataNewLabels
53+ widgetId: "previewCommentInputLoader"
54+ }
55+ }
56+
4257 SignalSpy {
4358 id: spy
4459 signalName: "triggered"
@@ -81,5 +96,18 @@ Rectangle {
8196 compare(args[2]["comment"], data.inputText);
8297 }
8398 }
99+
100+ function test_singleton() {
101+ var commentTextAreaLoader = findChild(previewCommentInputLoader, "commentTextArea")
102+
103+ commentTextAreaLoader.text = "singleton test text";
104+
105+ // Simulate widget being destroyed and recreated
106+ previewCommentInputLoader.active = false;
107+ previewCommentInputLoader.active = true;
108+
109+ commentTextAreaLoader = findChild(previewCommentInputLoader, "commentTextArea")
110+ tryCompare(commentTextAreaLoader, "text", "singleton test text");
111+ }
84112 }
85113}
tests/qmltests/Dash/Previews/tst_PreviewInlineVideo.qml−2
@@ -87,14 +87,12 @@ Rectangle {
8787 }
8888
8989 sourceComponent: PreviewInlineVideo {
90− width: parent.width
9190 widgetData: widgetData0
9291
9392 rootItem: inner
9493 }
9594 }
9695 }
97−
9896 }
9997
10098 Rectangle {
tests/qmltests/Dash/Previews/tst_PreviewRatingInput.qml+32
@@ -53,6 +53,21 @@ Rectangle {
5353 widgetId: "previewRatingInput"
5454 }
5555
56+ Loader {
57+ id: previewRatingInputLoader
58+ anchors {
59+ left: parent.left
60+ right: parent.right
61+ top: previewRatingInput.bottom
62+ topMargin: units.gu(2)
63+ }
64+
65+ sourceComponent: PreviewRatingInput {
66+ widgetData: widgetDataBoth
67+ widgetId: "previewRatingInputLoader"
68+ }
69+ }
70+
5671 SignalSpy {
5772 id: spy
5873 signalName: "triggered"
@@ -210,5 +225,22 @@ Rectangle {
210225 compare(args[2]["review"], data.triggeredData["review"]);
211226 verify(args[2]["author"] !== undefined); // Just verifying it exists now
212227 }
228+
229+ function test_singleton() {
230+ var ratingLoader = findChild(previewRatingInputLoader, "rating");
231+ var reviewTextAreaLoader = findChild(previewRatingInputLoader, "reviewTextArea");
232+
233+ ratingLoader.value = 3;
234+ reviewTextAreaLoader.text = "singleton test text";
235+
236+ // Simulate widget being destroyed and recreated
237+ previewRatingInputLoader.active = false;
238+ previewRatingInputLoader.active = true;
239+
240+ ratingLoader = findChild(previewRatingInputLoader, "rating");
241+ reviewTextAreaLoader = findChild(previewRatingInputLoader, "reviewTextArea");
242+ tryCompare(ratingLoader, "value", 3);
243+ tryCompare(reviewTextAreaLoader, "text", "singleton test text");
244+ }
213245 }
214246}