1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/notification_platform_bridge_win.h"
#include <windows.ui.notifications.h>
#include <wrl/client.h>
#include <wrl/implements.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/hash/hash.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_com_initializer.h"
#include "base/win/scoped_hstring.h"
#include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/win/fake_itoastnotification.h"
#include "chrome/browser/notifications/win/fake_notification_image_retainer.h"
#include "chrome/browser/notifications/win/notification_launch_id.h"
#include "chrome/browser/notifications/win/notification_template_builder.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notifier_id.h"
namespace mswr = Microsoft::WRL;
namespace winui = ABI::Windows::UI;
using message_center::Notification;
namespace {
constexpr char kLaunchId[] =
"0|0|Default|aumi|0|https://example.com/|notification_id";
constexpr char kOrigin[] = "https://www.google.com/";
constexpr char kNotificationId[] = "id";
constexpr char kProfileId[] = "Default";
constexpr wchar_t kAppUserModelId[] = L"aumi";
constexpr wchar_t kAppUserModelId2[] = L"aumi2";
constexpr char kAppUserModelIdUTF8[] = "aumi";
} // namespace
class NotificationPlatformBridgeWinTest : public testing::Test {
public:
NotificationPlatformBridgeWinTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
NotificationPlatformBridgeWinTest(const NotificationPlatformBridgeWinTest&) =
delete;
NotificationPlatformBridgeWinTest& operator=(
const NotificationPlatformBridgeWinTest&) = delete;
~NotificationPlatformBridgeWinTest() override = default;
protected:
mswr::ComPtr<winui::Notifications::IToastNotification2> GetToast(
NotificationPlatformBridgeWin* bridge,
const NotificationLaunchId& launch_id,
bool renotify,
const std::string& profile_id,
const std::wstring& app_user_model_id,
bool incognito) {
DCHECK(bridge);
GURL origin(kOrigin);
auto notification = std::make_unique<message_center::Notification>(
message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId, u"title",
u"message", ui::ImageModel(), u"display_source", origin,
message_center::NotifierId(origin),
message_center::RichNotificationData(), nullptr /* delegate */);
notification->set_renotify(renotify);
FakeNotificationImageRetainer image_retainer;
std::wstring xml_template =
BuildNotificationTemplate(&image_retainer, launch_id, *notification);
mswr::ComPtr<winui::Notifications::IToastNotification> toast =
bridge->GetToastNotificationForTesting(*notification, xml_template,
profile_id, app_user_model_id,
incognito);
if (!toast) {
LOG(ERROR) << "GetToastNotificationForTesting failed";
return nullptr;
}
mswr::ComPtr<winui::Notifications::IToastNotification2> toast2;
HRESULT hr = toast.As<winui::Notifications::IToastNotification2>(&toast2);
if (FAILED(hr)) {
LOG(ERROR) << "Converting to IToastNotification2 failed";
return nullptr;
}
return toast2;
}
content::BrowserTaskEnvironment task_environment_;
};
TEST_F(NotificationPlatformBridgeWinTest, GroupAndTag) {
base::win::ScopedCOMInitializer com_initializer;
NotificationPlatformBridgeWin bridge;
NotificationLaunchId launch_id(kLaunchId);
ASSERT_TRUE(launch_id.is_valid());
mswr::ComPtr<winui::Notifications::IToastNotification2> toast2 =
GetToast(&bridge, launch_id, /*renotify=*/false, kProfileId,
kAppUserModelId, /*incognito=*/false);
ASSERT_TRUE(toast2);
HSTRING hstring_group;
ASSERT_HRESULT_SUCCEEDED(toast2->get_Group(&hstring_group));
base::win::ScopedHString group(hstring_group);
// NOTE: If you find yourself needing to change this value, make sure that
// NotificationPlatformBridgeWinImpl::Close supports specifying the right
// group value for RemoveGroupedTagWithId.
ASSERT_EQ(L"Notifications", group.Get());
HSTRING hstring_tag;
ASSERT_HRESULT_SUCCEEDED(toast2->get_Tag(&hstring_tag));
base::win::ScopedHString tag(hstring_tag);
std::string tag_data = std::string(kNotificationId) + "|" + kProfileId + "|" +
kAppUserModelIdUTF8 + "|0";
ASSERT_EQ(base::NumberToWString(base::Hash(tag_data)), tag.Get());
// Let tasks on |notification_task_runner_| of |bridge| run before its dtor.
task_environment_.RunUntilIdle();
}
TEST_F(NotificationPlatformBridgeWinTest, GroupAndTagUniqueness) {
base::win::ScopedCOMInitializer com_initializer;
NotificationPlatformBridgeWin bridge;
NotificationLaunchId launch_id(kLaunchId);
ASSERT_TRUE(launch_id.is_valid());
mswr::ComPtr<winui::Notifications::IToastNotification2> toastA;
mswr::ComPtr<winui::Notifications::IToastNotification2> toastB;
HSTRING hstring_tagA;
HSTRING hstring_tagB;
// Different profiles, same incognito status -> Unique tags.
{
toastA = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile1",
kAppUserModelId, /*incognito=*/true);
toastB = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile2",
kAppUserModelId, /*incognito=*/true);
ASSERT_TRUE(toastA);
ASSERT_TRUE(toastB);
ASSERT_HRESULT_SUCCEEDED(toastA->get_Tag(&hstring_tagA));
base::win::ScopedHString tagA(hstring_tagA);
ASSERT_HRESULT_SUCCEEDED(toastB->get_Tag(&hstring_tagB));
base::win::ScopedHString tagB(hstring_tagB);
ASSERT_NE(tagA.Get(), tagB.Get());
}
// Same profile, different incognito status -> Unique tags.
{
toastA = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile1",
kAppUserModelId, /*incognito=*/true);
toastB = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile1",
kAppUserModelId, /*incognito=*/false);
ASSERT_TRUE(toastA);
ASSERT_TRUE(toastB);
ASSERT_HRESULT_SUCCEEDED(toastA->get_Tag(&hstring_tagA));
base::win::ScopedHString tagA(hstring_tagA);
ASSERT_HRESULT_SUCCEEDED(toastB->get_Tag(&hstring_tagB));
base::win::ScopedHString tagB(hstring_tagB);
ASSERT_NE(tagA.Get(), tagB.Get());
}
// Same profile, same incognito status -> Identical tags.
{
toastA = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile1",
kAppUserModelId, /*incognito=*/true);
toastB = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile1",
kAppUserModelId, /*incognito=*/true);
ASSERT_TRUE(toastA);
ASSERT_TRUE(toastB);
ASSERT_HRESULT_SUCCEEDED(toastA->get_Tag(&hstring_tagA));
base::win::ScopedHString tagA(hstring_tagA);
ASSERT_HRESULT_SUCCEEDED(toastB->get_Tag(&hstring_tagB));
base::win::ScopedHString tagB(hstring_tagB);
ASSERT_EQ(tagA.Get(), tagB.Get());
}
// Same profile, same incognito status, different app user model id
// -> Unique tags.
{
toastA = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile1",
kAppUserModelId, /*incognito=*/true);
toastB = GetToast(&bridge, launch_id, /*renotify=*/false, "Profile1",
kAppUserModelId2, /*incognito=*/false);
ASSERT_TRUE(toastA);
ASSERT_TRUE(toastB);
ASSERT_HRESULT_SUCCEEDED(toastA->get_Tag(&hstring_tagA));
base::win::ScopedHString tagA(hstring_tagA);
ASSERT_HRESULT_SUCCEEDED(toastB->get_Tag(&hstring_tagB));
base::win::ScopedHString tagB(hstring_tagB);
ASSERT_NE(tagA.Get(), tagB.Get());
}
// Let tasks on |notification_task_runner_| of |bridge| run before its dtor.
task_environment_.RunUntilIdle();
}
TEST_F(NotificationPlatformBridgeWinTest, Suppress) {
base::win::ScopedCOMInitializer com_initializer;
NotificationPlatformBridgeWin bridge;
std::vector<mswr::ComPtr<winui::Notifications::IToastNotification>>
notifications;
bridge.SetDisplayedNotificationsForTesting(¬ifications);
mswr::ComPtr<winui::Notifications::IToastNotification2> toast2;
boolean suppress;
NotificationLaunchId launch_id(kLaunchId);
ASSERT_TRUE(launch_id.is_valid());
// Make sure this works a toast is not suppressed when no notifications are
// registered.
toast2 = GetToast(&bridge, launch_id, /*renotify=*/false, kProfileId,
kAppUserModelId, /*incognito=*/false);
ASSERT_TRUE(toast2);
ASSERT_HRESULT_SUCCEEDED(toast2->get_SuppressPopup(&suppress));
ASSERT_FALSE(suppress);
toast2.Reset();
// Register a single notification with a specific tag.
std::string tag_data = std::string(kNotificationId) + "|" + kProfileId + "|" +
kAppUserModelIdUTF8 + "|0";
std::wstring tag = base::NumberToWString(base::Hash(tag_data));
// Microsoft::WRL::Make() requires FakeIToastNotification to derive from
// RuntimeClass.
notifications.push_back(Microsoft::WRL::Make<FakeIToastNotification>(
L"<toast launch=\"0|0|Default|aumi|0|https://foo.com/|id\"></toast>",
tag));
// Request this notification with renotify true (should not be suppressed).
toast2 = GetToast(&bridge, launch_id, /*renotify=*/true, kProfileId,
kAppUserModelId, /*incognito=*/false);
ASSERT_TRUE(toast2);
ASSERT_HRESULT_SUCCEEDED(toast2->get_SuppressPopup(&suppress));
ASSERT_FALSE(suppress);
toast2.Reset();
// Request this notification with renotify false (should be suppressed).
toast2 = GetToast(&bridge, launch_id, /*renotify=*/false, kProfileId,
kAppUserModelId, /*incognito=*/false);
ASSERT_TRUE(toast2);
ASSERT_HRESULT_SUCCEEDED(toast2->get_SuppressPopup(&suppress));
ASSERT_TRUE(suppress);
toast2.Reset();
// Let tasks on |notification_task_runner_| of |bridge| run before its dtor.
task_environment_.RunUntilIdle();
// Do this after we've finished running tasks to avoid touching
// synchronize_displayed_notifications_timer_. See crbug.com/1220122.
bridge.SetDisplayedNotificationsForTesting(nullptr);
}
|