Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: services/ui/public/cpp/tests/gpu_unittest.cc

Issue 2965103002: gpu: Make the mus-gpu client-lib test-friendly. (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/ui/public/cpp/tests/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "services/ui/public/cpp/gpu/gpu.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/test/scoped_task_environment.h"
9 #include "gpu/config/gpu_info.h"
10 #include "mojo/public/cpp/bindings/binding_set.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace ui {
14
15 namespace {
16
17 class TestGpuImpl : public mojom::Gpu {
18 public:
19 TestGpuImpl() {}
20 ~TestGpuImpl() override {}
21
22 mojom::GpuPtr GetPtr() {
23 mojom::GpuPtr ptr;
24 bindings_.AddBinding(this, mojo::MakeRequest(&ptr));
25 return ptr;
26 }
27
28 void WaitUntilChannelRequest() {
29 DCHECK(quit_closure_.is_null());
30 base::RunLoop run_loop;
31 quit_closure_ = run_loop.QuitClosure();
32 run_loop.Run();
33 quit_closure_ = base::Closure();
34 }
35
36 bool RespondToGpuChannelRequests() {
37 if (establish_channel_callback_.is_null())
38 return false;
39 constexpr int client_id = 1;
40 mojo::ScopedMessagePipeHandle handle;
41 base::ResetAndReturn(&establish_channel_callback_)
42 .Run(client_id, std::move(handle), gpu::GPUInfo());
43 return true;
44 }
45
46 // ui::mojom::Gpu overrides:
47 void EstablishGpuChannel(
48 const EstablishGpuChannelCallback& callback) override {
49 establish_channel_callback_ = callback;
50 if (!quit_closure_.is_null())
51 quit_closure_.Run();
52 }
53
54 void CreateGpuMemoryBuffer(
55 gfx::GpuMemoryBufferId id,
56 const gfx::Size& size,
57 gfx::BufferFormat format,
58 gfx::BufferUsage usage,
59 const ui::mojom::Gpu::CreateGpuMemoryBufferCallback& callback) override {}
60
61 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
62 const gpu::SyncToken& sync_token) override {}
63
64 private:
65 EstablishGpuChannelCallback establish_channel_callback_;
66
67 mojo::BindingSet<mojom::Gpu> bindings_;
68 base::Closure quit_closure_;
69
70 DISALLOW_COPY_AND_ASSIGN(TestGpuImpl);
71 };
72
73 } // namespace
74
75 class GpuTest : public testing::Test {
76 public:
77 GpuTest() {}
78 ~GpuTest() override {}
79
80 Gpu* gpu() { return gpu_.get(); }
81
82 TestGpuImpl* gpu_impl() { return &gpu_impl_; }
83
84 // testing::Test:
85 void SetUp() override {
86 auto factory =
87 base::BindRepeating(&TestGpuImpl::GetPtr, base::Unretained(&gpu_impl_));
88 gpu_ = base::WrapUnique(new Gpu(std::move(factory), nullptr));
89 }
90
91 private:
92 base::test::ScopedTaskEnvironment env_;
93 TestGpuImpl gpu_impl_;
94 std::unique_ptr<Gpu> gpu_;
95
96 DISALLOW_COPY_AND_ASSIGN(GpuTest);
97 };
98
99 // Tests that multiple calls for establishing a gpu channel are all notified
100 // correctly when the channel is established (or fails to establish).
101 TEST_F(GpuTest, EstablishRequestsQueued) {
102 int counter = 2;
103 base::RunLoop run_loop;
104 // A callback that decrements the counter, and runs the callback when the
105 // counter reaches 0.
106 auto callback = base::Bind(
107 [](int* counter, const base::Closure& callback,
108 scoped_refptr<gpu::GpuChannelHost> channel) {
109 --(*counter);
110 if (*counter == 0)
111 callback.Run();
112 },
113 &counter, run_loop.QuitClosure());
114 gpu()->EstablishGpuChannel(callback);
115 gpu()->EstablishGpuChannel(callback);
116 EXPECT_EQ(2, counter);
117 gpu_impl()->WaitUntilChannelRequest();
118
119 EXPECT_TRUE(gpu_impl()->RespondToGpuChannelRequests());
120 run_loop.Run();
121 EXPECT_EQ(0, counter);
122 }
123
124 } // namespace ui
OLDNEW
« no previous file with comments | « services/ui/public/cpp/tests/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698