File: wayland_client_test_server.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (123 lines) | stat: -rw-r--r-- 4,201 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 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/clients/test/wayland_client_test_server.h"
#include "base/command_line.h"
#include "base/debug/debugger.h"
#include "base/functional/bind.h"
#include "base/process/launch.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "components/exo/wayland/clients/test/wayland_client_test.h"
#include "components/viz/test/test_gpu_service_holder.h"
#include "mojo/core/embedder/embedder.h"

namespace exo {
namespace {

const char kRunWithExternalWaylandServer[] = "run-with-external-wayland-server";

}

WaylandClientTestSuiteServer::WaylandClientTestSuiteServer(int argc,
                                                           char** argv)
    : ash::AshTestSuite(argc, argv),
      run_with_external_wayland_server_(
          base::CommandLine::ForCurrentProcess()->HasSwitch(
              kRunWithExternalWaylandServer)) {}

WaylandClientTestSuiteServer::~WaylandClientTestSuiteServer() = default;

int WaylandClientTestSuiteServer::Run() {
  Initialize();

  base::Thread client_thread("ClientThread");
  client_thread.Start();

  // |task_environment_| main thread will be ClientThread while running tests.
  task_environment_->DetachFromThread();

  base::RunLoop run_loop;
  client_thread.task_runner()->PostTask(
      FROM_HERE,
      base::BindOnce(&WaylandClientTestSuiteServer::RunTestsOnClientThread,
                     base::Unretained(this), run_loop.QuitWhenIdleClosure()));
  run_loop.Run();

  // |task_environment_| destruction will be performed on the test suite's main
  // thread, detach from the ClientThread.
  task_environment_->DetachFromThread();

  Shutdown();
  return result_;
}

void WaylandClientTestSuiteServer::Initialize() {
  if (!base::debug::BeingDebugged())
    base::RaiseProcessToHighPriority();

  if (run_with_external_wayland_server_) {
    base::TestSuite::Initialize();

    task_environment_ = std::make_unique<base::test::TaskEnvironment>(
        base::test::TaskEnvironment::MainThreadType::UI);
  } else {
    // We only need initialized ash related stuff for running wayland server
    // within the test.
    ash::AshTestSuite::Initialize();

    // Initialize task environment here instead of Test::SetUp(), because all
    // tests and their SetUp() will be running in client thread.
    task_environment_ = std::make_unique<base::test::TaskEnvironment>(
        base::test::TaskEnvironment::MainThreadType::UI);

    // Set the UI thread task runner to WaylandClientTest, so all tests can
    // post tasks to UI thread.
    WaylandClientTest::SetUIThreadTaskRunner(
        base::SingleThreadTaskRunner::GetCurrentDefault());
  }
}

void WaylandClientTestSuiteServer::Shutdown() {
  if (run_with_external_wayland_server_) {
    task_environment_.reset();
    base::TestSuite::Shutdown();
  } else {
    WaylandClientTest::SetUIThreadTaskRunner(nullptr);
    task_environment_.reset();
    ash::AshTestSuite::Shutdown();
  }
}

void WaylandClientTestSuiteServer::RunTestsOnClientThread(
    base::OnceClosure finished_closure) {
  result_ = RUN_ALL_TESTS();
  std::move(finished_closure).Run();
}

int WaylandClientTestSuiteServer::TestMain(
    int argc,
    char** argv,
    WaylandClientTestSuiteServer::CreateServerCallback create_server_callback) {
  mojo::core::Init();

  // The TaskEnvironment and UI thread don't get reset between tests so don't
  // reset the GPU thread either. Destroying the GPU service is problematic
  // because tasks on the UI thread can live on past the end of the test and
  // keep references to GPU thread objects.
  viz::TestGpuServiceHolder::DoNotResetOnTestExit();

  std::unique_ptr<WaylandClientTestSuiteServer> server =
      std::move(create_server_callback).Run(argc, argv);

  return base::LaunchUnitTestsSerially(
      argc, argv,
      base::BindOnce(&WaylandClientTestSuiteServer::Run,
                     base::Unretained(server.get())));
}

}  // namespace exo