on xenial help sign in
make the greeter's clock update in sync with indicators
commit4cd6ded1d5cda9f5483401ebd907a2cccef267ea authorMichael Zanetti <michael.zanetti@canonical.com> dateFri 11 Oct 2013, 14:14 (13 years ago) parentbfec158
3 files changed+40−35
Greeter/Clock.qml+31−12 tests/qmltests/CMakeLists.txt+1−1 tests/qmltests/Greeter/tst_Clock.qml+8−22
Greeter/Clock.qml+31−12
@@ -16,6 +16,7 @@
1616
1717import QtQuick 2.0
1818import Ubuntu.Components 0.1
19+import Unity.Indicators 0.1 as Indicators
1920
2021Item {
2122 id: clock
@@ -23,17 +24,35 @@ Item {
2324 implicitWidth: childrenRect.width
2425 implicitHeight: childrenRect.height
2526
26− property date __date
27− property alias __timerInterval: timer.interval
28− readonly property bool __timerRunning: timer.running
27+ // Allows to set the current Date. Will be overwritten if active
28+ property date currentDate
2929
30− Timer {
31− id: timer
32− interval: 1000 * 60
33− running: clock.enabled && clock.visible && clock.opacity != 0
34− repeat: true
35− triggeredOnStart: true
36− onTriggered: __date = new Date
30+ // If active, time will be updated through the indicators service
31+ property bool active: clock.enabled && clock.visible
32+
33+ Component.onCompleted: {
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+ }
3756 }
3857
3958 Column {
@@ -47,7 +66,7 @@ Item {
4766 font.pixelSize: units.gu(7.5)
4867 color: "white"
4968 opacity: 0.5
50− text: Qt.formatTime(__date)
69+ text: Qt.formatTime(clock.currentDate)
5170 font.weight: Font.Light
5271 }
5372
@@ -59,7 +78,7 @@ Item {
5978 fontSize: "medium"
6079 color: "white"
6180 opacity: 0.5
62− text: Qt.formatDate(__date, Qt.DefaultLocaleLongDate)
81+ text: Qt.formatDate(clock.currentDate, Qt.DefaultLocaleLongDate)
6382 font.weight: Font.Light
6483 }
6584 }
tests/qmltests/CMakeLists.txt+1−1
@@ -17,7 +17,7 @@ add_qml_test(Components MathLocal)
1717add_qml_test(Components OpenEffect)
1818add_qml_test(Components RatingStars)
1919add_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)
2121add_qml_test(Panel IndicatorItem)
2222add_qml_test(utils/Unity/Test UnityTest)
2323
tests/qmltests/Greeter/tst_Clock.qml+8−22
@@ -43,8 +43,7 @@ Rectangle {
4343 var dateString = Qt.formatDate(dateObj, Qt.DefaultLocaleLongDate)
4444 var timeString = Qt.formatTime(dateObj)
4545
46− clock.__timerInterval = 60000
47− clock.__date = dateObj
46+ clock.currentDate = dateObj
4847 var dateLabel = findChild(clock, "dateLabel")
4948 compare(dateLabel.text, dateString, "Not the expected date")
5049 var timeLabel = findChild(clock, "timeLabel")
@@ -57,33 +56,20 @@ Rectangle {
5756 var timeString = Qt.formatTime(dateObj)
5857
5958 clock.enabled = false
60− compare(clock.__timerRunning, false, "Timer should not be running")
61− clock.__date = dateObj
62− clock.__timerInterval = 5
63− wait(5) // spin event loop (only that would trigger the timer and reveal eventual bugs)
59+ var timeModel = findInvisibleChild(clock, "timeModel")
60+
61+ compare(timeModel.menuObjectPath, "", "Clock shouldn't be connected to Indicators when not active.")
62+
63+ clock.currentDate = dateObj
64+
6465 var dateLabel = findChild(clock, "dateLabel")
6566 compare(dateLabel.text, dateString, "Not the expected date")
6667 var timeLabel = findChild(clock, "timeLabel")
6768 compare(timeLabel.text, timeString, "Not the expected time")
6869
6970 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− }
7671
77− function test_timerRunning() {
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")
72+ verify(timeModel.menuObjectPath != "", "Should be connected to Indicators.")
8773 }
8874 }
8975}