Skip to content

Commit

Permalink
Merge pull request #23 from ugnelis/added-tests
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
ugnelis committed May 6, 2018
2 parents 73eb05d + 7fa2234 commit b467511
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.5)

project(instant_translator)

set (CMAKE_CXX_STANDARD 11)

# Place binaries and libraries according to GNU standards.
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
Expand Down
11 changes: 9 additions & 2 deletions src/apis/googleapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ QString GoogleAPI::translate(const QString &input,
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

RequestManager requestManager;

std::unique_ptr<QNetworkAccessManager> manager =
std::unique_ptr<QNetworkAccessManager>(new QNetworkAccessManager());

RequestManager requestManager(nullptr, std::move(manager));
requestManager.postRequest(request, postJsonDocument.toJson());

// Parse the replay.
Expand Down Expand Up @@ -69,7 +73,10 @@ QStringList GoogleAPI::getSupportedLanguages() const {
QUrl url(urlString);
QNetworkRequest request(url);

RequestManager requestManager;
std::unique_ptr<QNetworkAccessManager> manager =
std::unique_ptr<QNetworkAccessManager>(new QNetworkAccessManager());

RequestManager requestManager(nullptr, std::move(manager));
requestManager.getRequest(request);

// Parse the replay.
Expand Down
1 change: 1 addition & 0 deletions src/apis/googleapi.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef INSTANT_TRANSLATOR_GOOGLEAPI_H
#define INSTANT_TRANSLATOR_GOOGLEAPI_H

#include <memory>
#include <QObject>
#include <QSettings>
#include <QJsonDocument>
Expand Down
62 changes: 62 additions & 0 deletions src/utils/customnetworkreply.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "customnetworkreply.h"

CustomNetworkReply::CustomNetworkReply(QObject *parent)
: QNetworkReply(parent), content(nullptr), offset(0) {
}

CustomNetworkReply::~CustomNetworkReply() {
}

void CustomNetworkReply::setHttpStatusCode(int code, const QByteArray &statusText) {
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, code);
if (statusText.isNull())
return;

setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, statusText);
}

void CustomNetworkReply::setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value) {
QNetworkReply::setHeader(header, value);
}

void CustomNetworkReply::setContentType(const QByteArray &contentType) {
setHeader(QNetworkRequest::ContentTypeHeader, contentType);
}

void CustomNetworkReply::setContent(const QString &content) {
setContent(content.toUtf8());
}

void CustomNetworkReply::setContent(const QByteArray &content) {
this->content = content;
offset = 0;

open(ReadOnly | Unbuffered);
setHeader(QNetworkRequest::ContentLengthHeader, QVariant(content.size()));

QTimer::singleShot(0, this, SIGNAL(readyRead()));
QTimer::singleShot(0, this, SIGNAL(finished()));
}

void CustomNetworkReply::abort() {
// No operation.
}

qint64 CustomNetworkReply::bytesAvailable() const {
return content.size() - offset + QIODevice::bytesAvailable();
}

bool CustomNetworkReply::isSequential() const {
return true;
}

qint64 CustomNetworkReply::readData(char *data, qint64 maxSize) {
if (offset >= content.size())
return -1;

qint64 number = qMin(maxSize, content.size() - offset);
memcpy(data, content.constData() + offset, number);
offset += number;

return number;
}
93 changes: 93 additions & 0 deletions src/utils/customnetworkreply.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#ifndef INSTANT_TRANSLATOR_CUSTOMNETWORKREPLY_H
#define INSTANT_TRANSLATOR_CUSTOMNETWORKREPLY_H

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QBuffer>
#include <QTimer>

/**
* Custom QNetworkReply class.
*/
class CustomNetworkReply : public QNetworkReply {
Q_OBJECT

public:
/**
* Constructs an object with parent object parent.
* @param parent Parent of an object may be viewed as the object's owner.
*/
explicit CustomNetworkReply(QObject *parent = nullptr);

/**
* Destructor.
*/
~CustomNetworkReply() override;

/**
*
* @param code
* @param statusText
*/
void setHttpStatusCode(int code, const QByteArray &statusText = QByteArray());

/**
* Sets the value of the known header header to be
* value, overriding any previously set headers.
* @param header Header.
* @param value Value.
*/
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value);

/**
* Set content type, e.g., "text/html".
* @param contentType Content type.
*/
void setContentType(const QByteArray &contentType);

/**
* Set content.
* @param content Content.
*/
void setContent(const QString &content);

/**
* Set content.
* @param content Content.
*/
void setContent(const QByteArray &content);

/**
* Aborts the operation immediately and close
* down any network connections still open.
*/
void abort() override;

/**
* Returns the number of bytes that are available for reading.
* @return Bytes that are available for reading.
*/
qint64 bytesAvailable() const override;

/**
* Returns true if this device is sequential; otherwise returns false.
* @return Returns true if this device is sequential.
*/
bool isSequential() const override;

protected:
/**
* Reads up to maxSize bytes from the device into data, and
* returns the number of bytes read or -1 if an error occurred.
* @param data Data.
* @param maxSize Max size.
* @return returns the number of bytes read or -1 if an error occurred.
*/
qint64 readData(char *data, qint64 maxSize) override;

private:
QByteArray content;
qint64 offset;
};

#endif //INSTANT_TRANSLATOR_CUSTOMNETWORKREPLY_H
18 changes: 9 additions & 9 deletions src/utils/requestmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include "requestmanager.h"

RequestManager::RequestManager(QObject *parent)
: QObject(parent), manager(new QNetworkAccessManager) {
RequestManager::RequestManager(QObject *parent, std::unique_ptr<QNetworkAccessManager> manager)
: QObject(parent), manager(std::move(manager)), networkReplyData(QByteArray("")) {
}

RequestManager::~RequestManager() {
delete manager;
}

void RequestManager::postRequest(const QNetworkRequest &request, const QByteArray &data) {
networkReply = manager->post(request, data);
QNetworkReply *networkReply = manager->post(request, data);

// Wait for the reply.
QEventLoop loop;
Expand All @@ -20,10 +19,12 @@ void RequestManager::postRequest(const QNetworkRequest &request, const QByteArra
SLOT(quit())
);
loop.exec();

networkReplyData = networkReply->readAll();
}

void RequestManager::getRequest(const QNetworkRequest &request) {
networkReply = manager->get(request);
QNetworkReply *networkReply = manager->get(request);

// Wait for the reply.
QEventLoop loop;
Expand All @@ -34,11 +35,10 @@ void RequestManager::getRequest(const QNetworkRequest &request) {
SLOT(quit())
);
loop.exec();

networkReplyData = networkReply->readAll();
}

QByteArray RequestManager::getReply() {
if (networkReply == nullptr) {
return QByteArray();
}
return networkReply->readAll();
return networkReplyData;
}
8 changes: 5 additions & 3 deletions src/utils/requestmanager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef INSTANT_TRANSLATOR_REQUESTMANAGER_H
#define INSTANT_TRANSLATOR_REQUESTMANAGER_H

#include <memory>
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
Expand All @@ -12,8 +13,9 @@ Q_OBJECT
/**
* Constructs an object with parent object parent.
* @param parent Parent of an object may be viewed as the object's owner.
* @param manager Network access manager.
*/
explicit RequestManager(QObject *parent = nullptr);
RequestManager(QObject *parent, std::unique_ptr<QNetworkAccessManager> manager);

/**
* Destructor.
Expand All @@ -40,8 +42,8 @@ Q_OBJECT
QByteArray getReply();

private:
QNetworkAccessManager *manager; // Network manager.
QNetworkReply *networkReply; // Network reply.
std::unique_ptr<QNetworkAccessManager> manager; // Network manager.
QByteArray networkReplyData; // Network reply data.
};

#endif //INSTANT_TRANSLATOR_REQUESTMANAGER_H
22 changes: 19 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
set(CMAKE_CXX_STANDARD 11)

# Find includes in corresponding build directories.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Instruct CMake to run uic automatically when needed.
set(CMAKE_AUTOUIC ON)
# Add a compiler flag.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Find the Qt libraries.
find_package(Qt5Widgets REQUIRED)
Expand All @@ -23,15 +33,21 @@ include_directories("${PROJECT_SOURCE_DIR}/src")

# Source files.
set(SOURCE_FILES
language_test.cpp
utils/customnetworkreply_test.cpp
${PROJECT_SOURCE_DIR}/src/utils/customnetworkreply.cpp
${PROJECT_SOURCE_DIR}/src/utils/customnetworkreply.h
utils/language_test.cpp
${PROJECT_SOURCE_DIR}/src/utils/language.cpp
${PROJECT_SOURCE_DIR}/src/utils/language.h)
${PROJECT_SOURCE_DIR}/src/utils/language.h
utils/requestmanager_test.cpp
${PROJECT_SOURCE_DIR}/src/utils/requestmanager.cpp
${PROJECT_SOURCE_DIR}/src/utils/requestmanager.h)

# Tell CMake to create the executable.
add_executable(unit_tests ${SOURCE_FILES} ${RESOURCE_FILES_ADDED} ${UI_HEADERS})

# Use the Widgets module from Qt5.
target_link_libraries(unit_tests ${QT5_LIBRARIES} gtest_main)
target_link_libraries(unit_tests ${QT5_LIBRARIES} gtest_main gmock_main)

# Add tests.
add_test(
Expand Down
8 changes: 0 additions & 8 deletions tests/language_test.cpp

This file was deleted.

54 changes: 54 additions & 0 deletions tests/utils/customnetworkreply_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <QList>
#include <QNetworkReply>
#include "utils/customnetworkreply.h"

TEST(CustomNetworkReplyTests, setContentType_CheckContentType_True) {
CustomNetworkReply reply;
reply.setContentType("application/json");
QList<QNetworkReply::RawHeaderPair> rawHeaderList = reply.rawHeaderPairs();

ASSERT_EQ("application/json", rawHeaderList.at(0).second);
}

TEST(CustomNetworkReplyTests, setContent_GiveQStringAsAValue_True) {
CustomNetworkReply reply;
reply.setContent(QString("This is test!"));
QByteArray content = reply.readAll();

ASSERT_EQ(QString("This is test!"), content);
}

TEST(CustomNetworkReplyTests, setContent_GiveQByteArrayAsAValue_True) {
CustomNetworkReply reply;
reply.setContent(QByteArray("This is test!"));
QByteArray content = reply.readAll();

ASSERT_EQ(QString("This is test!"), content);
}

TEST(CustomNetworkReplyTests, abort_DoNotDoAnything_True) {
CustomNetworkReply reply;
reply.setContent(QByteArray(""));
QByteArray content = reply.readAll();
reply.abort();

ASSERT_EQ(QString(""), content);
}

TEST(CustomNetworkReplyTests, bytesAvailable_CheckBytes_True) {
CustomNetworkReply reply;
reply.setContent(QByteArray("This is test!"));

qint64 bytesAvailable = reply.bytesAvailable();

ASSERT_EQ(13, bytesAvailable);
}

TEST(CustomNetworkReplyTests, isSequential_CheckIfItIsSequeantial_True) {
CustomNetworkReply reply;
bool isSequential = reply.isSequential();

ASSERT_EQ(true, isSequential);
}
Loading

0 comments on commit b467511

Please sign in to comment.