This repository has been archived by the owner on May 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#69 Implement UserScript::import and UserScript::load
- Loading branch information
1 parent
95e5089
commit 20c87aa
Showing
6 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 41 additions & 3 deletions
44
lib/MellowPlayer/Infrastructure/StreamingServices/UserScript.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,53 @@ | ||
#include <QtCore/QDir> | ||
#include <MellowPlayer/Infrastructure/Helpers/FileHelper.hpp> | ||
#include <QtCore/QUuid> | ||
#include "UserScript.hpp" | ||
|
||
using namespace MellowPlayer::Application; | ||
using namespace MellowPlayer::Infrastructure; | ||
|
||
void UserScript::import(const QString& path) | ||
bool UserScriptDirectory::create() const | ||
{ | ||
qDebug() << "importing" << path; | ||
QString path = FileHelper::userScriptsDirectory(); | ||
QDir qDir(path); | ||
if (!qDir.exists()) | ||
return qDir.mkpath(path); | ||
return true; | ||
} | ||
|
||
void UserScript::load(const QString& path) | ||
QString UserScriptDirectory::generateFileName() const { | ||
return path() + QDir::separator() + QUuid::createUuid().toString() + ".js"; | ||
} | ||
|
||
QString UserScriptDirectory::path() const { | ||
return FileHelper::userScriptsDirectory(); | ||
} | ||
|
||
bool UserScript::import(const QString& sourcePath) | ||
{ | ||
UserScriptDirectory directory; | ||
if (directory.create() && QFile::exists(sourcePath)) | ||
{ | ||
QString destinationPath = directory.generateFileName(); | ||
path_ = destinationPath; | ||
qDebug() << "importing" << sourcePath << " to " << destinationPath; | ||
if (QFile::copy(sourcePath, destinationPath)) | ||
return load(destinationPath); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool UserScript::load(const QString& path) | ||
{ | ||
qDebug() << "loading" << path; | ||
path_ = path; | ||
|
||
QFile file(path); | ||
if (file.open(QFile::ReadOnly)) { | ||
auto content = file.readAll(); | ||
code_ = QString::fromUtf8(content); | ||
return true; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
tests/IntegrationTests/Infrastructure/StreamingServices/UserScriptTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <catch.hpp> | ||
#include <MellowPlayer/Infrastructure/StreamingServices/UserScript.hpp> | ||
#include <QtCore/QDir> | ||
#include <MellowPlayer/Infrastructure/Helpers/FileHelper.hpp> | ||
|
||
using namespace MellowPlayer::Infrastructure; | ||
|
||
SCENARIO("UserScriptDirectoryTests") | ||
{ | ||
GIVEN("a UserScriptDirectory instance") | ||
{ | ||
UserScriptDirectory directory; | ||
QDir dir(FileHelper::userScriptsDirectory()); | ||
dir.removeRecursively(); | ||
REQUIRE(!dir.exists()); | ||
|
||
WHEN("get path") | ||
{ | ||
auto path = directory.path(); | ||
|
||
THEN("path is not empty") | ||
{ | ||
REQUIRE(!path.isEmpty()); | ||
} | ||
} | ||
|
||
WHEN("create directory") | ||
{ | ||
directory.create(); | ||
|
||
THEN("directory exists") | ||
{ | ||
REQUIRE(QDir().exists(directory.path())); | ||
} | ||
} | ||
|
||
WHEN("generating two file name") | ||
{ | ||
auto fileName1 = directory.generateFileName(); | ||
auto fileName2 = directory.generateFileName(); | ||
|
||
THEN("they are different") | ||
{ | ||
REQUIRE(fileName1 != fileName2); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
SCENARIO("UserScriptTests") | ||
{ | ||
GIVEN("A UserScript instance") | ||
{ | ||
UserScript userScript; | ||
QDir dir(FileHelper::userScriptsDirectory()); | ||
dir.removeRecursively(); | ||
REQUIRE(!dir.exists()); | ||
|
||
WHEN("importing a file that does not exists") | ||
{ | ||
bool retVal = userScript.import("/an/invalid/path"); | ||
THEN("import return value is false") | ||
{ | ||
REQUIRE(!retVal); | ||
} | ||
} | ||
|
||
WHEN("importing a file that exists") | ||
{ | ||
QByteArray fileContent("alert('custom user script')"); | ||
QString sourceFilePath = FileHelper::appDataDirectory() + "fakeUserScript.js"; | ||
QFile file(sourceFilePath); | ||
file.open(QFile::WriteOnly); | ||
file.write(fileContent); | ||
file.close(); | ||
|
||
bool retVal = userScript.import(sourceFilePath); | ||
|
||
THEN("import return value is true") | ||
{ | ||
REQUIRE(retVal); | ||
|
||
AND_THEN("the file exists") | ||
{ | ||
REQUIRE(QFile::exists(userScript.path())); | ||
} | ||
|
||
AND_THEN("the file content is loaded and available via the code property") | ||
{ | ||
REQUIRE(userScript.code().toStdString() == fileContent.toStdString()); | ||
} | ||
} | ||
} | ||
} | ||
} |