Add unit test for Device

Reviewers: stikonas, cjlcarvalho

Reviewed By: stikonas

Differential Revision: https://phabricator.kde.org/D22629
This commit is contained in:
Shubham Jangra 2019-07-22 01:18:31 +05:30
parent a97162fb04
commit 7af8045f32
3 changed files with 180 additions and 0 deletions

View File

@ -62,3 +62,7 @@ set(SMARTPARSER ${CMAKE_SOURCE_DIR}/src/core/smartdiskinformation.cpp
# Test SMART support
kpm_test(testsmart testsmart.cpp ${SMARTPARSER})
add_test(NAME testsmart COMMAND testsmart ${BACKEND})
# Test Device
kpm_test(testdevice testdevice.cpp)
add_test(NAME testdevice COMMAND testdevice ${BACKEND})

130
test/testdevice.cpp Normal file
View File

@ -0,0 +1,130 @@
/*************************************************************************
* Copyright (C) 2019 by Shubham <aryan100jangid@gmail.com> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
// SPDX-License-Identifier: GPL-3.0+
#include "helpers.h"
#include "testdevice.h"
#include "backend/corebackend.h"
#include "backend/corebackendmanager.h"
#include <QtAlgorithms>
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
KPMCoreInitializer init;
if (argc == 2)
init = KPMCoreInitializer(argv[1]);
return init.isValid() ? true : false;
CoreBackend *backend = CoreBackendManager::self()->backend();
if (!backend) {
qWarning() << "Failed to load backend plugin";
return false;
}
TestDevice device;
if (!device.testDeviceName() || !device.testDeviceNode() || !device.testDeviceSize() || !device.testDeviceTotalSectors())
return false;
return app.exec();
}
TestDevice::TestDevice()
{
operationStack = new OperationStack();
deviceScanner = new DeviceScanner(nullptr, *operationStack);
deviceScanner->scan();
// Get list of available devices on the system
devices = operationStack->previewDevices();
}
TestDevice::~TestDevice()
{
delete operationStack;
delete deviceScanner;
// Delete the list of devices
qDeleteAll(devices.begin(), devices.end());
devices.clear();
}
bool TestDevice::testDeviceName()
{
if (devices.isEmpty()) {
return false;
} else {
for (const auto &device : devices) {
if (device->name() == QString())
return false;
}
}
return true;
}
bool TestDevice::testDeviceNode()
{
if (devices.isEmpty()) {
return false;
} else {
for (const auto &device : devices) {
if (device->deviceNode() == QString())
return false;
}
}
return true;
}
bool TestDevice::testDeviceSize()
{
if (devices.isEmpty()) {
return false;
} else {
for (const auto &device : devices) {
if (device->logicalSize() < 0)
return false;
}
}
return true;
}
bool TestDevice::testDeviceTotalSectors()
{
if (devices.isEmpty()) {
return false;
} else {
for (const auto &device : devices) {
if (device->totalLogical() < 0)
return false;
}
}
return true;
}

46
test/testdevice.h Normal file
View File

@ -0,0 +1,46 @@
/*************************************************************************
* Copyright (C) 2019 by Shubham <aryan100jangid@gmail.com> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
**************************************************************************/
// SPDX-License-Identifier: GPL-3.0+
#ifndef TESTDEVICE_H
#define TESTDEVICE_H
#include "core/device.h"
#include "core/devicescanner.h"
#include "core/operationstack.h"
#include <QList>
class TestDevice
{
public:
TestDevice();
~TestDevice();
bool testDeviceName();
bool testDeviceNode();
bool testDeviceSize();
bool testDeviceTotalSectors();
private:
OperationStack *operationStack;
DeviceScanner *deviceScanner;
QList <Device*> devices;
};
#endif // TESTDEVICE_H