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

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

Issue 2285593002: Add ScopedSurfaceRequestManager (Closed)
Patch Set: Addressing comments 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/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 "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gl/android/scoped_java_surface.h"
13 #include "ui/gl/android/surface_texture.h"
14
15 namespace content {
16
17 class ScopedSurfaceRequestManagerUnitTest : public testing::Test {
18 public:
19 ScopedSurfaceRequestManagerUnitTest() {
20 manager = ScopedSurfaceRequestManager::GetInstance();
21
22 // The need to reset the callbacks because the
23 // ScopedSurfaceRequestManager's lifetime outlive the tests.
24 manager->clear_callbacks_for_testing();
watk 2016/08/31 00:47:36 Can you create a new manager instead?
tguilbert 2016/08/31 17:58:56 Not really, the constructor is private. I would ha
25
26 surface_texture = gl::SurfaceTexture::Create(0);
27 dummy_request =
28 base::Bind(&ScopedSurfaceRequestManagerUnitTest::DummyCallback,
29 base::Unretained(this));
30 specific_logging_request =
31 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback,
32 base::Unretained(this), kSpecificCallbackId);
33 }
34
35 // No-op callback.
36 void DummyCallback(gl::ScopedJavaSurface surface) {}
37
38 // Callback that updates |last_received_request| to allow differentiation
39 // between callback instances in tests.
40 void LoggingCallback(int request_id, gl::ScopedJavaSurface surface) {
41 last_received_request = request_id;
42 }
43
44 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB dummy_request;
45 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB specific_logging_request;
46 scoped_refptr<gl::SurfaceTexture> surface_texture;
47
48 uint64_t last_received_request;
49 const uint64_t kSpecificCallbackId = 1357;
50 const uint64_t kDummyToken = 0xABCDE;
51
52 ScopedSurfaceRequestManager* manager;
watk 2016/08/31 00:47:35 We usually use the underscore suffix for members i
tguilbert 2016/08/31 17:58:56 Interesting. TIL! In my mind underscores were only
53
54 content::TestBrowserThreadBundle thread_bundle_;
55
56 DISALLOW_COPY_AND_ASSIGN(ScopedSurfaceRequestManagerUnitTest);
57 };
58
59 // Makes sure we can successfully register a callback.
60 TEST_F(ScopedSurfaceRequestManagerUnitTest, RegisterRequest_ShouldSucceed) {
61 EXPECT_EQ(0, manager->callback_count_for_testing());
62
63 manager->RegisterScopedSurfaceRequest(dummy_request);
64
65 EXPECT_EQ(1, manager->callback_count_for_testing());
66 }
67
68 // Makes sure we can successfully register multiple callbacks, and that they
69 // return distinct request tokens.
70 TEST_F(ScopedSurfaceRequestManagerUnitTest,
71 RegisterMultipleRequests_ShouldSucceed) {
72 EXPECT_EQ(0, manager->callback_count_for_testing());
watk 2016/08/31 00:47:35 This duplicates the assertion made in RegisterRequ
tguilbert 2016/08/31 17:58:56 Done.
73
74 uint64_t token1 = manager->RegisterScopedSurfaceRequest(dummy_request);
75 uint64_t token2 = manager->RegisterScopedSurfaceRequest(dummy_request);
76
77 EXPECT_EQ(2, manager->callback_count_for_testing());
78 EXPECT_NE(token1, token2);
79 }
80
81 // Makes sure GetInstance() is idempotent/that the class is a proper singleton.
82 TEST_F(ScopedSurfaceRequestManagerUnitTest, VerifySingleton_ShouldSucceed) {
83 manager->RegisterScopedSurfaceRequest(dummy_request);
84
85 ScopedSurfaceRequestManager* manager_other =
86 ScopedSurfaceRequestManager::GetInstance();
87
88 EXPECT_EQ(manager, manager_other);
watk 2016/08/31 00:47:36 This test can be EXPECT_EQ(manager, ScopedSurfaceR
tguilbert 2016/08/31 17:58:56 Done.
89 EXPECT_EQ(1, manager_other->callback_count_for_testing());
90 }
91
92 // Makes sure we can unregister a callback after registering it.
93 TEST_F(ScopedSurfaceRequestManagerUnitTest,
94 GetRegisteredRequest_ShouldSucceed) {
95 EXPECT_EQ(0, manager->callback_count_for_testing());
96
97 uint64_t token = manager->RegisterScopedSurfaceRequest(dummy_request);
98 EXPECT_EQ(1, manager->callback_count_for_testing());
99
100 manager->UnregisterScopedSurfaceRequest(token);
101
102 EXPECT_EQ(0, manager->callback_count_for_testing());
103 }
104
105 // Makes sure that unregistering a callback only affects the specified callback.
106 TEST_F(ScopedSurfaceRequestManagerUnitTest,
107 GetRegisteredRequestFromMultipleRequests_ShouldSucceed) {
108 EXPECT_EQ(0, manager->callback_count_for_testing());
109
110 uint64_t token = manager->RegisterScopedSurfaceRequest(dummy_request);
111 manager->RegisterScopedSurfaceRequest(dummy_request);
112 EXPECT_EQ(2, manager->callback_count_for_testing());
113
114 manager->UnregisterScopedSurfaceRequest(token);
115
116 EXPECT_EQ(1, manager->callback_count_for_testing());
117 }
118
119 // Makes sure that unregistration is a noop permitted when there are no
120 // registered requests.
121 TEST_F(ScopedSurfaceRequestManagerUnitTest,
122 UnregisteredRequest_ShouldReturnNullCallback) {
123 EXPECT_EQ(0, manager->callback_count_for_testing());
124
125 manager->UnregisterScopedSurfaceRequest(123);
126
127 EXPECT_EQ(0, manager->callback_count_for_testing());
128 }
129
130 // Makes sure that unregistering and invalid |request_token| doesn't affect
watk 2016/08/31 00:47:36 an
tguilbert 2016/08/31 17:58:56 Done.
131 // other registered callbacks.
132 TEST_F(ScopedSurfaceRequestManagerUnitTest,
133 GetUnregisteredRequestFromMultipleRequests_ShouldReturnNullCallback) {
134 EXPECT_EQ(0, manager->callback_count_for_testing());
135
136 manager->RegisterScopedSurfaceRequest(dummy_request);
137
138 manager->UnregisterScopedSurfaceRequest(kDummyToken);
139
140 EXPECT_EQ(1, manager->callback_count_for_testing());
141 }
142
143 // Makes sure that trying to fulfill a request for an invalid |request_token|
144 // does nothing, and does not affect other callbacks.
145 TEST_F(ScopedSurfaceRequestManagerUnitTest,
146 FulfillUnregisteredRequest_ShouldDoNothing) {
147 EXPECT_EQ(0, manager->callback_count_for_testing());
148
149 manager->RegisterScopedSurfaceRequest(specific_logging_request);
150
151 last_received_request = 0;
152
153 manager->FulfillScopedSurfaceRequest(
154 kDummyToken, gl::ScopedJavaSurface(surface_texture.get()));
155
156 EXPECT_EQ(1, manager->callback_count_for_testing());
157 EXPECT_NE(kSpecificCallbackId, 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(ScopedSurfaceRequestManagerUnitTest,
163 FulfillRegisteredRequest_ShouldSucceed) {
164 EXPECT_EQ(0, manager->callback_count_for_testing());
165
166 const uint64_t kOtherCallbackId = 5678;
167
168 uint64_t specific_token =
169 manager->RegisterScopedSurfaceRequest(specific_logging_request);
170
171 manager->RegisterScopedSurfaceRequest(
172 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback,
173 base::Unretained(this), kOtherCallbackId));
174
175 last_received_request = 0;
176
177 manager->FulfillScopedSurfaceRequest(
178 specific_token, gl::ScopedJavaSurface(surface_texture.get()));
179
180 EXPECT_EQ(1, manager->callback_count_for_testing());
181 EXPECT_EQ(kSpecificCallbackId, last_received_request);
182 }
183
184 // Makes sure that the code is resilient to empty callbacks.
watk 2016/08/31 00:47:35 Easier to DCHECK the request is not null IMO
tguilbert 2016/08/31 17:58:56 I added a DCHECK in the Register(), and replaced t
watk 2016/08/31 18:12:14 DCHECK in register SG. Yes, I'd say keep the if in
tguilbert 2016/08/31 20:44:44 Oops! Forgot about unregistered CBs returning null
185 TEST_F(ScopedSurfaceRequestManagerUnitTest,
186 FulfillEmptyRequest_ShouldNotCrash) {
187 uint64_t token = manager->RegisterScopedSurfaceRequest(
188 base::Callback<void(gl::ScopedJavaSurface)>());
189
190 manager->FulfillScopedSurfaceRequest(
191 token, gl::ScopedJavaSurface(surface_texture.get()));
192
193 EXPECT_EQ(0, manager->callback_count_for_testing());
194 }
195
196 // Makes sure that the ScopedSurfaceRequestConduit implementation properly
197 // fulfills requests.
198 TEST_F(ScopedSurfaceRequestManagerUnitTest,
199 ForwardSurfaceTexture_ShouldFulfillRequest) {
200 uint64_t token =
201 manager->RegisterScopedSurfaceRequest(specific_logging_request);
202
203 last_received_request = 0;
watk 2016/08/31 00:47:35 initialize in the constructor/SetUp
tguilbert 2016/08/31 17:58:56 Done.
204
205 manager->ForwardSurfaceTextureForSurfaceRequest(token, surface_texture.get());
206
207 EXPECT_EQ(0, manager->callback_count_for_testing());
watk 2016/08/31 00:47:35 delete
tguilbert 2016/08/31 17:58:56 I feel like this one should stay, because, without
208 EXPECT_EQ(kSpecificCallbackId, last_received_request);
209 }
210
211 } // Content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698