Renames & Add new properties to QBatteryInfo
[qt:qtsystems.git] / tests / auto / systeminfo / qbatteryinfo / tst_qbatteryinfo.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
5 ** Contact: http://www.qt-project.org/legal
6 **
7 ** This file is part of the QtSystems module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Digia.  For licensing terms and
15 ** conditions see http://qt.digia.com/licensing.  For further information
16 ** use the contact form at http://qt.digia.com/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 2.1 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL included in the
22 ** packaging of this file.  Please review the following information to
23 ** ensure the GNU Lesser General Public License version 2.1 requirements
24 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** In addition, as a special exception, Digia gives you certain additional
27 ** rights.  These rights are described in the Digia Qt LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43 #include <QtTest/QtTest>
44 #include "qbatteryinfo.h"
45
46 QT_USE_NAMESPACE
47
48 class tst_QBatteryInfo : public QObject
49 {
50     Q_OBJECT
51
52 private slots:
53     void tst_capacity();
54     void tst_flow();
55     void tst_invalid();
56     void tst_setBatteryIndex();
57 };
58
59 void tst_QBatteryInfo::tst_capacity()
60 {
61     QBatteryInfo batteryInfo;
62
63     int count = batteryInfo.batteryCount();
64     for (int i = 0; i < count; ++i) {
65         batteryInfo.setBatteryIndex(i);
66         int max = batteryInfo.maximumCapacity();
67         int remaining = batteryInfo.remainingCapacity();
68
69         QVERIFY(max == -1 || remaining == -1 || remaining <= max);
70     }
71 }
72
73 void tst_QBatteryInfo::tst_flow()
74 {
75     QBatteryInfo batteryInfo;
76
77     int count = batteryInfo.batteryCount();
78     for (int i = 0; i < count; ++i) {
79         batteryInfo.setBatteryIndex(i);
80         QBatteryInfo::ChargingState chargingState = batteryInfo.chargingState();
81         switch (chargingState) {
82         case QBatteryInfo::Charging:
83             QVERIFY(batteryInfo.currentFlow() < 0);
84             QVERIFY(batteryInfo.remainingChargingTime() >= 0);
85             break;
86         case QBatteryInfo::IdleChargingState:
87             QVERIFY(batteryInfo.currentFlow() >= 0);
88             QVERIFY(batteryInfo.remainingChargingTime() == 0);
89             break;
90         case QBatteryInfo::Discharging:
91             QVERIFY(batteryInfo.currentFlow() > 0);
92             QVERIFY(batteryInfo.remainingChargingTime() == 0);
93             break;
94         case QBatteryInfo::UnknownChargingState:
95             QVERIFY(batteryInfo.currentFlow() == 0);
96             QVERIFY(batteryInfo.remainingChargingTime() == -1);
97             break;
98         }
99     }
100 }
101
102 void tst_QBatteryInfo::tst_invalid()
103 {
104     QBatteryInfo batteryInfo;
105
106     batteryInfo.setBatteryIndex(-1);
107     QVERIFY(batteryInfo.currentFlow() == 0);
108     QVERIFY(batteryInfo.maximumCapacity() == -1);
109     QVERIFY(batteryInfo.remainingCapacity() == -1);
110     QVERIFY(batteryInfo.remainingChargingTime() == -1);
111     QVERIFY(batteryInfo.voltage() == -1);
112     QVERIFY(batteryInfo.chargingState() == QBatteryInfo::UnknownChargingState);
113     QVERIFY(batteryInfo.levelStatus() == QBatteryInfo::LevelUnknown);
114
115     int count = batteryInfo.batteryCount();
116     batteryInfo.setBatteryIndex(count);
117     QVERIFY(batteryInfo.currentFlow() == 0);
118     QVERIFY(batteryInfo.maximumCapacity() == -1);
119     QVERIFY(batteryInfo.remainingCapacity() == -1);
120     QVERIFY(batteryInfo.remainingChargingTime() == -1);
121     QVERIFY(batteryInfo.voltage() == -1);
122     QVERIFY(batteryInfo.chargingState() == QBatteryInfo::UnknownChargingState);
123     QVERIFY(batteryInfo.levelStatus() == QBatteryInfo::LevelUnknown);
124 }
125
126 void tst_QBatteryInfo::tst_setBatteryIndex()
127 {
128     QBatteryInfo batteryInfo;
129
130     QCOMPARE(batteryInfo.batteryIndex(), 0);
131
132     batteryInfo.setBatteryIndex(1);
133     QCOMPARE(batteryInfo.batteryIndex(), 1);
134
135     batteryInfo.setBatteryIndex(1000);
136     QCOMPARE(batteryInfo.batteryIndex(), 1000);
137
138     batteryInfo.setBatteryIndex(-1);
139     QCOMPARE(batteryInfo.batteryIndex(), -1);
140 }
141
142 QTEST_MAIN(tst_QBatteryInfo)
143 #include "tst_qbatteryinfo.moc"