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