on xenial help sign in
Make the greeter's clock update in sync with the indicators.
0155022Michael Zanetti
Greeter/Clock.qml+31−12
@@ -16,6 +16,7 @@
16
16
17import QtQuick 2.0
17import QtQuick 2.0
18import Ubuntu.Components 0.1
18import Ubuntu.Components 0.1
19+import Unity.Indicators 0.1 as Indicators
19
20
20Item {
21Item {
21 id: clock
22 id: clock
@@ -23,17 +24,35 @@ Item {
23 implicitWidth: childrenRect.width
24 implicitWidth: childrenRect.width
24 implicitHeight: childrenRect.height
25 implicitHeight: childrenRect.height
25
26
27+ // Allows to set the current Date. Will be overwritten if active
26− property date __date
28+ property date currentDate
27− property alias __timerInterval: timer.interval
28− readonly property bool __timerRunning: timer.running
29
29
30− Timer {
30+ // If active, time will be updated through the indicators service
31− id: timer
32− interval: 1000 * 60
33− running: clock.enabled && clock.visible && clock.opacity != 0
31+ property bool active: clock.enabled && clock.visible
34− repeat: true
32+
35− triggeredOnStart: true
33+ Component.onCompleted: {
36− onTriggered: __date = new Date
34+ if (active) {
35+ currentDate = new Date()
36+ }
37+ }
38+
39+ Indicators.CachedUnityMenuModel {
40+ id: timeModel
41+ objectName: "timeModel"
42+
43+ busName: "com.canonical.indicator.datetime"
44+ actionsObjectPath: "/com/canonical/indicator/datetime"
45+ menuObjectPath: clock.active ? "/com/canonical/indicator/datetime/phone" : ""
46+
47+ Indicators.RootActionState {
48+ menu: timeModel.model
49+ onUpdated: {
50+ if (timeLabel.text != rightLabel) {
51+ timeLabel.text = rightLabel;
52+ clock.currentDate = new Date();
53+ }
54+ }
55+ }
37 }
56 }
38
57
39 Column {
58 Column {
@@ -47,7 +66,7 @@ Item {
47 font.pixelSize: units.gu(7.5)
66 font.pixelSize: units.gu(7.5)
48 color: "white"
67 color: "white"
49 opacity: 0.5
68 opacity: 0.5
50− text: Qt.formatTime(__date)
69+ text: Qt.formatTime(clock.currentDate)
51 font.weight: Font.Light
70 font.weight: Font.Light
52 }
71 }
53
72
@@ -59,7 +78,7 @@ Item {
59 fontSize: "medium"
78 fontSize: "medium"
60 color: "white"
79 color: "white"
61 opacity: 0.5
80 opacity: 0.5
62− text: Qt.formatDate(__date, Qt.DefaultLocaleLongDate)
81+ text: Qt.formatDate(clock.currentDate, Qt.DefaultLocaleLongDate)
63 font.weight: Font.Light
82 font.weight: Font.Light
64 }
83 }
65 }
84 }
tests/qmltests/CMakeLists.txt+1−1
@@ -17,7 +17,7 @@ add_qml_test(Components MathLocal)
17add_qml_test(Components OpenEffect)
17add_qml_test(Components OpenEffect)
18add_qml_test(Components RatingStars)
18add_qml_test(Components RatingStars)
19add_qml_test(Components TimeLocal)
19add_qml_test(Components TimeLocal)
20−add_qml_test(Greeter Clock)
20+add_qml_test(Greeter Clock IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/plugins)
21add_qml_test(Panel IndicatorItem)
21add_qml_test(Panel IndicatorItem)
22add_qml_test(utils/Unity/Test UnityTest)
22add_qml_test(utils/Unity/Test UnityTest)
23
23
tests/qmltests/Greeter/tst_Clock.qml+8−22
@@ -43,8 +43,7 @@ Rectangle {
43 var dateString = Qt.formatDate(dateObj, Qt.DefaultLocaleLongDate)
43 var dateString = Qt.formatDate(dateObj, Qt.DefaultLocaleLongDate)
44 var timeString = Qt.formatTime(dateObj)
44 var timeString = Qt.formatTime(dateObj)
45
45
46− clock.__timerInterval = 60000
46+ clock.currentDate = dateObj
47− clock.__date = dateObj
48 var dateLabel = findChild(clock, "dateLabel")
47 var dateLabel = findChild(clock, "dateLabel")
49 compare(dateLabel.text, dateString, "Not the expected date")
48 compare(dateLabel.text, dateString, "Not the expected date")
50 var timeLabel = findChild(clock, "timeLabel")
49 var timeLabel = findChild(clock, "timeLabel")
@@ -57,33 +56,20 @@ Rectangle {
57 var timeString = Qt.formatTime(dateObj)
56 var timeString = Qt.formatTime(dateObj)
58
57
59 clock.enabled = false
58 clock.enabled = false
60− compare(clock.__timerRunning, false, "Timer should not be running")
59+ var timeModel = findInvisibleChild(clock, "timeModel")
60+
61+ compare(timeModel.menuObjectPath, "", "Clock shouldn't be connected to Indicators when not active.")
62+
61− clock.__date = dateObj
63+ clock.currentDate = dateObj
62− clock.__timerInterval = 5
64+
63− wait(5) // spin event loop (only that would trigger the timer and reveal eventual bugs)
64 var dateLabel = findChild(clock, "dateLabel")
65 var dateLabel = findChild(clock, "dateLabel")
65 compare(dateLabel.text, dateString, "Not the expected date")
66 compare(dateLabel.text, dateString, "Not the expected date")
66 var timeLabel = findChild(clock, "timeLabel")
67 var timeLabel = findChild(clock, "timeLabel")
67 compare(timeLabel.text, timeString, "Not the expected time")
68 compare(timeLabel.text, timeString, "Not the expected time")
68
69
69 clock.enabled = true
70 clock.enabled = true
70− compare(clock.__timerRunning, true, "Timer should be running")
71− wait(0) // spin event loop to trigger the timer
72− verify(dateLabel.text !== dateString)
73− if (timeLabel.text.indexOf("11:13") != -1) wait(60000) // next test will fail at 11:13, wait 1 minute
74− verify(timeLabel.text !== timeString)
75− }
76
71
77− function test_timerRunning() {
72+ verify(timeModel.menuObjectPath != "", "Should be connected to Indicators.")
78− // tests for clock.enabled property are already in test_dateUpdate()
79− clock.opacity = 0.0
80− compare(clock.__timerRunning, false, "Timer should not be running")
81− clock.opacity = 1.0
82− compare(clock.__timerRunning, true, "Timer should be running")
83− clock.visible = false
84− compare(clock.__timerRunning, false, "Timer should not be running")
85− clock.visible = true
86− compare(clock.__timerRunning, true, "Timer should be running")
87 }
73 }
88 }
74 }
89}
75}