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

ash / auth / views / auth_header_view_pixeltest.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 <memory>
#include <optional>
#include <string>

#include "ash/auth/views/auth_header_view.h"
#include "ash/style/dark_light_mode_controller_impl.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_util.h"
#include "ash/test/pixel/ash_pixel_differ.h"
#include "ash/test/pixel/ash_pixel_test_init_params.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

const std::string kUserEmail("user1@gmail.com");

const std::u16string kTitle(u"Auth header view pixeltest title");
const std::u16string kErrorTitle(u"Auth header view pixeltest error");

const std::u16string kDescription(u"Auth header view pixeltest description");

class AuthHeaderPixelTest : public AshTestBase {
 public:
  AuthHeaderPixelTest() = default;
  AuthHeaderPixelTest(const AuthHeaderPixelTest&) = delete;
  AuthHeaderPixelTest& operator=(const AuthHeaderPixelTest&) = delete;
  ~AuthHeaderPixelTest() override = default;

 protected:
  std::optional<pixel_test::InitParams> CreatePixelTestInitParams()
      const override {
    return pixel_test::InitParams();
  }

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

    widget_ = CreateFramelessTestWidget();

    AccountId account_id = AccountId::FromUserEmail(kUserEmail);
    auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
    fake_user_manager->AddUser(account_id);
    scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
        std::move(fake_user_manager));

    std::unique_ptr<AuthHeaderView> header_view =
        std::make_unique<AuthHeaderView>(account_id, kTitle, kDescription);

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

    widget_->SetSize(header_view->GetPreferredSize());
    widget_->Show();

    header_view_ = widget_->SetContentsView(std::move(header_view));

    auto* dark_light_mode_controller = DarkLightModeControllerImpl::Get();
    dark_light_mode_controller->SetAutoScheduleEnabled(false);
    // Test Base should setup the dark mode.
    EXPECT_TRUE(dark_light_mode_controller->IsDarkModeEnabled());
  }

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

  std::unique_ptr<views::Widget> widget_;
  raw_ptr<AuthHeaderView> header_view_ = nullptr;
  std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;
};

// Verify the header component look like in DayMode
TEST_F(AuthHeaderPixelTest, DayMode) {
  DarkLightModeControllerImpl::Get()->SetDarkModeEnabledForTest(false);
  //  Verify the UI.
  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "DayMode", /*revision_number=*/1, header_view_));
  // Verify the error.
  header_view_->SetErrorTitle(kErrorTitle);
  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "Error", /*revision_number=*/1, header_view_));
  // Verify the restore
  header_view_->RestoreTitle();
  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "Restore", /*revision_number=*/1, header_view_));
}

}  // namespace
}  // namespace ash