OLD | NEW |
(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_requests_for_testing(); |
| 25 |
| 26 last_received_request_ = 0; |
| 27 dummy_token_ = base::UnguessableToken::Deserialize(123, 456); |
| 28 |
| 29 surface_texture = gl::SurfaceTexture::Create(0); |
| 30 dummy_request_ = |
| 31 base::Bind(&ScopedSurfaceRequestManagerUnitTest::DummyCallback, |
| 32 base::Unretained(this)); |
| 33 specific_logging_request_ = |
| 34 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
| 35 base::Unretained(this), kSpecificCallbackId); |
| 36 } |
| 37 |
| 38 // No-op callback. |
| 39 void DummyCallback(gl::ScopedJavaSurface surface) {} |
| 40 |
| 41 // Callback that updates |last_received_request_| to allow differentiation |
| 42 // between callback instances in tests. |
| 43 void LoggingCallback(int request_id, gl::ScopedJavaSurface surface) { |
| 44 last_received_request_ = request_id; |
| 45 } |
| 46 |
| 47 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB dummy_request_; |
| 48 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB specific_logging_request_; |
| 49 scoped_refptr<gl::SurfaceTexture> surface_texture; |
| 50 |
| 51 int last_received_request_; |
| 52 const int kSpecificCallbackId = 1357; |
| 53 base::UnguessableToken dummy_token_; |
| 54 |
| 55 ScopedSurfaceRequestManager* manager_; |
| 56 |
| 57 content::TestBrowserThreadBundle thread_bundle_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(ScopedSurfaceRequestManagerUnitTest); |
| 60 }; |
| 61 |
| 62 // Makes sure we can successfully register a callback. |
| 63 TEST_F(ScopedSurfaceRequestManagerUnitTest, RegisterRequest_ShouldSucceed) { |
| 64 EXPECT_EQ(0, manager_->request_count_for_testing()); |
| 65 |
| 66 base::UnguessableToken token = |
| 67 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 68 |
| 69 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 70 EXPECT_FALSE(token.is_empty()); |
| 71 } |
| 72 |
| 73 // Makes sure we can successfully register multiple callbacks, and that they |
| 74 // return distinct request tokens. |
| 75 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 76 RegisterMultipleRequests_ShouldSucceed) { |
| 77 base::UnguessableToken token1 = |
| 78 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 79 base::UnguessableToken token2 = |
| 80 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 81 |
| 82 EXPECT_EQ(2, manager_->request_count_for_testing()); |
| 83 EXPECT_NE(token1, token2); |
| 84 } |
| 85 |
| 86 // Makes sure GetInstance() is idempotent/that the class is a proper singleton. |
| 87 TEST_F(ScopedSurfaceRequestManagerUnitTest, VerifySingleton_ShouldSucceed) { |
| 88 EXPECT_EQ(manager_, ScopedSurfaceRequestManager::GetInstance()); |
| 89 } |
| 90 |
| 91 // Makes sure we can unregister a callback after registering it. |
| 92 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 93 GetRegisteredRequest_ShouldSucceed) { |
| 94 base::UnguessableToken token = |
| 95 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 96 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 97 |
| 98 manager_->UnregisterScopedSurfaceRequest(token); |
| 99 |
| 100 EXPECT_EQ(0, manager_->request_count_for_testing()); |
| 101 } |
| 102 |
| 103 // Makes sure that unregistering a callback only affects the specified callback. |
| 104 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 105 GetRegisteredRequestFromMultipleRequests_ShouldSucceed) { |
| 106 base::UnguessableToken token = |
| 107 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 108 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 109 EXPECT_EQ(2, manager_->request_count_for_testing()); |
| 110 |
| 111 manager_->UnregisterScopedSurfaceRequest(token); |
| 112 |
| 113 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 114 } |
| 115 |
| 116 // Makes sure that unregistration is a noop permitted when there are no |
| 117 // registered requests. |
| 118 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 119 UnregisteredRequest_ShouldReturnNullCallback) { |
| 120 manager_->UnregisterScopedSurfaceRequest(dummy_token_); |
| 121 |
| 122 EXPECT_EQ(0, manager_->request_count_for_testing()); |
| 123 } |
| 124 |
| 125 // Makes sure that unregistering an invalid |request_token| doesn't affect |
| 126 // other registered callbacks. |
| 127 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 128 GetUnregisteredRequestFromMultipleRequests_ShouldReturnNullCallback) { |
| 129 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 130 |
| 131 manager_->UnregisterScopedSurfaceRequest(dummy_token_); |
| 132 |
| 133 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 134 } |
| 135 |
| 136 // Makes sure that trying to fulfill a request for an invalid |request_token| |
| 137 // does nothing, and does not affect other callbacks. |
| 138 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 139 FulfillUnregisteredRequest_ShouldDoNothing) { |
| 140 manager_->RegisterScopedSurfaceRequest(specific_logging_request_); |
| 141 |
| 142 manager_->FulfillScopedSurfaceRequest( |
| 143 dummy_token_, gl::ScopedJavaSurface(surface_texture.get())); |
| 144 |
| 145 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 146 EXPECT_NE(kSpecificCallbackId, last_received_request_); |
| 147 } |
| 148 |
| 149 // Makes sure that trying to fulfill a request fulfills the right request, and |
| 150 // does not affect other registered requests. |
| 151 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 152 FulfillRegisteredRequest_ShouldSucceed) { |
| 153 base::UnguessableToken specific_token = |
| 154 manager_->RegisterScopedSurfaceRequest(specific_logging_request_); |
| 155 |
| 156 const uint64_t kOtherCallbackId = 5678; |
| 157 manager_->RegisterScopedSurfaceRequest( |
| 158 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
| 159 base::Unretained(this), kOtherCallbackId)); |
| 160 |
| 161 manager_->FulfillScopedSurfaceRequest( |
| 162 specific_token, gl::ScopedJavaSurface(surface_texture.get())); |
| 163 |
| 164 base::RunLoop().RunUntilIdle(); |
| 165 |
| 166 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 167 EXPECT_EQ(kSpecificCallbackId, last_received_request_); |
| 168 } |
| 169 |
| 170 // Makes sure that the ScopedSurfaceRequestConduit implementation properly |
| 171 // fulfills requests. |
| 172 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 173 ForwardSurfaceTexture_ShouldFulfillRequest) { |
| 174 base::UnguessableToken token = |
| 175 manager_->RegisterScopedSurfaceRequest(specific_logging_request_); |
| 176 |
| 177 manager_->ForwardSurfaceTextureForSurfaceRequest(token, |
| 178 surface_texture.get()); |
| 179 |
| 180 base::RunLoop().RunUntilIdle(); |
| 181 |
| 182 EXPECT_EQ(0, manager_->request_count_for_testing()); |
| 183 EXPECT_EQ(kSpecificCallbackId, last_received_request_); |
| 184 } |
| 185 |
| 186 } // Content |
OLD | NEW |