| 1 | /**************************************************************************** |
|---|---|
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd 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:LGPL21$ |
| 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 The Qt Company. For licensing terms |
| 15 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free |
| 21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and |
| 22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the |
| 23 | ** following information to ensure the GNU Lesser General Public License |
| 24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and |
| 25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 26 | ** |
| 27 | ** As a special exception, The Qt Company gives you certain additional |
| 28 | ** rights. These rights are described in The Qt Company LGPL Exception |
| 29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 30 | ** |
| 31 | ** $QT_END_LICENSE$ |
| 32 | ** |
| 33 | ****************************************************************************/ |
| 34 | |
| 35 | #include <QtTest/QtTest> |
| 36 | #include "qbatteryinfo.h" |
| 37 | |
| 38 | QT_USE_NAMESPACE |
| 39 | |
| 40 | class tst_QBatteryInfo : public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | |
| 44 | private slots: |
| 45 | void tst_capacity(); |
| 46 | void tst_flow(); |
| 47 | void tst_invalid(); |
| 48 | void tst_setBatteryIndex(); |
| 49 | }; |
| 50 | |
| 51 | void tst_QBatteryInfo::tst_capacity() |
| 52 | { |
| 53 | QBatteryInfo batteryInfo; |
| 54 | |
| 55 | int count = batteryInfo.batteryCount(); |
| 56 | for (int i = 0; i < count; ++i) { |
| 57 | batteryInfo.setBatteryIndex(i); |
| 58 | int max = batteryInfo.maximumCapacity(); |
| 59 | int remaining = batteryInfo.remainingCapacity(); |
| 60 | |
| 61 | QVERIFY(max == -1 || remaining == -1 || remaining <= max); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void tst_QBatteryInfo::tst_flow() |
| 66 | { |
| 67 | QBatteryInfo batteryInfo; |
| 68 | |
| 69 | int count = batteryInfo.batteryCount(); |
| 70 | for (int i = 0; i < count; ++i) { |
| 71 | batteryInfo.setBatteryIndex(i); |
| 72 | QBatteryInfo::ChargingState chargingState = batteryInfo.chargingState(); |
| 73 | switch (chargingState) { |
| 74 | case QBatteryInfo::Charging: |
| 75 | QVERIFY(batteryInfo.currentFlow() < 0); |
| 76 | QVERIFY(batteryInfo.remainingChargingTime() >= 0); |
| 77 | break; |
| 78 | case QBatteryInfo::IdleChargingState: |
| 79 | QVERIFY(batteryInfo.currentFlow() >= 0); |
| 80 | QVERIFY(batteryInfo.remainingChargingTime() == 0); |
| 81 | break; |
| 82 | case QBatteryInfo::Discharging: |
| 83 | QVERIFY(batteryInfo.currentFlow() > 0); |
| 84 | QVERIFY(batteryInfo.remainingChargingTime() == 0); |
| 85 | break; |
| 86 | case QBatteryInfo::UnknownChargingState: |
| 87 | QVERIFY(batteryInfo.currentFlow() == 0); |
| 88 | QVERIFY(batteryInfo.remainingChargingTime() == -1); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void tst_QBatteryInfo::tst_invalid() |
| 95 | { |
| 96 | QBatteryInfo batteryInfo; |
| 97 | |
| 98 | batteryInfo.setBatteryIndex(-1); |
| 99 | QVERIFY(batteryInfo.currentFlow() == 0); |
| 100 | QVERIFY(batteryInfo.maximumCapacity() == -1); |
| 101 | QVERIFY(batteryInfo.remainingCapacity() == -1); |
| 102 | QVERIFY(batteryInfo.remainingChargingTime() == -1); |
| 103 | QVERIFY(batteryInfo.voltage() == -1); |
| 104 | QVERIFY(batteryInfo.chargingState() == QBatteryInfo::UnknownChargingState); |
| 105 | QVERIFY(batteryInfo.levelStatus() == QBatteryInfo::LevelUnknown); |
| 106 | |
| 107 | int count = batteryInfo.batteryCount(); |
| 108 | batteryInfo.setBatteryIndex(count); |
| 109 | QVERIFY(batteryInfo.currentFlow() == 0); |
| 110 | QVERIFY(batteryInfo.maximumCapacity() == -1); |
| 111 | QVERIFY(batteryInfo.remainingCapacity() == -1); |
| 112 | QVERIFY(batteryInfo.remainingChargingTime() == -1); |
| 113 | QVERIFY(batteryInfo.voltage() == -1); |
| 114 | QVERIFY(batteryInfo.chargingState() == QBatteryInfo::UnknownChargingState); |
| 115 | QVERIFY(batteryInfo.levelStatus() == QBatteryInfo::LevelUnknown); |
| 116 | } |
| 117 | |
| 118 | void tst_QBatteryInfo::tst_setBatteryIndex() |
| 119 | { |
| 120 | QBatteryInfo batteryInfo; |
| 121 | |
| 122 | QCOMPARE(batteryInfo.batteryIndex(), 0); |
| 123 | |
| 124 | batteryInfo.setBatteryIndex(1); |
| 125 | QCOMPARE(batteryInfo.batteryIndex(), 1); |
| 126 | |
| 127 | batteryInfo.setBatteryIndex(1000); |
| 128 | QCOMPARE(batteryInfo.batteryIndex(), 1000); |
| 129 | |
| 130 | batteryInfo.setBatteryIndex(-1); |
| 131 | QCOMPARE(batteryInfo.batteryIndex(), -1); |
| 132 | } |
| 133 | |
| 134 | QTEST_MAIN(tst_QBatteryInfo) |
| 135 | #include "tst_qbatteryinfo.moc" |
| 136 |
