blob: 969823df79eb83a73413ee3b0a3cc4f61e223dba [file] [log] [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/exo/wayland/wayland_display_output.h"
#include <cstdint>
#include "base/synchronization/waitable_event.h"
#include "base/test/task_environment.h"
#include "components/exo/wayland/test/client_util.h"
#include "components/exo/wayland/test/server_util.h"
#include "components/exo/wayland/test/wayland_server_test.h"
#include "components/exo/wayland/wayland_display_observer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace exo::wayland {
namespace {
class WaylandDisplayOutputTest : public test::WaylandServerTest {
public:
WaylandDisplayOutputTest()
: test::WaylandServerTest(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
WaylandDisplayOutputTest(const WaylandDisplayOutputTest&) = delete;
WaylandDisplayOutputTest& operator=(const WaylandDisplayOutputTest&) = delete;
~WaylandDisplayOutputTest() override = default;
void TearDown() override {
task_environment()->RunUntilIdle();
test::WaylandServerTest::TearDown();
}
};
} // namespace
// TODO(crbug.com/1420468): Failing on an ASAN + LSAN builder.
#if defined(ADDRESS_SANITIZER) && defined(LEAK_SANITIZER)
#define MAYBE_DelayedSelfDestruct DISABLED_DelayedSelfDestruct
#else
#define MAYBE_DelayedSelfDestruct DelayedSelfDestruct
#endif
TEST_F(WaylandDisplayOutputTest, MAYBE_DelayedSelfDestruct) {
class ClientData : public test::TestClient::CustomData {
public:
uint32_t output_name = 0;
uint32_t output_version = 0;
};
// Start with 2 displays.
UpdateDisplay("800x600,1024x786");
// Store info about the 2nd display on the client.
test::ResourceKey output_resource_key;
PostToClientAndWait([&](test::TestClient* client) {
auto data = std::make_unique<ClientData>();
data->output_name = client->globals().output.name();
data->output_version =
wl_proxy_get_version(reinterpret_cast<wl_proxy*>(client->output()));
client->set_data(std::move(data));
output_resource_key = test::client_util::GetResourceKey(client->output());
});
auto* display_handler =
test::server_util::GetUserDataForResource<WaylandDisplayHandler>(
server_.get(), output_resource_key);
EXPECT_EQ(display_handler->id(), GetSecondaryDisplay().id());
// Remove the 2nd display.
UpdateDisplay("800x600");
// Fast forward until a couple deletes have been attempted.
task_environment()->FastForwardBy(
WaylandDisplayOutput::kDeleteTaskDelay *
(WaylandDisplayOutput::kDeleteRetries - 0.5));
// Try binding and check for client error.
PostToClientAndWait([&](test::TestClient* client) {
auto* data = client->GetDataAs<ClientData>();
EXPECT_TRUE(static_cast<wl_output*>(
wl_registry_bind(client->globals().registry.get(), data->output_name,
&wl_output_interface, data->output_version)));
client->Roundtrip();
EXPECT_EQ(wl_display_get_error(client->display()), 0);
});
}
// Verify that in the case where an output is added and removed quickly before
// the client's initial bind, the server still waits for the full amount of
// delete delays before deleting the global resource.
// TODO(crbug.com/1420468): Flaky on an ASAN + LSAN builder.
#if defined(ADDRESS_SANITIZER) && defined(LEAK_SANITIZER)
#define MAYBE_DelayedSelfDestructBeforeFirstBind \
DISABLED_DelayedSelfDestructBeforeFirstBind
#else
#define MAYBE_DelayedSelfDestructBeforeFirstBind \
DelayedSelfDestructBeforeFirstBind
#endif
TEST_F(WaylandDisplayOutputTest, MAYBE_DelayedSelfDestructBeforeFirstBind) {
UpdateDisplay("800x600");
// Block client thread so the initial bind request doesn't happen yet.
base::WaitableEvent block_bind_event;
ASSERT_TRUE(client_thread_->task_runner()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&] { block_bind_event.Wait(); })));
// Quickly add then remove a display while the client is blocked.
UpdateDisplay("800x600,1024x786");
UpdateDisplay("800x600");
// Fast forward so at least one delete has been attempted.
task_environment()->FastForwardBy(
WaylandDisplayOutput::kDeleteTaskDelay *
(WaylandDisplayOutput::kDeleteRetries - 0.5));
// Unblock client thread so the bind request happens now.
block_bind_event.Signal();
client_thread_->FlushForTesting();
// Check for client error.
PostToClientAndWait([&](test::TestClient* client) {
client->Roundtrip();
EXPECT_EQ(wl_display_get_error(client->display()), 0);
});
}
} // namespace exo::wayland