Commit e9374ae2 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

[CrOS] Make LowDiskNotification use NotificationDisplayService instead

of direct access to MessageCenter.

Manual test: add `LowDiskSpace(10000);` to end of constructor.

Bug: 783018
Change-Id: I3c1f7dd591e4c84b9d41742e6c042e63c7003138
Reviewed-on: https://chromium-review.googlesource.com/775059
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517673}
parent c0da091a
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chrome/app/vector_icons/vector_icons.h" #include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/chrome_pages.h" #include "chrome/browser/ui/chrome_pages.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
...@@ -23,7 +24,6 @@ ...@@ -23,7 +24,6 @@
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/chromeos/resources/grit/ui_chromeos_resources.h" #include "ui/chromeos/resources/grit/ui_chromeos_resources.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification.h" #include "ui/message_center/notification.h"
#include "ui/message_center/notification_types.h" #include "ui/message_center/notification_types.h"
#include "ui/message_center/notifier_id.h" #include "ui/message_center/notifier_id.h"
...@@ -37,31 +37,12 @@ const uint64_t kNotificationSevereThreshold = 512 << 20; // 512MB ...@@ -37,31 +37,12 @@ const uint64_t kNotificationSevereThreshold = 512 << 20; // 512MB
constexpr base::TimeDelta kNotificationInterval = constexpr base::TimeDelta kNotificationInterval =
base::TimeDelta::FromMinutes(2); base::TimeDelta::FromMinutes(2);
class LowDiskNotificationDelegate
: public message_center::NotificationDelegate {
public:
LowDiskNotificationDelegate() {}
// message_center::NotificationDelegate
void ButtonClick(int button_index) override {
chrome::ShowSettingsSubPageForProfile(
ProfileManager::GetActiveUserProfile(), kStoragePage);
}
private:
~LowDiskNotificationDelegate() override {}
DISALLOW_COPY_AND_ASSIGN(LowDiskNotificationDelegate);
};
} // namespace } // namespace
namespace chromeos { namespace chromeos {
LowDiskNotification::LowDiskNotification() LowDiskNotification::LowDiskNotification()
: message_center_(g_browser_process->message_center()), : notification_interval_(kNotificationInterval), weak_ptr_factory_(this) {
notification_interval_(kNotificationInterval),
weak_ptr_factory_(this) {
DCHECK(DBusThreadManager::Get()->GetCryptohomeClient()); DCHECK(DBusThreadManager::Get()->GetCryptohomeClient());
DBusThreadManager::Get()->GetCryptohomeClient()->AddObserver(this); DBusThreadManager::Get()->GetCryptohomeClient()->AddObserver(this);
} }
...@@ -88,14 +69,16 @@ void LowDiskNotification::LowDiskSpace(uint64_t free_disk_bytes) { ...@@ -88,14 +69,16 @@ void LowDiskNotification::LowDiskSpace(uint64_t free_disk_bytes) {
if (severity != last_notification_severity_ || if (severity != last_notification_severity_ ||
(severity == HIGH && (severity == HIGH &&
now - last_notification_time_ > notification_interval_)) { now - last_notification_time_ > notification_interval_)) {
message_center_->AddNotification(CreateNotification(severity)); Profile* profile = ProfileManager::GetLastUsedProfile();
NotificationDisplayService::GetForProfile(profile)->Display(
NotificationCommon::TRANSIENT, *CreateNotification(severity, profile));
last_notification_time_ = now; last_notification_time_ = now;
last_notification_severity_ = severity; last_notification_severity_ = severity;
} }
} }
std::unique_ptr<message_center::Notification> std::unique_ptr<message_center::Notification>
LowDiskNotification::CreateNotification(Severity severity) { LowDiskNotification::CreateNotification(Severity severity, Profile* profile) {
base::string16 title; base::string16 title;
base::string16 message; base::string16 message;
gfx::Image icon; gfx::Image icon;
...@@ -128,12 +111,17 @@ LowDiskNotification::CreateNotification(Severity severity) { ...@@ -128,12 +111,17 @@ LowDiskNotification::CreateNotification(Severity severity) {
message_center::NotifierId::SYSTEM_COMPONENT, message_center::NotifierId::SYSTEM_COMPONENT,
ash::system_notifier::kNotifierDisk); ash::system_notifier::kNotifierDisk);
auto on_click = base::Bind(
[](Profile* profile, int button_index) {
chrome::ShowSettingsSubPageForProfile(profile, kStoragePage);
},
profile);
std::unique_ptr<message_center::Notification> notification = std::unique_ptr<message_center::Notification> notification =
ash::system_notifier::CreateSystemNotification( ash::system_notifier::CreateSystemNotification(
message_center::NOTIFICATION_TYPE_SIMPLE, kLowDiskId, title, message, message_center::NOTIFICATION_TYPE_SIMPLE, kLowDiskId, title, message,
icon, base::string16(), GURL(), notifier_id, optional_fields, icon, base::string16(), GURL(), notifier_id, optional_fields,
new LowDiskNotificationDelegate(), kNotificationStorageFullIcon, new message_center::HandleNotificationButtonClickDelegate(on_click),
warning_level); kNotificationStorageFullIcon, warning_level);
return notification; return notification;
} }
...@@ -147,11 +135,6 @@ LowDiskNotification::Severity LowDiskNotification::GetSeverity( ...@@ -147,11 +135,6 @@ LowDiskNotification::Severity LowDiskNotification::GetSeverity(
return Severity::NONE; return Severity::NONE;
} }
void LowDiskNotification::SetMessageCenterForTest(
message_center::MessageCenter* message_center) {
message_center_ = message_center;
}
void LowDiskNotification::SetNotificationIntervalForTest( void LowDiskNotification::SetNotificationIntervalForTest(
base::TimeDelta notification_interval) { base::TimeDelta notification_interval) {
notification_interval_ = notification_interval; notification_interval_ = notification_interval;
......
...@@ -15,9 +15,10 @@ ...@@ -15,9 +15,10 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "chromeos/dbus/cryptohome_client.h" #include "chromeos/dbus/cryptohome_client.h"
class Profile;
namespace message_center { namespace message_center {
class Notification; class Notification;
class MessageCenter;
} }
namespace chromeos { namespace chromeos {
...@@ -50,22 +51,19 @@ class LowDiskNotification : public CryptohomeClient::Observer { ...@@ -50,22 +51,19 @@ class LowDiskNotification : public CryptohomeClient::Observer {
// Creates a notification for the specified severity. If the severity does // Creates a notification for the specified severity. If the severity does
// not match a known value MEDIUM is used by default. // not match a known value MEDIUM is used by default.
std::unique_ptr<message_center::Notification> CreateNotification( std::unique_ptr<message_center::Notification> CreateNotification(
Severity severity); Severity severity,
Profile* profile);
// Gets the severity of the low disk status based on the amount of free space // Gets the severity of the low disk status based on the amount of free space
// left on the disk. // left on the disk.
Severity GetSeverity(uint64_t free_disk_bytes); Severity GetSeverity(uint64_t free_disk_bytes);
// Sets the MessageCenter instance to use. Should only be used in tests.
void SetMessageCenterForTest(message_center::MessageCenter* message_center);
// Sets the minimum time to wait between notifications of the same severity. // Sets the minimum time to wait between notifications of the same severity.
// Should only be used in tests. // Should only be used in tests.
void SetNotificationIntervalForTest(base::TimeDelta interval); void SetNotificationIntervalForTest(base::TimeDelta interval);
base::Time last_notification_time_; base::Time last_notification_time_;
Severity last_notification_severity_ = NONE; Severity last_notification_severity_ = NONE;
message_center::MessageCenter* message_center_;
base::TimeDelta notification_interval_; base::TimeDelta notification_interval_;
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
base::WeakPtrFactory<LowDiskNotification> weak_ptr_factory_; base::WeakPtrFactory<LowDiskNotification> weak_ptr_factory_;
......
...@@ -8,17 +8,17 @@ ...@@ -8,17 +8,17 @@
#include <utility> #include <utility>
#include "base/test/scoped_task_environment.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_cryptohome_client.h" #include "chromeos/dbus/fake_cryptohome_client.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/message_center/fake_message_center.h" #include "ui/message_center/notification.h"
#include "ui/message_center/message_center.h"
namespace { namespace {
...@@ -30,39 +30,44 @@ const uint64_t kHighNotification = (512 << 20) - 1; ...@@ -30,39 +30,44 @@ const uint64_t kHighNotification = (512 << 20) - 1;
namespace chromeos { namespace chromeos {
class LowDiskNotificationTest : public testing::Test, class LowDiskNotificationTest : public BrowserWithTestWindowTest {
public message_center::FakeMessageCenter {
public: public:
LowDiskNotificationTest() {} LowDiskNotificationTest() {}
~LowDiskNotificationTest() override {}
void SetUp() override { void SetUp() override {
DBusThreadManager::GetSetterForTesting()->SetCryptohomeClient( DBusThreadManager::GetSetterForTesting()->SetCryptohomeClient(
std::unique_ptr<CryptohomeClient>(new FakeCryptohomeClient)); std::unique_ptr<CryptohomeClient>(new FakeCryptohomeClient));
message_center::MessageCenter::Initialize();
BrowserWithTestWindowTest::SetUp();
tester_ = std::make_unique<NotificationDisplayServiceTester>(
profile_manager()->profile_manager()->GetLastUsedProfile());
tester_->SetNotificationAddedClosure(base::Bind(
&LowDiskNotificationTest::OnNotificationAdded, base::Unretained(this)));
low_disk_notification_.reset(new LowDiskNotification()); low_disk_notification_.reset(new LowDiskNotification());
low_disk_notification_->SetMessageCenterForTest(this);
low_disk_notification_->SetNotificationIntervalForTest(
base::TimeDelta::FromMilliseconds(10));
notification_count_ = 0; notification_count_ = 0;
} }
void TearDown() override { void TearDown() override {
low_disk_notification_.reset(); low_disk_notification_.reset();
last_notification_.reset(); BrowserWithTestWindowTest::TearDown();
message_center::MessageCenter::Shutdown();
DBusThreadManager::Shutdown();
} }
void AddNotification( base::Optional<message_center::Notification> GetNotification() {
std::unique_ptr<message_center::Notification> notification) override { return tester_->GetNotification("low_disk");
last_notification_ = std::move(notification);
notification_count_++;
} }
void SetNotificationThrottlingInterval(int ms) {
low_disk_notification_->SetNotificationIntervalForTest(
base::TimeDelta::FromMilliseconds(ms));
}
void OnNotificationAdded() { notification_count_++; }
protected: protected:
base::test::ScopedTaskEnvironment scoped_task_environment_; std::unique_ptr<NotificationDisplayServiceTester> tester_;
std::unique_ptr<LowDiskNotification> low_disk_notification_; std::unique_ptr<LowDiskNotification> low_disk_notification_;
std::unique_ptr<message_center::Notification> last_notification_;
int notification_count_; int notification_count_;
}; };
...@@ -70,8 +75,9 @@ TEST_F(LowDiskNotificationTest, MediumLevelNotification) { ...@@ -70,8 +75,9 @@ TEST_F(LowDiskNotificationTest, MediumLevelNotification) {
base::string16 expected_title = base::string16 expected_title =
l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_TITLE); l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_TITLE);
low_disk_notification_->LowDiskSpace(kMediumNotification); low_disk_notification_->LowDiskSpace(kMediumNotification);
EXPECT_NE(nullptr, last_notification_); auto notification = GetNotification();
EXPECT_EQ(expected_title, last_notification_->title()); ASSERT_TRUE(notification);
EXPECT_EQ(expected_title, notification->title());
EXPECT_EQ(1, notification_count_); EXPECT_EQ(1, notification_count_);
} }
...@@ -80,28 +86,29 @@ TEST_F(LowDiskNotificationTest, HighLevelReplacesMedium) { ...@@ -80,28 +86,29 @@ TEST_F(LowDiskNotificationTest, HighLevelReplacesMedium) {
l10n_util::GetStringUTF16(IDS_CRITICALLY_LOW_DISK_NOTIFICATION_TITLE); l10n_util::GetStringUTF16(IDS_CRITICALLY_LOW_DISK_NOTIFICATION_TITLE);
low_disk_notification_->LowDiskSpace(kMediumNotification); low_disk_notification_->LowDiskSpace(kMediumNotification);
low_disk_notification_->LowDiskSpace(kHighNotification); low_disk_notification_->LowDiskSpace(kHighNotification);
EXPECT_NE(nullptr, last_notification_); auto notification = GetNotification();
EXPECT_EQ(expected_title, last_notification_->title()); ASSERT_TRUE(notification);
EXPECT_EQ(expected_title, notification->title());
EXPECT_EQ(2, notification_count_); EXPECT_EQ(2, notification_count_);
} }
TEST_F(LowDiskNotificationTest, NotificationsAreThrottled) { TEST_F(LowDiskNotificationTest, NotificationsAreThrottled) {
SetNotificationThrottlingInterval(10000000);
low_disk_notification_->LowDiskSpace(kHighNotification); low_disk_notification_->LowDiskSpace(kHighNotification);
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5));
low_disk_notification_->LowDiskSpace(kHighNotification); low_disk_notification_->LowDiskSpace(kHighNotification);
EXPECT_EQ(1, notification_count_); EXPECT_EQ(1, notification_count_);
} }
TEST_F(LowDiskNotificationTest, HighNotificationsAreShownAfterThrottling) { TEST_F(LowDiskNotificationTest, HighNotificationsAreShownAfterThrottling) {
SetNotificationThrottlingInterval(-1);
low_disk_notification_->LowDiskSpace(kHighNotification); low_disk_notification_->LowDiskSpace(kHighNotification);
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15));
low_disk_notification_->LowDiskSpace(kHighNotification); low_disk_notification_->LowDiskSpace(kHighNotification);
EXPECT_EQ(2, notification_count_); EXPECT_EQ(2, notification_count_);
} }
TEST_F(LowDiskNotificationTest, MediumNotificationsAreNotShownAfterThrottling) { TEST_F(LowDiskNotificationTest, MediumNotificationsAreNotShownAfterThrottling) {
SetNotificationThrottlingInterval(-1);
low_disk_notification_->LowDiskSpace(kMediumNotification); low_disk_notification_->LowDiskSpace(kMediumNotification);
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15));
low_disk_notification_->LowDiskSpace(kMediumNotification); low_disk_notification_->LowDiskSpace(kMediumNotification);
EXPECT_EQ(1, notification_count_); EXPECT_EQ(1, notification_count_);
} }
......
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