[core] Eliminate platform::assetRoot()

I regenerated assets.zip so that all file paths have an `assets/` prefix, as the Android AssetFileSource implementation asserts, and removed `TEST_DATA` from the paths.
This commit is contained in:
John Firebaugh
2016-01-11 15:00:56 -08:00
parent 86c8446d3a
commit 5a7d2fd60e
29 changed files with 29 additions and 89 deletions

View File

@@ -30,6 +30,7 @@ int main(int argc, char *argv[]) {
double pixelRatio = 1.0;
static std::string output = "out.png";
std::string cache_file = "cache.sqlite";
std::string asset_root = ".";
std::vector<std::string> classes;
std::string token;
bool debug = false;
@@ -48,6 +49,7 @@ int main(int argc, char *argv[]) {
("debug", po::bool_switch(&debug)->default_value(debug), "Debug mode")
("output,o", po::value(&output)->value_name("file")->default_value(output), "Output file name")
("cache,d", po::value(&cache_file)->value_name("file")->default_value(cache_file), "Cache database file name")
("assets,d", po::value(&asset_root)->value_name("file")->default_value(asset_root), "Directory to which asset:// URLs will resolve")
;
try {
@@ -64,7 +66,7 @@ int main(int argc, char *argv[]) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fileSource(cache_file);
DefaultFileSource fileSource(cache_file, asset_root);
// Try to load the token from the environment.
if (!token.size()) {

View File

@@ -11,7 +11,6 @@
'sources': [
'../platform/android/src/log_android.cpp',
'../platform/android/src/asset_root.cpp',
'../platform/default/async_task.cpp',
'../platform/default/run_loop.cpp',
'../platform/default/thread.cpp',

View File

@@ -21,7 +21,6 @@
'../platform/darwin/log_nslog.mm',
'../platform/darwin/string_nsstring.mm',
'../platform/darwin/application_root.mm',
'../platform/darwin/asset_root.mm',
'../platform/darwin/image.mm',
'../platform/darwin/nsthread.mm',
'../platform/darwin/reachability.m',

View File

@@ -15,7 +15,6 @@
'../platform/default/string_stdlib.cpp',
'../platform/default/run_loop.cpp',
'../platform/default/application_root.cpp',
'../platform/default/asset_root.cpp',
'../platform/default/thread.cpp',
'../platform/default/image.cpp',
'../platform/default/png_reader.cpp',

View File

@@ -21,7 +21,6 @@
'../platform/darwin/log_nslog.mm',
'../platform/darwin/string_nsstring.mm',
'../platform/darwin/application_root.mm',
'../platform/darwin/asset_root.mm',
'../platform/darwin/image.mm',
'../platform/darwin/nsthread.mm',
'../platform/darwin/reachability.m',

View File

@@ -18,9 +18,6 @@ std::string lowercase(const std::string &string);
// Returns the path to the root folder of the application.
const std::string &applicationRoot();
// Returns the path to the asset location.
const std::string &assetRoot();
// Makes the current thread low priority.
void makeThreadLowPriority();

View File

@@ -7,7 +7,7 @@ namespace mbgl {
class DefaultFileSource : public FileSource {
public:
DefaultFileSource(const std::string& cachePath = ":memory:", const std::string& root = "");
DefaultFileSource(const std::string& cachePath, const std::string& assetRoot);
~DefaultFileSource() override;
void setAccessToken(const std::string&);

View File

@@ -99,7 +99,7 @@ private:
AssetFileSource::AssetFileSource(const std::string& root)
: thread(std::make_unique<util::Thread<Impl>>(
util::ThreadContext{"AssetFileSource", util::ThreadType::Worker, util::ThreadPriority::Regular},
root.empty() ? platform::assetRoot() : root)) {
root)) {
}
AssetFileSource::~AssetFileSource() = default;

View File

@@ -1,15 +0,0 @@
#include <mbgl/platform/platform.hpp>
#include "jni.hpp"
#include <libgen.h>
namespace mbgl {
namespace platform {
// Returns the path to the root folder of the application.
const std::string &assetRoot() {
return mbgl::android::apkPath;
}
}
}

View File

@@ -75,7 +75,10 @@ NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int a
return;
}
fileSource = std::make_unique<mbgl::DefaultFileSource>(mbgl::android::cachePath + "/mbgl-cache.db");
fileSource = std::make_unique<mbgl::DefaultFileSource>(
mbgl::android::cachePath + "/mbgl-cache.db",
mbgl::android::apkPath);
map = std::make_unique<mbgl::Map>(*this, *fileSource, MapMode::Continuous);
float zoomFactor = map->getMaxZoom() - map->getMinZoom() + 1;

View File

@@ -1,16 +0,0 @@
#import <Foundation/Foundation.h>
#include <mbgl/platform/platform.hpp>
namespace mbgl {
namespace platform {
// Returns the path to the root folder of the application.
const std::string &assetRoot() {
static const std::string root = []() -> std::string {
return [[[[NSBundle mainBundle] resourceURL] path] UTF8String];
}();
return root;
}
}
}

View File

@@ -67,7 +67,7 @@ private:
AssetFileSource::AssetFileSource(const std::string& root)
: thread(std::make_unique<util::Thread<Impl>>(
util::ThreadContext{"AssetFileSource", util::ThreadType::Worker, util::ThreadPriority::Regular},
root.empty() ? platform::assetRoot() : root)) {
root)) {
}
AssetFileSource::~AssetFileSource() = default;

View File

@@ -1,28 +0,0 @@
#include <mbgl/platform/platform.hpp>
#include <uv.h>
#include <libgen.h>
namespace mbgl {
namespace platform {
// Returns the path to the root folder of the application.
const std::string &assetRoot() {
static const std::string root = []() -> std::string {
size_t max = 0;
std::string dir;
do {
// Gradually increase the length of the string in case the path was truncated.
max += 256;
dir.resize(max);
uv_exepath(const_cast<char *>(dir.data()), &max);
} while (max == dir.size());
dir.resize(max - 1);
dir = dirname(const_cast<char *>(dir.c_str()));
return dir;
}();
return root;
}
}
}

View File

@@ -31,8 +31,8 @@ public:
OnlineFileSource onlineFileSource;
};
DefaultFileSource::DefaultFileSource(const std::string& cachePath, const std::string& root)
: impl(std::make_unique<DefaultFileSource::Impl>(cachePath, root)) {
DefaultFileSource::DefaultFileSource(const std::string& cachePath, const std::string& assetRoot)
: impl(std::make_unique<DefaultFileSource::Impl>(cachePath, assetRoot)) {
}
DefaultFileSource::~DefaultFileSource() = default;

View File

@@ -280,7 +280,7 @@ std::chrono::steady_clock::duration MGLDurationInSeconds(float duration)
NSString *libraryDirectory = [paths objectAtIndex:0];
fileCachePath = [libraryDirectory stringByAppendingPathComponent:@"cache.db"];
}
_mbglFileSource = new mbgl::DefaultFileSource([fileCachePath UTF8String]);
_mbglFileSource = new mbgl::DefaultFileSource([fileCachePath UTF8String], [[[[NSBundle mainBundle] resourceURL] path] UTF8String]);
// setup mbgl map
_mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous);

View File

@@ -105,7 +105,7 @@ int main(int argc, char *argv[]) {
view = std::make_unique<GLFWView>(fullscreen, benchmark);
mbgl::DefaultFileSource fileSource("/tmp/mbgl-cache.db");
mbgl::DefaultFileSource fileSource("/tmp/mbgl-cache.db", ".");
fileSource.setMaximumCacheEntrySize(1 * 1024 * 1024); // 1 MB
fileSource.setMaximumCacheSize(50 * 1024 * 1024); // 50 MB

View File

@@ -246,7 +246,7 @@ public:
error:nil];
NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:@"cache.db"];
NSString *cachePath = cacheURL ? cacheURL.path : @"";
_mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String);
_mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [[[[NSBundle mainBundle] resourceURL] path] UTF8String]);
_mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous);
@@ -524,8 +524,7 @@ public:
}
if (![styleURL scheme]) {
// Assume a relative path into the application’s resource folder,
// defined in mbgl::platform::assetRoot().
// Assume a relative path into the application’s resource folder.
styleURL = [NSURL URLWithString:[@"asset://" stringByAppendingString:styleURL.absoluteString]];
}

View File

@@ -18,9 +18,10 @@ TEST(API, RepeatedRender) {
auto display = std::make_shared<mbgl::HeadlessDisplay>();
HeadlessView view(display, 1, 256, 512);
#ifdef MBGL_ASSET_ZIP
DefaultFileSource fileSource(":memory", "test/fixtures/api/assets.zip");
// Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/`
DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets.zip");
#else
DefaultFileSource fileSource;
DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets");
#endif
Log::setObserver(std::make_unique<FixtureLogObserver>());
@@ -40,7 +41,7 @@ TEST(API, RepeatedRender) {
}
{
map.setStyleJSON(style, "TEST_DATA/suite");
map.setStyleJSON(style, "");
std::promise<PremultipliedImage> promise;
map.renderStill([&promise](std::exception_ptr, PremultipliedImage&& image) {
promise.set_value(std::move(image));

Binary file not shown.

View File

@@ -10,5 +10,5 @@
"name": "Streets",
"scheme": "xyz",
"tilejson": "2.0.0",
"tiles": [ "asset://TEST_DATA/fixtures/tiles/streets/{z}-{x}-{y}.vector.pbf" ]
"tiles": [ "asset://streets/{z}-{x}-{y}.vector.pbf" ]
}

View File

@@ -4,7 +4,7 @@
"sources": {
"mapbox": {
"type": "vector",
"url": "asset://TEST_DATA/fixtures/tiles/streets.json"
"url": "asset://streets.json"
}
},
"layers": [{

Binary file not shown.

0
test/fixtures/storage/assets/empty vendored Normal file
View File

View File

@@ -11,9 +11,10 @@ namespace {
std::string getFileSourceRoot() {
#ifdef MBGL_ASSET_ZIP
// Regenerate with `cd test/fixtures/storage/ && zip -r assets.zip assets/`
return "test/fixtures/storage/assets.zip";
#else
return "";
return "test/fixtures/storage/assets";
#endif
}
@@ -22,7 +23,7 @@ public:
TestWorker(mbgl::AssetFileSource* fs_) : fs(fs_) {}
void run(std::function<void()> endCallback) {
const std::string asset("asset://TEST_DATA/fixtures/storage/nonempty");
const std::string asset("asset://nonempty");
requestCallback = [this, asset, endCallback](mbgl::Response res) {
EXPECT_EQ(nullptr, res.error);
@@ -100,7 +101,7 @@ TEST_F(Storage, AssetEmptyFile) {
AssetFileSource fs(getFileSourceRoot());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/empty" }, [&](Response res) {
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://empty" }, [&](Response res) {
req.reset();
EXPECT_EQ(nullptr, res.error);
EXPECT_EQ(false, res.stale);
@@ -122,7 +123,7 @@ TEST_F(Storage, AssetNonEmptyFile) {
AssetFileSource fs(getFileSourceRoot());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/nonempty" }, [&](Response res) {
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://nonempty" }, [&](Response res) {
req.reset();
EXPECT_EQ(nullptr, res.error);
EXPECT_EQ(false, res.stale);
@@ -144,7 +145,7 @@ TEST_F(Storage, AssetNonExistentFile) {
AssetFileSource fs(getFileSourceRoot());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/does_not_exist" }, [&](Response res) {
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://does_not_exist" }, [&](Response res) {
req.reset();
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::NotFound, res.error->reason);
@@ -167,7 +168,7 @@ TEST_F(Storage, AssetReadDirectory) {
AssetFileSource fs(getFileSourceRoot());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage" }, [&](Response res) {
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://directory" }, [&](Response res) {
req.reset();
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::NotFound, res.error->reason);