SF: Add a first real unit test

Use the newly defined mocks to substitute for the real instances,
allowing a simple direct test of
SurfaceFlinger::processDisplayChangesLocked(), for the case of a display
being added.

[Test execution time 25ms]

Test: libsurfaceflinger_unittest passes on Pixel XL
Bug: None
Change-Id: Icdb763ecc3adb1a8bf04268f3dc5716606e6e7e8
diff --git a/services/surfaceflinger/tests/unittests/DisplayTransactionTest.cpp b/services/surfaceflinger/tests/unittests/DisplayTransactionTest.cpp
index fc1b564..3841209 100644
--- a/services/surfaceflinger/tests/unittests/DisplayTransactionTest.cpp
+++ b/services/surfaceflinger/tests/unittests/DisplayTransactionTest.cpp
@@ -22,11 +22,29 @@
 
 #include <log/log.h>
 
+#include "MockComposer.h"
+#include "MockEventThread.h"
+#include "MockRenderEngine.h"
 #include "TestableSurfaceFlinger.h"
 
 namespace android {
 namespace {
 
+using testing::_;
+using testing::ByMove;
+using testing::DoAll;
+using testing::Mock;
+using testing::Return;
+using testing::SetArgPointee;
+
+using android::hardware::graphics::common::V1_0::Hdr;
+using android::Hwc2::Error;
+using android::Hwc2::IComposer;
+using android::Hwc2::IComposerClient;
+
+constexpr int32_t DEFAULT_REFRESH_RATE = 1666666666;
+constexpr int32_t DEFAULT_DPI = 320;
+
 class DisplayTransactionTest : public testing::Test {
 protected:
     DisplayTransactionTest();
@@ -36,12 +54,24 @@
     void setupPrimaryDisplay(int width, int height);
 
     TestableSurfaceFlinger mFlinger;
+    mock::EventThread* mEventThread = new mock::EventThread();
+
+    // These mocks are created by the test, but are destroyed by SurfaceFlinger
+    // by virtue of being stored into a std::unique_ptr. However we still need
+    // to keep a reference to them for use in setting up call expectations.
+    RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
+    Hwc2::mock::Composer* mComposer = new Hwc2::mock::Composer();
 };
 
 DisplayTransactionTest::DisplayTransactionTest() {
     const ::testing::TestInfo* const test_info =
             ::testing::UnitTest::GetInstance()->current_test_info();
     ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
+
+    mFlinger.mutableEventThread().reset(mEventThread);
+    mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
+
+    setupComposer(0);
 }
 
 DisplayTransactionTest::~DisplayTransactionTest() {
@@ -50,13 +80,92 @@
     ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
 }
 
-TEST_F(DisplayTransactionTest, PlaceholderTrivialTest) {
-    auto result = mFlinger.getDefaultDisplayDeviceLocked();
-    EXPECT_EQ(nullptr, result.get());
+void DisplayTransactionTest::setupComposer(int virtualDisplayCount) {
+    EXPECT_CALL(*mComposer, getCapabilities())
+            .WillOnce(Return(std::vector<IComposer::Capability>()));
+    EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
+    mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
 
-    EXPECT_EQ(nullptr, mFlinger.mutableBuiltinDisplays()[0].get());
-    mFlinger.mutableBuiltinDisplays()[0] = new BBinder();
-    EXPECT_NE(nullptr, mFlinger.mutableBuiltinDisplays()[0].get());
+    Mock::VerifyAndClear(mComposer);
+}
+
+void DisplayTransactionTest::setupPrimaryDisplay(int width, int height) {
+    EXPECT_CALL(*mComposer, getDisplayType(DisplayDevice::DISPLAY_PRIMARY, _))
+            .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL),
+                            Return(Error::NONE)));
+    EXPECT_CALL(*mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
+    EXPECT_CALL(*mComposer, getDisplayConfigs(_, _))
+            .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{0}), Return(Error::NONE)));
+    EXPECT_CALL(*mComposer,
+                getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
+                                    IComposerClient::Attribute::WIDTH, _))
+            .WillOnce(DoAll(SetArgPointee<3>(width), Return(Error::NONE)));
+    EXPECT_CALL(*mComposer,
+                getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
+                                    IComposerClient::Attribute::HEIGHT, _))
+            .WillOnce(DoAll(SetArgPointee<3>(height), Return(Error::NONE)));
+    EXPECT_CALL(*mComposer,
+                getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
+                                    IComposerClient::Attribute::VSYNC_PERIOD, _))
+            .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
+    EXPECT_CALL(*mComposer,
+                getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
+                                    IComposerClient::Attribute::DPI_X, _))
+            .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
+    EXPECT_CALL(*mComposer,
+                getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
+                                    IComposerClient::Attribute::DPI_Y, _))
+            .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
+
+    mFlinger.setupPrimaryDisplay();
+
+    Mock::VerifyAndClear(mComposer);
+}
+
+TEST_F(DisplayTransactionTest, processDisplayChangesLockedProcessesPrimaryDisplayConnected) {
+    using android::hardware::graphics::common::V1_0::ColorMode;
+
+    setupPrimaryDisplay(1920, 1080);
+
+    sp<BBinder> token = new BBinder();
+    mFlinger.mutableCurrentState().displays.add(token, {DisplayDevice::DISPLAY_PRIMARY, true});
+
+    EXPECT_CALL(*mComposer, getActiveConfig(DisplayDevice::DISPLAY_PRIMARY, _))
+            .WillOnce(DoAll(SetArgPointee<1>(0), Return(Error::NONE)));
+    EXPECT_CALL(*mComposer, getColorModes(DisplayDevice::DISPLAY_PRIMARY, _))
+            .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::NATIVE})),
+                            Return(Error::NONE)));
+
+    EXPECT_CALL(*mComposer, getHdrCapabilities(DisplayDevice::DISPLAY_PRIMARY, _, _, _, _))
+            .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
+
+    auto reSurface = new RE::mock::Surface();
+    EXPECT_CALL(*mRenderEngine, createSurface())
+            .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(reSurface))));
+    EXPECT_CALL(*reSurface, setAsync(false)).Times(1);
+    EXPECT_CALL(*reSurface, setCritical(true)).Times(1);
+    EXPECT_CALL(*reSurface, setNativeWindow(_)).Times(1);
+    EXPECT_CALL(*reSurface, queryWidth()).WillOnce(Return(1920));
+    EXPECT_CALL(*reSurface, queryHeight()).WillOnce(Return(1080));
+
+    EXPECT_CALL(*mEventThread, onHotplugReceived(DisplayDevice::DISPLAY_PRIMARY, true)).Times(1);
+
+    mFlinger.processDisplayChangesLocked();
+
+    ASSERT_TRUE(mFlinger.mutableDisplays().indexOfKey(token) >= 0);
+
+    const auto& device = mFlinger.mutableDisplays().valueFor(token);
+    ASSERT_TRUE(device.get());
+    EXPECT_TRUE(device->isSecure());
+    EXPECT_TRUE(device->isPrimary());
+
+    ssize_t i = mFlinger.mutableDrawingState().displays.indexOfKey(token);
+    ASSERT_GE(0, i);
+    const auto& draw = mFlinger.mutableDrawingState().displays[i];
+    EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, draw.type);
+
+    EXPECT_CALL(*mComposer, setVsyncEnabled(0, IComposerClient::Vsync::DISABLE))
+            .WillOnce(Return(Error::NONE));
 }
 
 } // namespace