summaryrefslogtreecommitdiff
path: root/test/storage/offline_download.test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/storage/offline_download.test.cpp')
-rw-r--r--test/storage/offline_download.test.cpp159
1 files changed, 125 insertions, 34 deletions
diff --git a/test/storage/offline_download.test.cpp b/test/storage/offline_download.test.cpp
index 57780eba40..492e68e869 100644
--- a/test/storage/offline_download.test.cpp
+++ b/test/storage/offline_download.test.cpp
@@ -1,5 +1,7 @@
#include <mbgl/test/stub_file_source.hpp>
#include <mbgl/test/fake_file_source.hpp>
+#include <mbgl/test/fixture_log_observer.hpp>
+#include <mbgl/test/sqlite3_test_fs.hpp>
#include <mbgl/storage/offline.hpp>
#include <mbgl/storage/offline_database.hpp>
@@ -10,11 +12,29 @@
#include <mbgl/util/compression.hpp>
#include <mbgl/util/string.hpp>
+#include <sqlite3.hpp>
#include <gtest/gtest.h>
#include <iostream>
using namespace mbgl;
using namespace std::literals::string_literals;
+using mapbox::sqlite::ResultCode;
+
+#ifndef __QT__ // Qt doesn't expose the ability to register virtual file system handlers.
+static constexpr const char* filename = "test/fixtures/offline_download/offline.db";
+static constexpr const char* filename_test_fs = "file:test/fixtures/offline_download/offline.db?vfs=test_fs";
+
+static void deleteDatabaseFiles() {
+ // Delete leftover journaling files as well.
+ util::deleteFile(filename);
+ util::deleteFile(filename + "-wal"s);
+ util::deleteFile(filename + "-journal"s);
+}
+
+static FixtureLog::Message warning(ResultCode code, const char* message) {
+ return { EventSeverity::Warning, Event::Database, static_cast<int64_t>(code), message };
+}
+#endif
class MockObserver : public OfflineRegionObserver {
public:
@@ -37,13 +57,16 @@ public:
class OfflineTest {
public:
+ OfflineTest(const std::string& path = ":memory:") : db(path) {
+ }
+
util::RunLoop loop;
StubFileSource fileSource;
- OfflineDatabase db { ":memory:" };
+ OfflineDatabase db;
std::size_t size = 0;
- OfflineRegion createRegion() {
- OfflineRegionDefinition definition { "", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 1.0 };
+ auto createRegion() {
+ OfflineTilePyramidRegionDefinition definition { "", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 1.0 };
OfflineRegionMetadata metadata;
return db.createRegion(definition, metadata);
}
@@ -60,9 +83,10 @@ public:
TEST(OfflineDownload, NoSubresources) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -100,9 +124,10 @@ TEST(OfflineDownload, NoSubresources) {
TEST(OfflineDownload, InlineSource) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -140,9 +165,10 @@ TEST(OfflineDownload, InlineSource) {
TEST(OfflineDownload, GeoJSONSource) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -175,9 +201,10 @@ TEST(OfflineDownload, GeoJSONSource) {
TEST(OfflineDownload, Activate) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -250,9 +277,10 @@ TEST(OfflineDownload, Activate) {
TEST(OfflineDownload, DoesNotFloodTheFileSourceWithRequests) {
FakeFileSource fileSource;
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, fileSource);
@@ -272,9 +300,10 @@ TEST(OfflineDownload, DoesNotFloodTheFileSourceWithRequests) {
TEST(OfflineDownload, GetStatusNoResources) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
OfflineRegionStatus status = download.getStatus();
@@ -289,9 +318,10 @@ TEST(OfflineDownload, GetStatusNoResources) {
TEST(OfflineDownload, GetStatusStyleComplete) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -311,9 +341,10 @@ TEST(OfflineDownload, GetStatusStyleComplete) {
TEST(OfflineDownload, GetStatusStyleAndSourceComplete) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -337,9 +368,10 @@ TEST(OfflineDownload, GetStatusStyleAndSourceComplete) {
TEST(OfflineDownload, RequestError) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -365,9 +397,10 @@ TEST(OfflineDownload, RequestError) {
TEST(OfflineDownload, RequestErrorsAreRetried) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -398,9 +431,10 @@ TEST(OfflineDownload, RequestErrorsAreRetried) {
TEST(OfflineDownload, TileCountLimitExceededNoTileResponse) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -440,9 +474,10 @@ TEST(OfflineDownload, TileCountLimitExceededNoTileResponse) {
TEST(OfflineDownload, TileCountLimitExceededWithTileResponse) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -494,9 +529,10 @@ TEST(OfflineDownload, TileCountLimitExceededWithTileResponse) {
TEST(OfflineDownload, WithPreviouslyExistingTile) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -528,9 +564,10 @@ TEST(OfflineDownload, WithPreviouslyExistingTile) {
TEST(OfflineDownload, ReactivatePreviouslyCompletedDownload) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -556,7 +593,7 @@ TEST(OfflineDownload, ReactivatePreviouslyCompletedDownload) {
test.loop.run();
OfflineDownload redownload(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -595,9 +632,10 @@ TEST(OfflineDownload, ReactivatePreviouslyCompletedDownload) {
TEST(OfflineDownload, Deactivate) {
OfflineTest test;
- OfflineRegion region = test.createRegion();
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
OfflineDownload download(
- region.getID(),
+ region->getID(),
OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
test.db, test.fileSource);
@@ -621,3 +659,56 @@ TEST(OfflineDownload, Deactivate) {
test.loop.run();
}
+
+#ifndef __QT__ // Qt doesn't expose the ability to register virtual file system handlers.
+TEST(OfflineDownload, DiskFull) {
+ FixtureLog log;
+ deleteDatabaseFiles();
+ test::SQLite3TestFS fs;
+
+ OfflineTest test{ filename_test_fs };
+ EXPECT_EQ(0u, log.uncheckedCount());
+
+ auto region = test.createRegion();
+ ASSERT_TRUE(region);
+ EXPECT_EQ(0u, log.uncheckedCount());
+
+ // Simulate a full disk.
+ fs.setWriteLimit(8192);
+
+ OfflineDownload download(
+ region->getID(),
+ OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
+ test.db, test.fileSource);
+
+ bool hasRequestedStyle = false;
+
+ test.fileSource.styleResponse = [&] (const Resource& resource) {
+ EXPECT_EQ("http://127.0.0.1:3000/style.json", resource.url);
+ hasRequestedStyle = true;
+ return test.response("empty.style.json");
+ };
+
+ auto observer = std::make_unique<MockObserver>();
+
+ observer->statusChangedFn = [&] (OfflineRegionStatus status) {
+ EXPECT_EQ(OfflineRegionDownloadState::Active, status.downloadState);
+ EXPECT_EQ(0u, status.completedResourceCount);
+ EXPECT_EQ(0u, status.completedResourceSize);
+ EXPECT_EQ(hasRequestedStyle, status.requiredResourceCountIsPrecise);
+ EXPECT_FALSE(status.complete());
+
+ if (hasRequestedStyle) {
+ EXPECT_EQ(1u, log.count(warning(ResultCode::Full, "Can't write region resources: database or disk is full")));
+ EXPECT_EQ(0u, log.uncheckedCount());
+ test.loop.stop();
+ }
+ };
+
+ download.setObserver(std::move(observer));
+ download.setState(OfflineRegionDownloadState::Active);
+
+ test.loop.run();
+ EXPECT_EQ(0u, log.uncheckedCount());
+}
+#endif // __QT__