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

ash / system / accessibility / facegaze_bubble_controller_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/accessibility/facegaze_bubble_controller.h"

#include "ash/accessibility/accessibility_controller.h"
#include "ash/shell.h"
#include "ash/system/accessibility/facegaze_bubble_view.h"
#include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_provider.h"
#include "ui/views/accessibility/view_accessibility.h"

namespace ash {

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

  // AshTestBase:
  void SetUp() override {
    scoped_feature_list_.InitAndEnableFeature(
        ::features::kAccessibilityFaceGaze);
    AshTestBase::SetUp();
    Shell::Get()->accessibility_controller()->face_gaze().SetEnabled(true);
  }

  FaceGazeBubbleController* GetController() {
    return Shell::Get()
        ->accessibility_controller()
        ->GetFaceGazeBubbleControllerForTest();
  }

  void Update(const std::u16string& text, bool is_warning) {
    GetController()->UpdateBubble(text, is_warning);
  }

  FaceGazeBubbleView* GetView() {
    return GetController()->facegaze_bubble_view_;
  }

  bool IsVisible() { return GetController()->widget_->IsVisible(); }

  const std::u16string& GetBubbleText() {
    return GetView()->GetTextForTesting();
  }

  bool IsShowTimerRunning() { return GetController()->show_timer_.IsRunning(); }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(FaceGazeBubbleControllerTest, LabelText) {
  EXPECT_FALSE(GetView());
  Update(u"Testing", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_EQ(u"Testing", GetBubbleText());

  Update(u"", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_EQ(u"", GetBubbleText());
}

TEST_F(FaceGazeBubbleControllerTest, UpdateColor) {
  EXPECT_FALSE(GetView());
  Update(u"Default", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  ui::ColorProvider* color_provider = GetView()->GetColorProvider();
  EXPECT_EQ(
      color_provider->GetColor(cros_tokens::kCrosSysSystemBaseElevatedOpaque),
      GetView()->color());

  Update(u"Warning", /*is_warning=*/true);
  EXPECT_TRUE(GetView());
  EXPECT_EQ(color_provider->GetColor(cros_tokens::kCrosSysWarningContainer),
            GetView()->color());

  Update(u"Default", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_EQ(
      color_provider->GetColor(cros_tokens::kCrosSysSystemBaseElevatedOpaque),
      GetView()->color());
}

TEST_F(FaceGazeBubbleControllerTest, AccessibleProperties) {
  Update(u"", /*is_warning=*/false);
  ui::AXNodeData data;
  GetView()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.role, ax::mojom::Role::kGenericContainer);
}

TEST_F(FaceGazeBubbleControllerTest, Hide) {
  Update(u"Testing", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_TRUE(IsVisible());

  // Simulate mouse hover event to hide the view.
  GetEventGenerator()->MoveMouseTo(
      GetView()->GetBoundsInScreen().CenterPoint());
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());
}

TEST_F(FaceGazeBubbleControllerTest, ShowAfterHide) {
  Update(u"Testing", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_TRUE(IsVisible());

  // Simulate mouse hover event to hide the view.
  GetEventGenerator()->MoveMouseTo(
      GetView()->GetBoundsInScreen().CenterPoint());
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());

  // The view remains hidden while the timer is running.
  task_environment()->FastForwardBy(base::Milliseconds(500));
  EXPECT_TRUE(IsShowTimerRunning());
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());

  // Move mouse away from the view to ensure it's not hidden again.
  GetEventGenerator()->MoveMouseTo(-100, -100);

  // Ensure the view is shown again after a timeout has elapsed.
  task_environment()->FastForwardBy(base::Milliseconds(600));
  EXPECT_FALSE(IsShowTimerRunning());
  EXPECT_TRUE(GetView());
  EXPECT_TRUE(IsVisible());
}

TEST_F(FaceGazeBubbleControllerTest, RepeatedlyHidden) {
  Update(u"Testing", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_TRUE(IsVisible());

  // Simulate mouse hover event to hide the view.
  GetEventGenerator()->MoveMouseTo(
      GetView()->GetBoundsInScreen().CenterPoint());
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());

  // The view remains hidden while the timer is running.
  task_environment()->FastForwardBy(base::Milliseconds(500));
  EXPECT_TRUE(IsShowTimerRunning());
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());

  // The view gets hidden again because it still overlaps with the mouse
  // location.
  task_environment()->FastForwardBy(base::Seconds(2));
  EXPECT_TRUE(IsShowTimerRunning());
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());
}

TEST_F(FaceGazeBubbleControllerTest, UpdateWhileHidden) {
  Update(u"Testing", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_TRUE(IsVisible());

  // Simulate mouse hover event to hide the view.
  GetEventGenerator()->MoveMouseTo(
      GetView()->GetBoundsInScreen().CenterPoint());
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());

  // The view should still be hidden, even if updated, because the timer is
  // running.
  EXPECT_TRUE(IsShowTimerRunning());
  Update(u"Hello world", /*is_warning=*/false);
  EXPECT_TRUE(GetView());
  EXPECT_FALSE(IsVisible());
}

}  // namespace ash