File: drm_thread_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download | duplicates (8)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/drm/gpu/drm_thread.h"

#include <utility>

#include "base/test/task_environment.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/ozone/platform/drm/gpu/fake_drm_device_generator.h"
namespace ui {

namespace {

void StubTask() {}

void StubTaskWithDoneFeedback(bool* done) {
  *done = true;
}

}  // namespace

class DrmThreadTest : public testing::Test {
 protected:
  // Overridden from testing::Test
  void SetUp() override {
    drm_thread_.Start(base::DoNothing(),
                      std::make_unique<FakeDrmDeviceGenerator>());
    drm_thread_.task_runner()->PostTask(
        FROM_HERE, base::BindOnce(&DrmThread::AddDrmDeviceReceiver,
                                  base::Unretained(&drm_thread_),
                                  drm_device_.BindNewPipeAndPassReceiver()));
    drm_thread_.FlushForTesting();
  }

  std::unique_ptr<base::WaitableEvent> PostStubTaskWithWaitableEvent() {
    base::OnceClosure task = base::BindOnce(StubTask);
    auto event = std::make_unique<base::WaitableEvent>(
        base::WaitableEvent::ResetPolicy::AUTOMATIC,
        base::WaitableEvent::InitialState::NOT_SIGNALED);
    drm_thread_.task_runner()->PostTask(
        FROM_HERE, base::BindOnce(&DrmThread::RunTaskAfterDeviceReady,
                                  base::Unretained(&drm_thread_),
                                  std::move(task), event.get()));
    return event;
  }

  void PostStubTask(bool* done) {
    *done = false;
    base::OnceClosure task = base::BindOnce(StubTaskWithDoneFeedback, done);
    drm_thread_.task_runner()->PostTask(
        FROM_HERE, base::BindOnce(&DrmThread::RunTaskAfterDeviceReady,
                                  base::Unretained(&drm_thread_),
                                  std::move(task), nullptr));
  }

  void AddGraphicsDevice() {
    base::FilePath file_path("/dev/null");
    base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_WRITE |
                                   base::File::FLAG_READ);
    drm_device_->AddGraphicsDevice(
        file_path,
        mojo::PlatformHandle(base::ScopedFD(file.TakePlatformFile())));
  }

  base::test::TaskEnvironment env_;
  DrmThread drm_thread_;
  mojo::Remote<ozone::mojom::DrmDevice> drm_device_;
};

TEST_F(DrmThreadTest, RunTaskAfterDeviceReady) {
  bool called = false;

  // Post 2 tasks. One with WaitableEvent and one without. They should block on
  // a graphics device becoming available.
  std::unique_ptr<base::WaitableEvent> event = PostStubTaskWithWaitableEvent();
  PostStubTask(&called);
  drm_thread_.FlushForTesting();
  EXPECT_FALSE(event->IsSignaled());
  EXPECT_FALSE(called);

  // Add the graphics device. The tasks should run.
  AddGraphicsDevice();
  drm_thread_.FlushForTesting();
  ASSERT_TRUE(event->IsSignaled());
  ASSERT_TRUE(called);

  // Now that a graphics device is available, further tasks should execute
  // immediately.
  event = PostStubTaskWithWaitableEvent();
  PostStubTask(&called);
  drm_thread_.FlushForTesting();
  ASSERT_TRUE(event->IsSignaled());
  ASSERT_TRUE(called);
}

// Verifies that we gracefully handle the case where CheckOverlayCapabilities()
// is called on a destroyed window.
TEST_F(DrmThreadTest, CheckOverlayCapabilitiesDestroyedWindow) {
  gfx::AcceleratedWidget widget = 5;
  constexpr gfx::Rect bounds(10, 10);
  constexpr size_t candidates_size = 9;
  std::vector<OverlaySurfaceCandidate> candidates(candidates_size);
  std::vector<OverlayStatus> result;
  AddGraphicsDevice();
  drm_device_->CreateWindow(widget, bounds);
  drm_device_->DestroyWindow(widget);
  drm_device_.FlushForTesting();
  drm_thread_.task_runner()->PostTask(
      FROM_HERE, base::BindOnce(&DrmThread::CheckOverlayCapabilitiesSync,
                                base::Unretained(&drm_thread_), widget,
                                candidates, &result));
  drm_thread_.FlushForTesting();
  EXPECT_EQ(candidates_size, result.size());
  for (const auto& status : result) {
    EXPECT_EQ(OVERLAY_STATUS_NOT, status);
  }
}

}  // namespace ui