Commit 6b611e74 authored by Nancy Wang's avatar Nancy Wang Committed by Chromium LUCI CQ

Fix the FullRestoreServiceTest unit tests.

The parameter id of the function HandleRestoreNotificationClicked is
user-after-free on builder "Linux ChromiumOS MSan Tests", because the
notification is closed at the beginning of
HandleRestoreNotificationClicked, which may cause `id` becomes
uninitialized.

Move notification_ as the class variable to hold the notitification id
in the function HandleRestoreNotificationClicked to resolve the unit
tests issue. And enable the disabled unit tests.

BUG=1146900
BUG=1164559

Change-Id: Iabf8ad0233944cce071b3c2be1a10ca746ed2a6b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2619409
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842107}
parent cfe86edf
...@@ -113,38 +113,37 @@ void FullRestoreService::ShowRestoreNotification(const std::string& id) { ...@@ -113,38 +113,37 @@ void FullRestoreService::ShowRestoreNotification(const std::string& id) {
else else
message_id = IDS_SET_RESTORE_NOTIFICATION_MESSAGE; message_id = IDS_SET_RESTORE_NOTIFICATION_MESSAGE;
std::unique_ptr<message_center::Notification> notification = notification_ = ash::CreateSystemNotification(
ash::CreateSystemNotification( message_center::NOTIFICATION_TYPE_SIMPLE, id,
message_center::NOTIFICATION_TYPE_SIMPLE, id, l10n_util::GetStringUTF16(title_id),
l10n_util::GetStringUTF16(title_id), l10n_util::GetStringUTF16(message_id),
l10n_util::GetStringUTF16(message_id), l10n_util::GetStringUTF16(IDS_RESTORE_NOTIFICATION_DISPLAY_SOURCE),
l10n_util::GetStringUTF16(IDS_RESTORE_NOTIFICATION_DISPLAY_SOURCE), GURL(),
GURL(), message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
message_center::NotifierId( id),
message_center::NotifierType::SYSTEM_COMPONENT, id), notification_data,
notification_data, base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>( base::BindRepeating(
base::BindRepeating( &FullRestoreService::HandleRestoreNotificationClicked,
&FullRestoreService::HandleRestoreNotificationClicked, weak_ptr_factory_.GetWeakPtr())),
weak_ptr_factory_.GetWeakPtr(), id)), kFullRestoreNotificationIcon,
kFullRestoreNotificationIcon, message_center::SystemNotificationWarningLevel::NORMAL);
message_center::SystemNotificationWarningLevel::NORMAL); notification_->set_priority(message_center::SYSTEM_PRIORITY);
notification->set_priority(message_center::SYSTEM_PRIORITY);
auto* notification_display_service = auto* notification_display_service =
NotificationDisplayService::GetForProfile(profile_); NotificationDisplayService::GetForProfile(profile_);
DCHECK(notification_display_service); DCHECK(notification_display_service);
notification_display_service->Display(NotificationHandler::Type::TRANSIENT, notification_display_service->Display(NotificationHandler::Type::TRANSIENT,
*notification, *notification_,
/*metadata=*/nullptr); /*metadata=*/nullptr);
} }
void FullRestoreService::HandleRestoreNotificationClicked( void FullRestoreService::HandleRestoreNotificationClicked(
const std::string& id,
base::Optional<int> button_index) { base::Optional<int> button_index) {
DCHECK(notification_);
if (!is_shut_down_) { if (!is_shut_down_) {
NotificationDisplayService::GetForProfile(profile_)->Close( NotificationDisplayService::GetForProfile(profile_)->Close(
NotificationHandler::Type::TRANSIENT, id); NotificationHandler::Type::TRANSIENT, notification_->id());
} }
if (!button_index.has_value() || if (!button_index.has_value() ||
...@@ -153,7 +152,7 @@ void FullRestoreService::HandleRestoreNotificationClicked( ...@@ -153,7 +152,7 @@ void FullRestoreService::HandleRestoreNotificationClicked(
return; return;
} }
if (id == kSetRestorePrefNotificationId) { if (notification_->id() == kSetRestorePrefNotificationId) {
// Show the 'On Startup' OS setting page. // Show the 'On Startup' OS setting page.
chrome::SettingsWindowManager::GetInstance()->ShowOSSettings( chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
profile_, chromeos::settings::mojom::kOnStartupSectionPath); profile_, chromeos::settings::mojom::kOnStartupSectionPath);
......
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
class Profile; class Profile;
namespace message_center {
class Notification;
}
namespace chromeos { namespace chromeos {
namespace full_restore { namespace full_restore {
...@@ -56,8 +60,7 @@ class FullRestoreService : public KeyedService { ...@@ -56,8 +60,7 @@ class FullRestoreService : public KeyedService {
// Show the restore notification on startup. // Show the restore notification on startup.
void ShowRestoreNotification(const std::string& id); void ShowRestoreNotification(const std::string& id);
void HandleRestoreNotificationClicked(const std::string& id, void HandleRestoreNotificationClicked(base::Optional<int> button_index);
base::Optional<int> button_index);
// Implement the restoration. // Implement the restoration.
void Restore(); void Restore();
...@@ -74,6 +77,8 @@ class FullRestoreService : public KeyedService { ...@@ -74,6 +77,8 @@ class FullRestoreService : public KeyedService {
std::unique_ptr<FullRestoreDataHandler> restore_data_handler_; std::unique_ptr<FullRestoreDataHandler> restore_data_handler_;
std::unique_ptr<message_center::Notification> notification_;
base::WeakPtrFactory<FullRestoreService> weak_ptr_factory_{this}; base::WeakPtrFactory<FullRestoreService> weak_ptr_factory_{this};
}; };
......
...@@ -189,8 +189,7 @@ class FullRestoreServiceTest : public testing::Test { ...@@ -189,8 +189,7 @@ class FullRestoreServiceTest : public testing::Test {
// If the system is crash, show the crash notification, and verify the restore // If the system is crash, show the crash notification, and verify the restore
// flag when click the restore button. // flag when click the restore button.
// TODO(crbug.com/1046900): Fix this unit test. TEST_F(FullRestoreServiceTest, CrashAndRestore) {
TEST_F(FullRestoreServiceTest, DISABLED_CrashAndRestore) {
profile()->set_last_session_exited_cleanly(false); profile()->set_last_session_exited_cleanly(false);
CreateFullRestoreServiceForTesting(); CreateFullRestoreServiceForTesting();
...@@ -400,8 +399,7 @@ TEST_F(FullRestoreServiceTest, Upgrading) { ...@@ -400,8 +399,7 @@ TEST_F(FullRestoreServiceTest, Upgrading) {
// If the OS restore setting is 'Ask every time', after reboot, show the restore // If the OS restore setting is 'Ask every time', after reboot, show the restore
// notification, and verify the restore flag when click the restore button. // notification, and verify the restore flag when click the restore button.
// TODO(crbug.com/1046900): Fix this unit test. TEST_F(FullRestoreServiceTest, AskEveryTimeAndRestore) {
TEST_F(FullRestoreServiceTest, DISABLED_AskEveryTimeAndRestore) {
profile()->GetPrefs()->SetInteger( profile()->GetPrefs()->SetInteger(
kRestoreAppsAndPagesPrefName, kRestoreAppsAndPagesPrefName,
static_cast<int>(RestoreOption::kAskEveryTime)); static_cast<int>(RestoreOption::kAskEveryTime));
...@@ -476,8 +474,7 @@ TEST_F(FullRestoreServiceTest, NotRestore) { ...@@ -476,8 +474,7 @@ TEST_F(FullRestoreServiceTest, NotRestore) {
// If the restore option has been selected 3 times, show the set restore // If the restore option has been selected 3 times, show the set restore
// notification. // notification.
// TODO(crbug.com/1046900): Fix this unit test. TEST_F(FullRestoreServiceTest, SetRestorePrefNotification) {
TEST_F(FullRestoreServiceTest, DISABLED_SetRestorePrefNotification) {
profile()->GetPrefs()->SetInteger( profile()->GetPrefs()->SetInteger(
kRestoreAppsAndPagesPrefName, kRestoreAppsAndPagesPrefName,
static_cast<int>(RestoreOption::kAskEveryTime)); static_cast<int>(RestoreOption::kAskEveryTime));
...@@ -511,8 +508,7 @@ TEST_F(FullRestoreServiceTest, DISABLED_SetRestorePrefNotification) { ...@@ -511,8 +508,7 @@ TEST_F(FullRestoreServiceTest, DISABLED_SetRestorePrefNotification) {
// When |kRestoreSelectedCountPrefName| = 3, if the restore option is selected // When |kRestoreSelectedCountPrefName| = 3, if the restore option is selected
// again, |kRestoreSelectedCountPrefName| should not change. // again, |kRestoreSelectedCountPrefName| should not change.
// TODO(crbug.com/1046900): Fix this unit test. TEST_F(FullRestoreServiceTest, RestoreSelectedCount) {
TEST_F(FullRestoreServiceTest, DISABLED_RestoreSelectedCount) {
profile()->GetPrefs()->SetInteger( profile()->GetPrefs()->SetInteger(
kRestoreAppsAndPagesPrefName, kRestoreAppsAndPagesPrefName,
static_cast<int>(RestoreOption::kAskEveryTime)); static_cast<int>(RestoreOption::kAskEveryTime));
......
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