| Index: media/mojo/clients/mojo_android_overlay_unittest.cc
|
| diff --git a/media/mojo/clients/mojo_android_overlay_unittest.cc b/media/mojo/clients/mojo_android_overlay_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6b88bad8b93f7e8a3afc358189780e9ef6029702
|
| --- /dev/null
|
| +++ b/media/mojo/clients/mojo_android_overlay_unittest.cc
|
| @@ -0,0 +1,176 @@
|
| +// Copyright 2017 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 <memory>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/run_loop.h"
|
| +#include "media/base/mock_filters.h"
|
| +#include "media/mojo/clients/mojo_android_overlay.h"
|
| +#include "mojo/public/cpp/bindings/interface_request.h"
|
| +#include "mojo/public/cpp/bindings/strong_binding.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using ::testing::_;
|
| +using ::testing::StrictMock;
|
| +
|
| +namespace media {
|
| +
|
| +class MockAndroidOverlay : public StrictMock<mojom::AndroidOverlay> {
|
| + public:
|
| + MOCK_METHOD1(ScheduleLayout, void(const gfx::Rect& rect));
|
| +};
|
| +
|
| +// Handy class with client-level callback mocks.
|
| +class ClientCallbacks {
|
| + public:
|
| + virtual void OnReady() = 0;
|
| + virtual void OnDestroyed() = 0;
|
| +};
|
| +
|
| +class MockClientCallbacks : public StrictMock<ClientCallbacks> {
|
| + public:
|
| + MOCK_METHOD0(OnReady, void());
|
| + MOCK_METHOD0(OnDestroyed, void());
|
| +};
|
| +
|
| +class MockAndroidOverlayProvider
|
| + : public StrictMock<mojom::AndroidOverlayProvider> {
|
| + public:
|
| + // These argument types lack default constructors, so gmock can't mock them.
|
| + void CreateOverlay(mojom::AndroidOverlayClientPtr client,
|
| + mojom::AndroidOverlayConfigPtr config) override {
|
| + client_ = std::move(client);
|
| + config_ = std::move(config);
|
| + OverlayCreated();
|
| + }
|
| +
|
| + MOCK_METHOD0(OverlayCreated, void(void));
|
| +
|
| + mojom::AndroidOverlayClientPtr client_;
|
| + mojom::AndroidOverlayConfigPtr config_;
|
| +};
|
| +
|
| +class MojoAndroidOverlayTest : public ::testing::Test {
|
| + public:
|
| + MojoAndroidOverlayTest()
|
| + : provider_binding_(&mock_provider_), overlay_binding_(&mock_overlay_) {}
|
| +
|
| + ~MojoAndroidOverlayTest() override {}
|
| +
|
| + void SetUp() override {
|
| + provider_ = provider_binding_.CreateInterfacePtrAndBind();
|
| +
|
| + // Set up default config.
|
| + config_.rect = gfx::Rect(100, 200, 300, 400);
|
| + config_.ready_cb = base::Bind(&MockClientCallbacks::OnReady,
|
| + base::Unretained(&callbacks_));
|
| + config_.destroyed_cb = base::Bind(&MockClientCallbacks::OnDestroyed,
|
| + base::Unretained(&callbacks_));
|
| + }
|
| +
|
| + void TearDown() override {
|
| + overlay_client_.reset();
|
| + base::RunLoop().RunUntilIdle();
|
| + }
|
| +
|
| + // Create an overlay in |overlay_client_| using the current config, but do
|
| + // not initialize it with an overlay impl.
|
| + void CreateOverlay() {
|
| + EXPECT_CALL(mock_provider_, OverlayCreated());
|
| + overlay_client_.reset(new MojoAndroidOverlay(
|
| + std::move(provider_), render_frame_id_, renderer_pid_, config_));
|
| + base::RunLoop().RunUntilIdle();
|
| + }
|
| +
|
| + // Create an overlay, then provide it with |mock_overlay_|.
|
| + void CreateAndInitializeOverlay() {
|
| + CreateOverlay();
|
| +
|
| + // Notify the client about the overlay mock.
|
| + mock_provider_.client_->OnInitialized(
|
| + overlay_binding_.CreateInterfacePtrAndBind());
|
| + base::RunLoop().RunUntilIdle();
|
| + }
|
| +
|
| + // Notify |overlay_client_| that the surface is ready.
|
| + void CreateSurface() {
|
| + EXPECT_CALL(callbacks_, OnReady());
|
| + const int surface_key = 123;
|
| + mock_provider_.client_->OnSurfaceReady(surface_key);
|
| + base::RunLoop().RunUntilIdle();
|
| + }
|
| +
|
| + // Destroy the overlay. This includes onSurfaceDestroyed cases.
|
| + void DestroyOverlay() {
|
| + EXPECT_CALL(callbacks_, OnDestroyed());
|
| + mock_provider_.client_->OnDestroyed();
|
| + base::RunLoop().RunUntilIdle();
|
| + }
|
| +
|
| + // Mojo stuff.
|
| + base::MessageLoop loop;
|
| + mojom::AndroidOverlayProviderPtr provider_;
|
| + mojo::Binding<mojom::AndroidOverlayProvider> provider_binding_;
|
| + mojo::Binding<mojom::AndroidOverlay> overlay_binding_;
|
| +
|
| + // The mock provider that |overlay_client_| will talk to.
|
| + MockAndroidOverlayProvider mock_provider_;
|
| +
|
| + // The mock overlay impl that |mock_provider_| will provide.
|
| + MockAndroidOverlay mock_overlay_;
|
| +
|
| + // The client under test.
|
| + std::unique_ptr<AndroidOverlay> overlay_client_;
|
| +
|
| + // Inital config for |CreateOverlay|.
|
| + // Set to sane values, but feel free to modify before CreateOverlay().
|
| + int render_frame_id_ = 1;
|
| + int renderer_pid_ = 2;
|
| + AndroidOverlay::Config config_;
|
| + MockClientCallbacks callbacks_;
|
| +};
|
| +
|
| +// Verify basic create => init => ready => destroyed.
|
| +TEST_F(MojoAndroidOverlayTest, CreateInitReadyDestroy) {
|
| + CreateAndInitializeOverlay();
|
| + CreateSurface();
|
| + DestroyOverlay();
|
| +}
|
| +
|
| +// Verify that initialization failure results in an onDestroyed callback.
|
| +TEST_F(MojoAndroidOverlayTest, InitFailure) {
|
| + CreateOverlay();
|
| + DestroyOverlay();
|
| +}
|
| +
|
| +// Verify that we can destroy the overlay before providing a surface.
|
| +TEST_F(MojoAndroidOverlayTest, CreateInitDestroy) {
|
| + CreateAndInitializeOverlay();
|
| + DestroyOverlay();
|
| +}
|
| +
|
| +// Test that layouts happen.
|
| +TEST_F(MojoAndroidOverlayTest, LayoutOverlay) {
|
| + CreateAndInitializeOverlay();
|
| + CreateSurface();
|
| +
|
| + gfx::Rect new_layout(5, 6, 7, 8);
|
| + EXPECT_CALL(mock_overlay_, ScheduleLayout(new_layout));
|
| + overlay_client_->ScheduleLayout(new_layout);
|
| +}
|
| +
|
| +// Test that layouts are ignored before the client is notified about a surface.
|
| +TEST_F(MojoAndroidOverlayTest, LayoutBeforeSurfaceIsIgnored) {
|
| + CreateAndInitializeOverlay();
|
| +
|
| + gfx::Rect new_layout(5, 6, 7, 8);
|
| + EXPECT_CALL(mock_overlay_, ScheduleLayout(_)).Times(0);
|
| + overlay_client_->ScheduleLayout(new_layout);
|
| +}
|
| +
|
| +} // namespace media
|
|
|