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 "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/gl/android/scoped_java_surface.h" |
| 12 #include "ui/gl/android/surface_texture.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 class ScopedSurfaceRequestManagerUnitTest : public testing::Test { |
| 17 public: |
| 18 ScopedSurfaceRequestManagerUnitTest() { |
| 19 manager = ScopedSurfaceRequestManager::GetInstance(); |
| 20 |
| 21 // The need to reset the callbacks because the |
| 22 // ScopedSurfaceRequestManager's lifetime outlive the tests. |
| 23 manager->clear_callbacks_for_testing(); |
| 24 |
| 25 surface_texture = gl::SurfaceTexture::Create(0); |
| 26 dummy_request = |
| 27 base::Bind(&ScopedSurfaceRequestManagerUnitTest::DummyCallback, |
| 28 base::Unretained(this)); |
| 29 } |
| 30 |
| 31 // No-op callback. |
| 32 void DummyCallback(gl::ScopedJavaSurface surface) {} |
| 33 |
| 34 // Callback that updates |last_received_request| to allow differentiation |
| 35 // between callback instances in tests. |
| 36 void LoggingCallback(int request_id, gl::ScopedJavaSurface surface) { |
| 37 last_received_request = request_id; |
| 38 } |
| 39 |
| 40 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB dummy_request; |
| 41 scoped_refptr<gl::SurfaceTexture> surface_texture; |
| 42 |
| 43 uint64_t last_received_request; |
| 44 |
| 45 ScopedSurfaceRequestManager* manager; |
| 46 |
| 47 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(ScopedSurfaceRequestManagerUnitTest); |
| 49 }; |
| 50 |
| 51 // Makes sure we can successfully register a callback. |
| 52 TEST_F(ScopedSurfaceRequestManagerUnitTest, RegisterRequest_ShouldSucceed) { |
| 53 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 54 |
| 55 manager->RegisterScopedSurfaceRequest(1, dummy_request); |
| 56 EXPECT_EQ(1, manager->callback_count_for_testing()); |
| 57 } |
| 58 |
| 59 // Makes sure we can successfully register multiple callbacks. |
| 60 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 61 RegisterMultipleRequests_ShouldSucceed) { |
| 62 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 63 |
| 64 manager->RegisterScopedSurfaceRequest(1, dummy_request); |
| 65 manager->RegisterScopedSurfaceRequest(2, dummy_request); |
| 66 |
| 67 EXPECT_EQ(2, manager->callback_count_for_testing()); |
| 68 } |
| 69 |
| 70 // Makes sure GetInstance() is idempotent/that the class is a proper singleton. |
| 71 TEST_F(ScopedSurfaceRequestManagerUnitTest, VerifySingleton_ShouldSucceed) { |
| 72 manager->RegisterScopedSurfaceRequest(1, dummy_request); |
| 73 |
| 74 ScopedSurfaceRequestManager* manager_other = |
| 75 ScopedSurfaceRequestManager::GetInstance(); |
| 76 |
| 77 EXPECT_EQ(manager, manager_other); |
| 78 EXPECT_EQ(1, manager_other->callback_count_for_testing()); |
| 79 } |
| 80 |
| 81 // Makes sure we can fetch a callback after registering it, and fetching the |
| 82 // callback will also unregister it. |
| 83 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 84 GetRegisteredRequest_ShouldSucceed) { |
| 85 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 86 |
| 87 manager->RegisterScopedSurfaceRequest(1, dummy_request); |
| 88 EXPECT_EQ(1, manager->callback_count_for_testing()); |
| 89 |
| 90 auto request = manager->GetAndUnregisterScopedSurfaceRequest(1); |
| 91 |
| 92 EXPECT_TRUE(!request.is_null()); |
| 93 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 94 } |
| 95 |
| 96 // Makes sure that fetching a callback only affects the fetched callback. |
| 97 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 98 GetRegisteredRequestFromMultipleRequests_ShouldSucceed) { |
| 99 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 100 |
| 101 manager->RegisterScopedSurfaceRequest(1, dummy_request); |
| 102 manager->RegisterScopedSurfaceRequest(2, dummy_request); |
| 103 EXPECT_EQ(2, manager->callback_count_for_testing()); |
| 104 |
| 105 auto request = manager->GetAndUnregisterScopedSurfaceRequest(1); |
| 106 |
| 107 EXPECT_TRUE(!request.is_null()); |
| 108 EXPECT_EQ(1, manager->callback_count_for_testing()); |
| 109 } |
| 110 |
| 111 // Makes sure that an invalid |request_id| returns a null callback. |
| 112 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 113 GetUnregisteredRequest_ShouldReturnNullCallback) { |
| 114 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 115 |
| 116 auto request = manager->GetAndUnregisterScopedSurfaceRequest(1); |
| 117 |
| 118 EXPECT_TRUE(request.is_null()); |
| 119 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 120 } |
| 121 |
| 122 // Makes sure that an invalid |request_id| returns a null callback and doesn't |
| 123 // affect other registered callbacks. |
| 124 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 125 GetUnregisteredRequestFromMultipleRequests_ShouldReturnNullCallback) { |
| 126 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 127 |
| 128 manager->RegisterScopedSurfaceRequest(1, dummy_request); |
| 129 |
| 130 auto request = manager->GetAndUnregisterScopedSurfaceRequest(2); |
| 131 |
| 132 EXPECT_TRUE(request.is_null()); |
| 133 EXPECT_EQ(1, manager->callback_count_for_testing()); |
| 134 } |
| 135 |
| 136 // Makes sure that trying to fulfill a request for an invalid |request_id| does |
| 137 // nothing, and does not affect other callbacks. |
| 138 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 139 FulfillUnregisteredRequest_ShouldDoNothing) { |
| 140 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 141 |
| 142 const uint64_t kSpecificCallbackId = 1234; |
| 143 const uint64_t kOtherCallbackId = 5678; |
| 144 |
| 145 manager->RegisterScopedSurfaceRequest( |
| 146 kSpecificCallbackId, |
| 147 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
| 148 base::Unretained(this), kSpecificCallbackId)); |
| 149 |
| 150 last_received_request = kOtherCallbackId; |
| 151 |
| 152 manager->FulfillScopedSurfaceRequest(1, surface_texture.get()); |
| 153 |
| 154 EXPECT_EQ(1, manager->callback_count_for_testing()); |
| 155 EXPECT_EQ(kOtherCallbackId, last_received_request); |
| 156 } |
| 157 |
| 158 // Makes sure that trying to fulfill a request fulfills the right request, and |
| 159 // does not affect other registered requests. |
| 160 TEST_F(ScopedSurfaceRequestManagerUnitTest, |
| 161 FulfillRegisteredRequest_ShouldSucceed) { |
| 162 EXPECT_EQ(0, manager->callback_count_for_testing()); |
| 163 |
| 164 const uint64_t kSpecificCallbackId = 1234; |
| 165 const uint64_t kOtherCallbackId = 5678; |
| 166 const uint64_t kDummyId = 9876; |
| 167 |
| 168 manager->RegisterScopedSurfaceRequest( |
| 169 kSpecificCallbackId, |
| 170 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
| 171 base::Unretained(this), kSpecificCallbackId)); |
| 172 |
| 173 manager->RegisterScopedSurfaceRequest( |
| 174 kDummyId, |
| 175 base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
| 176 base::Unretained(this), kDummyId)); |
| 177 |
| 178 last_received_request = kOtherCallbackId; |
| 179 |
| 180 manager->FulfillScopedSurfaceRequest(kSpecificCallbackId, |
| 181 surface_texture.get()); |
| 182 |
| 183 EXPECT_EQ(1, manager->callback_count_for_testing()); |
| 184 EXPECT_EQ(kSpecificCallbackId, last_received_request); |
| 185 } |
| 186 |
| 187 } // Content |
OLD | NEW |