Index: content/browser/android/scoped_surface_request_manager_unittest.cc |
diff --git a/content/browser/android/scoped_surface_request_manager_unittest.cc b/content/browser/android/scoped_surface_request_manager_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cea7df659d6b3924e614045aa066cc5875f13271 |
--- /dev/null |
+++ b/content/browser/android/scoped_surface_request_manager_unittest.cc |
@@ -0,0 +1,187 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/android/scoped_surface_request_manager.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback_forward.h" |
+#include "base/run_loop.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gl/android/scoped_java_surface.h" |
+#include "ui/gl/android/surface_texture.h" |
+ |
+namespace content { |
+ |
+class ScopedSurfaceRequestManagerUnitTest : public testing::Test { |
+ public: |
+ ScopedSurfaceRequestManagerUnitTest() { |
+ manager = ScopedSurfaceRequestManager::GetInstance(); |
+ |
+ // The need to reset the callbacks because the |
+ // ScopedSurfaceRequestManager's lifetime outlive the tests. |
+ manager->clear_callbacks_for_testing(); |
+ |
+ surface_texture = gl::SurfaceTexture::Create(0); |
+ dummy_request = |
+ base::Bind(&ScopedSurfaceRequestManagerUnitTest::DummyCallback, |
+ base::Unretained(this)); |
+ } |
+ |
+ // No-op callback. |
+ void DummyCallback(gl::ScopedJavaSurface surface) {} |
+ |
+ // Callback that updates |last_received_request| to allow differentiation |
+ // between callback instances in tests. |
+ void LoggingCallback(int request_id, gl::ScopedJavaSurface surface) { |
+ last_received_request = request_id; |
+ } |
+ |
+ ScopedSurfaceRequestManager::ScopedSurfaceRequestCB dummy_request; |
+ scoped_refptr<gl::SurfaceTexture> surface_texture; |
+ |
+ uint64_t last_received_request; |
+ |
+ ScopedSurfaceRequestManager* manager; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(ScopedSurfaceRequestManagerUnitTest); |
+}; |
+ |
+// Makes sure we can successfully register a callback. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, RegisterRequest_ShouldSucceed) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ manager->RegisterScopedSurfaceRequest(1, dummy_request); |
+ EXPECT_EQ(1, manager->callback_count_for_testing()); |
+} |
+ |
+// Makes sure we can successfully register multiple callbacks. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, |
+ RegisterMultipleRequests_ShouldSucceed) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ manager->RegisterScopedSurfaceRequest(1, dummy_request); |
+ manager->RegisterScopedSurfaceRequest(2, dummy_request); |
+ |
+ EXPECT_EQ(2, manager->callback_count_for_testing()); |
+} |
+ |
+// Makes sure GetInstance() is idempotent/that the class is a proper singleton. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, VerifySingleton_ShouldSucceed) { |
+ manager->RegisterScopedSurfaceRequest(1, dummy_request); |
+ |
+ ScopedSurfaceRequestManager* manager_other = |
+ ScopedSurfaceRequestManager::GetInstance(); |
+ |
+ EXPECT_EQ(manager, manager_other); |
+ EXPECT_EQ(1, manager_other->callback_count_for_testing()); |
+} |
+ |
+// Makes sure we can fetch a callback after registering it, and fetching the |
+// callback will also unregister it. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, |
+ GetRegisteredRequest_ShouldSucceed) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ manager->RegisterScopedSurfaceRequest(1, dummy_request); |
+ EXPECT_EQ(1, manager->callback_count_for_testing()); |
+ |
+ auto request = manager->GetAndUnregisterScopedSurfaceRequest(1); |
+ |
+ EXPECT_TRUE(!request.is_null()); |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+} |
+ |
+// Makes sure that fetching a callback only affects the fetched callback. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, |
+ GetRegisteredRequestFromMultipleRequests_ShouldSucceed) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ manager->RegisterScopedSurfaceRequest(1, dummy_request); |
+ manager->RegisterScopedSurfaceRequest(2, dummy_request); |
+ EXPECT_EQ(2, manager->callback_count_for_testing()); |
+ |
+ auto request = manager->GetAndUnregisterScopedSurfaceRequest(1); |
+ |
+ EXPECT_TRUE(!request.is_null()); |
+ EXPECT_EQ(1, manager->callback_count_for_testing()); |
+} |
+ |
+// Makes sure that an invalid |request_id| returns a null callback. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, |
+ GetUnregisteredRequest_ShouldReturnNullCallback) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ auto request = manager->GetAndUnregisterScopedSurfaceRequest(1); |
+ |
+ EXPECT_TRUE(request.is_null()); |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+} |
+ |
+// Makes sure that an invalid |request_id| returns a null callback and doesn't |
+// affect other registered callbacks. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, |
+ GetUnregisteredRequestFromMultipleRequests_ShouldReturnNullCallback) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ manager->RegisterScopedSurfaceRequest(1, dummy_request); |
+ |
+ auto request = manager->GetAndUnregisterScopedSurfaceRequest(2); |
+ |
+ EXPECT_TRUE(request.is_null()); |
+ EXPECT_EQ(1, manager->callback_count_for_testing()); |
+} |
+ |
+// Makes sure that trying to fulfill a request for an invalid |request_id| does |
+// nothing, and does not affect other callbacks. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, |
+ FulfillUnregisteredRequest_ShouldDoNothing) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ const uint64_t kSpecificCallbackId = 1234; |
+ const uint64_t kOtherCallbackId = 5678; |
+ |
+ manager->RegisterScopedSurfaceRequest( |
+ kSpecificCallbackId, |
+ base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
+ base::Unretained(this), kSpecificCallbackId)); |
+ |
+ last_received_request = kOtherCallbackId; |
+ |
+ manager->FulfillScopedSurfaceRequest(1, surface_texture.get()); |
+ |
+ EXPECT_EQ(1, manager->callback_count_for_testing()); |
+ EXPECT_EQ(kOtherCallbackId, last_received_request); |
+} |
+ |
+// Makes sure that trying to fulfill a request fulfills the right request, and |
+// does not affect other registered requests. |
+TEST_F(ScopedSurfaceRequestManagerUnitTest, |
+ FulfillRegisteredRequest_ShouldSucceed) { |
+ EXPECT_EQ(0, manager->callback_count_for_testing()); |
+ |
+ const uint64_t kSpecificCallbackId = 1234; |
+ const uint64_t kOtherCallbackId = 5678; |
+ const uint64_t kDummyId = 9876; |
+ |
+ manager->RegisterScopedSurfaceRequest( |
+ kSpecificCallbackId, |
+ base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
+ base::Unretained(this), kSpecificCallbackId)); |
+ |
+ manager->RegisterScopedSurfaceRequest( |
+ kDummyId, |
+ base::Bind(&ScopedSurfaceRequestManagerUnitTest::LoggingCallback, |
+ base::Unretained(this), kDummyId)); |
+ |
+ last_received_request = kOtherCallbackId; |
+ |
+ manager->FulfillScopedSurfaceRequest(kSpecificCallbackId, |
+ surface_texture.get()); |
+ |
+ EXPECT_EQ(1, manager->callback_count_for_testing()); |
+ EXPECT_EQ(kSpecificCallbackId, last_received_request); |
+} |
+ |
+} // Content |