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