OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/ui/low_disk_notification.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/threading/platform_thread.h" |
| 10 #include "base/time/time.h" |
| 11 #include "chrome/grit/generated_resources.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/fake_cryptohome_client.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/chromeos/resources/grit/ui_chromeos_resources.h" |
| 18 #include "ui/message_center/fake_message_center.h" |
| 19 #include "ui/message_center/message_center.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Copied from low_disk_notification.cc |
| 24 const uint64_t kMediumNotification = (1 << 30) - 1; |
| 25 const uint64_t kHighNotification = (512 << 20) - 1; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 namespace chromeos { |
| 30 |
| 31 class LowDiskNotificationTest : public testing::Test, |
| 32 public message_center::FakeMessageCenter { |
| 33 public: |
| 34 LowDiskNotificationTest() {} |
| 35 |
| 36 void SetUp() override { |
| 37 DBusThreadManager::GetSetterForTesting()->SetCryptohomeClient( |
| 38 std::unique_ptr<CryptohomeClient>(new FakeCryptohomeClient)); |
| 39 message_center::MessageCenter::Initialize(); |
| 40 low_disk_notification_.reset(new LowDiskNotification()); |
| 41 low_disk_notification_->SetMessageCenterForTest(this); |
| 42 low_disk_notification_->SetNotificationIntervalForTest( |
| 43 base::TimeDelta::FromMilliseconds(10)); |
| 44 notification_count_ = 0; |
| 45 } |
| 46 |
| 47 void TearDown() override { |
| 48 low_disk_notification_.reset(); |
| 49 last_notification_.reset(); |
| 50 message_center::MessageCenter::Shutdown(); |
| 51 DBusThreadManager::Shutdown(); |
| 52 } |
| 53 |
| 54 void AddNotification( |
| 55 std::unique_ptr<message_center::Notification> notification) override { |
| 56 last_notification_ = std::move(notification); |
| 57 notification_count_++; |
| 58 } |
| 59 |
| 60 protected: |
| 61 std::unique_ptr<LowDiskNotification> low_disk_notification_; |
| 62 std::unique_ptr<message_center::Notification> last_notification_; |
| 63 int notification_count_; |
| 64 }; |
| 65 |
| 66 TEST_F(LowDiskNotificationTest, MediumLevelNotification) { |
| 67 base::string16 expected_title = |
| 68 l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_TITLE); |
| 69 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 70 EXPECT_NE(nullptr, last_notification_); |
| 71 EXPECT_EQ(expected_title, last_notification_->title()); |
| 72 EXPECT_EQ(1, notification_count_); |
| 73 } |
| 74 |
| 75 TEST_F(LowDiskNotificationTest, HighLevelReplacesMedium) { |
| 76 base::string16 expected_title = |
| 77 l10n_util::GetStringUTF16(IDS_CRITICALLY_LOW_DISK_NOTIFICATION_TITLE); |
| 78 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 79 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 80 EXPECT_NE(nullptr, last_notification_); |
| 81 EXPECT_EQ(expected_title, last_notification_->title()); |
| 82 EXPECT_EQ(2, notification_count_); |
| 83 } |
| 84 |
| 85 TEST_F(LowDiskNotificationTest, NotificationsAreThrottled) { |
| 86 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 87 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); |
| 88 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 89 EXPECT_EQ(1, notification_count_); |
| 90 } |
| 91 |
| 92 TEST_F(LowDiskNotificationTest, HighNotificationsAreShownAfterThrottling) { |
| 93 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 94 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15)); |
| 95 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 96 EXPECT_EQ(2, notification_count_); |
| 97 } |
| 98 |
| 99 TEST_F(LowDiskNotificationTest, MediumNotificationsAreNotShownAfterThrottling) { |
| 100 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 101 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15)); |
| 102 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 103 EXPECT_EQ(1, notification_count_); |
| 104 } |
| 105 |
| 106 } // namespace chromeos |
OLD | NEW |