Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
vm_tools: Adds tests for xdg_wm_base
Browse files Browse the repository at this point in the history
+ Adds tests for xdg_wm_base
* Modifies some test fixtures to accommodate these tests
* Renames some variables to clear up what they refer to
    Namely this involves adding `sommelier_` prefix to variables that
    are sommelier's objects, while `client_` prefix to variables that
    are the client's objects.

BUG=b:255439799
TEST=meson build && ninja -C build && build/sommelier_test

Change-Id: I893e332e72e8a10456f9fde1de5e2005da41c62c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/4399946
Reviewed-by: Chloe Pelling <[email protected]>
Tested-by: Justin Huang <[email protected]>
Commit-Queue: Justin Huang <[email protected]>
Auto-Submit: Justin Huang <[email protected]>
  • Loading branch information
Justin Huang authored and Chromeos LUCI committed May 10, 2023
1 parent ad79613 commit 7f5c420
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 33 deletions.
123 changes: 94 additions & 29 deletions sommelier-xdg-shell-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include "sommelier-xdg-shell.h" // NOLINT(build/include_directory)
#include "xdg-shell-shim.h" // NOLINT(build/include_directory)

// Fake constant serial number to be used in the tests below.
const uint32_t kFakeSerial = 721077;

namespace vm_tools {
namespace sommelier {

Expand Down Expand Up @@ -51,12 +54,66 @@ class XdgShellTest : public WaylandTestBase {
std::unique_ptr<FakeWaylandClient> client;
};

class XdgWmBaseTest : public XdgShellTest {
public:
void SetUp() override {
sommelier_xdg_wm_base = nullptr;
EXPECT_CALL(mock_xdg_wm_base_shim_, add_listener(_, _, _))
.WillOnce([this](struct xdg_wm_base* xdg_wm_base,
const xdg_wm_base_listener* listener,
void* user_data) {
sommelier_xdg_wm_base =
static_cast<struct sl_host_xdg_shell*>(user_data);
xdg_wm_base_add_listener(xdg_wm_base, listener, user_data);
return 0;
});
WaylandTestBase::SetUp();

client_surface = client->CreateSurface();
Pump();
}

protected:
// Sommelier's xdg_wm_base instance.
struct sl_host_xdg_shell* sommelier_xdg_wm_base;
wl_surface* client_surface;
};

TEST_F(XdgWmBaseTest, CreatePositioner_ForwardsCorrectly) {
EXPECT_CALL(mock_xdg_wm_base_shim_,
create_positioner(sommelier_xdg_wm_base->proxy));
xdg_wm_base_create_positioner(client->GetXdgWmBase());
}

TEST_F(XdgWmBaseTest, GetXdgSurface_ForwardsCorrectly) {
EXPECT_CALL(mock_xdg_wm_base_shim_,
get_xdg_surface(sommelier_xdg_wm_base->proxy, _));
xdg_wm_base_get_xdg_surface(client->GetXdgWmBase(), client_surface);
}

TEST_F(XdgWmBaseTest, Ping_SendsCorrectly) {
EXPECT_CALL(mock_xdg_wm_base_shim_,
get_user_data(sommelier_xdg_wm_base->proxy))
.WillOnce([](struct xdg_wm_base* xdg_wm_base) {
return xdg_wm_base_get_user_data(xdg_wm_base);
});
EXPECT_CALL(mock_xdg_wm_base_shim_, send_ping(_, kFakeSerial));

HostEventHandler(sommelier_xdg_wm_base->proxy)
->ping(nullptr, sommelier_xdg_wm_base->proxy, kFakeSerial);
}

TEST_F(XdgWmBaseTest, Pong_ForwardsCorrectly) {
EXPECT_CALL(mock_xdg_wm_base_shim_, pong(_, kFakeSerial));
xdg_wm_base_pong(client->GetXdgWmBase(), kFakeSerial);
}

class XdgPositionerTest : public XdgShellTest {
public:
void SetUp() override {
WaylandTestBase::SetUp();

captured_proxy = nullptr;
sommelier_positioner = nullptr;

EXPECT_CALL(mock_xdg_wm_base_shim_, create_positioner(_))
.WillOnce([](struct xdg_wm_base* xdg_wm_base) {
Expand All @@ -67,69 +124,73 @@ class XdgPositionerTest : public XdgShellTest {
.WillOnce(
[this](struct xdg_positioner* xdg_positioner, void* user_data) {
// Capture the object pointers so we can verify below.
captured_proxy = xdg_positioner;
sommelier_positioner = xdg_positioner;
xdg_positioner_set_user_data(xdg_positioner, user_data);
});

positioner = client->CreatePositioner();
client_positioner = client->CreatePositioner();

Pump();
}

protected:
struct xdg_positioner* captured_proxy;
struct xdg_positioner* positioner;
struct xdg_positioner* sommelier_positioner;
struct xdg_positioner* client_positioner;
};

TEST_F(XdgPositionerTest, SetSize_ForwardsUnscaled) {
EXPECT_CALL(mock_xdg_positioner_shim_, set_size(captured_proxy, 100, 100));
xdg_positioner_set_size(positioner, 100, 100);
EXPECT_CALL(mock_xdg_positioner_shim_,
set_size(sommelier_positioner, 100, 100));
xdg_positioner_set_size(client_positioner, 100, 100);
}

TEST_F(XdgPositionerTest, SetSize_AppliesCtxScale) {
ctx.scale = 2.0;
EXPECT_CALL(mock_xdg_positioner_shim_, set_size(captured_proxy, 50, 50));
EXPECT_CALL(mock_xdg_positioner_shim_,
set_size(sommelier_positioner, 50, 50));

xdg_positioner_set_size(positioner, 100, 100);
xdg_positioner_set_size(client_positioner, 100, 100);
}

TEST_F(XdgPositionerTest, SetSize_UnscaledWithDirectScale) {
ctx.use_direct_scale = true;
EXPECT_CALL(mock_xdg_positioner_shim_, set_size(captured_proxy, 100, 100));
EXPECT_CALL(mock_xdg_positioner_shim_,
set_size(sommelier_positioner, 100, 100));

xdg_positioner_set_size(positioner, 100, 100);
xdg_positioner_set_size(client_positioner, 100, 100);
}

TEST_F(XdgPositionerTest, SetSize_AppliesXdgScaleWithDirectScale) {
ctx.use_direct_scale = true;
ctx.xdg_scale_x = 2.0;
ctx.xdg_scale_y = 4.0;
EXPECT_CALL(mock_xdg_positioner_shim_, set_size(captured_proxy, 50, 25));
EXPECT_CALL(mock_xdg_positioner_shim_,
set_size(sommelier_positioner, 50, 25));

xdg_positioner_set_size(positioner, 100, 100);
xdg_positioner_set_size(client_positioner, 100, 100);
}

TEST_F(XdgPositionerTest, SetAnchorRect_ForwardsUnscaled) {
EXPECT_CALL(mock_xdg_positioner_shim_,
set_anchor_rect(captured_proxy, 0, 0, 100, 100));
set_anchor_rect(sommelier_positioner, 0, 0, 100, 100));

xdg_positioner_set_anchor_rect(positioner, 0, 0, 100, 100);
xdg_positioner_set_anchor_rect(client_positioner, 0, 0, 100, 100);
}

TEST_F(XdgPositionerTest, SetAnchorRect_AppliesCtxScale) {
ctx.scale = 2.0;
EXPECT_CALL(mock_xdg_positioner_shim_,
set_anchor_rect(captured_proxy, 0, 0, 50, 50));
set_anchor_rect(sommelier_positioner, 0, 0, 50, 50));

xdg_positioner_set_anchor_rect(positioner, 0, 0, 100, 100);
xdg_positioner_set_anchor_rect(client_positioner, 0, 0, 100, 100);
}

TEST_F(XdgPositionerTest, SetAnchorRect_UnscaledWithDirectScale) {
ctx.use_direct_scale = true;
EXPECT_CALL(mock_xdg_positioner_shim_,
set_anchor_rect(captured_proxy, 0, 0, 100, 100));
set_anchor_rect(sommelier_positioner, 0, 0, 100, 100));

xdg_positioner_set_anchor_rect(positioner, 0, 0, 100, 100);
xdg_positioner_set_anchor_rect(client_positioner, 0, 0, 100, 100);
}

TEST_F(XdgPositionerTest, SetAnchorRect_AppliesXdgScaleWithDirectScale) {
Expand All @@ -138,41 +199,45 @@ TEST_F(XdgPositionerTest, SetAnchorRect_AppliesXdgScaleWithDirectScale) {
ctx.xdg_scale_y = 4.0;

EXPECT_CALL(mock_xdg_positioner_shim_,
set_anchor_rect(captured_proxy, 0, 0, 50, 25));
set_anchor_rect(sommelier_positioner, 0, 0, 50, 25));

xdg_positioner_set_anchor_rect(positioner, 0, 0, 100, 100);
xdg_positioner_set_anchor_rect(client_positioner, 0, 0, 100, 100);
}

TEST_F(XdgPositionerTest, SetOffset_ForwardsUnscaled) {
EXPECT_CALL(mock_xdg_positioner_shim_, set_offset(captured_proxy, 100, 100));
EXPECT_CALL(mock_xdg_positioner_shim_,
set_offset(sommelier_positioner, 100, 100));

xdg_positioner_set_offset(positioner, 100, 100);
xdg_positioner_set_offset(client_positioner, 100, 100);
}

TEST_F(XdgPositionerTest, SetOffset_AppliesCtxScale) {
ctx.scale = 2.0;

EXPECT_CALL(mock_xdg_positioner_shim_, set_offset(captured_proxy, 50, 50));
EXPECT_CALL(mock_xdg_positioner_shim_,
set_offset(sommelier_positioner, 50, 50));

xdg_positioner_set_offset(positioner, 100, 100);
xdg_positioner_set_offset(client_positioner, 100, 100);
}

TEST_F(XdgPositionerTest, SetOffset_UnscaledWithDirectScale) {
ctx.use_direct_scale = true;

EXPECT_CALL(mock_xdg_positioner_shim_, set_offset(captured_proxy, 100, 100));
EXPECT_CALL(mock_xdg_positioner_shim_,
set_offset(sommelier_positioner, 100, 100));

xdg_positioner_set_offset(positioner, 100, 100);
xdg_positioner_set_offset(client_positioner, 100, 100);
}

TEST_F(XdgPositionerTest, SetOffset_AppliesXdgScaleWithDirectScale) {
ctx.use_direct_scale = true;
ctx.xdg_scale_x = 2.0;
ctx.xdg_scale_y = 4.0;

EXPECT_CALL(mock_xdg_positioner_shim_, set_offset(captured_proxy, 50, 25));
EXPECT_CALL(mock_xdg_positioner_shim_,
set_offset(sommelier_positioner, 50, 25));

xdg_positioner_set_offset(positioner, 100, 100);
xdg_positioner_set_offset(client_positioner, 100, 100);
}

} // namespace sommelier
Expand Down
5 changes: 5 additions & 0 deletions testing/sommelier-test-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ uint32_t AuraToplevelId(sl_window* window) {
return wl_proxy_get_id(reinterpret_cast<wl_proxy*>(window->aura_toplevel));
}

uint32_t SurfaceId(wl_surface* wl_surface) {
assert(wl_surface);
return wl_proxy_get_id(reinterpret_cast<wl_proxy*>(wl_surface));
}

} // namespace sommelier
} // namespace vm_tools
2 changes: 2 additions & 0 deletions testing/sommelier-test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct WlToListener;
using type = Listener; \
};

MAP_STRUCT_TO_LISTENER(xdg_wm_base*, xdg_wm_base_listener);
MAP_STRUCT_TO_LISTENER(xdg_surface*, xdg_surface_listener);
MAP_STRUCT_TO_LISTENER(xdg_toplevel*, xdg_toplevel_listener);
MAP_STRUCT_TO_LISTENER(wl_output*, wl_output_listener);
Expand Down Expand Up @@ -52,6 +53,7 @@ const typename WlToListener<T>::type* HostEventHandler(T proxy) {
uint32_t XdgToplevelId(sl_window* window);
uint32_t AuraSurfaceId(sl_window* window);
uint32_t AuraToplevelId(sl_window* window);
uint32_t SurfaceId(wl_surface* wl_surface);

} // namespace sommelier
} // namespace vm_tools
Expand Down
17 changes: 14 additions & 3 deletions testing/wayland-test-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ class FakeWaylandClient {
Flush();
}

// Create a surface and return its ID
uint32_t CreateSurface() {
// Create a surface.
struct wl_surface* CreateSurface() {
struct wl_surface* surface = wl_compositor_create_surface(compositor);
Flush();
return wl_proxy_get_id(reinterpret_cast<wl_proxy*>(surface));
return surface;
}

// Returns the xdg_wm_base object.
struct xdg_wm_base* GetXdgWmBase() { return xdg_wm_base; }

// Create an xdg_positioner object.
struct xdg_positioner* CreatePositioner() {
struct xdg_positioner* positioner =
Expand All @@ -99,6 +102,14 @@ class FakeWaylandClient {
return positioner;
}

// Create an xdg_surface object.
struct xdg_surface* CreateXdgSurface() {
struct xdg_surface* surface =
xdg_wm_base_get_xdg_surface(xdg_wm_base, CreateSurface());
Flush();
return surface;
}

void Flush() { wl_display_flush(client_display); }

// Represents the client from the server's (Sommelier's) end.
Expand Down
2 changes: 1 addition & 1 deletion testing/x11-test-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class X11TestBase : public WaylandTestBase {
// Pretend we created a frame window too
window->frame_id = GenerateId();

window->host_surface_id = xwayland->CreateSurface();
window->host_surface_id = SurfaceId(xwayland->CreateSurface());
sl_window_update(window);
Pump();
// Default to the first output if any exist.
Expand Down

0 comments on commit 7f5c420

Please sign in to comment.