File: surface_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (133 lines) | stat: -rw-r--r-- 4,240 bytes parent folder | download | duplicates (9)
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
// Copyright 2022 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/surface.h"

#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "components/exo/wayland/test/client_util.h"
#include "components/exo/wayland/test/server_util.h"
#include "components/exo/wayland/test/test_buffer.h"
#include "components/exo/wayland/test/wayland_server_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/display.h"
#include "ui/display/display_switches.h"
#include "ui/gfx/geometry/size_f.h"

namespace exo::wayland {
namespace {

class WaylandSurfaceTest : public test::WaylandServerTest,
                           public ::testing::WithParamInterface<float> {
 public:
  WaylandSurfaceTest() = default;

  WaylandSurfaceTest(const WaylandSurfaceTest&) = delete;
  WaylandSurfaceTest& operator=(const WaylandSurfaceTest&) = delete;

  ~WaylandSurfaceTest() override = default;

  // test::WaylandServerTest:
  void SetUp() override {
    base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
    // Set the device scale factor.
    command_line->AppendSwitchASCII(
        switches::kForceDeviceScaleFactor,
        base::StringPrintf("%f", device_scale_factor()));

    WaylandServerTest::SetUp();
  }

  void TearDown() override {
    WaylandServerTest::TearDown();
    display::Display::ResetForceDeviceScaleFactorForTesting();
  }

 private:
  float device_scale_factor() const { return GetParam(); }
};

class TestBufferListener : public test::BufferListener {
 public:
  int32_t release_buffer_call_count() const {
    return release_buffer_call_count_;
  }

  // test::BufferListener:
  void OnRelease(wl_buffer* resource) override { release_buffer_call_count_++; }

 private:
  int32_t release_buffer_call_count_ = 0;
};

// Instantiate the values of device scale factor in the parameterized tests.
INSTANTIATE_TEST_SUITE_P(All,
                         WaylandSurfaceTest,
                         testing::Values(1.0f, 1.25f, 2.0f));

TEST_P(WaylandSurfaceTest, Attach) {
  class ClientData : public test::TestClient::CustomData {
   public:
    std::unique_ptr<test::TestBuffer> buffer;
    std::unique_ptr<wl_surface> surface;
    TestBufferListener buffer_listener;
  };

  test::ResourceKey surface_key;

  PostToClientAndWait([&](test::TestClient* client) {
    ASSERT_TRUE(client->InitShmBufferFactory(256 * 256 * 4));

    auto data = std::make_unique<ClientData>();

    data->buffer = client->shm_buffer_factory()->CreateBuffer(0, 256, 256);
    data->buffer->SetListener(&data->buffer_listener);
    data->surface.reset(wl_compositor_create_surface(client->compositor()));
    wl_surface_attach(data->surface.get(), data->buffer->resource(), 0, 0);

    surface_key = test::client_util::GetResourceKey(data->surface.get());
    client->set_data(std::move(data));
  });

  Surface* surface = test::server_util::GetUserDataForResource<Surface>(
      server_.get(), surface_key);

  ASSERT_TRUE(surface);
  EXPECT_TRUE(surface->HasPendingAttachedBuffer());

  PostToClientAndWait([&](test::TestClient* client) {
    ClientData* data = client->GetDataAs<ClientData>();
    wl_surface_commit(data->surface.get());
  });
  EXPECT_EQ(gfx::SizeF(256, 256), surface->content_size());

  PostToClientAndWait([&](test::TestClient* client) {
    ClientData* data = client->GetDataAs<ClientData>();

    // Commit without calling Attach() should have no effect.
    wl_surface_commit(data->surface.get());

    client->Roundtrip();

    EXPECT_EQ(0, data->buffer_listener.release_buffer_call_count());

    // Attach a null buffer to surface, this should release the previously
    // attached buffer.
    wl_surface_attach(data->surface.get(), nullptr, 0, 0);
  });
  EXPECT_FALSE(surface->HasPendingAttachedBuffer());

  PostToClientAndWait([&](test::TestClient* client) {
    ClientData* data = client->GetDataAs<ClientData>();
    wl_surface_commit(data->surface.get());

    client->Roundtrip();

    EXPECT_EQ(1, data->buffer_listener.release_buffer_call_count());
  });
  EXPECT_TRUE(surface->content_size().IsEmpty());
}

}  // namespace
}  // namespace exo::wayland