Add C++ Unit Tests for Android (#2563)

This commit is contained in:
Bart Louwers
2024-07-04 01:16:29 +03:00
committed by GitHub
parent 61c3b280ba
commit 5d5bd84d95
14 changed files with 117 additions and 50 deletions

View File

@@ -4,17 +4,65 @@
#include <unistd.h>
#include <mutex>
#include <thread>
#include <gtest/gtest.h>
#include <android/log.h>
using namespace mbgl;
using namespace mbgl::android;
std::string testResultToStr(const ::testing::TestResult* testResult) {
if (testResult->Passed()) return " (PASSED)";
if (testResult->Skipped()) return " (SKIPPED)";
if (testResult->Failed()) return " (FAILED)";
return "";
}
class GTestAndroidLogger : public ::testing::EmptyTestEventListener {
public:
void OnTestProgramStart(const ::testing::UnitTest&) override {
__android_log_print(ANDROID_LOG_INFO, tag, "Test program started.");
}
void OnTestProgramEnd(const ::testing::UnitTest& unit_test) override {
__android_log_print(
ANDROID_LOG_INFO, tag, "Test program ended with %d tests run.", unit_test.total_test_count());
}
void OnTestStart(const ::testing::TestInfo& test_info) override {
__android_log_print(ANDROID_LOG_INFO, tag, "Starting: %s.%s", test_info.test_case_name(), test_info.name());
}
void OnTestEnd(const ::testing::TestInfo& test_info) override {
__android_log_print(ANDROID_LOG_INFO,
tag,
"Finished: %s.%s%s",
test_info.test_case_name(),
test_info.name(),
testResultToStr(test_info.result()).c_str());
}
void OnTestPartResult(const ::testing::TestPartResult& test_part_result) override {
__android_log_print(ANDROID_LOG_INFO,
tag,
"%s: %s",
test_part_result.failed() ? "FAILED" : "PASSED",
test_part_result.summary());
}
const char* tag = "GTest";
};
std::atomic<bool> running{true};
std::atomic<bool> success{false};
std::once_flag done;
ALooper* looper = NULL;
void runner() {
std::vector<std::string> arguments = {"mbgl-test-runner", "--gtest_output=xml:/sdcard/test/results/results.xml"};
void runner(const std::string& gtestOutput) {
::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
listeners.Append(new GTestAndroidLogger); // takes ownership
std::vector<std::string> arguments = {
"mbgl-test-runner", "--gtest_output=xml:" + gtestOutput, "--gtest_filter=-StringIndexer.GetOOBIdentity"};
std::vector<char*> argv;
for (const auto& arg : arguments) {
argv.push_back((char*)arg.data());
@@ -38,14 +86,15 @@ void android_main(struct android_app* app) {
std::string storagePath(app->activity->internalDataPath);
std::string zipFile = storagePath + "/data.zip";
const std::string gtestOutput = storagePath + "/results.xml";
if (copyFile(env, app->activity->assetManager, zipFile, storagePath, "data.zip")) {
if (chdir("/sdcard")) {
mbgl::Log::Error(mbgl::Event::General, "Failed to change the directory to /sdcard");
if (chdir(storagePath.c_str())) {
mbgl::Log::Error(mbgl::Event::General, "Failed to change the directory to storage dir");
changeState(env, app, success);
} else {
unZipFile(env, zipFile, "/sdcard/");
runnerThread = std::thread(runner);
unZipFile(env, zipFile, storagePath);
runnerThread = std::thread(runner, gtestOutput);
}
} else {
mbgl::Log::Error(mbgl::Event::General, "Failed to copy zip file '" + zipFile + "' to external storage");