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 low_disk_notification_.reset(new LowDiskNotification()); |
| 40 low_disk_notification_->SetMessageCenterForTest(this); |
| 41 low_disk_notification_->SetNotificationIntervalForTest( |
| 42 base::TimeDelta::FromMilliseconds(10)); |
| 43 notification_count_ = 0; |
| 44 } |
| 45 |
| 46 void TearDown() override { |
| 47 low_disk_notification_.reset(); |
| 48 last_notification_.reset(); |
| 49 DBusThreadManager::Shutdown(); |
| 50 } |
| 51 |
| 52 void AddNotification( |
| 53 std::unique_ptr<message_center::Notification> notification) override { |
| 54 last_notification_ = std::move(notification); |
| 55 notification_count_++; |
| 56 } |
| 57 |
| 58 protected: |
| 59 std::unique_ptr<LowDiskNotification> low_disk_notification_; |
| 60 std::unique_ptr<message_center::Notification> last_notification_; |
| 61 int notification_count_; |
| 62 }; |
| 63 |
| 64 TEST_F(LowDiskNotificationTest, MediumLevelNotification) { |
| 65 base::string16 expected_title = |
| 66 l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_TITLE); |
| 67 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 68 EXPECT_NE(nullptr, last_notification_); |
| 69 EXPECT_EQ(expected_title, last_notification_->title()); |
| 70 EXPECT_EQ(1, notification_count_); |
| 71 } |
| 72 |
| 73 TEST_F(LowDiskNotificationTest, HighLevelReplacesMedium) { |
| 74 base::string16 expected_title = |
| 75 l10n_util::GetStringUTF16(IDS_CRITICALLY_LOW_DISK_NOTIFICATION_TITLE); |
| 76 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 77 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 78 EXPECT_NE(nullptr, last_notification_); |
| 79 EXPECT_EQ(expected_title, last_notification_->title()); |
| 80 EXPECT_EQ(2, notification_count_); |
| 81 } |
| 82 |
| 83 TEST_F(LowDiskNotificationTest, NotificationsAreThrottled) { |
| 84 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 85 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); |
| 86 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 87 EXPECT_EQ(1, notification_count_); |
| 88 } |
| 89 |
| 90 TEST_F(LowDiskNotificationTest, HighNotificationsAreShownAfterThrottling) { |
| 91 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 92 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15)); |
| 93 low_disk_notification_->OnLowDiskSpace(kHighNotification); |
| 94 EXPECT_EQ(2, notification_count_); |
| 95 } |
| 96 |
| 97 TEST_F(LowDiskNotificationTest, MediumNotificationsAreNotShownAfterThrottling) { |
| 98 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 99 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(15)); |
| 100 low_disk_notification_->OnLowDiskSpace(kMediumNotification); |
| 101 EXPECT_EQ(1, notification_count_); |
| 102 } |
| 103 |
| 104 } // namespace chromeos |
OLD | NEW |