Skip to content

Commit

Permalink
Refactored unregistering tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
pmpurifoy committed Jun 11, 2021
1 parent 6f55098 commit bd4bc2b
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions test/TestApps/PushNotificationsTestApp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ using namespace winrt::Windows::Storage::Streams;
winrt::guid remoteId1(L"a2e4a323-b518-4799-9e80-0b37aeb0d225"); // Generated from ms.portal.azure.com
winrt::guid remoteId2(L"CA1A4AB2-AC1D-4EFC-A132-E5A191CA285A"); // Dummy guid from visual studio guid tool generator

PushNotificationRegistrationToken appToken = nullptr;
PushNotificationRegistrationToken fakeToken = nullptr;
PushNotificationRegistrationToken g_appToken = nullptr;

bool ChannelRequestUsingNullRemoteId()
{
Expand Down Expand Up @@ -60,6 +59,7 @@ bool ChannelRequestUsingRemoteId()
return channelOperationResult == S_OK;
}

// Verify calling channel close will fail when called twice.
bool MultipleChannelClose()
{
auto channelOperation = PushNotificationManager::CreateChannelAsync(remoteId1);
Expand Down Expand Up @@ -111,14 +111,22 @@ bool MultipleChannelRequestUsingMultipleRemoteId()

bool ActivatorTest()
{
PushNotificationManager::UnregisterActivator(std::exchange(appToken, nullptr), PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
PushNotificationManager::UnregisterActivator(std::exchange(g_appToken, nullptr), PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);

try
{
PushNotificationActivationInfo info(
PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator,
c_fakeComServerId);

PushNotificationRegistrationToken fakeToken = nullptr;
auto scope_exit = wil::scope_exit([&] {
if (fakeToken)
{
PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
}
});

fakeToken = PushNotificationManager::RegisterActivator(info);
if (!fakeToken.TaskRegistration())
{
Expand All @@ -134,11 +142,19 @@ bool ActivatorTest()
return true;
}

// Verify calling register activator with null PushNotificationActivationInfo is not allowed.
bool RegisterActivatorNullDetails()
{
try
{
PushNotificationManager::RegisterActivator(nullptr);
PushNotificationRegistrationToken fakeToken = nullptr;
auto scope_exit = wil::scope_exit([&] {
if (fakeToken)
{
PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
}
});
fakeToken = PushNotificationManager::RegisterActivator(nullptr);
}
catch (...)
{
Expand All @@ -147,6 +163,7 @@ bool RegisterActivatorNullDetails()
return false;
}

// Verify calling register activator with null clsid is not allowed.
bool RegisterActivatorNullClsid()
{
winrt::hresult hr = S_OK;
Expand All @@ -155,7 +172,16 @@ bool RegisterActivatorNullClsid()
PushNotificationActivationInfo info(
PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator,
winrt::guid()); // Null guid
PushNotificationManager::RegisterActivator(info);

PushNotificationRegistrationToken fakeToken = nullptr;
auto scope_exit = wil::scope_exit([&] {
if (fakeToken)
{
PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
}
});

fakeToken = PushNotificationManager::RegisterActivator(info);
}
catch (...)
{
Expand All @@ -164,6 +190,7 @@ bool RegisterActivatorNullClsid()
return false;
}

// Verify unregistering activator with a null token is not allowed.
bool UnregisterActivatorNullToken()
{
winrt::hresult hr = S_OK;
Expand All @@ -178,6 +205,8 @@ bool UnregisterActivatorNullToken()
return false;
}

// Verify unregistering an activator with null background registration is not allowed
// if PushTrigger option is specified.
bool UnregisterActivatorNullBackgroundRegistration()
{
winrt::hresult hr = S_OK;
Expand All @@ -193,6 +222,7 @@ bool UnregisterActivatorNullBackgroundRegistration()
return false;
}

// Verify registering multiple activators is not allowed.
bool MultipleRegisterActivatorTest()
{
winrt::hresult hr = S_OK;
Expand All @@ -202,6 +232,14 @@ bool MultipleRegisterActivatorTest()
PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator,
c_fakeComServerId); // Fake clsid to test multiple activators

PushNotificationRegistrationToken fakeToken = nullptr;
auto scope_exit = wil::scope_exit([&] {
if (fakeToken)
{
PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
}
});

fakeToken = PushNotificationManager::RegisterActivator(info);
}
catch (...)
Expand All @@ -211,7 +249,7 @@ bool MultipleRegisterActivatorTest()
return false;
}

bool BackgroundActivationTest() // Activating application for background.
bool BackgroundActivationTest() // Activating application for background test.
{
return true;
}
Expand Down Expand Up @@ -247,33 +285,21 @@ bool runUnitTest(std::string unitTest)
return it->second();
}

// Cleanup function for exiting test app.
void UnregisterClsid()
{
if (appToken)
{
PushNotificationManager::UnregisterActivator(appToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
}

if (fakeToken)
{
PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
}

}

int main() try
{
bool testResult = false;
auto scope_exit = wil::scope_exit([&] {
UnregisterClsid();
if (g_appToken)
{
PushNotificationManager::UnregisterActivator(g_appToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator);
}
});

PushNotificationActivationInfo info(
PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator,
winrt::guid(c_comServerId)); // same clsid as app manifest

appToken = PushNotificationManager::RegisterActivator(info);
g_appToken = PushNotificationManager::RegisterActivator(info);

auto args = AppInstance::GetCurrent().GetActivatedEventArgs();
auto kind = args.Kind();
Expand Down

0 comments on commit bd4bc2b

Please sign in to comment.