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
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420

ash / system / time / time_view_unittest.cc [blame]

// Copyright 2013 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/time/time_view.h"

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/test/ash_test_base.h"
#include "base/i18n/time_formatting.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "ui/gfx/geometry/point.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/widget/widget.h"

namespace ash {

class TimeViewTest : public AshTestBase {
 public:
  TimeViewTest()
      : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  TimeViewTest(const TimeViewTest&) = delete;
  TimeViewTest& operator=(const TimeViewTest&) = delete;
  ~TimeViewTest() override = default;

  void SetUp() override {
    AshTestBase::SetUp();
    widget_ = CreateFramelessTestWidget();
    widget_->SetFullscreen(true);
  }

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

  TimeView* time_view() { return time_view_; }

  // Access to private fields of |time_view_|.
  views::View* horizontal_time_label_container() {
    return time_view_->horizontal_time_label_container_.get();
  }
  views::View* vertical_time_label_container() {
    return time_view_->vertical_time_label_container_.get();
  }
  views::View* horizontal_date_label_container() {
    return time_view_->horizontal_date_label_container_.get();
  }
  views::View* vertical_date_view_container() {
    return time_view_->vertical_date_view_container_.get();
  }
  views::Label* horizontal_time_label_() {
    return time_view_->horizontal_time_label_;
  }
  views::Label* vertical_label_hours() {
    return time_view_->vertical_label_hours_;
  }

  views::Label* vertical_label_minutes() {
    return time_view_->vertical_label_minutes_;
  }
  views::Label* horizontal_date_label() {
    return time_view_->horizontal_date_label_;
  }
  VerticalDateView* vertical_date() { return time_view_->vertical_date_view_; }

  views::Label* vertical_date_label() {
    return time_view_->vertical_date_view_
               ? time_view_->vertical_date_view_->text_label_.get()
               : nullptr;
  }

  void UpdateText() { time_view_->UpdateText(); }

  // Creates a time view with horizontal or vertical |clock_layout|.
  void CreateTimeView(TimeView::ClockLayout clock_layout,
                      TimeView::Type type = TimeView::kTime) {
    time_view_ = widget_->SetContentsView(std::make_unique<TimeView>(
        clock_layout, Shell::Get()->system_tray_model()->clock(), type));
  }

 private:
  std::unique_ptr<views::Widget> widget_;
  // Owned by `widget_`.
  raw_ptr<TimeView, DanglingUntriaged> time_view_;
  base::WeakPtrFactory<TimeViewTest> weak_factory_{this};
};

class TimeViewObserver : public views::ViewObserver {
 public:
  explicit TimeViewObserver(views::View* observed_view) {
    observation_.Observe(observed_view);
  }
  TimeViewObserver(const TimeViewObserver&) = delete;
  TimeViewObserver& operator=(const TimeViewObserver&) = delete;
  ~TimeViewObserver() override = default;

  void reset_preferred_size_changed_called() {
    preferred_size_changed_called_ = false;
  }

  bool preferred_size_changed_called() const {
    return preferred_size_changed_called_;
  }

  // views::ViewObserver:
  void OnViewPreferredSizeChanged(views::View* observed_view) override {
    preferred_size_changed_called_ = true;
  }

 private:
  base::ScopedObservation<views::View, views::ViewObserver> observation_{this};
  bool preferred_size_changed_called_ = false;
};

// Test the basics of the time view, mostly to ensure we don't leak memory.
TEST_F(TimeViewTest, Basics) {
  // A newly created horizontal clock only has the horizontal label.
  CreateTimeView(TimeView::ClockLayout::HORIZONTAL_CLOCK);
  EXPECT_TRUE(horizontal_time_label_container()->GetVisible());
  ASSERT_FALSE(vertical_time_label_container()->GetVisible());

  // Switching the clock to vertical updates the labels.
  time_view()->UpdateClockLayout(TimeView::ClockLayout::VERTICAL_CLOCK);
  ASSERT_FALSE(horizontal_time_label_container()->GetVisible());
  EXPECT_TRUE(vertical_time_label_container()->GetVisible());

  // Switching back to horizontal updates the labels again.
  time_view()->UpdateClockLayout(TimeView::ClockLayout::HORIZONTAL_CLOCK);
  EXPECT_TRUE(horizontal_time_label_container()->GetVisible());
  ASSERT_FALSE(vertical_time_label_container()->GetVisible());
}

// TODO(crbug.com/355355015): flaky test.
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER)
#define MAYBE_TimeViewFiresAccessibilityEvents \
  DISABLED_TimeViewFiresAccessibilityEvents
#else
#define MAYBE_TimeViewFiresAccessibilityEvents TimeViewFiresAccessibilityEvents
#endif
// Test accessibility events emitted by the time view's labels during updates.
TEST_F(TimeViewTest, MAYBE_TimeViewFiresAccessibilityEvents) {
  views::test::AXEventCounter counter(views::AXEventManager::Get());

  CreateTimeView(TimeView::ClockLayout::HORIZONTAL_CLOCK);

  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));

  // Set current time to 08:00.
  // There should be one text-changed accessibility event for each time-related
  // label, none for the date-related labels, and one for the time view button.
  task_environment()->AdvanceClock(base::Time::Now().LocalMidnight() +
                                   base::Hours(32) - base::Time::Now());
  UpdateText();

  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));

  // Changing the layout does not change the text. Hence no text-changed events
  // are fired.
  counter.ResetAllCounts();
  time_view()->UpdateClockLayout(TimeView::ClockLayout::VERTICAL_CLOCK);
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));

  // Call update text when the time has not changed. Because the time has not
  // changed, the text has not changed. Hence no text-changed events are fired.
  counter.ResetAllCounts();
  UpdateText();
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));

  // Move to 08:01 and update the text again. There should be one text-changed
  // accessibility event for each time-related label whose text has changed,
  // i.e. the horizontal label and the vertical minutes label. The time view
  // button should also fire an event since the displayed text changed.
  counter.ResetAllCounts();
  task_environment()->FastForwardBy(base::Minutes(1));
  UpdateText();
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));
}

// Test `PreferredSizeChanged()` is called when there's a size change of the
// `TimeView`.
TEST_F(TimeViewTest, UpdateSize) {
  // Set current time to 8:00AM for testing.
  task_environment()->AdvanceClock(base::Time::Now().LocalMidnight() +
                                   base::Hours(32) - base::Time::Now());

  // A newly created horizontal clock only has the horizontal label.
  CreateTimeView(TimeView::ClockLayout::HORIZONTAL_CLOCK);
  TimeViewObserver test_observer(time_view());
  test_observer.reset_preferred_size_changed_called();

  EXPECT_FALSE(test_observer.preferred_size_changed_called());

  // Move to 9:59AM. There should be no layout change of the `time_view()`.
  task_environment()->FastForwardBy(base::Minutes(119));
  EXPECT_FALSE(test_observer.preferred_size_changed_called());

  // Move to 10:00AM. There should be a layout change of the `time_view()`.
  task_environment()->FastForwardBy(base::Seconds(61));
  EXPECT_TRUE(test_observer.preferred_size_changed_called());
}

// Test that for horizontal clocks, the "AM/PM" text can be enabled and
// disabled.
TEST_F(TimeViewTest, EnableAmPmText) {
  // Set current time to 8:00AM for testing.
  task_environment()->AdvanceClock(base::Time::Now().LocalMidnight() +
                                   base::Hours(32) - base::Time::Now());

  // A newly created horizontal clock only has the horizontal label.
  CreateTimeView(TimeView::ClockLayout::HORIZONTAL_CLOCK, TimeView::kTime);

  // Ensure that the "AM/PM" flag is disabled by default.
  ASSERT_EQ(time_view()->GetAmPmClockTypeForTesting(),
            base::AmPmClockType::kDropAmPm);
  auto* horizontal_label = time_view()->GetHorizontalTimeLabelForTesting();

  // Ensure that the "AM/PM" text isn't visible.
  ASSERT_FALSE(horizontal_label->GetText().ends_with(u"AM"));
  ASSERT_FALSE(horizontal_label->GetText().ends_with(u"PM"));

  // Ensure that the "AM/PM" flag can be enabled.
  time_view()->SetAmPmClockType(base::AmPmClockType::kKeepAmPm);
  ASSERT_EQ(time_view()->GetAmPmClockTypeForTesting(),
            base::AmPmClockType::kKeepAmPm);

  // Ensure that the "AM/PM" text is visible.
  ASSERT_TRUE(horizontal_label->GetText().ends_with(u"AM"));

  // Advance time by 12 hours.
  task_environment()->FastForwardBy(base::Hours(12));

  // Ensure that the transition from "AM" to "PM" occurs as time moves.
  ASSERT_TRUE(horizontal_label->GetText().ends_with(u"PM"));

  // Ensure that the "AM/PM" text isn't visible in vertical clocks.
  time_view()->UpdateClockLayout(TimeView::ClockLayout::VERTICAL_CLOCK);
  auto* vertical_minutes_label =
      time_view()->GetVerticalMinutesLabelForTesting();
  auto* vertical_hours_label = time_view()->GetVerticalHoursLabelForTesting();
  ASSERT_FALSE(vertical_minutes_label->GetText().ends_with(u"AM"));
  ASSERT_FALSE(vertical_hours_label->GetText().ends_with(u"PM"));
}

// Test the Date view of the time view.
TEST_F(TimeViewTest, DateView) {
  // A newly created horizontal Date only has the horizontal date view.
  CreateTimeView(TimeView::ClockLayout::HORIZONTAL_CLOCK, TimeView::kDate);
  EXPECT_TRUE(horizontal_date_label_container()->GetVisible());
  EXPECT_FALSE(vertical_date_view_container()->GetVisible());

  // Switching the date to vertical updates the views.
  time_view()->UpdateClockLayout(TimeView::ClockLayout::VERTICAL_CLOCK);
  EXPECT_FALSE(horizontal_date_label_container()->GetVisible());
  EXPECT_TRUE(vertical_date_view_container()->GetVisible());

  // Switching back to horizontal updates the views again.
  time_view()->UpdateClockLayout(TimeView::ClockLayout::HORIZONTAL_CLOCK);
  EXPECT_TRUE(horizontal_date_label_container()->GetVisible());
  EXPECT_FALSE(vertical_date_view_container()->GetVisible());
}

// Test accessibility events emitted by the date view's labels during updates.
TEST_F(TimeViewTest, DateViewFiresAccessibilityEvents) {
  views::test::AXEventCounter counter(views::AXEventManager::Get());

  CreateTimeView(TimeView::ClockLayout::HORIZONTAL_CLOCK, TimeView::kDate);
  // We shouldn't fire any events through the construction of the view with
  // default values.
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));

  // Set current time to 08:00.
  // There should be one text-changed accessibility event for each date-related
  // label, none for the time-related labels, and one for the time view button.
  task_environment()->AdvanceClock(base::Time::Now().LocalMidnight() +
                                   base::Hours(32) - base::Time::Now());
  UpdateText();

  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));

  // Changing the layout doesn't change the text. Hence no text-changed events
  // are fired.
  counter.ResetAllCounts();
  time_view()->UpdateClockLayout(TimeView::ClockLayout::VERTICAL_CLOCK);
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));

  // Call update text when the time has not changed. Because the time has not
  // changed, the text has not changed. Hence no text-changed events are fired.
  counter.ResetAllCounts();
  UpdateText();
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));

  // Move to 08:01 and update the text again. Because this is the date view, we
  // do not have any accessibility events for text changing in the time-related
  // labels. And because the date has not changed, we should not have any events
  // for the date-related labels either. The time view button should fire an
  // event since the displayed text changed.
  counter.ResetAllCounts();
  task_environment()->FastForwardBy(base::Minutes(1));
  UpdateText();
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_time_label_()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_hours()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_label_minutes()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                horizontal_date_label()));
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kTextChanged,
                                vertical_date_label()));
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kTextChanged, time_view()));
}

TEST_F(TimeViewTest, AccessibleProperties) {
  CreateTimeView(TimeView::ClockLayout::HORIZONTAL_CLOCK);
  ui::AXNodeData data;

  time_view()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.role, ax::mojom::Role::kTime);
}

}  // namespace ash