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
|
// Copyright 2016 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/notifications/low_disk_notification.h"
#include <stdint.h>
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/system_notification_helper.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_manager.h"
#include "chromeos/ash/components/dbus/dbus_thread_manager.h"
#include "chromeos/ash/components/dbus/userdataauth/fake_userdataauth_client.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "components/user_manager/fake_user_manager.h"
#include "google_apis/gaia/gaia_id.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/message_center/public/cpp/notification.h"
namespace {
// Copied from low_disk_notification.cc
const uint64_t kMediumNotification = (1 << 30) - 1;
const uint64_t kHighNotification = (512 << 20) - 1;
} // namespace
namespace ash {
class LowDiskNotificationTest : public BrowserWithTestWindowTest {
public:
LowDiskNotificationTest() = default;
~LowDiskNotificationTest() override = default;
void SetUp() override {
BrowserWithTestWindowTest::SetUp();
UserDataAuthClient::InitializeFake();
GetCrosSettingsHelper()->ReplaceDeviceSettingsProviderWithStub();
GetCrosSettingsHelper()->SetBoolean(kDeviceShowLowDiskSpaceNotification,
true);
TestingBrowserProcess::GetGlobal()->SetSystemNotificationHelper(
std::make_unique<SystemNotificationHelper>());
tester_ = std::make_unique<NotificationDisplayServiceTester>(
nullptr /* profile */);
tester_->SetNotificationAddedClosure(base::BindRepeating(
&LowDiskNotificationTest::OnNotificationAdded, base::Unretained(this)));
low_disk_notification_ = std::make_unique<LowDiskNotification>();
notification_count_ = 0;
medium_message_.set_disk_free_bytes(kMediumNotification);
high_message_.set_disk_free_bytes(kHighNotification);
}
void TearDown() override {
low_disk_notification_.reset();
UserDataAuthClient::Shutdown();
BrowserWithTestWindowTest::TearDown();
}
std::optional<message_center::Notification> GetNotification() {
return tester_->GetNotification("low_disk");
}
void SetNotificationThrottlingInterval(int ms) {
low_disk_notification_->SetNotificationIntervalForTest(
base::Milliseconds(ms));
}
void OnNotificationAdded() { notification_count_++; }
protected:
std::unique_ptr<NotificationDisplayServiceTester> tester_;
std::unique_ptr<LowDiskNotification> low_disk_notification_;
int notification_count_;
// A LowDiskSpace protobuf message that contains `kMediumNotification`.
::user_data_auth::LowDiskSpace medium_message_;
// A LowDiskSpace protobuf message that contains `kHighNotification`.
::user_data_auth::LowDiskSpace high_message_;
};
TEST_F(LowDiskNotificationTest, MediumLevelNotification) {
std::u16string expected_title =
l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_TITLE);
low_disk_notification_->LowDiskSpace(medium_message_);
auto notification = GetNotification();
ASSERT_TRUE(notification);
EXPECT_EQ(expected_title, notification->title());
EXPECT_EQ(1, notification_count_);
}
TEST_F(LowDiskNotificationTest, HighLevelReplacesMedium) {
std::u16string expected_title =
l10n_util::GetStringUTF16(IDS_CRITICALLY_LOW_DISK_NOTIFICATION_TITLE);
low_disk_notification_->LowDiskSpace(medium_message_);
low_disk_notification_->LowDiskSpace(high_message_);
auto notification = GetNotification();
ASSERT_TRUE(notification);
EXPECT_EQ(expected_title, notification->title());
EXPECT_EQ(2, notification_count_);
}
TEST_F(LowDiskNotificationTest, NotificationsAreThrottled) {
SetNotificationThrottlingInterval(10000000);
low_disk_notification_->LowDiskSpace(high_message_);
low_disk_notification_->LowDiskSpace(high_message_);
EXPECT_EQ(1, notification_count_);
}
TEST_F(LowDiskNotificationTest, HighNotificationsAreShownAfterThrottling) {
SetNotificationThrottlingInterval(-1);
low_disk_notification_->LowDiskSpace(high_message_);
low_disk_notification_->LowDiskSpace(high_message_);
EXPECT_EQ(2, notification_count_);
}
TEST_F(LowDiskNotificationTest, MediumNotificationsAreNotShownAfterThrottling) {
SetNotificationThrottlingInterval(-1);
low_disk_notification_->LowDiskSpace(medium_message_);
low_disk_notification_->LowDiskSpace(medium_message_);
EXPECT_EQ(1, notification_count_);
}
TEST_F(LowDiskNotificationTest, ShowForMultipleUsersWhenEnrolled) {
user_manager()->AddGaiaUser(
AccountId::FromUserEmailGaiaId("test_user1@example.com",
GaiaId("1234567891")),
user_manager::UserType::kRegular);
user_manager()->AddGaiaUser(
AccountId::FromUserEmailGaiaId("test_user2@example.com",
GaiaId("1234567892")),
user_manager::UserType::kRegular);
SetNotificationThrottlingInterval(-1);
low_disk_notification_->LowDiskSpace(high_message_);
EXPECT_EQ(1, notification_count_);
}
TEST_F(LowDiskNotificationTest, SupressedForMultipleUsersWhenEnrolled) {
user_manager()->AddGaiaUser(
AccountId::FromUserEmailGaiaId("test_user1@example.com",
GaiaId("1234567891")),
user_manager::UserType::kRegular);
user_manager()->AddGaiaUser(
AccountId::FromUserEmailGaiaId("test_user2@example.com",
GaiaId("1234567892")),
user_manager::UserType::kRegular);
GetCrosSettingsHelper()->SetBoolean(kDeviceShowLowDiskSpaceNotification,
false);
SetNotificationThrottlingInterval(-1);
low_disk_notification_->LowDiskSpace(high_message_);
EXPECT_EQ(0, notification_count_);
}
TEST_F(LowDiskNotificationTest, DemoModeSkipNotification) {
GetCrosSettingsHelper()->InstallAttributes()->SetDemoMode();
SetNotificationThrottlingInterval(-1);
low_disk_notification_->LowDiskSpace(high_message_);
EXPECT_EQ(0, notification_count_);
}
} // namespace ash
|