#include #include #include #include #include #include "TestHarness.hpp" #include "core/UiQueue.hpp" #include "spec/ImageLimits.hpp" #include "ui/RemoteImageView.hpp" #include "ui/RemoteImages.hpp" #include "ui/TextEngine.hpp" using namespace dp::ui; using dp::HttpResponse; using dp::Result; using Done = std::function)>; namespace { std::string svg(int w, int h) { return ""; } struct Fake { // The fetcher captures this object by reference: never leave it installed after the test. ~Fake() { RemoteImages::instance().configure(RemoteImages::Config{}); RemoteImages::instance().clear(); } std::vector> pending; // held until released: async behaviour int calls = 0; bool manual = false; int status = 200; std::string body = svg(40, 20); RemoteImages::Fetcher fetcher() { return [this](const std::string& url, Done done) { ++calls; if (manual) { pending.emplace_back(url, std::move(done)); return; } HttpResponse r; r.status = status; r.body = body; done(std::move(r)); }; } void release(std::size_t i) { HttpResponse r; r.status = status; r.body = body; auto d = std::move(pending[i].second); d(std::move(r)); } }; void setup(Fake& f, double cooldown = 30, std::size_t bytes = 0, std::size_t entries = 0) { RemoteImages::Config c; c.fetcher = f.fetcher(); c.cooldownSeconds = cooldown; c.cacheBytes = bytes; c.maxEntries = entries; RemoteImages::instance().configure(c); RemoteImages::instance().clear(); } bool waitFor(const std::function& cond) { for (int i = 0; i < 600; ++i) { dp::UiQueue::instance().drain(); if (cond()) return true; std::this_thread::sleep_for(std::chrono::milliseconds(5)); } return false; } } // namespace DP_TEST(remoteimages_load_decode_and_cache_hit) { Fake f; setup(f); auto& ri = RemoteImages::instance(); const auto v0 = ri.version(); CHECK(ri.get("http://h/a.svg", 100) == nullptr); CHECK(ri.isLoading("http://h/a.svg")); CHECK(waitFor([&] { return ri.version() > v0; })); BitmapPtr b = ri.get("http://h/a.svg", 100); CHECK(b != nullptr); CHECK(b && b->width() > 0 && b->height() > 0); CHECK(!ri.isLoading("http://h/a.svg")); CHECK(ri.get("http://h/a.svg", 100) == b); CHECK_EQ(f.calls, 1); CHECK_EQ(ri.cachedBytes(), b->bytes()); } DP_TEST(remoteimages_inflight_dedup_and_concurrency_cap) { Fake f; f.manual = true; setup(f); auto& ri = RemoteImages::instance(); for (int i = 0; i < 5; ++i) ri.get("http://h/same.svg", 64); CHECK_EQ(f.calls, 1); for (int i = 0; i < 20; ++i) ri.get("http://h/" + std::to_string(i) + ".svg", 64); CHECK_EQ(f.calls, dp::spec::ImageLimits::kMaxConcurrentLoads); CHECK_EQ(ri.inFlight(), dp::spec::ImageLimits::kMaxConcurrentLoads); f.release(0); CHECK(waitFor([&] { return ri.get("http://h/same.svg", 64) != nullptr; })); CHECK_EQ(f.calls, dp::spec::ImageLimits::kMaxConcurrentLoads + 1); // a queued load took the free slot ri.clear(); f.pending.clear(); } DP_TEST(remoteimages_failure_memo_cooldown_and_retry) { Fake f; f.status = 500; setup(f, 0.15); auto& ri = RemoteImages::instance(); ri.get("http://h/bad.svg", 64); CHECK(ri.failed("http://h/bad.svg")); // synchronous fake failure CHECK(ri.get("http://h/bad.svg", 64) == nullptr); CHECK_EQ(f.calls, 1); // memo: no new request inside the cooldown std::this_thread::sleep_for(std::chrono::milliseconds(200)); f.status = 200; ri.get("http://h/bad.svg", 64); CHECK_EQ(f.calls, 2); // cooldown over: automatic retry CHECK(waitFor([&] { return ri.get("http://h/bad.svg", 64) != nullptr; })); f.status = 404; setup(f, 30); ri.get("http://h/x.svg", 64); CHECK(ri.failed("http://h/x.svg")); f.status = 200; ri.retry("http://h/x.svg"); // explicit retry ignores the cooldown CHECK(waitFor([&] { return ri.get("http://h/x.svg", 64) != nullptr; })); CHECK(!ri.failed("http://h/x.svg")); } DP_TEST(remoteimages_undecodable_body_fails) { Fake f; f.body = "this is not an image"; setup(f); auto& ri = RemoteImages::instance(); const auto v0 = ri.version(); ri.get("http://h/junk", 64); CHECK(waitFor([&] { return ri.version() > v0; })); CHECK(ri.failed("http://h/junk")); CHECK(ri.get("http://h/junk", 64) == nullptr); } DP_TEST(remoteimages_lru_byte_cap_and_entry_cap) { Fake f; f.body = svg(100, 100); setup(f, 30, 100 * 100 * 4 * 2 + 10); // room for two 100x100 bitmaps auto& ri = RemoteImages::instance(); for (int i = 0; i < 4; ++i) { const std::string u = "http://h/" + std::to_string(i); ri.get(u, 100); CHECK(waitFor([&] { return ri.get(u, 100) != nullptr; })); } CHECK(ri.cachedBytes() <= 100 * 100 * 4 * 2 + 10); CHECK(ri.entryCount() <= 3); const int before = f.calls; ri.get("http://h/0", 100); // evicted long ago: loads again CHECK_EQ(f.calls, before + 1); CHECK(waitFor([&] { return ri.get("http://h/0", 100) != nullptr; })); setup(f, 30, 0, 3); for (int i = 0; i < 8; ++i) { const std::string u = "http://h/e" + std::to_string(i); ri.get(u, 100); waitFor([&] { return ri.get(u, 100) != nullptr; }); CHECK(ri.entryCount() <= 3); } } DP_TEST(remoteimages_vector_reraster_larger) { Fake f; f.body = svg(50, 50); setup(f); auto& ri = RemoteImages::instance(); ri.get("http://h/v.svg", 64); CHECK(waitFor([&] { return ri.get("http://h/v.svg", 64) != nullptr; })); const int small = ri.get("http://h/v.svg", 64)->width(); BitmapPtr still = ri.get("http://h/v.svg", 512); // old bitmap stays while the larger one loads CHECK(still != nullptr); CHECK(waitFor([&] { BitmapPtr b = ri.get("http://h/v.svg", 512); return b && b->width() > small; })); CHECK_EQ(f.calls, 2); } DP_TEST(remoteimages_rejects_bad_urls) { Fake f; setup(f); auto& ri = RemoteImages::instance(); CHECK(ri.get("", 64) == nullptr); CHECK(ri.get(std::string(dp::spec::ImageLimits::kMaxUrlLength + 1, 'a'), 64) == nullptr); CHECK_EQ(f.calls, 0); CHECK_EQ(ri.entryCount(), 0u); } DP_TEST(remoteimages_view_factory_shows_placeholder_then_image) { Fake f; f.manual = true; setup(f); TextEngine::instance().init(); Env::reset(); ViewPtr v = RemoteImage("http://h/view.svg", ContentMode::Fit, RoundedRectShape(4, Color::rgb(0x808080))->frame(30, 30)); Size s = v->measure(Proposal::width(100)); CHECK(s.w > 29.0f && s.w < 31.0f); f.status = 200; f.release(0); auto& ri = RemoteImages::instance(); CHECK(waitFor([&] { return ri.get("http://h/view.svg", 512) != nullptr; })); ViewPtr v2 = RemoteImage("http://h/view.svg"); s = v2->measure(Proposal::width(100)); CHECK(s.w > 99.0f && s.h > 40.0f && s.h < 60.0f); // 40x20 svg scaled to width 100 }