void RemotingTests::TestSummonOnCurrent()

in src/cascadia/UnitTests_Remoting/RemotingTests.cpp [1767:2017]


    void RemotingTests::TestSummonOnCurrent()
    {
        Log::Comment(L"Tests summoning a window, using OnCurrentDesktop to only"
                     L"select windows on the current desktop.");

        const winrt::guid guid1{ Utils::GuidFromString(L"{11111111-1111-1111-1111-111111111111}") };
        const winrt::guid guid2{ Utils::GuidFromString(L"{22222222-2222-2222-2222-222222222222}") };

        constexpr auto monarch0PID = 12345u;
        constexpr auto peasant1PID = 23456u;
        constexpr auto peasant2PID = 34567u;
        constexpr auto peasant3PID = 45678u;

        auto m0 = make_private<Remoting::implementation::Monarch>(monarch0PID);
        auto p1 = make_private<Remoting::implementation::Peasant>(peasant1PID);
        auto p2 = make_private<Remoting::implementation::Peasant>(peasant2PID);
        auto p3 = make_private<Remoting::implementation::Peasant>(peasant3PID);

        p1->WindowName(L"one");
        p2->WindowName(L"two");
        p3->WindowName(L"three");

        VERIFY_ARE_EQUAL(0, p1->GetID());
        VERIFY_ARE_EQUAL(0, p2->GetID());
        VERIFY_ARE_EQUAL(0, p3->GetID());

        m0->AddPeasant(*p1);
        m0->AddPeasant(*p2);
        m0->AddPeasant(*p3);

        VERIFY_ARE_EQUAL(1, p1->GetID());
        VERIFY_ARE_EQUAL(2, p2->GetID());
        VERIFY_ARE_EQUAL(3, p3->GetID());

        VERIFY_ARE_EQUAL(3u, m0->_peasants.size());

        bool p1ExpectedToBeSummoned = false;
        bool p2ExpectedToBeSummoned = false;
        bool p3ExpectedToBeSummoned = false;

        p1->SummonRequested([&](auto&&, auto&&) {
            Log::Comment(L"p1 summoned");
            VERIFY_IS_TRUE(p1ExpectedToBeSummoned);
        });
        p2->SummonRequested([&](auto&&, auto&&) {
            Log::Comment(L"p2 summoned");
            VERIFY_IS_TRUE(p2ExpectedToBeSummoned);
        });
        p3->SummonRequested([&](auto&&, auto&&) {
            Log::Comment(L"p3 summoned");
            VERIFY_IS_TRUE(p3ExpectedToBeSummoned);
        });

        {
            Log::Comment(L"Activate the first peasant, first desktop");
            Remoting::WindowActivatedArgs activatedArgs{ p1->GetID(),
                                                         p1->GetPID(), // USE PID as HWND, because these values don't _really_ matter
                                                         guid1,
                                                         winrt::clock().now() };
            p1->ActivateWindow(activatedArgs);
        }
        {
            Log::Comment(L"Activate the second peasant, second desktop");
            Remoting::WindowActivatedArgs activatedArgs{ p2->GetID(),
                                                         p2->GetPID(), // USE PID as HWND, because these values don't _really_ matter
                                                         guid2,
                                                         winrt::clock().now() };
            p2->ActivateWindow(activatedArgs);
        }
        {
            Log::Comment(L"Activate the third peasant, first desktop");
            Remoting::WindowActivatedArgs activatedArgs{ p3->GetID(),
                                                         p3->GetPID(), // USE PID as HWND, because these values don't _really_ matter
                                                         guid1,
                                                         winrt::clock().now() };
            p3->ActivateWindow(activatedArgs);
        }

        Log::Comment(L"Create a mock IVirtualDesktopManager to handle checking if a window is on a given desktop");
        auto manager = winrt::make_self<MockDesktopManager>();
        m0->_desktopManager = manager.try_as<IVirtualDesktopManager>();

        auto firstCallback = [&](HWND h, BOOL* result) -> HRESULT {
            Log::Comment(L"firstCallback: Checking if window is on desktop 1");

            const uint64_t hwnd = reinterpret_cast<uint64_t>(h);
            if (hwnd == peasant1PID || hwnd == peasant3PID)
            {
                *result = true;
            }
            else if (hwnd == peasant2PID)
            {
                *result = false;
            }
            else
            {
                VERIFY_IS_TRUE(false, L"IsWindowOnCurrentVirtualDesktop called with unexpected value");
            }
            return S_OK;
        };
        manager->pfnIsWindowOnCurrentVirtualDesktop = firstCallback;

        Remoting::SummonWindowSelectionArgs args;

        Log::Comment(L"Summon window three - it is the MRU on desktop 1");
        p3ExpectedToBeSummoned = true;
        args.OnCurrentDesktop(true);
        m0->SummonWindow(args);
        VERIFY_IS_TRUE(args.FoundMatch());

        {
            Log::Comment(L"Activate the first peasant, first desktop");
            Remoting::WindowActivatedArgs activatedArgs{ p1->GetID(),
                                                         p1->GetPID(), // USE PID as HWND, because these values don't _really_ matter
                                                         guid1,
                                                         winrt::clock().now() };
            p1->ActivateWindow(activatedArgs);
        }

        Log::Comment(L"Summon window one - it is the MRU on desktop 1");
        p1ExpectedToBeSummoned = true;
        p2ExpectedToBeSummoned = false;
        p3ExpectedToBeSummoned = false;
        args.FoundMatch(false);
        args.OnCurrentDesktop(true);
        m0->SummonWindow(args);
        VERIFY_IS_TRUE(args.FoundMatch());

        Log::Comment(L"Now we'll pretend we switched to desktop 2");

        auto secondCallback = [&](HWND h, BOOL* result) -> HRESULT {
            Log::Comment(L"secondCallback: Checking if window is on desktop 2");
            const uint64_t hwnd = reinterpret_cast<uint64_t>(h);
            if (hwnd == peasant1PID || hwnd == peasant3PID)
            {
                *result = false;
            }
            else if (hwnd == peasant2PID)
            {
                *result = true;
            }
            else
            {
                VERIFY_IS_TRUE(false, L"IsWindowOnCurrentVirtualDesktop called with unexpected value");
            }
            return S_OK;
        };
        manager->pfnIsWindowOnCurrentVirtualDesktop = secondCallback;

        Log::Comment(L"Summon window one - it is the MRU on desktop 2");
        p1ExpectedToBeSummoned = false;
        p2ExpectedToBeSummoned = true;
        p3ExpectedToBeSummoned = false;
        args.FoundMatch(false);
        args.OnCurrentDesktop(true);
        m0->SummonWindow(args);
        VERIFY_IS_TRUE(args.FoundMatch());

        {
            Log::Comment(L"Activate the third peasant, second desktop");
            Remoting::WindowActivatedArgs activatedArgs{ p3->GetID(),
                                                         p3->GetPID(), // USE PID as HWND, because these values don't _really_ matter
                                                         guid2,
                                                         winrt::clock().now() };
            p3->ActivateWindow(activatedArgs);
        }

        auto thirdCallback = [&](HWND h, BOOL* result) -> HRESULT {
            Log::Comment(L"thirdCallback: Checking if window is on desktop 2. (windows 2 and 3 are)");
            const uint64_t hwnd = reinterpret_cast<uint64_t>(h);
            if (hwnd == peasant1PID)
            {
                *result = false;
            }
            else if (hwnd == peasant2PID || hwnd == peasant3PID)
            {
                *result = true;
            }
            else
            {
                VERIFY_IS_TRUE(false, L"IsWindowOnCurrentVirtualDesktop called with unexpected value");
            }
            return S_OK;
        };
        manager->pfnIsWindowOnCurrentVirtualDesktop = thirdCallback;

        Log::Comment(L"Summon window three - it is the MRU on desktop 2");
        p1ExpectedToBeSummoned = false;
        p2ExpectedToBeSummoned = false;
        p3ExpectedToBeSummoned = true;
        args.FoundMatch(false);
        args.OnCurrentDesktop(true);
        m0->SummonWindow(args);
        VERIFY_IS_TRUE(args.FoundMatch());

        Log::Comment(L"Now we'll pretend we switched to desktop 1");

        auto fourthCallback = [&](HWND h, BOOL* result) -> HRESULT {
            Log::Comment(L"fourthCallback: Checking if window is on desktop 1. (window 1 is)");
            const uint64_t hwnd = reinterpret_cast<uint64_t>(h);
            if (hwnd == peasant1PID)
            {
                *result = true;
            }
            else if (hwnd == peasant2PID || hwnd == peasant3PID)
            {
                *result = false;
            }
            else
            {
                VERIFY_IS_TRUE(false, L"IsWindowOnCurrentVirtualDesktop called with unexpected value");
            }
            return S_OK;
        };
        manager->pfnIsWindowOnCurrentVirtualDesktop = fourthCallback;

        Log::Comment(L"Summon window one - it is the only window on desktop 1");
        p1ExpectedToBeSummoned = true;
        p2ExpectedToBeSummoned = false;
        p3ExpectedToBeSummoned = false;
        args.FoundMatch(false);
        args.OnCurrentDesktop(true);
        m0->SummonWindow(args);
        VERIFY_IS_TRUE(args.FoundMatch());

        Log::Comment(L"Now we'll pretend we switched to desktop 3");

        auto fifthCallback = [&](HWND h, BOOL* result) -> HRESULT {
            Log::Comment(L"fifthCallback: Checking if window is on desktop 3. (none are)");
            const uint64_t hwnd = reinterpret_cast<uint64_t>(h);
            if (hwnd == peasant1PID || hwnd == peasant2PID || hwnd == peasant3PID)
            {
                *result = false;
            }
            else
            {
                VERIFY_IS_TRUE(false, L"IsWindowOnCurrentVirtualDesktop called with unexpected value");
            }
            return S_OK;
        };
        manager->pfnIsWindowOnCurrentVirtualDesktop = fifthCallback;

        Log::Comment(L"This summon won't find a window.");
        p1ExpectedToBeSummoned = false;
        p2ExpectedToBeSummoned = false;
        p3ExpectedToBeSummoned = false;
        args.FoundMatch(false);
        args.OnCurrentDesktop(true);
        m0->SummonWindow(args);
        VERIFY_IS_FALSE(args.FoundMatch());
    }