File size: 13,471 Bytes
5cdf637 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | // SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors
#include <atomic>
#include <chrono>
#include <cstddef>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <utility>
#include <variant>
#include <vector>
#include "gtest/gtest.h"
#include "gui/gui.h"
#include "odb/db.h"
#include "odb/geom.h"
#include "tile_generator.h"
#include "web_chart.h"
#include "web_painter.h"
#include "web_viewer_hook.h"
namespace web {
namespace {
TEST(WebPainterTest, RecordsRect)
{
WebPainter painter(odb::Rect(0, 0, 1000, 1000), 0.256);
painter.setPen(gui::Painter::kRed, /*cosmetic=*/true);
painter.setBrush(gui::Painter::kBlue);
painter.drawRect(odb::Rect(10, 20, 100, 200));
ASSERT_EQ(painter.ops().size(), 1u);
const auto* rect = std::get_if<DrawRectOp>(painter.ops().data());
ASSERT_NE(rect, nullptr);
EXPECT_EQ(rect->rect.xMin(), 10);
EXPECT_EQ(rect->rect.yMax(), 200);
EXPECT_EQ(rect->pen.color.r, 0xff);
EXPECT_TRUE(rect->pen.cosmetic);
EXPECT_EQ(rect->brush.color.b, 0xff);
}
TEST(WebPainterTest, RecordsLine)
{
WebPainter painter(odb::Rect(0, 0, 1000, 1000), 1.0);
painter.setPen(gui::Painter::kGreen, /*cosmetic=*/false, /*width=*/3);
painter.drawLine(odb::Point(1, 2), odb::Point(3, 4));
ASSERT_EQ(painter.ops().size(), 1u);
const auto* line = std::get_if<DrawLineOp>(painter.ops().data());
ASSERT_NE(line, nullptr);
EXPECT_EQ(line->p1.x(), 1);
EXPECT_EQ(line->p2.y(), 4);
EXPECT_EQ(line->pen.width, 3);
}
TEST(WebPainterTest, KeepsWideStrokeRectNearTileEdge)
{
WebPainter painter(odb::Rect(0, 0, 100, 100), 1.0);
painter.setPen(gui::Painter::kRed, /*cosmetic=*/true, /*width=*/4);
painter.drawRect(odb::Rect(-2, 10, -1, 20));
ASSERT_EQ(painter.ops().size(), 1u);
EXPECT_NE(std::get_if<DrawRectOp>(painter.ops().data()), nullptr);
}
TEST(WebPainterTest, KeepsWideStrokeLineNearTileEdge)
{
WebPainter painter(odb::Rect(0, 0, 100, 100), 1.0);
painter.setPen(gui::Painter::kRed, /*cosmetic=*/true, /*width=*/4);
painter.drawLine(odb::Point(-10, 101), odb::Point(110, 101));
ASSERT_EQ(painter.ops().size(), 1u);
EXPECT_NE(std::get_if<DrawLineOp>(painter.ops().data()), nullptr);
}
TEST(WebPainterTest, KeepsWideStrokeCircleNearTileEdge)
{
WebPainter painter(odb::Rect(0, 0, 100, 100), 1.0);
painter.setPen(gui::Painter::kRed, /*cosmetic=*/true, /*width=*/4);
painter.drawCircle(102, 50, 1);
ASSERT_EQ(painter.ops().size(), 1u);
EXPECT_NE(std::get_if<DrawCircleOp>(painter.ops().data()), nullptr);
}
TEST(WebPainterTest, SaveRestoreState)
{
WebPainter painter(odb::Rect(0, 0, 1000, 1000), 1.0);
painter.setPen(gui::Painter::kRed);
painter.saveState();
painter.setPen(gui::Painter::kGreen);
painter.drawRect(odb::Rect(0, 0, 10, 10));
painter.restoreState();
painter.drawRect(odb::Rect(0, 0, 10, 10));
ASSERT_EQ(painter.ops().size(), 2u);
const auto* r1 = std::get_if<DrawRectOp>(painter.ops().data());
const auto* r2 = std::get_if<DrawRectOp>(&painter.ops()[1]);
ASSERT_NE(r1, nullptr);
ASSERT_NE(r2, nullptr);
EXPECT_EQ(r1->pen.color.g, 0xff); // green
EXPECT_EQ(r2->pen.color.r, 0xff); // red (restored)
}
// Helper: register a dummy client so pause() actually blocks
// (with no clients, pause() returns immediately).
static std::size_t registerDummyClient(WebViewerHook& hook)
{
return hook.sessions().add([](const std::string&) {});
}
TEST(SessionRegistryTest, BroadcastAndWaitNoSessions)
{
SessionRegistry registry;
// No sessions registered — should return true immediately.
EXPECT_TRUE(registry.broadcastAndWait("{}", std::chrono::milliseconds(100)));
}
TEST(SessionRegistryTest, BroadcastAndWaitFencesComplete)
{
SessionRegistry registry;
std::string received;
// Register a session whose SendAndWaitFn invokes the fence only after
// the simulated write has completed.
auto token = registry.add(
[&received](const std::string& json) { received = json; },
[&received](const std::string& json, std::function<void()> fence) {
std::thread([&received, json, fence = std::move(fence)]() {
received = json;
fence();
}).detach();
});
EXPECT_TRUE(registry.broadcastAndWait(R"({"type":"shutdown"})",
std::chrono::seconds(2)));
EXPECT_EQ(received, R"({"type":"shutdown"})");
registry.remove(token);
}
TEST(SessionRegistryTest, BroadcastAndWaitTimesOut)
{
SessionRegistry registry;
// Register a session whose SendAndWaitFn silently drops the fence.
auto token = registry.add(
[](const std::string&) {},
[](const std::string&, const std::function<void()>& /*fence*/) {
/* never called */
});
const auto t0 = std::chrono::steady_clock::now();
EXPECT_FALSE(registry.broadcastAndWait("{}", std::chrono::milliseconds(200)));
const auto elapsed = std::chrono::steady_clock::now() - t0;
// Should have waited approximately the timeout, not longer.
EXPECT_GE(elapsed, std::chrono::milliseconds(150));
EXPECT_LT(elapsed, std::chrono::seconds(2));
registry.remove(token);
}
TEST(SessionRegistryTest, BroadcastAndWaitDeadSession)
{
SessionRegistry registry;
std::atomic<bool> send_called{false};
// Register a session whose SendAndWaitFn calls the fence immediately
// (simulating a dead session that signals right away).
auto token = registry.add(
[&send_called](const std::string&) { send_called = true; },
[&send_called](const std::string&, const std::function<void()>& fence) {
send_called = true;
fence();
});
EXPECT_TRUE(registry.broadcastAndWait("{}", std::chrono::milliseconds(100)));
EXPECT_TRUE(send_called);
registry.remove(token);
}
TEST(SessionRegistryTest, BroadcastAndWaitLegacySendOnly)
{
SessionRegistry registry;
std::string received;
// Register with SendFn only (no SendAndWaitFn) — legacy path.
auto token
= registry.add([&received](const std::string& json) { received = json; });
// Should return true immediately since there are no fenceable sessions.
EXPECT_TRUE(registry.broadcastAndWait(R"({"ok":true})",
std::chrono::milliseconds(100)));
EXPECT_EQ(received, R"({"ok":true})");
registry.remove(token);
}
TEST(SessionRegistryTest, WaitForClientCanBeInterrupted)
{
SessionRegistry registry;
std::atomic<bool> interrupted{false};
std::atomic<bool> waiting{false};
bool got_client = true;
std::thread waiter([&]() {
got_client = registry.waitForClient(/*timeout_seconds=*/30, [&]() {
waiting.store(true);
return interrupted.load();
});
});
for (int i = 0; i < 200 && !waiting.load(); ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
ASSERT_TRUE(waiting.load());
const auto t0 = std::chrono::steady_clock::now();
interrupted.store(true);
registry.notifyClientWaiters();
waiter.join();
const auto elapsed = std::chrono::steady_clock::now() - t0;
EXPECT_FALSE(got_client);
EXPECT_LT(elapsed, std::chrono::seconds(5));
}
TEST(WebViewerHookTest, PauseUnblocksOnContinue)
{
WebViewerHook hook;
auto token = registerDummyClient(hook);
std::atomic<bool> resumed{false};
std::thread waiter([&]() {
hook.pause(/*timeout_ms=*/0);
resumed.store(true);
});
// Wait up to 500ms for the waiter to be inside pause(). isPaused()
// should flip to true during that window.
for (int i = 0; i < 50 && !hook.isPaused(); ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
EXPECT_TRUE(hook.isPaused());
hook.continueExecution();
waiter.join();
EXPECT_TRUE(resumed.load());
EXPECT_FALSE(hook.isPaused());
hook.sessions().remove(token);
}
TEST(WebViewerHookTest, PauseReturnsWhenNoClientsConnect)
{
WebViewerHook hook;
// No registered clients → pause waits for a client, then gives up.
// Register a client after a short delay to unblock faster.
std::thread late_register([&]() {
// Don't register — let the wait-for-client timeout expire.
// The internal timeout is 30s but we can't speed that up in a
// unit test without injecting it as a parameter, so just verify
// that pause() eventually returns and doesn't hang forever.
});
late_register.join();
// Instead, test the fast path: register a client, then remove it
// before calling pause. pause() sees no clients and waits, but
// we add one quickly to unblock.
std::atomic<bool> done{false};
std::thread pauser([&]() {
hook.pause(/*timeout_ms=*/0);
done.store(true);
});
// Give pause() a moment to enter waitForClient, then register.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
auto token = registerDummyClient(hook);
// Now pause is inside the pause CV wait — continue to unblock.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
hook.continueExecution();
pauser.join();
EXPECT_TRUE(done.load());
EXPECT_FALSE(hook.isPaused());
hook.sessions().remove(token);
}
TEST(WebViewerHookTest, ContinueInterruptsWaitForClient)
{
WebViewerHook hook;
std::atomic<bool> done{false};
std::thread pauser([&]() {
hook.pause(/*timeout_ms=*/0);
done.store(true);
});
// Give pause() a chance to enter the no-client wait. continueExecution()
// must wake that path instead of relying on the 30s client-connect timeout.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
const auto t0 = std::chrono::steady_clock::now();
hook.continueExecution();
pauser.join();
const auto elapsed = std::chrono::steady_clock::now() - t0;
EXPECT_TRUE(done.load());
EXPECT_LT(elapsed, std::chrono::seconds(5));
EXPECT_FALSE(hook.isPaused());
const auto token = registerDummyClient(hook);
const auto t1 = std::chrono::steady_clock::now();
hook.pause(/*timeout_ms=*/50);
const auto next_elapsed = std::chrono::steady_clock::now() - t1;
EXPECT_GE(next_elapsed, std::chrono::milliseconds(40));
hook.sessions().remove(token);
}
TEST(WebViewerHookTest, DestructorUnblocksPausedThread)
{
std::atomic<bool> resumed{false};
std::thread waiter;
{
WebViewerHook hook;
registerDummyClient(hook);
waiter = std::thread([&]() {
hook.pause(/*timeout_ms=*/0);
resumed.store(true);
});
// Wait until the thread is definitely inside pause().
for (int i = 0; i < 200 && !hook.isPaused(); ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
ASSERT_TRUE(hook.isPaused())
<< "Thread didn't reach pause() within 2s — test is broken";
} // hook destroyed → should signal cv → waiter returns
waiter.join();
EXPECT_TRUE(resumed.load());
}
TEST(WebViewerHookTest, PauseTimeoutReturns)
{
WebViewerHook hook;
registerDummyClient(hook);
const auto t0 = std::chrono::steady_clock::now();
hook.pause(/*timeout_ms=*/50);
const auto elapsed = std::chrono::steady_clock::now() - t0;
EXPECT_GE(elapsed, std::chrono::milliseconds(40));
EXPECT_LT(elapsed, std::chrono::milliseconds(5000));
EXPECT_FALSE(hook.isPaused());
}
TEST(WebViewerHookTest, SessionBroadcast)
{
WebViewerHook hook;
std::vector<std::string> seen;
std::mutex m;
const auto token = hook.sessions().add([&](const std::string& s) {
std::lock_guard<std::mutex> lock(m);
seen.push_back(s);
});
hook.redraw();
hook.sessions().remove(token);
hook.redraw(); // should not reach our callback
std::lock_guard<std::mutex> lock(m);
ASSERT_EQ(seen.size(), 1u);
EXPECT_NE(seen[0].find("debug_refresh"), std::string::npos);
}
TEST(WebViewerHookTest, CreatesChartAndTracksIt)
{
WebViewerHook hook;
gui::Chart* c1 = hook.createChart("GPL", "iter", {"hpwl", "overflow"});
ASSERT_NE(c1, nullptr);
c1->setXAxisFormat("%d");
c1->addPoint(0, {1.0, 2.0});
c1->addPoint(1, {3.0, 4.0});
const auto charts = hook.charts();
ASSERT_EQ(charts.size(), 1u);
EXPECT_EQ(charts[0]->name(), "GPL");
EXPECT_EQ(charts[0]->xAxisFormat(), "%d");
const auto pts = charts[0]->points();
ASSERT_EQ(pts.size(), 2u);
EXPECT_EQ(pts[1].x, 1);
EXPECT_EQ(pts[1].ys[1], 4.0);
}
TEST(WebRasterizerTest, HonorsCosmeticPenWidth)
{
std::unique_ptr<odb::dbDatabase, void (*)(odb::dbDatabase*)> db(
odb::dbDatabase::create(), odb::dbDatabase::destroy);
TileGenerator gen(db.get(), /*sta=*/nullptr, /*logger=*/nullptr);
const odb::Rect dbu_tile(0, 0, 256, 256);
const TileFrame frame{
.origin_x = 0.0, .origin_y = 0.0, .scale = 1.0, .cull = dbu_tile};
WebPainter narrow(dbu_tile, 1.0);
narrow.setPen(gui::Painter::kRed, /*cosmetic=*/true, /*width=*/1);
narrow.drawLine(odb::Point(10, 128), odb::Point(40, 128));
WebPainter wide(dbu_tile, 1.0);
wide.setPen(gui::Painter::kRed, /*cosmetic=*/true, /*width=*/4);
wide.drawLine(odb::Point(10, 128), odb::Point(40, 128));
const int image_bytes = 256 * 256 * 4;
std::vector<unsigned char> narrow_image(image_bytes, 0);
std::vector<unsigned char> wide_image(image_bytes, 0);
gen.rasterizeWebPainterOps(narrow_image, narrow.ops(), frame);
gen.rasterizeWebPainterOps(wide_image, wide.ops(), frame);
const auto alpha_at
= [](const std::vector<unsigned char>& image, int x, int y) {
return image[(y * 256 + x) * 4 + 3];
};
const int screen_y = 255 - 128;
EXPECT_EQ(alpha_at(narrow_image, 20, screen_y - 1), 0);
EXPECT_GT(alpha_at(wide_image, 20, screen_y - 1), 0);
}
} // namespace
} // namespace web
|