Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit

Permalink
[Windows] Refactor surface manager mocking (#48953)
Browse files Browse the repository at this point in the history
  • Loading branch information
loic-sharma authored Dec 13, 2023
1 parent ae61286 commit d202141
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
1 change: 1 addition & 0 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ executable("flutter_windows_unittests") {
"testing/flutter_window_test.h",
"testing/flutter_windows_engine_builder.cc",
"testing/flutter_windows_engine_builder.h",
"testing/mock_angle_surface_manager.h",
"testing/mock_direct_manipulation.h",
"testing/mock_gl_proc_table.h",
"testing/mock_text_input_manager.cc",
Expand Down
3 changes: 2 additions & 1 deletion shell/platform/windows/flutter_windows_engine_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "flutter/shell/platform/windows/public/flutter_windows.h"
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
#include "flutter/shell/platform/windows/testing/mock_angle_surface_manager.h"
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
#include "flutter/shell/platform/windows/testing/mock_windows_proc_table.h"
#include "flutter/shell/platform/windows/testing/test_keyboard.h"
Expand Down Expand Up @@ -131,7 +132,7 @@ TEST_F(FlutterWindowsEngineTest, RunDoesExpectedInitialization) {
}));

// Set the AngleSurfaceManager to !nullptr to test ANGLE rendering.
modifier.SetSurfaceManager(reinterpret_cast<AngleSurfaceManager*>(1));
modifier.SetSurfaceManager(std::make_unique<MockAngleSurfaceManager>());

engine->Run();

Expand Down
37 changes: 8 additions & 29 deletions shell/platform/windows/flutter_windows_view_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
#include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
#include "flutter/shell/platform/windows/testing/mock_angle_surface_manager.h"
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
#include "flutter/shell/platform/windows/testing/test_keyboard.h"

Expand Down Expand Up @@ -118,28 +119,6 @@ class MockFlutterWindowsEngine : public FlutterWindowsEngine {
FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsEngine);
};

class MockAngleSurfaceManager : public AngleSurfaceManager {
public:
MockAngleSurfaceManager() : AngleSurfaceManager(false) {}

MOCK_METHOD(bool,
CreateSurface,
(WindowsRenderTarget*, EGLint, EGLint),
(override));
MOCK_METHOD(void,
ResizeSurface,
(WindowsRenderTarget*, EGLint, EGLint, bool),
(override));
MOCK_METHOD(void, DestroySurface, (), (override));

MOCK_METHOD(bool, MakeCurrent, (), (override));
MOCK_METHOD(bool, ClearCurrent, (), (override));
MOCK_METHOD(void, SetVSyncEnabled, (bool), (override));

private:
FML_DISALLOW_COPY_AND_ASSIGN(MockAngleSurfaceManager);
};

} // namespace

// Ensure that submenu buttons have their expanded/collapsed status set
Expand Down Expand Up @@ -264,7 +243,7 @@ TEST(FlutterWindowsViewTest, Shutdown) {
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1);

modifier.SetSurfaceManager(surface_manager.release());
modifier.SetSurfaceManager(std::move(surface_manager));
view.SetEngine(engine.get());
}

Expand Down Expand Up @@ -847,7 +826,7 @@ TEST(FlutterWindowsViewTest, WindowResizeTests) {
EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1);

FlutterWindowsView view(std::move(window_binding_handler));
modifier.SetSurfaceManager(surface_manager.release());
modifier.SetSurfaceManager(std::move(surface_manager));
view.SetEngine(engine.get());

fml::AutoResetWaitableEvent metrics_sent_latch;
Expand Down Expand Up @@ -1250,7 +1229,7 @@ TEST(FlutterWindowsViewTest, DisablesVSyncAtStartup) {
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1);

modifier.SetSurfaceManager(surface_manager.release());
modifier.SetSurfaceManager(std::move(surface_manager));
view.SetEngine(engine.get());

view.CreateRenderSurface();
Expand Down Expand Up @@ -1283,7 +1262,7 @@ TEST(FlutterWindowsViewTest, EnablesVSyncAtStartup) {
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1);

modifier.SetSurfaceManager(surface_manager.release());
modifier.SetSurfaceManager(std::move(surface_manager));
view.SetEngine(engine.get());

view.CreateRenderSurface();
Expand Down Expand Up @@ -1320,7 +1299,7 @@ TEST(FlutterWindowsViewTest, DisablesVSyncAfterStartup) {
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1);

modifier.SetSurfaceManager(surface_manager.release());
modifier.SetSurfaceManager(std::move(surface_manager));
view.SetEngine(engine.get());

view.CreateRenderSurface();
Expand Down Expand Up @@ -1358,7 +1337,7 @@ TEST(FlutterWindowsViewTest, EnablesVSyncAfterStartup) {
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1);

modifier.SetSurfaceManager(surface_manager.release());
modifier.SetSurfaceManager(std::move(surface_manager));
view.SetEngine(engine.get());

view.CreateRenderSurface();
Expand Down Expand Up @@ -1399,7 +1378,7 @@ TEST(FlutterWindowsViewTest, UpdatesVSyncOnDwmUpdates) {
EXPECT_CALL(*engine.get(), Stop).Times(1);
EXPECT_CALL(*surface_manager.get(), DestroySurface).Times(1);

modifier.SetSurfaceManager(surface_manager.release());
modifier.SetSurfaceManager(std::move(surface_manager));
view.SetEngine(engine.get());

view.GetEngine()->OnDwmCompositionChanged();
Expand Down
8 changes: 3 additions & 5 deletions shell/platform/windows/testing/engine_modifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ class EngineModifier {
// engine unless overwritten again.
FlutterEngineProcTable& embedder_api() { return engine_->embedder_api_; }

// Explicitly sets the SurfaceManager being used by the FlutterWindowsEngine
// instance. This allows us to test fallback paths when a SurfaceManager fails
// to initialize for whatever reason.
// Override the surface manager used by the engine.
//
// Modifications are to the engine, and will last for the lifetime of the
// engine unless overwritten again.
void SetSurfaceManager(AngleSurfaceManager* surface_manager) {
engine_->surface_manager_.reset(surface_manager);
void SetSurfaceManager(std::unique_ptr<AngleSurfaceManager> surface_manager) {
engine_->surface_manager_ = std::move(surface_manager);
}

/// Reset the start_time field that is used to align vsync events.
Expand Down
41 changes: 41 additions & 0 deletions shell/platform/windows/testing/mock_angle_surface_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_ANGLE_SURFACE_MANAGER_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_ANGLE_SURFACE_MANAGER_H_

#include "flutter/fml/macros.h"
#include "flutter/shell/platform/windows/angle_surface_manager.h"
#include "gmock/gmock.h"

namespace flutter {
namespace testing {

/// Mock for the |AngleSurfaceManager| base class.
class MockAngleSurfaceManager : public AngleSurfaceManager {
public:
MockAngleSurfaceManager() : AngleSurfaceManager(false) {}

MOCK_METHOD(bool,
CreateSurface,
(WindowsRenderTarget*, EGLint, EGLint),
(override));
MOCK_METHOD(void,
ResizeSurface,
(WindowsRenderTarget*, EGLint, EGLint, bool),
(override));
MOCK_METHOD(void, DestroySurface, (), (override));

MOCK_METHOD(bool, MakeCurrent, (), (override));
MOCK_METHOD(bool, ClearCurrent, (), (override));
MOCK_METHOD(void, SetVSyncEnabled, (bool), (override));

private:
FML_DISALLOW_COPY_AND_ASSIGN(MockAngleSurfaceManager);
};

} // namespace testing
} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_ANGLE_SURFACE_MANAGER_H_

0 comments on commit d202141

Please sign in to comment.