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

ash / auth / views / auth_textfield_timer_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/auth_textfield_timer.h"

#include <memory>
#include <string>

#include "ash/auth/views/auth_textfield.h"
#include "ash/auth/views/test_support/mock_auth_textfield_observer.h"
#include "ash/test/ash_test_base.h"
#include "base/check.h"
#include "base/check_op.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/views/widget/widget.h"

namespace ash {

namespace {

constexpr std::u16string kPassword = u"password";

}  // namespace

class AuthTextfieldWithTimerUnitTest : public AshTestBase {
 public:
  AuthTextfieldWithTimerUnitTest()
      : AshTestBase(std::make_unique<base::test::TaskEnvironment>(
            base::test::TaskEnvironment::MainThreadType::UI,
            base::test::TaskEnvironment::TimeSource::MOCK_TIME)) {}
  AuthTextfieldWithTimerUnitTest(const AuthTextfieldWithTimerUnitTest&) =
      delete;
  AuthTextfieldWithTimerUnitTest& operator=(
      const AuthTextfieldWithTimerUnitTest&) = delete;
  ~AuthTextfieldWithTimerUnitTest() override = default;

  void SetTextfieldToFocus() {
    auth_textfield_->GetFocusManager()->SetFocusedView(auth_textfield_);
  }

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

    widget_ = CreateFramelessTestWidget();
    widget_->SetFullscreen(true);
    widget_->Show();

    auth_textfield_ = widget_->SetContentsView(
        std::make_unique<AuthTextfield>(AuthTextfield::AuthType::kPassword));
    mock_observer_ = std::make_unique<MockAuthTextfieldObserver>();
    auth_textfield_->SetText(kPassword);
    auth_textfield_->AddObserver(mock_observer_.get());
    auth_textfield_timer_ =
        std::make_unique<AuthTextfieldTimer>(auth_textfield_);
  }

  void TearDown() override {
    AshTestBase::TearDown();
    auth_textfield_timer_.reset();
    auth_textfield_->RemoveObserver(mock_observer_.get());
    mock_observer_.reset();
    auth_textfield_ = nullptr;
    widget_.reset();
  }

  std::unique_ptr<views::Widget> widget_;
  std::unique_ptr<MockAuthTextfieldObserver> mock_observer_;
  std::unique_ptr<AuthTextfieldTimer> auth_textfield_timer_;
  raw_ptr<AuthTextfield> auth_textfield_;
};

// Testing password become hidden after 5 sec elapsed without user interaction.
TEST_F(AuthTextfieldWithTimerUnitTest, HidePasswordTest) {
  SetTextfieldToFocus();
  CHECK(!auth_textfield_->IsTextVisible());
  auth_textfield_->SetTextVisible(true);
  EXPECT_CALL(*mock_observer_, OnTextVisibleChanged(false)).Times(1);
  task_environment()->FastForwardBy(base::Seconds(5));
  CHECK(!auth_textfield_->IsTextVisible());
}

// Testing password visibility not changing if the password is hidden after 10
// sec.
TEST_F(AuthTextfieldWithTimerUnitTest, HidePasswordTest2) {
  SetTextfieldToFocus();
  CHECK(!auth_textfield_->IsTextVisible());
  // Make the password visible.
  auth_textfield_->SetTextVisible(true);
  // Hide the password.
  auth_textfield_->SetTextVisible(false);
  EXPECT_CALL(*mock_observer_, OnTextVisibleChanged(false)).Times(0);
  EXPECT_CALL(*mock_observer_, OnTextVisibleChanged(true)).Times(0);
  task_environment()->FastForwardBy(base::Seconds(10));
  CHECK(!auth_textfield_->IsTextVisible());
}

// Testing password become hidden after 30 sec elapsed without user interaction.
TEST_F(AuthTextfieldWithTimerUnitTest, ClearPasswordTest) {
  SetTextfieldToFocus();
  CHECK(!auth_textfield_->IsTextVisible());
  // Make a user interaction.
  const std::u16string modifiedString = kPassword + u"s";
  EXPECT_CALL(*mock_observer_, OnContentsChanged(modifiedString)).Times(1);
  ui::test::EventGenerator* generator = GetEventGenerator();
  generator->PressAndReleaseKey(ui::VKEY_S);

  // After 30 sec without user interaction the textfield should be cleared.
  const std::u16string emptyStr;
  EXPECT_CALL(*mock_observer_, OnContentsChanged(emptyStr)).Times(1);
  task_environment()->FastForwardBy(base::Seconds(30));
  CHECK_EQ(auth_textfield_->GetText(), emptyStr);
}

}  // namespace ash