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
|
// Copyright 2020 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/muted_notification_handler.h"
#include <optional>
#include <string>
#include "base/functional/callback.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/browser_features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
class MockMutedNotificationHandlerDelegate
: public MutedNotificationHandler::Delegate {
public:
MockMutedNotificationHandlerDelegate() = default;
MockMutedNotificationHandlerDelegate(
const MockMutedNotificationHandlerDelegate&) = delete;
MockMutedNotificationHandlerDelegate& operator=(
const MockMutedNotificationHandlerDelegate&) = delete;
~MockMutedNotificationHandlerDelegate() override = default;
// MutedNotificationHandler::Delegate:
MOCK_METHOD(void, OnAction, (MutedNotificationHandler::Action), (override));
};
class MutedNotificationHandlerTest : public testing::TestWithParam<bool> {
public:
MutedNotificationHandlerTest() {
if (GetParam()) {
feature_list_.InitAndEnableFeature(
features::kMuteNotificationSnoozeAction);
} else {
feature_list_.InitAndDisableFeature(
features::kMuteNotificationSnoozeAction);
}
}
~MutedNotificationHandlerTest() override = default;
MockMutedNotificationHandlerDelegate& delegate() { return delegate_; }
MutedNotificationHandler& handler() { return handler_; }
private:
base::test::ScopedFeatureList feature_list_;
MockMutedNotificationHandlerDelegate delegate_;
MutedNotificationHandler handler_{&delegate_};
};
TEST_P(MutedNotificationHandlerTest, OnUserClose) {
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run());
EXPECT_CALL(delegate(),
OnAction(MutedNotificationHandler::Action::kUserClose));
handler().OnClose(/*profile=*/nullptr, GURL(),
/*notification_id=*/std::string(), /*by_user=*/true,
callback.Get());
}
TEST_P(MutedNotificationHandlerTest, OnNonUserClose) {
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run());
EXPECT_CALL(delegate(), OnAction).Times(0);
handler().OnClose(/*profile=*/nullptr, GURL(),
/*notification_id=*/std::string(), /*by_user=*/false,
callback.Get());
}
TEST_P(MutedNotificationHandlerTest, OnClickBody) {
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run());
EXPECT_CALL(delegate(),
OnAction(MutedNotificationHandler::Action::kBodyClick));
handler().OnClick(
/*profile=*/nullptr, GURL(), /*notification_id=*/std::string(),
/*action_index=*/std::nullopt, /*reply=*/std::nullopt, callback.Get());
}
TEST_P(MutedNotificationHandlerTest, OnClickSnooze) {
if (!GetParam())
return;
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run());
EXPECT_CALL(delegate(),
OnAction(MutedNotificationHandler::Action::kSnoozeClick));
handler().OnClick(
/*profile=*/nullptr, GURL(), /*notification_id=*/std::string(),
/*action_index=*/0, /*reply=*/std::nullopt, callback.Get());
}
TEST_P(MutedNotificationHandlerTest, OnClickShow) {
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run());
EXPECT_CALL(delegate(),
OnAction(MutedNotificationHandler::Action::kShowClick));
handler().OnClick(
/*profile=*/nullptr, GURL(), /*notification_id=*/std::string(),
/*action_index=*/GetParam() ? 1 : 0, /*reply=*/std::nullopt,
callback.Get());
}
INSTANTIATE_TEST_SUITE_P(, MutedNotificationHandlerTest, testing::Bool());
|