File: profile_picker_view_test_utils.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (155 lines) | stat: -rw-r--r-- 5,318 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 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_PROFILE_PICKER_VIEW_TEST_UTILS_H_
#define CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILE_PICKER_VIEW_TEST_UTILS_H_

#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#include "chrome/browser/ui/views/profiles/profile_management_flow_controller.h"
#include "chrome/browser/ui/views/profiles/profile_picker_view.h"
#include "chrome/browser/ui/views/profiles/profile_picker_web_contents_host.h"
#include "content/public/browser/web_contents_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/views/view_observer.h"

class Profile;
class ProfileManagementStepController;

namespace views {
class View;
}

// Waits until a view gets attached to its widget.
class ViewAddedWaiter : public views::ViewObserver {
 public:
  explicit ViewAddedWaiter(views::View* view);
  ~ViewAddedWaiter() override;

  void Wait();

 private:
  // ViewObserver:
  void OnViewAddedToWidget(views::View* observed_view) override;

  base::RunLoop run_loop_;
  const raw_ptr<views::View> view_;
  base::ScopedObservation<views::View, views::ViewObserver> observation_{this};
};

// Waits until a view is deleted.
class ViewDeletedWaiter : public ::views::ViewObserver {
 public:
  explicit ViewDeletedWaiter(views::View* view);
  ~ViewDeletedWaiter() override;

  // Waits until the view is deleted.
  void Wait();

 private:
  // ViewObserver:
  void OnViewIsDeleting(views::View* observed_view) override;

  base::RunLoop run_loop_;
  base::ScopedObservation<views::View, views::ViewObserver> observation_{this};
};

// Waits until `expected_url` is loaded in `web_view`. Supports the case when
// the page load happens in a different `content::WebContents` than the one
// displayed at the moment we start waiting.
class PickerLoadStopWaiter : public content::WebContentsObserver {
 public:
  enum class Mode {
    kWaitUntilUrlLoaded,
    kCheckUrlAtNextLoad,
  };

  PickerLoadStopWaiter(views::WebView* web_view,
                       const GURL& expected_url,
                       Mode wait_mode = Mode::kWaitUntilUrlLoaded);

  // content::WebContentsObserver:
  void DidStopLoading() override;

  // Waits until the navigation is completed.
  void Wait();

 private:
  void OnWebContentsAttached(views::WebView* web_view);
  bool ShouldKeepWaiting() const;

  const raw_ref<views::WebView> web_view_;
  const GURL expected_url_;
  const Mode wait_mode_;

  base::RunLoop run_loop_;
  base::CallbackListSubscription web_contents_attached_subscription_;
};

// View intended to be used in pixel tests to render a specific step in the
// Profile Picker view.
// Unlike the classic `ProfilePickerView`, this one does not interact with the
// global state nor try to reuse previously existing views. It does not
// guarantee that some unsupported state can't be reached, so user discretion
// is advised.
class ProfileManagementStepTestView : public ProfilePickerView {
 public:
  using StepControllerFactory =
      base::RepeatingCallback<std::unique_ptr<ProfileManagementStepController>(
          ProfilePickerWebContentsHost* host)>;

  explicit ProfileManagementStepTestView(
      ProfilePicker::Params&& params,
      ProfileManagementFlowController::Step step,
      StepControllerFactory step_controller_factory);
  ~ProfileManagementStepTestView() override;

  // Returns when the content of the step reached the first non-empty paint.
  void ShowAndWait(std::optional<gfx::Size> view_size = std::nullopt);

 protected:
  std::unique_ptr<ProfileManagementFlowController> CreateFlowController(
      Profile* picker_profile,
      ClearHostClosure clear_host_callback) override;

 private:
  const ProfileManagementFlowController::Step step_;
  StepControllerFactory step_controller_factory_;

  base::RunLoop run_loop_;
};

// Mock implementation of the `ProfilePickerWebContentsHost`.
class MockProfilePickerWebContentsHost : public ProfilePickerWebContentsHost {
 public:
  MockProfilePickerWebContentsHost();
  ~MockProfilePickerWebContentsHost();

  MOCK_METHOD(void,
              ShowScreen,
              (content::WebContents * contents,
               const GURL& url,
               base::OnceClosure navigation_finished_closure));
  MOCK_METHOD(void,
              ShowScreenInPickerContents,
              (const GURL& url, base::OnceClosure navigation_finished_closure));
  MOCK_METHOD(bool, ShouldUseDarkColors, (), (const));
  MOCK_METHOD(content::WebContents*, GetPickerContents, (), (const));
  MOCK_METHOD(void, SetNativeToolbarVisible, (bool visible));
  MOCK_METHOD(SkColor, GetPreferredBackgroundColor, (), (const));
  MOCK_METHOD(content::WebContentsDelegate*, GetWebContentsDelegate, ());
  MOCK_METHOD(web_modal::WebContentsModalDialogHost*,
              GetWebContentsModalDialogHost,
              ());
  MOCK_METHOD(void, Reset, (StepSwitchFinishedCallback callback));
  MOCK_METHOD(void,
              ShowForceSigninErrorDialog,
              (const ForceSigninUIError& error, bool success));
};

#endif  // CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILE_PICKER_VIEW_TEST_UTILS_H_