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

Side by Side Diff: content/browser/android/browser_scoped_surface_request_manager_unittest.cc

Issue 2285593002: Add ScopedSurfaceRequestManager (Closed)
Patch Set: Fixed signed/unsigned in UTs Created 4 years, 3 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
OLDNEW
(Empty)
1 // Copyright 2016 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 "content/browser/android/browser_scoped_surface_request_manager.h"
6
7 #include "base/bind.h"
8 #include "base/callback_forward.h"
9 #include "base/run_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gl/android/scoped_java_surface.h"
12 #include "ui/gl/android/surface_texture.h"
13
14 namespace content {
15
16 class BrowserScopedSurfaceRequestManagerUnitTest : public testing::Test {
17 public:
18 BrowserScopedSurfaceRequestManagerUnitTest() {
19 manager = BrowserScopedSurfaceRequestManager::GetInstance();
20
21 // The need to reset the callbacks because the
22 // BrowserScopedSurfaceRequestManager's lifetime outlive the tests.
23 manager->clear_callbacks_for_testing();
24
25 surface_texture = gl::SurfaceTexture::Create(0);
26 dummy_request =
27 base::Bind(&BrowserScopedSurfaceRequestManagerUnitTest::DummyCallback,
28 base::Unretained(this));
29 }
30
31 // No-op callback.
32 void DummyCallback(gl::ScopedJavaSurface surface) {}
33
34 // Callback that updates |last_received_request| to allow differentiation
35 // between callback instances in tests.
36 void LoggingCallback(int request_id, gl::ScopedJavaSurface surface) {
37 last_received_request = request_id;
38 }
39
40 gpu::ScopedSurfaceRequestManager::ScopedSurfaceRequestCB dummy_request;
41 scoped_refptr<gl::SurfaceTexture> surface_texture;
42
43 uint64_t last_received_request;
44
45 BrowserScopedSurfaceRequestManager* manager;
46
47 private:
48 DISALLOW_COPY_AND_ASSIGN(BrowserScopedSurfaceRequestManagerUnitTest);
49 };
50
51 // Makes sure we can successfully register a callback.
52 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
53 RegisterRequest_ShouldSucceed) {
54 EXPECT_EQ(0, manager->callback_count_for_testing());
55
56 manager->RegisterScopedSurfaceRequest(1, dummy_request);
57 EXPECT_EQ(1, manager->callback_count_for_testing());
58 }
59
60 // Makes sure we can successfully register multiple callbacks.
61 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
62 RegisterMultipleRequests_ShouldSucceed) {
63 EXPECT_EQ(0, manager->callback_count_for_testing());
64
65 manager->RegisterScopedSurfaceRequest(1, dummy_request);
66 manager->RegisterScopedSurfaceRequest(2, dummy_request);
67
68 EXPECT_EQ(2, manager->callback_count_for_testing());
69 }
70
71 // Makes sure GetInstance() is idempotent/that the class is a proper singleton.
72 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
73 VerifySingleton_ShouldSucceed) {
74 manager->RegisterScopedSurfaceRequest(1, dummy_request);
75
76 BrowserScopedSurfaceRequestManager* manager_other =
77 BrowserScopedSurfaceRequestManager::GetInstance();
78
79 EXPECT_EQ(manager, manager_other);
80 EXPECT_EQ(1, manager_other->callback_count_for_testing());
81 }
82
83 // Makes sure we can fetch a callback after registering it, and fetching the
84 // callback will also unregister it.
85 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
86 GetRegisteredRequest_ShouldSucceed) {
87 EXPECT_EQ(0, manager->callback_count_for_testing());
88
89 manager->RegisterScopedSurfaceRequest(1, dummy_request);
90 EXPECT_EQ(1, manager->callback_count_for_testing());
91
92 auto request = manager->GetAndUnregisterScopedSurfaceRequest(1);
93
94 EXPECT_TRUE(!request.is_null());
95 EXPECT_EQ(0, manager->callback_count_for_testing());
96 }
97
98 // Makes sure that fetching a callback only affects the fetched callback.
99 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
100 GetRegisteredRequestFromMultipleRequests_ShouldSucceed) {
101 EXPECT_EQ(0, manager->callback_count_for_testing());
102
103 manager->RegisterScopedSurfaceRequest(1, dummy_request);
104 manager->RegisterScopedSurfaceRequest(2, dummy_request);
105 EXPECT_EQ(2, manager->callback_count_for_testing());
106
107 auto request = manager->GetAndUnregisterScopedSurfaceRequest(1);
108
109 EXPECT_TRUE(!request.is_null());
110 EXPECT_EQ(1, manager->callback_count_for_testing());
111 }
112
113 // Makes sure that an invalid |request_id| returns a null callback.
114 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
115 GetUnregisteredRequest_ShouldReturnNullCallback) {
116 EXPECT_EQ(0, manager->callback_count_for_testing());
117
118 auto request = manager->GetAndUnregisterScopedSurfaceRequest(1);
119
120 EXPECT_TRUE(request.is_null());
121 EXPECT_EQ(0, manager->callback_count_for_testing());
122 }
123
124 // Makes sure that an invalid |request_id| returns a null callback and doesn't
125 // affect other registered callbacks.
126 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
127 GetUnregisteredRequestFromMultipleRequests_ShouldReturnNullCallback) {
128 EXPECT_EQ(0, manager->callback_count_for_testing());
129
130 manager->RegisterScopedSurfaceRequest(1, dummy_request);
131
132 auto request = manager->GetAndUnregisterScopedSurfaceRequest(2);
133
134 EXPECT_TRUE(request.is_null());
135 EXPECT_EQ(1, manager->callback_count_for_testing());
136 }
137
138 // Makes sure that trying to fulfill a request for an invalid |request_id| does
139 // nothing, and does not affect other callbacks.
140 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
141 FulfillUnregisteredRequest_ShouldDoNothing) {
142 EXPECT_EQ(0, manager->callback_count_for_testing());
143
144 const uint64_t kSpecificCallbackId = 1234;
145 const uint64_t kOtherCallbackId = 5678;
146
147 manager->RegisterScopedSurfaceRequest(
148 kSpecificCallbackId,
149 base::Bind(&BrowserScopedSurfaceRequestManagerUnitTest::LoggingCallback,
150 base::Unretained(this), kSpecificCallbackId));
151
152 last_received_request = kOtherCallbackId;
153
154 manager->FulfillScopedSurfaceRequest(1, surface_texture.get());
155
156 EXPECT_EQ(1, manager->callback_count_for_testing());
157 EXPECT_EQ(kOtherCallbackId, last_received_request);
158 }
159
160 // Makes sure that trying to fulfill a request fulfills the right request, and
161 // does not affect other registered requests.
162 TEST_F(BrowserScopedSurfaceRequestManagerUnitTest,
163 FulfillRegisteredRequest_ShouldSucceed) {
164 EXPECT_EQ(0, manager->callback_count_for_testing());
165
166 const uint64_t kSpecificCallbackId = 1234;
167 const uint64_t kOtherCallbackId = 5678;
168 const uint64_t kDummyId = 9876;
169
170 manager->RegisterScopedSurfaceRequest(
171 kSpecificCallbackId,
172 base::Bind(&BrowserScopedSurfaceRequestManagerUnitTest::LoggingCallback,
173 base::Unretained(this), kSpecificCallbackId));
174
175 manager->RegisterScopedSurfaceRequest(
176 kDummyId,
177 base::Bind(&BrowserScopedSurfaceRequestManagerUnitTest::LoggingCallback,
178 base::Unretained(this), kDummyId));
179
180 last_received_request = kOtherCallbackId;
181
182 manager->FulfillScopedSurfaceRequest(kSpecificCallbackId,
183 surface_texture.get());
184
185 EXPECT_EQ(1, manager->callback_count_for_testing());
186 EXPECT_EQ(kSpecificCallbackId, last_received_request);
187 }
188
189 } // Content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698