overte/tests/networking/src/ResourceTests.cpp
2016-04-12 13:51:11 -07:00

101 lines
3 KiB
C++

//
// ResoruceTests.cpp
//
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QNetworkDiskCache>
#include "ResourceCache.h"
#include "NetworkAccessManager.h"
#include "DependencyManager.h"
#include "ResourceTests.h"
QTEST_MAIN(ResourceTests)
void ResourceTests::initTestCase() {
auto resourceCacheSharedItems = DependencyManager::set<ResourceCacheSharedItems>();
const qint64 MAXIMUM_CACHE_SIZE = 1024 * 1024 * 1024; // 1GB
// set up the file cache
//QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
QString cachePath = "./resourceTestCache";
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
QNetworkDiskCache* cache = new QNetworkDiskCache();
cache->setMaximumCacheSize(MAXIMUM_CACHE_SIZE);
cache->setCacheDirectory(cachePath);
cache->clear(); // clear the cache
networkAccessManager.setCache(cache);
}
static QSharedPointer<Resource> resource;
static bool waitForSignal(QObject *sender, const char *signal, int timeout = 1000) {
QEventLoop loop;
QTimer timer;
timer.setInterval(timeout);
timer.setSingleShot(true);
loop.connect(sender, signal, SLOT(quit()));
loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
timer.start();
loop.exec();
return timer.isActive();
}
void ResourceTests::downloadFirst() {
// download the Mery fst file
QUrl meryUrl = QUrl("http://hifi-public.s3.amazonaws.com/marketplace/contents/e21c0b95-e502-4d15-8c41-ea2fc40f1125/3585ddf674869a67d31d5964f7b52de1.fst");
resource = QSharedPointer<Resource>::create(meryUrl, false);
resource->setSelf(resource);
const int timeout = 1000;
QEventLoop loop;
QTimer timer;
timer.setInterval(timeout);
timer.setSingleShot(true);
loop.connect(resource, SIGNAL(loaded(QNetworkReply&)), SLOT(quit()));
loop.connect(resource, SIGNAL(failed(QNetworkReply::NetworkError)), SLOT(quit()));
loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
timer.start();
resource->ensureLoading();
loop.exec();
QVERIFY(resource->isLoaded());
}
void ResourceTests::downloadAgain() {
// download the Mery fst file
QUrl meryUrl = QUrl("http://hifi-public.s3.amazonaws.com/marketplace/contents/e21c0b95-e502-4d15-8c41-ea2fc40f1125/3585ddf674869a67d31d5964f7b52de1.fst");
resource = QSharedPointer<Resource>::create(meryUrl, false);
resource->setSelf(resource);
const int timeout = 1000;
QEventLoop loop;
QTimer timer;
timer.setInterval(timeout);
timer.setSingleShot(true);
loop.connect(resource, SIGNAL(loaded(QNetworkReply&)), SLOT(quit()));
loop.connect(resource, SIGNAL(failed(QNetworkReply::NetworkError)), SLOT(quit()));
loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
timer.start();
resource->ensureLoading();
loop.exec();
QVERIFY(resource->isLoaded());
}