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
|
// Copyright 2019 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/ash/release_notes/release_notes_notification.h"
#include "ash/constants/ash_features.h"
#include "base/feature_list.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/version.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/prefs/testing_pref_service.h"
#include "components/version_info/version_info.h"
#include "ui/chromeos/devicetype_utils.h"
namespace ash {
class ReleaseNotesNotificationTest
: public BrowserWithTestWindowTest,
public ::testing::WithParamInterface</*notification_eligible=*/bool> {
public:
ReleaseNotesNotificationTest() : notification_eligible_(GetParam()) {}
ReleaseNotesNotificationTest(const ReleaseNotesNotificationTest&) = delete;
ReleaseNotesNotificationTest& operator=(const ReleaseNotesNotificationTest&) =
delete;
~ReleaseNotesNotificationTest() override = default;
// BrowserWithTestWindowTest:
void SetUp() override {
BrowserWithTestWindowTest::SetUp();
TestingBrowserProcess::GetGlobal()->SetSystemNotificationHelper(
std::make_unique<SystemNotificationHelper>());
tester_ = std::make_unique<NotificationDisplayServiceTester>(nullptr);
tester_->SetNotificationAddedClosure(
base::BindRepeating(&ReleaseNotesNotificationTest::OnNotificationAdded,
base::Unretained(this)));
release_notes_notification_ =
std::make_unique<ReleaseNotesNotification>(profile());
if (notification_eligible()) {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kReleaseNotesNotificationAllChannels,
features::
kReleaseNotesNotificationAlwaysEligible},
/*disabled_features=*/{});
} else {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kReleaseNotesNotificationAllChannels},
/*disabled_features=*/{
features::kReleaseNotesNotificationAlwaysEligible});
}
}
void TearDown() override {
release_notes_notification_.reset();
tester_.reset();
BrowserWithTestWindowTest::TearDown();
}
std::optional<std::string> GetDefaultProfileName() override {
// TODO(crbug.com/40286020): Use google.com domain to forcibly enable
// release note notification. Will merge into BrowserWithTestWindowTest.
return "primary_profile@google.com";
}
void OnNotificationAdded() { notification_count_++; }
bool notification_eligible() const { return notification_eligible_; }
protected:
bool HasReleaseNotesNotification() {
return tester_->GetNotification("show_release_notes_notification")
.has_value();
}
message_center::Notification GetReleaseNotesNotification() {
return tester_->GetNotification("show_release_notes_notification").value();
}
int notification_count_ = 0;
std::unique_ptr<ReleaseNotesNotification> release_notes_notification_;
private:
std::unique_ptr<NotificationDisplayServiceTester> tester_;
base::test::ScopedFeatureList scoped_feature_list_;
const bool notification_eligible_;
};
INSTANTIATE_TEST_SUITE_P(All,
ReleaseNotesNotificationTest,
/*notification_eligible=*/testing::Bool());
TEST_P(ReleaseNotesNotificationTest, DoNotShowReleaseNotesNotification) {
if (notification_eligible()) {
GTEST_SKIP() << "Notification is always shown with the notification "
"eligible feature turned on.";
}
auto release_notes_storage = std::make_unique<ReleaseNotesStorage>(profile());
// Set the pref to the last shown milestone to ensure release notes do not
// show.
profile()->GetPrefs()->SetInteger(
prefs::kHelpAppNotificationLastShownMilestone,
version_info::GetVersion().components()[0]);
release_notes_notification_->MaybeShowReleaseNotes();
EXPECT_FALSE(HasReleaseNotesNotification());
EXPECT_EQ(0, notification_count_);
}
TEST_P(ReleaseNotesNotificationTest, ShowReleaseNotesNotification) {
auto release_notes_storage = std::make_unique<ReleaseNotesStorage>(profile());
// Set the pref to an older milestone to ensure release notes do show.
profile()->GetPrefs()->SetInteger(
prefs::kHelpAppNotificationLastShownMilestone, 20);
release_notes_notification_->MaybeShowReleaseNotes();
// If notification eligible is enabled, then release notes should show.
EXPECT_EQ(notification_eligible(), HasReleaseNotesNotification());
EXPECT_EQ(notification_eligible() ? 1 : 0, notification_count_);
EXPECT_EQ(notification_eligible(),
release_notes_storage->ShouldShowSuggestionChip());
if (HasReleaseNotesNotification()) {
EXPECT_TRUE(HasReleaseNotesNotification());
EXPECT_EQ(ui::SubstituteChromeOSDeviceType(
IDS_RELEASE_NOTES_DEVICE_SPECIFIC_NOTIFICATION_TITLE),
GetReleaseNotesNotification().title());
EXPECT_EQ("Get highlights from the latest update",
base::UTF16ToASCII(GetReleaseNotesNotification().message()));
// And it show the release notes suggestion chip.
EXPECT_EQ(3, profile()->GetPrefs()->GetInteger(
prefs::kReleaseNotesSuggestionChipTimesLeftToShow));
}
}
} // namespace ash
|