summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/x/connection_unittest.cc
blob: de2285911a4cbf4adf5fd9df81d6ca5571fad422 (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/xproto.h"

#undef Bool

#include <xcb/xcb.h>

#include "testing/gtest/include/gtest/gtest.h"

namespace x11 {

namespace {

Window CreateWindow(Connection* connection) {
  Window window = connection->GenerateId<Window>();
  auto create_window_future = connection->CreateWindow({
      .depth = connection->default_root_depth().depth,
      .wid = window,
      .parent = connection->default_screen().root,
      .width = 1,
      .height = 1,
      .override_redirect = Bool32(true),
  });
  auto create_window_response = create_window_future.Sync();
  EXPECT_FALSE(create_window_response.error);
  return window;
}

}  // namespace

// Connection setup and teardown.
TEST(X11ConnectionTest, Basic) {
  Connection connection;
  ASSERT_TRUE(connection.XcbConnection());
  EXPECT_FALSE(xcb_connection_has_error(connection.XcbConnection()));
}

TEST(X11ConnectionTest, Request) {
  Connection connection;
  ASSERT_TRUE(connection.XcbConnection());
  EXPECT_FALSE(xcb_connection_has_error(connection.XcbConnection()));

  Window window = CreateWindow(&connection);

  auto attributes = connection.GetWindowAttributes({window}).Sync();
  ASSERT_TRUE(attributes);
  EXPECT_EQ(attributes->map_state, MapState::Unmapped);
  EXPECT_TRUE(attributes->override_redirect);

  auto geometry = connection.GetGeometry({window}).Sync();
  ASSERT_TRUE(geometry);
  EXPECT_EQ(geometry->x, 0);
  EXPECT_EQ(geometry->y, 0);
  EXPECT_EQ(geometry->width, 1u);
  EXPECT_EQ(geometry->height, 1u);
}

TEST(X11ConnectionTest, Event) {
  Connection connection;
  ASSERT_TRUE(connection.XcbConnection());
  EXPECT_FALSE(xcb_connection_has_error(connection.XcbConnection()));

  Window window = CreateWindow(&connection);

  auto cwa_future = connection.ChangeWindowAttributes({
      .window = window,
      .event_mask = EventMask::PropertyChange,
  });
  EXPECT_FALSE(cwa_future.Sync().error);

  auto prop_future = connection.ChangeProperty({
      .window = static_cast<x11::Window>(window),
      .property = x11::Atom::WM_NAME,
      .type = x11::Atom::STRING,
      .format = CHAR_BIT,
      .data_len = 1,
      .data = std::vector<uint8_t>{0},
  });
  EXPECT_FALSE(prop_future.Sync().error);

  connection.ReadResponses();
  ASSERT_EQ(connection.events().size(), 1u);
  XEvent& event = connection.events().front().xlib_event();
  auto property_notify_opcode = PropertyNotifyEvent::opcode;
  EXPECT_EQ(event.type, property_notify_opcode);
  EXPECT_EQ(event.xproperty.atom, static_cast<uint32_t>(x11::Atom::WM_NAME));
  EXPECT_EQ(event.xproperty.state, static_cast<int>(Property::NewValue));
}

TEST(X11ConnectionTest, Error) {
  Connection connection;
  ASSERT_TRUE(connection.XcbConnection());
  EXPECT_FALSE(xcb_connection_has_error(connection.XcbConnection()));

  Window invalid_window = connection.GenerateId<Window>();

  auto geometry = connection.GetGeometry({invalid_window}).Sync();
  ASSERT_FALSE(geometry);
  xcb_generic_error_t* error = geometry.error.get();
  EXPECT_EQ(error->error_code, XCB_DRAWABLE);
  EXPECT_EQ(error->resource_id, static_cast<uint32_t>(invalid_window));
}

}  // namespace x11