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

ash / auth / views / fingerprint_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/auth/views/fingerprint_view.h"

#include <memory>
#include <string>

#include "ash/public/cpp/login_types.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/test/test_simple_task_runner.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/window.h"
#include "ui/events/event.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/types/event_type.h"
#include "ui/views/background.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/widget/widget.h"

namespace ash {

class FingerprintViewUnitTest : public AshTestBase {
 public:
  FingerprintViewUnitTest()
      : AshTestBase(std::make_unique<base::test::TaskEnvironment>(
            base::test::TaskEnvironment::MainThreadType::UI,
            base::test::TaskEnvironment::TimeSource::MOCK_TIME)) {}

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

 protected:
  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();
    UpdateDisplay("600x800");

    widget_ = CreateFramelessTestWidget();
    std::unique_ptr<views::View> container_view =
        std::make_unique<views::View>();

    auto* layout =
        container_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
            views::BoxLayout::Orientation::kVertical));
    layout->set_cross_axis_alignment(
        views::BoxLayout::CrossAxisAlignment::kCenter);
    layout->set_main_axis_alignment(
        views::BoxLayout::MainAxisAlignment::kCenter);

    container_view->SetPreferredSize(gfx::Size({400, 300}));

    container_view->SetBackground(views::CreateThemedRoundedRectBackground(
        cros_tokens::kCrosSysSystemBaseElevated, 0));

    fingerprint_view_ =
        container_view->AddChildView(std::make_unique<FingerprintView>());
    widget_->SetBounds({400, 300});
    widget_->Show();

    container_view_ = widget_->SetContentsView(std::move(container_view));
  }

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

  raw_ptr<FingerprintView> fingerprint_view_ = nullptr;
  raw_ptr<views::View> container_view_ = nullptr;
  std::unique_ptr<views::Widget> widget_;
};

// Verify the AVAILABLE_WITH_FAILED_ATTEMPT state transitions.
TEST_F(FingerprintViewUnitTest, AvailableWithFailedAttemptTest) {
  FingerprintView::TestApi test_api(fingerprint_view_);

  fingerprint_view_->SetState(FingerprintState::AVAILABLE_DEFAULT);
  EXPECT_EQ(test_api.GetState(), FingerprintState::AVAILABLE_DEFAULT);

  fingerprint_view_->NotifyAuthFailure();
  EXPECT_EQ(test_api.GetState(),
            FingerprintState::AVAILABLE_WITH_FAILED_ATTEMPT);

  //  The reset should happen after 1300 msec.
  task_environment()->FastForwardBy(base::Seconds(2));
  EXPECT_EQ(test_api.GetState(), FingerprintState::AVAILABLE_DEFAULT);
}

// Verify the AVAILABLE_WITH_TOUCH_SENSOR_WARNING state transitions.
TEST_F(FingerprintViewUnitTest, AvailableWithTouchSensorWarningTest) {
  FingerprintView::TestApi test_api(fingerprint_view_);

  fingerprint_view_->SetState(FingerprintState::AVAILABLE_DEFAULT);
  EXPECT_EQ(test_api.GetState(), FingerprintState::AVAILABLE_DEFAULT);
  views::test::RunScheduledLayout(widget_.get());

  // The reset should happen after 1300 msec.
  ui::test::EventGenerator* generator = GetEventGenerator();
  generator->GestureTapAt(fingerprint_view_->GetBoundsInScreen().CenterPoint());
  EXPECT_EQ(test_api.GetState(),
            FingerprintState::AVAILABLE_WITH_TOUCH_SENSOR_WARNING);

  task_environment()->FastForwardBy(base::Seconds(4));
  EXPECT_EQ(test_api.GetState(), FingerprintState::AVAILABLE_DEFAULT);
}

}  // namespace ash