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::Nonce::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::Nonce 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::Nonce token = manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 67 |
| 68 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 69 EXPECT_FALSE(token.is_null()); |
| 70 } |
| 71 |
| 72 // Makes sure we can successfully register multiple callbacks, and that they |
| 73 // return distinct request tokens. |
| 74 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 75 RegisterMultipleRequests_ShouldSucceed) { |
| 76 base::Nonce token1 = manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 77 base::Nonce token2 = manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 78 |
| 79 EXPECT_EQ(2, manager_->request_count_for_testing()); |
| 80 EXPECT_NE(token1, token2); |
| 81 } |
| 82 |
| 83 // Makes sure GetInstance() is idempotent/that the class is a proper singleton. |
| 84 TEST_F(ScopedSurfaceRequestManagerUnitTest, VerifySingleton_ShouldSucceed) { |
| 85 EXPECT_EQ(manager_, ScopedSurfaceRequestManager::GetInstance()); |
| 86 } |
| 87 |
| 88 // Makes sure we can unregister a callback after registering it. |
| 89 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 90 GetRegisteredRequest_ShouldSucceed) { |
| 91 base::Nonce token = manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 92 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 93 |
| 94 manager_->UnregisterScopedSurfaceRequest(token); |
| 95 |
| 96 EXPECT_EQ(0, manager_->request_count_for_testing()); |
| 97 } |
| 98 |
| 99 // Makes sure that unregistering a callback only affects the specified callback. |
| 100 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 101 GetRegisteredRequestFromMultipleRequests_ShouldSucceed) { |
| 102 base::Nonce token = manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 103 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 104 EXPECT_EQ(2, manager_->request_count_for_testing()); |
| 105 |
| 106 manager_->UnregisterScopedSurfaceRequest(token); |
| 107 |
| 108 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 109 } |
| 110 |
| 111 // Makes sure that unregistration is a noop permitted when there are no |
| 112 // registered requests. |
| 113 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 114 UnregisteredRequest_ShouldReturnNullCallback) { |
| 115 manager_->UnregisterScopedSurfaceRequest(dummy_token_); |
| 116 |
| 117 EXPECT_EQ(0, manager_->request_count_for_testing()); |
| 118 } |
| 119 |
| 120 // Makes sure that unregistering an invalid |request_token| doesn't affect |
| 121 // other registered callbacks. |
| 122 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 123 GetUnregisteredRequestFromMultipleRequests_ShouldReturnNullCallback) { |
| 124 manager_->RegisterScopedSurfaceRequest(dummy_request_); |
| 125 |
| 126 manager_->UnregisterScopedSurfaceRequest(dummy_token_); |
| 127 |
| 128 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 129 } |
| 130 |
| 131 // Makes sure that trying to fulfill a request for an invalid |request_token| |
| 132 // does nothing, and does not affect other callbacks. |
| 133 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 134 FulfillUnregisteredRequest_ShouldDoNothing) { |
| 135 manager_->RegisterScopedSurfaceRequest(specific_logging_request_); |
| 136 |
| 137 manager_->FulfillScopedSurfaceRequest( |
| 138 dummy_token_, gl::ScopedJavaSurface(surface_texture.get())); |
| 139 |
| 140 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 141 EXPECT_NE(kSpecificCallbackId, last_received_request_); |
| 142 } |
| 143 |
| 144 // Makes sure that trying to fulfill a request fulfills the right request, and |
| 145 // does not affect other registered requests. |
| 146 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 147 FulfillRegisteredRequest_ShouldSucceed) { |
| 148 base::Nonce specific_token = |
| 149 manager_->RegisterScopedSurfaceRequest(specific_logging_request_); |
| 150 |
| 151 const uint64_t kOtherCallbackId = 5678; |
| 152 manager_->RegisterScopedSurfaceRequest( |
| 153 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
| 154 base::Unretained(this), kOtherCallbackId)); |
| 155 |
| 156 manager_->FulfillScopedSurfaceRequest( |
| 157 specific_token, gl::ScopedJavaSurface(surface_texture.get())); |
| 158 |
| 159 EXPECT_EQ(1, manager_->request_count_for_testing()); |
| 160 EXPECT_EQ(kSpecificCallbackId, last_received_request_); |
| 161 } |
| 162 |
| 163 // Makes sure that the ScopedSurfaceRequestConduit implementation properly |
| 164 // fulfills requests. |
| 165 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 166 ForwardSurfaceTexture_ShouldFulfillRequest) { |
| 167 base::Nonce token = |
| 168 manager_->RegisterScopedSurfaceRequest(specific_logging_request_); |
| 169 |
| 170 manager_->ForwardSurfaceTextureForSurfaceRequest(token, |
| 171 surface_texture.get()); |
| 172 |
| 173 EXPECT_EQ(0, manager_->request_count_for_testing()); |
| 174 EXPECT_EQ(kSpecificCallbackId, last_received_request_); |
| 175 } |
| 176 |
| 177 } // Content |
OLD | NEW |