2
* Copyright 2013 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
import UserMetrics 0.1
21
import UserMetricsTest 0.1
25
property string originalName: "test"
26
property string originalFormat: "Test Metric"
27
property string originalEmptyFormat: "Nope, no data"
28
property string originalDomain: "test-domain"
33
format: originalFormat
34
emptyFormat: originalEmptyFormat
35
domain: originalDomain
38
function test_metric() {
39
// Make sure the metric properties have been set properly
40
var info = dbusQuery.queryMetricInfo(1);
41
compare(info.name, originalName, "Metric name was not set properly");
42
compare(info.format, originalFormat, "Metric format was not set properly");
43
compare(info.emptyFormat, originalEmptyFormat, "Metric emptyFormat was not set properly");
44
compare(info.domain, originalDomain, "Metric domain was not set properly");
46
// Test update and increment with integers and floating point data
48
compare(dbusQuery.queryCurrentValue(1), 0, "Data not updated successfully")
50
compare(dbusQuery.queryCurrentValue(1), 1, "Data not incremented successfully")
51
metric.increment(7776);
52
compare(dbusQuery.queryCurrentValue(1), 7777, "Data not incremented successfully")
53
metric.increment(0.999);
54
compare(dbusQuery.queryCurrentValue(1), 7777.999, "Data not incremented successfully")
56
compare(dbusQuery.queryCurrentValue(1), 5.5, "Data not updated successfully")
58
// Check that when changing the metric format or emptyFormat nothing else changes and
59
// no new metrics are created
60
var newFormat = "Something else";
61
var newEmptyFormat = "Still no data";
62
var newDomain = "test-new-domain";
63
metric.format = newFormat;
64
metric.emptyFormat = newEmptyFormat;
65
metric.domain = newDomain;
66
info = dbusQuery.queryMetricInfo(1);
67
compare(info.name, originalName, "Metric name was changed without requesting it");
68
compare(info.format, newFormat, "Metric format was not changed properly");
69
compare(info.emptyFormat, newEmptyFormat, "Metric emptyFormat was not changed properly");
70
compare(info.domain, newDomain, "Metric domain was not changed properly");
71
info = dbusQuery.queryMetricInfo(2);
72
compare(info, null, "A new metric shouldn't have been created when just changing format");
74
// Check that when changing the metric name a new metric is created with the new name
75
var newName = "another_test";
76
metric.name = newName;
77
info = dbusQuery.queryMetricInfo(1);
78
compare(info.name, originalName, "Metric 1 name was changed instead of creating new metric");
79
info = dbusQuery.queryMetricInfo(2);
80
compare(info.name, newName, "Metric 2 was not created with proper name");
81
compare(info.format, newFormat, "Metric 2 was not created with proper format");
83
// Check that now the "metric" object points to the newly created metric and changes to it don't
84
// affect the old metric
86
compare(dbusQuery.queryCurrentValue(1), 5.5, "Metric 1 data was changed while metric 2 was in use");
87
compare(dbusQuery.queryCurrentValue(2), 0, "Metric 2 data was not updated correctly");
88
metric.increment(1.5);
89
compare(dbusQuery.queryCurrentValue(1), 5.5, "Metric 1 data was changed while metric 2 was in use");
90
compare(dbusQuery.queryCurrentValue(2), 1.5, "Metric 2 data was not incremented correctly");
92
// Make sure that making the "metric" object point back to the original metric by restoring the
93
// original name works fine and has no side effects.
94
metric.name = originalName;
96
compare(dbusQuery.queryCurrentValue(1), 0, "Metric 1 data was not changed when pointing to it");
97
compare(dbusQuery.queryCurrentValue(2), 1.5, "Metric 2 data was changed when pointing to metric 1");
98
info = dbusQuery.queryMetricInfo(3);
99
compare(info, null, "A new metric shouldn't have been created when pointing back to metric 1");