diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-04-23 09:42:31 +0200 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-04-25 10:00:23 +0300 |
commit | f5773adcce7f1395fd422f74754ed8d2b1ef90d4 (patch) | |
tree | 9dd6c56201ae1fe4fdf8c23260bcf1c139d84962 /test/renderer | |
parent | 83c33cdeb25831cde370b543d9e10c8d1859aec8 (diff) | |
download | qtlocation-mapboxgl-f5773adcce7f1395fd422f74754ed8d2b1ef90d4.tar.gz |
[core] Add unit test for ImageManager::reduceMemoryUsage
Diffstat (limited to 'test/renderer')
-rw-r--r-- | test/renderer/image_manager.test.cpp | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/test/renderer/image_manager.test.cpp b/test/renderer/image_manager.test.cpp index 51b257094a..a5fffce479 100644 --- a/test/renderer/image_manager.test.cpp +++ b/test/renderer/image_manager.test.cpp @@ -164,8 +164,10 @@ TEST(ImageManager, NotifiesRequestorImmediatelyIfDependenciesAreSatisfied) { class StubImageManagerObserver : public ImageManagerObserver { public: int count = 0; - virtual void onStyleImageMissing(const std::string&, std::function<void()> done) override { + std::function<void (const std::string&)> imageMissing = [](const std::string&){}; + virtual void onStyleImageMissing(const std::string& id, std::function<void()> done) override { count++; + imageMissing(id); done(); } }; @@ -234,3 +236,60 @@ TEST(ImageManager, OnStyleImageMissingAfterSpriteLoaded) { EXPECT_EQ(observer.count, 1); ASSERT_TRUE(notified); } + +TEST(ImageManager, ReduceMemoryUsage) { + ImageManager imageManager; + StubImageManagerObserver observer; + + observer.imageMissing = [&imageManager] (const std::string& id) { + imageManager.addImage(makeMutable<style::Image::Impl>(id, PremultipliedImage({ 16, 16 }), 1)); + }; + + imageManager.setObserver(&observer); + imageManager.setLoaded(true); + + // Single requestor + { + std::unique_ptr<StubImageRequestor> requestor = std::make_unique<StubImageRequestor>(imageManager); + imageManager.getImages(*requestor, std::make_pair(ImageDependencies{{"missing", ImageType::Icon}}, 0ull)); + EXPECT_EQ(observer.count, 1); + ASSERT_FALSE(imageManager.getImage("missing") == nullptr); + } + + // Reduce memory usage and check that unused image was deleted. + imageManager.reduceMemoryUse(); + ASSERT_TRUE(imageManager.getImage("missing") == nullptr); + + // Multiple requestors + { + std::unique_ptr<StubImageRequestor> requestor1 = std::make_unique<StubImageRequestor>(imageManager); + std::unique_ptr<StubImageRequestor> requestor2 = std::make_unique<StubImageRequestor>(imageManager); + imageManager.getImages(*requestor1, std::make_pair(ImageDependencies{{"missing", ImageType::Icon}}, 0ull)); + imageManager.getImages(*requestor2, std::make_pair(ImageDependencies{{"missing", ImageType::Icon}}, 1ull)); + EXPECT_EQ(observer.count, 2); + ASSERT_FALSE(imageManager.getImage("missing") == nullptr); + } + + // Reduce memory usage and check that unused image was deleted when all requestors are destructed. + imageManager.reduceMemoryUse(); + ASSERT_TRUE(imageManager.getImage("missing") == nullptr); + + // Multiple requestors, check that image resource is not destroyed if there is at least 1 requestor that uses it. + std::unique_ptr<StubImageRequestor> requestor = std::make_unique<StubImageRequestor>(imageManager); + { + std::unique_ptr<StubImageRequestor> requestor1 = std::make_unique<StubImageRequestor>(imageManager); + imageManager.getImages(*requestor, std::make_pair(ImageDependencies{{"missing", ImageType::Icon}}, 0ull)); + imageManager.getImages(*requestor1, std::make_pair(ImageDependencies{{"missing", ImageType::Icon}}, 1ull)); + EXPECT_EQ(observer.count, 3); + ASSERT_FALSE(imageManager.getImage("missing") == nullptr); + } + + // Reduce memory usage and check that requested image is not destructed. + imageManager.reduceMemoryUse(); + ASSERT_FALSE(imageManager.getImage("missing") == nullptr); + + // Release last requestor and check if resource was released after reduceMemoryUse(). + requestor.reset(); + imageManager.reduceMemoryUse(); + ASSERT_TRUE(imageManager.getImage("missing") == nullptr); +} |