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

ash / system / notification_center / views / timestamp_view_unittest.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/notification_center/views/timestamp_view.h"

#include "ash/test/ash_test_base.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/widget/widget.h"

namespace ash {

class TimestampViewTest : public AshTestBase {
 public:
  TimestampViewTest()
      : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

  TimestampViewTest(const TimestampViewTest&) = delete;
  TimestampViewTest& operator=(const TimestampViewTest&) = delete;
  ~TimestampViewTest() override = default;

  void SetUp() override {
    AshTestBase::SetUp();

    // `widget_` owns `timestamp_view_`.
    widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    timestamp_view_ = widget_->GetContentsView()->AddChildView(
        std::make_unique<TimestampView>());
  }

  void TearDown() override {
    timestamp_view_ = nullptr;
    widget_.reset();
    AshTestBase::TearDown();
  }

 protected:
  TimestampView* timestamp_view() { return timestamp_view_.get(); }

 private:
  raw_ptr<TimestampView> timestamp_view_;
  std::unique_ptr<views::Widget> widget_;
};

TEST_F(TimestampViewTest, TimestampTextUpdatedOverTime) {
  EXPECT_TRUE(timestamp_view()->GetVisible());

  timestamp_view()->SetTimestamp(base::Time::Now() + base::Hours(3) +
                                 base::Minutes(30));
  EXPECT_EQ(l10n_util::GetPluralStringFUTF16(
                IDS_MESSAGE_NOTIFICATION_DURATION_HOURS_SHORTEST_FUTURE, 3),
            timestamp_view()->GetText());

  task_environment()->AdvanceClock(base::Hours(3));
  task_environment()->RunUntilIdle();

  EXPECT_EQ(l10n_util::GetPluralStringFUTF16(
                IDS_MESSAGE_NOTIFICATION_DURATION_MINUTES_SHORTEST_FUTURE, 30),
            timestamp_view()->GetText());

  task_environment()->AdvanceClock(base::Minutes(30));
  task_environment()->RunUntilIdle();

  EXPECT_EQ(
      l10n_util::GetStringUTF16(IDS_MESSAGE_NOTIFICATION_NOW_STRING_SHORTEST),
      timestamp_view()->GetText());

  task_environment()->AdvanceClock(base::Days(2));
  task_environment()->RunUntilIdle();

  EXPECT_EQ(l10n_util::GetPluralStringFUTF16(
                IDS_MESSAGE_NOTIFICATION_DURATION_DAYS_SHORTEST, 2),
            timestamp_view()->GetText());
}

}  // namespace ash