Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring PushNotification unit tests #2389

Merged
merged 16 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,27 @@
<NMakeCleanCommandLine>REM TODO Clean</NMakeCleanCommandLine>

<MakeMsixOutputFilename>WindowsAppRuntime.Test.Singleton.msix</MakeMsixOutputFilename>
<TargetBasename>PushNotificationsLongRunningTask</TargetBasename>

<StartupExe>$(TargetBasename).StartupTask</StartupExe>
<StartupExeDir>$(OutDir)$(StartupExe)</StartupExeDir>
<StartupExeFile>$(StartupExeDir)\$(StartupExe).exe</StartupExeFile>

<TargetExe>$(TargetBasename)</TargetExe>
<TargetExeDir>$(OutDir)$(TargetExe)</TargetExeDir>
<TargetExeFile>$(TargetExeDir)\$(TargetExe).exe</TargetExeFile>

<TargetExeProxyStub>$(TargetBasename).ProxyStub</TargetExeProxyStub>
<TargetExeProxyStubDir>$(OutDir)$(TargetExeProxyStub)</TargetExeProxyStubDir>
<TargetExeProxyStubFile>$(TargetExeProxyStubDir)\$(TargetExeProxyStub).dll</TargetExeProxyStubFile>
</PropertyGroup>
<ItemGroup>
<ItemGroup>
<MakeMsixInputAssets Include="$(MSBuildThisFileDirectory)\Assets\*" />
<MakeMsixInputs Include="$(MSBuildThisFileDirectory)appxmanifest.xml" />
<MakeMsixInputs Include="$(OutDir)..\WindowsAppRuntime_DLL\Microsoft.Internal.FrameworkUdk.dll" />
<MakeMsixInputs Include="$(StartupExeFile)" />
<MakeMsixInputs Include="$(TargetExeFile)" />
<MakeMsixInputs Include="$(TargetExeProxyStubFile)" />
</ItemGroup>
<PropertyGroup>
<PackageCertificateKeyFile>$(RepoTestCertificatePFX)</PackageCertificateKeyFile>
Expand All @@ -107,6 +124,17 @@
<ItemGroup>
<Xml Include="appxmanifest.xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\dev\PushNotifications\PushNotificationsLongRunningTask.ProxyStub\PushNotificationsLongRunningTask.ProxyStub.vcxproj">
<Project>{bf3fced0-cadb-490a-93a7-4d90e1f45ab0}</Project>
</ProjectReference>
<ProjectReference Include="$(RepoRoot)\dev\PushNotifications\PushNotificationsLongRunningTask.StartupTask\PushNotificationsLongRunningTask.StartupTask.vcxproj">
<Project>{1debbff6-ee6e-4944-9de2-35b7a686af42}</Project>
</ProjectReference>
<ProjectReference Include="$(RepoRoot)\dev\PushNotifications\PushNotificationsLongRunningTask\PushNotificationsLongRunningTask.vcxproj">
<Project>{1307dd1b-bbe8-4cd0-b1a0-0db6d61eeaa0}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,52 @@
<Resource Language="en"/>
</Resources>

<Applications>
<Application Id="App"
Executable="PushNotificationsLongRunningTask.exe"
EntryPoint="Windows.FullTrustApplication">
<uap:VisualElements
DisplayName="Windows App SDK Test.PushNotificationsTask"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png"
Description="Push Notifications Long Running Task"
BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
<uap:SplashScreen Image="Assets\SplashScreen.png" uap5:Optional="true"/>
</uap:VisualElements>
<Extensions>
<com:Extension Category="windows.comServer">
<com:ComServer>
<com:ExeServer Executable="PushNotificationsLongRunningTask.exe" DisplayName="Push Notifications Long Running Task">
<com:Class Id="E739C755-0D09-48DF-A468-A5DF0B5422DC" DisplayName="Push Notifications Long Running Task"/>
</com:ExeServer>
</com:ComServer>
</com:Extension>
<com:Extension Category="windows.comInterface">
<com:ComInterface>
<com:ProxyStub Id="60FC21B2-B396-4D49-94F0-7555869FB93C" DisplayName="PushNotificationsLongRunningTask ProxyStub" Path="PushNotificationsLongRunningTask.ProxyStub.dll"/>
<com:Interface Id="60FC21B2-B396-4D49-94F0-7555869FB93C" ProxyStubClsid="60FC21B2-B396-4D49-94F0-7555869FB93C"/> <!-- IID_IWpnLrpPlatform -->
</com:ComInterface>
</com:Extension>
<com:Extension Category="windows.comInterface">
<com:ComInterface>
<com:ProxyStub Id="25604D55-9B17-426F-9D67-2B11B3A65598" DisplayName="PushNotificationsLongRunningTask ProxyStub" Path="PushNotificationsLongRunningTask.ProxyStub.dll"/>
<com:Interface Id="25604D55-9B17-426F-9D67-2B11B3A65598" ProxyStubClsid="25604D55-9B17-426F-9D67-2B11B3A65598"/> <!-- IID_IWpnForegroundSink -->
</com:ComInterface>
</com:Extension>
<uap5:Extension
Category="windows.startupTask"
Executable="PushNotificationsLongRunningTask.StartupTask.exe"
EntryPoint="Windows.FullTrustApplication">
<uap5:StartupTask
TaskId="PushNotificationsId"
Enabled="true"
DisplayName="Push Notifications Long Running Task" />
</uap5:Extension>
</Extensions>
</Application>
</Applications>

<Capabilities>
<rescap:Capability Name="runFullTrust" />
</Capabilities>
Expand Down
8 changes: 2 additions & 6 deletions test/PowerNotifications/APITests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

#include "pch.h"
#include "winrt/Microsoft.Windows.System.Power.h"
#include "winrt/Microsoft.Windows.PushNotifications.h"

using namespace std::chrono_literals;
using namespace winrt::Microsoft::Windows::System::Power;
using namespace winrt::Microsoft::Windows::PushNotifications;

namespace Test::PowerNotifications
{
Expand Down Expand Up @@ -53,10 +51,8 @@ namespace Test::PowerNotifications

TEST_METHOD(GetBatteryStatus)
{
/*auto value = PowerManager::BatteryStatus();
VERIFY_ARE_EQUAL(value, BatteryStatus::NotPresent);*/

PushNotificationManager::Default().Register();
auto value = PowerManager::BatteryStatus();
VERIFY_ARE_EQUAL(value, BatteryStatus::NotPresent);
}

TEST_METHOD(GetPowerSupplyStatus)
Expand Down
6 changes: 1 addition & 5 deletions test/PowerNotifications/PowerNotifications.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Windows.PushNotifications">
<HintPath>..\..\BuildOutput\Debug\x64\WindowsAppRuntime_DLL\Microsoft.Windows.PushNotifications.winmd</HintPath>
<IsWinMDFile>true</IsWinMDFile>
</Reference>
<Reference Include="Microsoft.Windows.System.Power">
<HintPath>$(OutDir)\..\WindowsAppRuntime_DLL\Microsoft.Windows.System.Power.winmd</HintPath>
<IsWinMDFile>true</IsWinMDFile>
Expand Down Expand Up @@ -214,4 +210,4 @@
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.210913.7\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.CppWinRT.2.0.210913.7\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.210913.7\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.CppWinRT.2.0.210913.7\build\native\Microsoft.Windows.CppWinRT.targets'))" />
</Target>
</Project>
</Project>
249 changes: 249 additions & 0 deletions test/PushNotificationTests/BaseTestSuite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#include "pch.h"

#include "PushNotification-Test-Constants.h"
#include "BaseTestSuite.h"

using namespace WEX::Common;
using namespace WEX::Logging;
using namespace WEX::TestExecution;

using namespace winrt::Windows::ApplicationModel::Activation;
using namespace winrt::Windows::ApplicationModel::Background;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::Management::Deployment;
using namespace winrt::Windows::Storage;
using namespace winrt::Windows::System;
using namespace winrt::Microsoft::Windows::PushNotifications;

void BaseTestSuite::ClassSetup()
{
::Test::Bootstrap::Setup();
bool isSelfContained{};
VERIFY_SUCCEEDED(TestData::TryGetValue(L"SelfContained", isSelfContained));

if (!isSelfContained)
{
::WindowsAppRuntime::SelfContained::TestInitialize(::Test::Bootstrap::TP::WindowsAppRuntimeFramework::c_PackageFamilyName);
}
}

void BaseTestSuite::ClassCleanup()
{
::Test::Bootstrap::Cleanup();
}

void BaseTestSuite::MethodCleanup()
{
if (!m_unregisteredFully)
{
PushNotificationManager::Default().UnregisterAll();
m_unregisteredFully = true;
}
}

HRESULT BaseTestSuite::ChannelRequestHelper(IAsyncOperationWithProgress<PushNotificationCreateChannelResult, PushNotificationCreateChannelStatus> const& channelOperation)
{
if (channelOperation.wait_for(c_timeout) != winrt::Windows::Foundation::AsyncStatus::Completed)
{
channelOperation.Cancel();
RETURN_WIN32(ERROR_TIMEOUT); // timed out or failed
}

auto result{ channelOperation.GetResults() };
auto status{ result.Status() };
RETURN_HR_IF(result.ExtendedError(), status != PushNotificationChannelStatus::CompletedSuccess);

result.Channel().Close();
return S_OK;
}

void BaseTestSuite::RegisterWithPushNotificationManager()
{
PushNotificationManager::Default().Register();
m_unregisteredFully = false;
}

void BaseTestSuite::UnregisterAllWithPushNotificationManager()
{
PushNotificationManager::Default().UnregisterAll();
m_unregisteredFully = true;
}

void BaseTestSuite::ChannelRequestUsingNullRemoteId()
{
if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().CreateChannelAsync(winrt::guid()).get(), E_INVALIDARG);
}
else
{
auto channelOperation{ PushNotificationManager::Default().CreateChannelAsync(winrt::guid()) };
VERIFY_ARE_EQUAL(ChannelRequestHelper(channelOperation), E_FAIL);
}
}

void BaseTestSuite::ChannelRequestUsingRemoteId()
{
if (PushNotificationManager::Default().IsSupported())
{
auto channelOperation{ PushNotificationManager::Default().CreateChannelAsync(c_azureRemoteId) };
VERIFY_SUCCEEDED(ChannelRequestHelper(channelOperation));
}
else
{
auto channelOperation{ PushNotificationManager::Default().CreateChannelAsync(c_azureRemoteId) };
VERIFY_ARE_EQUAL(ChannelRequestHelper(channelOperation), E_FAIL);
}
}

// Currently failing - https://github.com/microsoft/WindowsAppSDK/issues/2392
void BaseTestSuite::MultipleChannelClose()
{
auto channelOperation{ PushNotificationManager::Default().CreateChannelAsync(c_azureRemoteId) };
if (channelOperation.wait_for(c_timeout) != winrt::Windows::Foundation::AsyncStatus::Completed)
{
channelOperation.Cancel();
VERIFY_FAIL(L"Channel request hit timeout.");
}

auto result{ channelOperation.GetResults() };
VERIFY_ARE_EQUAL(result.Status(), PushNotificationChannelStatus::CompletedSuccess);

result.Channel().Close();
VERIFY_THROWS_HR(result.Channel().Close(), WPN_E_CHANNEL_CLOSED);
}

void BaseTestSuite::VerifyRegisterAndUnregister()
{
RegisterWithPushNotificationManager();

PushNotificationManager::Default().Unregister();
}

void BaseTestSuite::VerifyUnregisterFails()
{
if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().Unregister(), E_UNEXPECTED);
}
else
{
PushNotificationManager::Default().Unregister();
}
}

void BaseTestSuite::VerifyUnregisterAllFails()
{
if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().UnregisterAll(), E_UNEXPECTED);
}
else
{
PushNotificationManager::Default().UnregisterAll();
}
}

void BaseTestSuite::VerifyRegisterAndUnregisterAll()
{
RegisterWithPushNotificationManager();
UnregisterAllWithPushNotificationManager();
}

void BaseTestSuite::MultipleRegister()
{
RegisterWithPushNotificationManager();
if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().Register(), HRESULT_FROM_WIN32(ERROR_ALREADY_REGISTERED));
}
else
{
PushNotificationManager::Default().Register();
}
}

void BaseTestSuite::VerifyMultipleRegisterAndUnregister()
{
RegisterWithPushNotificationManager();

PushNotificationManager::Default().Unregister();

if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().Register(), HRESULT_FROM_WIN32(ERROR_ALREADY_REGISTERED));
}
else
{
PushNotificationManager::Default().Register();
}
}

void BaseTestSuite::VerifyMultipleRegisterAndUnregisterAll()
{
RegisterWithPushNotificationManager();
UnregisterAllWithPushNotificationManager();

RegisterWithPushNotificationManager();
UnregisterAllWithPushNotificationManager();
}

void BaseTestSuite::VerifyUnregisterTwice()
{
RegisterWithPushNotificationManager();

PushNotificationManager::Default().Unregister();
if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().Unregister(), E_UNEXPECTED);
}
else
{
PushNotificationManager::Default().Unregister();
}
}

void BaseTestSuite::VerifyUnregisterAllTwice()
{
RegisterWithPushNotificationManager();
UnregisterAllWithPushNotificationManager();

if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().UnregisterAll(), E_UNEXPECTED);
}
else
{
PushNotificationManager::Default().UnregisterAll();
}
}

void BaseTestSuite::VerifyUnregisterAndUnregisterAll()
{
RegisterWithPushNotificationManager();
PushNotificationManager::Default().Unregister();
UnregisterAllWithPushNotificationManager();
}

void BaseTestSuite::VerifyForegroundHandlerSucceeds()
{
PushNotificationManager::Default().PushReceived([](const auto&, PushNotificationReceivedEventArgs const& /* args */) {});
RegisterWithPushNotificationManager();
}

void BaseTestSuite::VerifyForegroundHandlerFails()
{
RegisterWithPushNotificationManager();
if (PushNotificationManager::Default().IsSupported())
{
VERIFY_THROWS_HR(PushNotificationManager::Default().PushReceived([](const auto&, PushNotificationReceivedEventArgs const& /* args */) {}), HRESULT_FROM_WIN32(ERROR_NOT_FOUND));
}
else
{
PushNotificationManager::Default().PushReceived([](const auto&, PushNotificationReceivedEventArgs const& /* args */) {});
}
}
Loading