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