Commit a64f3656 authored by Peter Beverloo's avatar Peter Beverloo Committed by Commit Bot

Move a bunch of tests to a new PersistentNotificationHandlerTest suite

The associated code moved to PersistentNotificationHandler, so let's
make sure that the tests follow. More functionality will be built on top
so this'll form a nice base.

Change-Id: Ib40bfdfbc3efb4d63506e8e8791370a70fa8e3f9
Reviewed-on: https://chromium-review.googlesource.com/1181904Reviewed-by: default avatarTim Volodine <timvolodine@chromium.org>
Commit-Queue: Peter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584714}
parent ad3996ca
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/persistent_notification_handler.h"
#include <memory>
#include "base/macros.h"
#include "chrome/browser/notifications/metrics/mock_notification_metrics_logger.h"
#include "chrome/browser/notifications/metrics/notification_metrics_logger_factory.h"
#include "chrome/browser/notifications/notification_permission_context.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/permission_type.h"
#include "content/public/test/mock_permission_manager.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/modules/permissions/permission_status.mojom.h"
using ::testing::_;
using ::testing::Return;
namespace {
const char kExampleNotificationId[] = "example_notification_id";
const char kExampleOrigin[] = "https://example.com";
class TestingProfileWithPermissionManager : public TestingProfile {
public:
TestingProfileWithPermissionManager()
: permission_manager_(
std::make_unique<
testing::NiceMock<content::MockPermissionManager>>()) {}
~TestingProfileWithPermissionManager() override = default;
// Sets the notification permission status to |permission_status|.
void SetNotificationPermissionStatus(
blink::mojom::PermissionStatus permission_status) {
ON_CALL(*permission_manager_,
GetPermissionStatus(content::PermissionType::NOTIFICATIONS, _, _))
.WillByDefault(Return(permission_status));
}
// TestingProfile overrides:
content::PermissionControllerDelegate* GetPermissionControllerDelegate()
override {
return permission_manager_.get();
}
private:
std::unique_ptr<content::MockPermissionManager> permission_manager_;
DISALLOW_COPY_AND_ASSIGN(TestingProfileWithPermissionManager);
};
class PersistentNotificationHandlerTest : public ::testing::Test {
public:
PersistentNotificationHandlerTest() : origin_(kExampleOrigin) {}
~PersistentNotificationHandlerTest() override = default;
// ::testing::Test overrides:
void SetUp() override {
mock_logger_ = static_cast<MockNotificationMetricsLogger*>(
NotificationMetricsLoggerFactory::GetInstance()
->SetTestingFactoryAndUse(
&profile_, &MockNotificationMetricsLogger::FactoryForTests));
}
protected:
content::TestBrowserThreadBundle thread_bundle_;
TestingProfileWithPermissionManager profile_;
// The origin for which these tests are being run.
GURL origin_;
// Owned by the |profile_| as a keyed service.
MockNotificationMetricsLogger* mock_logger_ = nullptr;
private:
DISALLOW_COPY_AND_ASSIGN(PersistentNotificationHandlerTest);
};
TEST_F(PersistentNotificationHandlerTest, OnClick_WithoutPermission) {
EXPECT_CALL(*mock_logger_, LogPersistentNotificationClickWithoutPermission());
profile_.SetNotificationPermissionStatus(
blink::mojom::PermissionStatus::DENIED);
std::unique_ptr<NotificationHandler> handler =
std::make_unique<PersistentNotificationHandler>();
handler->OnClick(&profile_, origin_, kExampleNotificationId,
base::nullopt /* action_index */, base::nullopt /* reply */,
base::DoNothing());
}
TEST_F(PersistentNotificationHandlerTest, OnClose_ByUser) {
EXPECT_CALL(*mock_logger_, LogPersistentNotificationClosedByUser());
std::unique_ptr<NotificationHandler> handler =
std::make_unique<PersistentNotificationHandler>();
handler->OnClose(&profile_, origin_, kExampleNotificationId,
/* by_user= */ true, base::DoNothing());
}
TEST_F(PersistentNotificationHandlerTest, OnClose_Programmatically) {
EXPECT_CALL(*mock_logger_, LogPersistentNotificationClosedProgrammatically());
std::unique_ptr<NotificationHandler> handler =
std::make_unique<PersistentNotificationHandler>();
handler->OnClose(&profile_, origin_, kExampleNotificationId,
/* by_user= */ false, base::DoNothing());
}
TEST_F(PersistentNotificationHandlerTest, DisableNotifications) {
std::unique_ptr<NotificationPermissionContext> permission_context =
std::make_unique<NotificationPermissionContext>(&profile_);
ASSERT_EQ(permission_context
->GetPermissionStatus(nullptr /* render_frame_host */, origin_,
origin_)
.content_setting,
CONTENT_SETTING_ASK);
std::unique_ptr<NotificationHandler> handler =
std::make_unique<PersistentNotificationHandler>();
handler->DisableNotifications(&profile_, origin_);
ASSERT_EQ(permission_context
->GetPermissionStatus(nullptr /* render_frame_host */, origin_,
origin_)
.content_setting,
CONTENT_SETTING_BLOCK);
}
} // namespace
...@@ -21,22 +21,18 @@ ...@@ -21,22 +21,18 @@
#include "chrome/browser/notifications/notification_display_service_impl.h" #include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/notification_display_service_tester.h" #include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/platform_notification_service_impl.h" #include "chrome/browser/notifications/platform_notification_service_impl.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/history/core/browser/history_database_params.h" #include "components/history/core/browser/history_database_params.h"
#include "components/history/core/browser/history_service.h" #include "components/history/core/browser/history_service.h"
#include "components/history/core/test/test_history_database.h" #include "components/history/core/test/test_history_database.h"
#include "components/ukm/test_ukm_recorder.h" #include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/permission_type.h"
#include "content/public/common/notification_resources.h" #include "content/public/common/notification_resources.h"
#include "content/public/common/platform_notification_data.h" #include "content/public/common/platform_notification_data.h"
#include "content/public/test/mock_permission_manager.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/buildflags/buildflags.h" #include "extensions/buildflags/buildflags.h"
#include "services/metrics/public/cpp/ukm_builders.h" #include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/modules/permissions/permission_status.mojom.h"
#if BUILDFLAG(ENABLE_EXTENSIONS) #if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
...@@ -47,8 +43,6 @@ ...@@ -47,8 +43,6 @@
#include "extensions/common/value_builder.h" #include "extensions/common/value_builder.h"
#endif // BUILDFLAG(ENABLE_EXTENSIONS) #endif // BUILDFLAG(ENABLE_EXTENSIONS)
using ::testing::_;
using ::testing::Return;
using content::NotificationResources; using content::NotificationResources;
using content::PlatformNotificationData; using content::PlatformNotificationData;
using content::NotificationDatabaseData; using content::NotificationDatabaseData;
...@@ -74,35 +68,6 @@ const char kTimeUntilCloseMillis[] = "TimeUntilClose"; ...@@ -74,35 +68,6 @@ const char kTimeUntilCloseMillis[] = "TimeUntilClose";
const char kTimeUntilFirstClickMillis[] = "TimeUntilFirstClick"; const char kTimeUntilFirstClickMillis[] = "TimeUntilFirstClick";
const char kTimeUntilLastClickMillis[] = "TimeUntilLastClick"; const char kTimeUntilLastClickMillis[] = "TimeUntilLastClick";
class TestingProfileWithPermissionManager : public TestingProfile {
public:
TestingProfileWithPermissionManager()
: permission_manager_(
std::make_unique<
testing::NiceMock<content::MockPermissionManager>>()) {}
~TestingProfileWithPermissionManager() override = default;
// Sets the notification permission status to |permission_status|.
void SetNotificationPermissionStatus(
blink::mojom::PermissionStatus permission_status) {
ON_CALL(*permission_manager_,
GetPermissionStatus(content::PermissionType::NOTIFICATIONS, _, _))
.WillByDefault(Return(permission_status));
}
// TestingProfile overrides:
content::PermissionControllerDelegate* GetPermissionControllerDelegate()
override {
return permission_manager_.get();
}
private:
std::unique_ptr<content::MockPermissionManager> permission_manager_;
DISALLOW_COPY_AND_ASSIGN(TestingProfileWithPermissionManager);
};
} // namespace } // namespace
class PlatformNotificationServiceTest : public testing::Test { class PlatformNotificationServiceTest : public testing::Test {
...@@ -144,8 +109,7 @@ class PlatformNotificationServiceTest : public testing::Test { ...@@ -144,8 +109,7 @@ class PlatformNotificationServiceTest : public testing::Test {
protected: protected:
content::TestBrowserThreadBundle thread_bundle_; content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
TestingProfileWithPermissionManager profile_;
std::unique_ptr<NotificationDisplayServiceTester> display_service_tester_; std::unique_ptr<NotificationDisplayServiceTester> display_service_tester_;
...@@ -199,43 +163,6 @@ TEST_F(PlatformNotificationServiceTest, DisplayPersistentThenClose) { ...@@ -199,43 +163,6 @@ TEST_F(PlatformNotificationServiceTest, DisplayPersistentThenClose) {
NotificationHandler::Type::WEB_PERSISTENT)); NotificationHandler::Type::WEB_PERSISTENT));
} }
TEST_F(PlatformNotificationServiceTest, OnPersistentNotificationClick) {
EXPECT_CALL(*mock_logger_, LogPersistentNotificationClickWithoutPermission());
profile_.SetNotificationPermissionStatus(
blink::mojom::PermissionStatus::DENIED);
NotificationHandler* handler =
NotificationDisplayServiceImpl::GetForProfile(&profile_)
->GetNotificationHandler(NotificationHandler::Type::WEB_PERSISTENT);
handler->OnClick(&profile_, GURL("https://example.com/"),
"fake_notification_id", base::nullopt /* action_index */,
base::nullopt /* reply */, base::DoNothing());
}
TEST_F(PlatformNotificationServiceTest, OnPersistentNotificationClosedByUser) {
EXPECT_CALL(*mock_logger_, LogPersistentNotificationClosedByUser());
NotificationHandler* handler =
NotificationDisplayServiceImpl::GetForProfile(&profile_)
->GetNotificationHandler(NotificationHandler::Type::WEB_PERSISTENT);
handler->OnClose(&profile_, GURL("https://example.com/"),
"fake_notification_id", true /* by_user */,
base::DoNothing());
}
TEST_F(PlatformNotificationServiceTest,
OnPersistentNotificationClosedProgrammatically) {
EXPECT_CALL(*mock_logger_, LogPersistentNotificationClosedProgrammatically());
NotificationHandler* handler =
NotificationDisplayServiceImpl::GetForProfile(&profile_)
->GetNotificationHandler(NotificationHandler::Type::WEB_PERSISTENT);
handler->OnClose(&profile_, GURL("https://example.com/"),
"fake_notification_id", false /* by_user */,
base::DoNothing());
}
TEST_F(PlatformNotificationServiceTest, DisplayNonPersistentPropertiesMatch) { TEST_F(PlatformNotificationServiceTest, DisplayNonPersistentPropertiesMatch) {
std::vector<int> vibration_pattern( std::vector<int> vibration_pattern(
kNotificationVibrationPattern, kNotificationVibrationPattern,
......
...@@ -2526,6 +2526,7 @@ test("unit_tests") { ...@@ -2526,6 +2526,7 @@ test("unit_tests") {
"../browser/notifications/notification_permission_context_unittest.cc", "../browser/notifications/notification_permission_context_unittest.cc",
"../browser/notifications/notification_platform_bridge_chromeos_unittest.cc", "../browser/notifications/notification_platform_bridge_chromeos_unittest.cc",
"../browser/notifications/notification_platform_bridge_mac_unittest.mm", "../browser/notifications/notification_platform_bridge_mac_unittest.mm",
"../browser/notifications/persistent_notification_handler_unittest.cc",
"../browser/notifications/platform_notification_service_unittest.cc", "../browser/notifications/platform_notification_service_unittest.cc",
"../browser/notifications/stub_alert_dispatcher_mac.h", "../browser/notifications/stub_alert_dispatcher_mac.h",
"../browser/notifications/stub_alert_dispatcher_mac.mm", "../browser/notifications/stub_alert_dispatcher_mac.mm",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment