File: profiles_pixel_test_utils.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (135 lines) | stat: -rw-r--r-- 5,082 bytes parent folder | download | duplicates (8)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILES_PIXEL_TEST_UTILS_H_
#define CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILES_PIXEL_TEST_UTILS_H_

#include <memory>
#include <string>

#include "base/scoped_environment_variable_override.h"
#include "chrome/browser/signin/signin_browser_test_base.h"
#include "components/signin/public/identity_manager/tribool.h"

namespace base {
class CommandLine;

namespace test {
class ScopedFeatureList;
}  // namespace test
}  // namespace base

namespace signin {
class IdentityTestEnvironment;
}

struct AccountInfo;

// Parameters that are used for most of the pixel tests. These params
// will be used to create combinations with the test name as `test_suffix` and
// will be passed to the parametrised test as the second argument of
// `INSTANTIATE_TEST_SUITE_P`.
struct PixelTestParam {
  std::string test_suffix = "";
  bool use_dark_theme = false;
  bool use_right_to_left_language = false;

  // Predefined sizes:
  //
  // Small windows: both width and height.
  static constexpr gfx::Size kSmallWindowSize{750, 590};
  // Portrait mode: longer height than width compared to the default ratio./
  // Height of 750px is the maximum height a pixel test can reach.
  static constexpr gfx::Size kPortraitModeWindowSize{700, 750};
  // Leaving `window_size` unset means using the default size of the view.
  std::optional<gfx::Size> window_size;
};

enum class AccountManagementStatus {
  kManaged = 0,
  kNonManaged,
};

// Returns an AccountInfo with all fields filled in, such that
// AccountInfo::IsValid() is true.
AccountInfo FillAccountInfo(
    const CoreAccountInfo& core_info,
    AccountManagementStatus management_status,
    signin::Tribool
        can_show_history_sync_opt_ins_without_minor_mode_restrictions);

// Used to create a dummy account and sign it in, by default as a primary
// account.
AccountInfo SignInWithAccount(
    signin::IdentityTestEnvironment& identity_test_env,
    AccountManagementStatus management_status =
        AccountManagementStatus::kNonManaged,
    std::optional<signin::ConsentLevel> consent_level =
        signin::ConsentLevel::kSignin,
    signin::Tribool
        can_show_history_sync_opt_ins_without_minor_mode_restrictions =
            signin::Tribool::kTrue);

// Sets up the parameters that are passed to the command line. For example,
// to enable dark mode, we need to pass `kForceDarkMode` to the command line.
// This function should be called inside the `SetUpCommandLine` function.
void SetUpPixelTestCommandLine(
    const PixelTestParam& params,
    std::unique_ptr<base::ScopedEnvironmentVariableOverride>& env_variables,
    base::CommandLine* command_line);

// Enables and disables the features that we need for the test. This function
// will automatically add dark mode and the first run experience feature when
// used.
void InitPixelTestFeatures(const PixelTestParam& params,
                           base::test::ScopedFeatureList& feature_list);

// Base class for pixel tests for profiles-related features.
//
// Reduces test boilerplate by:
// - applying the configuration from `PixelTestParam` passed via the
//   constructor, removing the need to call `SetUpPixelTestCommandLine()` and
//   `InitPixelTestFeatures()`.
// - providing helpers to set up the account, see `SignInWithAccount()`.
template <typename T>
class ProfilesPixelTestBaseT : public SigninBrowserTestBaseT<T> {
 public:
  template <typename... Args>
  explicit ProfilesPixelTestBaseT(const PixelTestParam& params, Args&&... args)
      : SigninBrowserTestBaseT<T>(std::forward<Args>(args)...),
        test_configuration_(params) {
    InitPixelTestFeatures(test_configuration_, scoped_feature_list_);
  }

  ~ProfilesPixelTestBaseT() override = default;

  // Used to create a dummy account and sign it in, by default as a primary
  // account.
  AccountInfo SignInWithAccount(
      AccountManagementStatus management_status =
          AccountManagementStatus::kNonManaged,
      std::optional<signin::ConsentLevel> consent_level =
          signin::ConsentLevel::kSignin,
      signin::Tribool
          can_show_history_sync_opt_ins_without_minor_mode_restrictions =
              signin::Tribool::kTrue) {
    return ::SignInWithAccount(
        *this->identity_test_env(), management_status, consent_level,
        can_show_history_sync_opt_ins_without_minor_mode_restrictions);
  }

  // SigninBrowserTestBaseT overrides:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    SigninBrowserTestBaseT<T>::SetUpCommandLine(command_line);
    SetUpPixelTestCommandLine(test_configuration_, scoped_env_override_,
                              command_line);
  }

 private:
  const PixelTestParam test_configuration_;
  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<base::ScopedEnvironmentVariableOverride> scoped_env_override_;
};

#endif  // CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILES_PIXEL_TEST_UTILS_H_