File: interactive_views_test.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 (942 lines) | stat: -rw-r--r-- 40,222 bytes parent folder | download | duplicates (5)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
// 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 UI_VIEWS_INTERACTION_INTERACTIVE_VIEWS_TEST_H_
#define UI_VIEWS_INTERACTION_INTERACTIVE_VIEWS_TEST_H_

#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <variant>

#include "base/callback_list.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/interactive_test.h"
#include "ui/base/interaction/interactive_test_definitions.h"
#include "ui/base/interaction/interactive_test_internal.h"
#include "ui/base/metadata/metadata_types.h"
#include "ui/base/test/ui_controls.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/interaction/interaction_test_util_mouse.h"
#include "ui/views/interaction/interactive_views_test_internal.h"
#include "ui/views/interaction/polling_view_observer.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/view.h"
#include "ui/views/view_utils.h"

namespace views::test {

// Provides interactive test functionality for Views.
//
// Interactive tests use InteractionSequence, ElementTracker, and
// InteractionTestUtil to provide a common library of concise test methods. This
// convenience API is nicknamed "Kombucha" (see
// //chrome/test/interaction/README.md for more information).
//
// This class is not a test fixture; it is a mixin that can be added to existing
// test classes using `InteractiveViewsTestT<T>` - or just use
// `InteractiveViewsTest`, which *is* a test fixture (preferred; see below).
//
// To use Kombucha for in-process browser tests, instead see:
// //chrome/test/interaction/interactive_browser_test.h
class InteractiveViewsTestApi : public ui::test::InteractiveTestApi {
 public:
  InteractiveViewsTestApi();
  ~InteractiveViewsTestApi() override;

  // Returns an object that can be used to inject mouse inputs. Generally,
  // prefer to use methods like MoveMouseTo, MouseClick, and DragMouseTo.
  InteractionTestUtilMouse& mouse_util() { return test_impl().mouse_util(); }

  // Shorthand to convert a tracked element into a View. The element should be
  // a views::TrackedElementViews and of type `T`.
  template <typename T = View>
  static T* AsView(ui::TrackedElement* el);
  template <typename T = View>
  static const T* AsView(const ui::TrackedElement* el);

  // Runs a test InteractionSequence from a series of Steps or StepBuilders with
  // RunSynchronouslyForTesting(). Hooks both the completed and aborted
  // callbacks to ensure completion, and prints an error on failure. The context
  // will be pulled from `context_widget()`.
  template <typename... Args>
    requires(sizeof...(Args) > 0 &&
             (ui::test::internal::IsValueOrRvalue<Args> && ...))
  bool RunTestSequence(Args&&... steps);

  // Naming views:
  //
  // The following methods name a view (to be referred to later in the test
  // sequence by name) based on some kind of rule or relationship. The View need
  // not have an ElementIdentifier assigned ahead of time, so this is useful for
  // finding random or dynamically-created views.
  //
  // For example:
  //
  //   RunTestSequence(
  //     ...
  //     NameView(kThirdTabName,
  //              base::BindLambdaForTesting([&](){
  //                return browser_view->tabstrip()->tab_at(3);
  //              }))
  //     WithElement(kThirdTabName, ...)
  //     ...
  //   );
  //
  // How the view is named will depend on which version of the method you use;
  // the

  // Determines if a view matches some predicate.
  using ViewMatcher = base::RepeatingCallback<bool(const View*)>;

  // Specifies a View not relative to any particular other View.
  using AbsoluteViewSpecifier = std::variant<
      // Specify a view that is known at the time the sequence is created. The
      // View must persist until the step executes.
      View*,
      // Specify a view pointer that will be valid by the time the step
      // executes. Use `std::ref()` to wrap the pointer that will receive the
      // value.
      std::reference_wrapper<View*>,
      // Find and return a view based on an arbitrary rule.
      base::OnceCallback<View*()>>;

  // Specifies a view relative to its parent.
  using ChildViewSpecifier = std::variant<
      // The index of the child in the parent view. An out of bounds index will
      // generate an error.
      size_t,
      // Specifies a filter that is applied to the children; the first child
      // view to satisfy the filter (i.e. return true) is named.
      ViewMatcher>;

  // Methods that name views.

  // Names a view relative to another view `relative_to` based on an arbitrary
  // rule. The resulting view does not need to be a descendant (or even an
  // ancestor) of `relative_to`.
  //
  // Your `find_callback` should take a pointer to a View or a derived type and
  // return a pointer to a View or derived type.
  template <typename C,
            typename V = internal::ViewArgType<0, C>,
            typename R = std::remove_cv_t<
                std::remove_pointer_t<ui::test::internal::ReturnTypeOf<C>>>>
    requires ui::test::internal::HasSignature<C, R*(V*)>
  [[nodiscard]] static StepBuilder NameViewRelative(
      ElementSpecifier relative_to,
      std::string_view name,
      C&& find_callback);

  [[nodiscard]] static StepBuilder NameView(std::string_view name,
                                            AbsoluteViewSpecifier spec);

  [[nodiscard]] static StepBuilder NameChildView(ElementSpecifier parent,
                                                 std::string_view name,
                                                 ChildViewSpecifier spec);

  [[nodiscard]] static StepBuilder NameDescendantView(ElementSpecifier ancestor,
                                                      std::string_view name,
                                                      ViewMatcher matcher);

  // Names the `index` (0-indexed) child view of `parent` that is of type `V`.
  template <typename V>
    requires internal::IsView<V>
  [[nodiscard]] static StepBuilder NameChildViewByType(ElementSpecifier parent,
                                                       std::string_view name,
                                                       size_t index = 0);

  // Names the `index` (0-indexed) descendant view of `parent` in depth-first
  // traversal order that is of type `V`.
  template <typename V>
    requires internal::IsView<V>
  [[nodiscard]] static StepBuilder NameDescendantViewByType(
      ElementSpecifier ancestor,
      std::string_view name,
      size_t index = 0);

  // As WithElement(), but `view` should resolve to a TrackedElementViews
  // wrapping a view of type `V`.
  template <typename F, typename V = internal::ViewArgType<0, F>>
    requires ui::test::internal::HasSignature<F, void(V*)>
  [[nodiscard]] static StepBuilder WithView(ElementSpecifier view,
                                            F&& function);

  // As CheckElement(), but `view` should resolve to a TrackedElementViews
  // wrapping a view of type `V`.
  template <typename F, typename V = internal::ViewArgType<0, F>>
  // NOLINTNEXTLINE(readability/casting)
    requires ui::test::internal::HasSignature<F, bool(V*)>
  [[nodiscard]] static StepBuilder CheckView(ElementSpecifier view, F&& check);

  // As CheckView(), but checks that the result of calling `function` on `view`
  // matches `matcher`. If not, the mismatch is printed and the test fails.
  //
  // `matcher` can be any type `M` that resolves or converts to type
  // `Matcher<R>`.
  template <typename F,
            typename M,
            typename R = ui::test::internal::ReturnTypeOf<F>,
            typename V = internal::ViewArgType<0, F>>
    requires ui::test::internal::HasSignature<F, R(V*)>
  [[nodiscard]] static StepBuilder CheckView(ElementSpecifier view,
                                             F&& function,
                                             M&& matcher);

  // As CheckView() but checks that `matcher` matches the value returned by
  // calling `property` on `view`. On failure, logs the matcher error and fails
  // the test.
  //
  // `V` is the View class, `R` is the type of the property being checked, and
  // `M` is the type of the matcher, or the constant that will be converted to
  // an equality matcher (e.g. `1` would be converted to `testing::Eq(1)`).
  //
  // `matcher` must resolve or convert to type `Matcher<R>`.
  template <typename V, typename R, typename M>
    requires internal::IsView<V>
  [[nodiscard]] static StepBuilder CheckViewProperty(ElementSpecifier view,
                                                     R (V::*property)() const,
                                                     M&& matcher);

  // Adds a step that waits for `property` to match `matcher` on `view`. The
  // `add_listener` method must be specified in this version of the function.
  //
  // `V` is the View class, `R` is the type of the property being checked, and
  // `M` is the type of the matcher, or the constant that will be converted to
  // an equality matcher (e.g. `1` would be converted to `testing::Eq(1)`).
  //
  // NOTE: Prefer using the `WaitForViewProperty` macro as it is more concise
  // and creates its own unique event (so you don't have to specify one). For
  // example, the following are equivalent:
  // ```
  //   WaitForViewPropertyCallback(
  //       kMyViewId,
  //       &View::GetEnabled,
  //       &View::AddEnabledChangedListener,
  //       true,
  //       kMyCustomEventType)
  // ```
  // and:
  // ```
  //   WaitForViewProperty(kMyViewId, View, Enabled, true)
  // ```
  //
  // Usage notes:
  // - Do not use with the "Visible" property; use `WaitForShow()` and
  //   `WaitForHide()` instead.
  // - Specify a unique `event` to avoid collisions between parallel or
  //   subsequent wait steps.
  template <typename V, typename R, typename M>
    requires internal::IsView<V>
  [[nodiscard]] static MultiStep WaitForViewPropertyCallback(
      ElementSpecifier view,
      R (V::*property)() const,
      base::CallbackListSubscription (V::*add_listener)(
          ui::metadata::PropertyChangedCallback),
      M&& matcher,
      ui::CustomElementEventType event);

  // Creates a state observer with `id` which polls the view in the current
  // context with `view_id`. If the view is present, uses `callback` to update
  // the state value; otherwise the value is `std::nullopt` (the actual state
  // value is of type `std::optional<T>`).
  //
  // The element, if present, must resolve to a View of the correct type, or the
  // test will fail.
  //
  // See `PollState()` and `PollElement()` for usage details and caveats.
  // Specifically be aware that polling may miss a transient state; prefer to
  // send a custom event or use `WaitForViewPropertyCallback()` if possible.
  template <typename T, typename V, typename C>
    requires internal::IsView<V> &&
             ui::test::internal::HasSignature<C, T(const V*)>
  [[nodiscard]] StepBuilder PollView(
      ui::test::StateIdentifier<PollingViewObserver<T, V>> id,
      ui::ElementIdentifier view_id,
      C&& callback,
      base::TimeDelta polling_interval = ui::test::PollingStateObserver<
          std::optional<T>>::kDefaultPollingInterval);

  // Creates a state observer with `id` which polls `property` on the view in
  // the current context with `view_id`. If the view is not present, the state
  // value will be set to `std::nullopt` (the actual state value is of type
  // `std::optional<T>`).
  //
  // The element, if present, must resolve to a View of the correct type, or the
  // test will fail.
  //
  // See `PollState()` and `PollElement()` for usage details and caveats.
  // Specifically be aware that polling may miss a transient state; prefer to
  // send a custom event or use `WaitForViewPropertyCallback()` if possible.
  template <typename R, typename V, typename T = std::remove_cvref_t<R>>
    requires internal::IsView<V>
  [[nodiscard]] StepBuilder PollViewProperty(
      ui::test::StateIdentifier<PollingViewPropertyObserver<T, V>> id,
      ui::ElementIdentifier view_id,
      R (V::*property)() const,
      base::TimeDelta polling_interval = ui::test::PollingStateObserver<
          std::optional<T>>::kDefaultPollingInterval);

  // Scrolls `view` into the visible viewport if it is currently scrolled
  // outside its container. The view must be otherwise present and visible.
  // Has no effect if the view is not in a scroll container.
  [[nodiscard]] static StepBuilder ScrollIntoView(ElementSpecifier view);

  // Indicates that the center point of the target element should be used for a
  // mouse move.
  struct CenterPoint {};

  // Function that returns a destination for a move or drag.
  using AbsolutePositionCallback = base::OnceCallback<gfx::Point()>;

  // Specifies an absolute position for a mouse move or drag that does not need
  // a reference element.
  using AbsolutePositionSpecifier = std::variant<
      // Use this specific position. This value is stored when the sequence is
      // created; use gfx::Point* if you want to capture a point during sequence
      // execution.
      gfx::Point,
      // As above, but the position is read from the reference on execution
      // instead of copied when the test sequence is constructed. Use this when
      // you want to calculate and cache a point during test execution for later
      // use. The pointer must remain valid through the end of the test.
      std::reference_wrapper<gfx::Point>,
      // Use the return value of the supplied callback
      AbsolutePositionCallback>;

  // Specifies how the `reference_element` should be used (or not) to generate a
  // target point for a mouse move.
  using RelativePositionCallback =
      base::OnceCallback<gfx::Point(ui::TrackedElement* reference_element)>;

  // Specifies how the target position of a mouse operation (in screen
  // coordinates) will be determined.
  using RelativePositionSpecifier = std::variant<
      // Default to the centerpoint of the reference element, which should be a
      // views::View.
      CenterPoint,
      // Use the return value of the supplied callback.
      RelativePositionCallback>;

  // Move the mouse to the specified `position` in screen coordinates. The
  // `reference` element will be used based on how `position` is specified.
  //
  // This verb is only available in interactive test suites; see
  // `RequireInteractiveTest()`.
  [[nodiscard]] StepBuilder MoveMouseTo(AbsolutePositionSpecifier position);
  [[nodiscard]] StepBuilder MoveMouseTo(
      ElementSpecifier reference,
      RelativePositionSpecifier position = CenterPoint());

  // Clicks mouse button `button` at the current cursor position.
  //
  // This verb is only available in interactive test suites; see
  // `RequireInteractiveTest()`.
  //
  // The optional `modifier_keys` parameter can be set to any combination of
  // `ui_controls::AcceleratorState`.
  [[nodiscard]] StepBuilder ClickMouse(
      ui_controls::MouseButton button = ui_controls::LEFT,
      bool release = true,
      int modifier_keys = ui_controls::kNoAccelerator);

  // Depresses the left mouse button at the current cursor position and drags to
  // the target `position`. The `reference` element will be used based on how
  // `position` is specified.
  //
  // This verb is only available in interactive test suites; see
  // `RequireInteractiveTest()`.
  [[nodiscard]] StepBuilder DragMouseTo(AbsolutePositionSpecifier position,
                                        bool release = true);
  [[nodiscard]] StepBuilder DragMouseTo(
      ElementSpecifier reference,
      RelativePositionSpecifier position = CenterPoint(),
      bool release = true);

  // Releases the specified mouse button. Use when you previously called
  // ClickMouse() or DragMouseTo() with `release` = false.
  //
  // This verb is only available in interactive test suites; see
  // `RequireInteractiveTest()`.
  //
  // The optional `modifier_keys` parameter can be set to any combination of
  // `ui_controls::AcceleratorState`.
  [[nodiscard]] StepBuilder ReleaseMouse(
      ui_controls::MouseButton button = ui_controls::LEFT,
      int modifier_keys = ui_controls::kNoAccelerator);

  // As IfElement(), but `condition` takes a single argument that is a const
  // View pointer. If `element` is not a view of type V, then the test will
  // fail.
  template <typename C, typename V = internal::ViewArgType<0, C>>
    requires ui::test::internal::HasSignature<
        C,
        bool(const V*)>  // NOLINT(readability/casting)
  [[nodiscard]] static StepBuilder IfView(ElementSpecifier element,
                                          C&& condition,
                                          ThenBlock then_steps,
                                          ElseBlock else_steps = Else());

  // As IfElementMatches(), but `function` takes a single argument that is a
  // const View pointer. If `element` is not a view of type V, then the test
  // will fail.
  template <typename F,
            typename M,
            typename R = ui::test::internal::ReturnTypeOf<F>,
            typename V = internal::ViewArgType<0, F>>
    requires ui::test::internal::HasSignature<F, R(const V*)>
  [[nodiscard]] static StepBuilder IfViewMatches(ElementSpecifier element,
                                                 F&& function,
                                                 M&& matcher,
                                                 ThenBlock then_steps,
                                                 ElseBlock else_steps = Else());

  // Executes `then_steps` if `property` of the view `element` (which must be of
  // the correct View type) matches `matcher`, otherwise executes `else_steps`.
  //
  // Note that bare literal strings cannot be passed as `matcher` for properties
  // with string values, you will need to either explicitly pass a
  // std::[u16]string or explicitly construct a testing::Eq matcher.
  template <typename R, typename M, typename V>
    requires internal::IsView<V>
  [[nodiscard]] static StepBuilder IfViewPropertyMatches(
      ElementSpecifier element,
      R (V::*property)() const,
      M&& matcher,
      ThenBlock then_steps,
      ElseBlock else_steps = Else());

  // On some platforms, context menu operations run in an OS message pump that
  // ignores non-input events, so async Kombucha does not work, as the posted
  // tasks won't be run.
  //
  // Wrap any context menu operation (including the triggering event, if it is a
  // `ClickMouse(ui_controls::RIGHT)`) up to and including the step that closes
  // the context menu in this modifier. If your test fails to close the context
  // menu, it may hang, as there is no single automated way to clean up context
  // menus in Views.
  template <typename... Args>
  [[nodiscard]] static MultiStep MayInvolveNativeContextMenu(Args&&... args) {
#if BUILDFLAG(IS_MAC)
    return WithoutDelay(std::forward<Args>(args)...);
#else
    return Steps(std::forward<Args>(args)...);
#endif
  }

  // Sets the context widget. Must be called before RunTestSequence() or any of
  // the mouse functions.
  void SetContextWidget(Widget* context_widget);
  Widget* context_widget() { return context_widget_.get(); }

 protected:
  explicit InteractiveViewsTestApi(
      std::unique_ptr<internal::InteractiveViewsTestPrivate> private_test_impl);

 private:
  using FindViewCallback = base::OnceCallback<View*(View*)>;
  static FindViewCallback GetFindViewCallback(AbsoluteViewSpecifier spec);
  static FindViewCallback GetFindViewCallback(ChildViewSpecifier spec);

  // Recursively finds an element that matches `matcher` starting with (but
  // not including) `from`. If `recursive` is true, searches all descendants,
  // otherwise searches children.
  static views::View* FindMatchingView(const views::View* from,
                                       ViewMatcher& matcher,
                                       bool recursive);

  // Converts a *PositionSpecifier to an appropriate *PositionCallback.
  static RelativePositionCallback GetPositionCallback(
      AbsolutePositionSpecifier spec);
  static RelativePositionCallback GetPositionCallback(
      RelativePositionSpecifier spec);

  internal::InteractiveViewsTestPrivate& test_impl() {
    return static_cast<internal::InteractiveViewsTestPrivate&>(
        InteractiveTestApi::private_test_impl());
  }

  // Creates the follow-up step for a mouse action.
  StepBuilder CreateMouseFollowUpStep(std::string_view description);

  base::WeakPtr<Widget> context_widget_;
};

// Template that adds InteractiveViewsTestApi to any test fixture. Prefer to use
// InteractiveViewsTest unless you specifically need to inherit from another
// test class.
//
// You must call SetContextWidget() before using RunTestSequence() or any of the
// mouse actions.
//
// See //chrome/test/interaction/README.md for usage.
template <typename T>
class InteractiveViewsTestT : public T, public InteractiveViewsTestApi {
 public:
  template <typename... Args>
  explicit InteractiveViewsTestT(Args&&... args)
      : T(std::forward<Args>(args)...) {}

  ~InteractiveViewsTestT() override = default;

 protected:
  void SetUp() override {
    T::SetUp();
    private_test_impl().DoTestSetUp();
  }

  void TearDown() override {
    private_test_impl().DoTestTearDown();
    T::TearDown();
  }
};

// Convenience test fixture for Views tests that supports
// InteractiveViewsTestApi.
//
// You must call SetContextWidget() before using RunTestSequence() or any of the
// mouse actions.
//
// See //chrome/test/interaction/README.md for usage.
using InteractiveViewsTest = InteractiveViewsTestT<ViewsTestBase>;

// Template definitions:

// static
template <class T>
T* InteractiveViewsTestApi::AsView(ui::TrackedElement* el) {
  auto* const views_el = el->AsA<TrackedElementViews>();
  CHECK(views_el);
  T* const view = AsViewClass<T>(views_el->view());
  CHECK(view);
  return view;
}

// static
template <class T>
const T* InteractiveViewsTestApi::AsView(const ui::TrackedElement* el) {
  const auto* const views_el = el->AsA<TrackedElementViews>();
  CHECK(views_el);
  const T* const view = AsViewClass<T>(views_el->view());
  CHECK(view);
  return view;
}

template <typename... Args>
  requires(sizeof...(Args) > 0 &&
           (ui::test::internal::IsValueOrRvalue<Args> && ...))
bool InteractiveViewsTestApi::RunTestSequence(Args&&... steps) {
  return RunTestSequenceInContext(
      ElementTrackerViews::GetContextForWidget(context_widget()),
      std::forward<Args>(steps)...);
}

// static
template <typename C, typename V, typename R>
  requires ui::test::internal::HasSignature<C, R*(V*)>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::NameViewRelative(
    ElementSpecifier relative_to,
    std::string_view name,
    C&& find_callback) {
  StepBuilder builder;
  builder.SetDescription(
      base::StringPrintf("NameViewRelative( \"%s\" )", name.data()));
  ui::test::internal::SpecifyElement(builder, relative_to);
  builder.SetMustBeVisibleAtStart(true);
  builder.SetStartCallback(base::BindOnce(
      [](base::OnceCallback<R*(V*)> find_callback, std::string name,
         ui::InteractionSequence* seq, ui::TrackedElement* el) {
        V* relative_to = nullptr;
        if (el->identifier() !=
            ui::test::internal::kInteractiveTestPivotElementId) {
          if (!el->IsA<TrackedElementViews>()) {
            LOG(ERROR) << "NameView(): Target element is not a View.";
            seq->FailForTesting();
            return;
          }
          View* const view = el->AsA<TrackedElementViews>()->view();
          if (!IsViewClass<V>(view)) {
            LOG(ERROR) << "NameView(): Target View is of type "
                       << view->GetClassName() << " but expected "
                       << V::kViewClassName;
            seq->FailForTesting();
            return;
          }
          relative_to = AsViewClass<V>(view);
        }
        View* const result = std::move(find_callback).Run(relative_to);
        if (!result) {
          LOG(ERROR) << "NameView(): No View found.";
          seq->FailForTesting();
          return;
        }
        auto* const target_element =
            ElementTrackerViews::GetInstance()->GetElementForView(
                result, /* assign_temporary_id =*/true);
        if (!target_element) {
          LOG(ERROR)
              << "NameView(): attempting to name View that is not visible.";
          seq->FailForTesting();
          return;
        }
        seq->NameElement(target_element, name);
      },
      ui::test::internal::MaybeBind(std::forward<C>(find_callback)),
      std::string(name)));
  return builder;
}

// static
template <typename F, typename V>
  requires ui::test::internal::HasSignature<F, void(V*)>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::WithView(
    ElementSpecifier view,
    F&& function) {
  StepBuilder builder;
  builder.SetDescription("WithView()");
  ui::test::internal::SpecifyElement(builder, view);
  builder.SetMustBeVisibleAtStart(true);
  builder.SetStartCallback(base::BindOnce(
      [](base::OnceCallback<void(V*)> function, ui::InteractionSequence* seq,
         ui::TrackedElement* el) { std::move(function).Run(AsView<V>(el)); },
      ui::test::internal::MaybeBind(std::forward<F>(function))));
  return builder;
}

// static
template <typename C, typename V>
  requires ui::test::internal::HasSignature<
      C,
      bool(const V*)>  // NOLINT(readability/casting)
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::IfView(
    ElementSpecifier element,
    C&& condition,
    ThenBlock then_steps,
    ElseBlock else_steps) {
  return std::move(
      IfElement(element,
                base::BindOnce(
                    [](base::OnceCallback<bool(const V*)> condition,
                       const ui::InteractionSequence* seq,
                       const ui::TrackedElement* el) {
                      const V* const view = el ? AsView<V>(el) : nullptr;
                      return std::move(condition).Run(view);
                    },
                    ui::test::internal::MaybeBind(std::forward<C>(condition))),
                std::move(then_steps), std::move(else_steps))
          .SetDescription("IfView()"));
}

// static
template <typename F, typename M, typename R, typename V>
  requires ui::test::internal::HasSignature<F, R(const V*)>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::IfViewMatches(
    ElementSpecifier element,
    F&& function,
    M&& matcher,
    ThenBlock then_steps,
    ElseBlock else_steps) {
  return std::move(
      IfElementMatches(
          element,
          base::BindOnce(
              [](base::OnceCallback<R(const V*)> condition,
                 const ui::InteractionSequence* seq,
                 const ui::TrackedElement* el) {
                const V* const view = el ? AsView<V>(el) : nullptr;
                return std::move(condition).Run(view);
              },
              ui::test::internal::MaybeBind(std::forward<F>(function))),
          testing::Matcher<ui::test::internal::MatcherTypeFor<R>>(
              std::forward<M>(matcher)),
          std::move(then_steps), std::move(else_steps))
          .SetDescription("IfViewMatches()"));
}

// static
template <typename R, typename M, typename V>
  requires internal::IsView<V>
ui::InteractionSequence::StepBuilder
InteractiveViewsTestApi::IfViewPropertyMatches(ElementSpecifier element,
                                               R (V::*property)() const,
                                               M&& matcher,
                                               ThenBlock then_steps,
                                               ElseBlock else_steps) {
  using Return = std::remove_cvref_t<R>;
  base::OnceCallback<Return(const V*)> function = base::BindOnce(
      [](R (V::*property)() const, const V* view) -> Return {
        return (view->*property)();
      },
      std::move(property));
  return std::move(IfViewMatches(element, std::move(function),
                                 std::forward<M>(matcher),
                                 std::move(then_steps), std::move(else_steps))
                       .SetDescription("IfViewPropertyMatches()"));
}

// static
template <typename V>
  requires internal::IsView<V>
ui::InteractionSequence::StepBuilder
InteractiveViewsTestApi::NameChildViewByType(ElementSpecifier parent,
                                             std::string_view name,
                                             size_t index) {
  return std::move(NameChildView(parent, name,
                                 base::BindRepeating(
                                     [](size_t& index, const View* view) {
                                       if (IsViewClass<V>(view)) {
                                         if (index == 0) {
                                           return true;
                                         }
                                         --index;
                                       }
                                       return false;
                                     },
                                     base::OwnedRef(index)))
                       .SetDescription(base::StringPrintf(
                           "NameChildViewByType<%s>( \"%s\" %zu )",
                           V::kViewClassName, name.data(), index)));
}

// static
template <typename V>
  requires internal::IsView<V>
ui::InteractionSequence::StepBuilder
InteractiveViewsTestApi::NameDescendantViewByType(ElementSpecifier ancestor,
                                                  std::string_view name,
                                                  size_t index) {
  return std::move(NameDescendantView(ancestor, name,
                                      base::BindRepeating(
                                          [](size_t& index, const View* view) {
                                            if (IsViewClass<V>(view)) {
                                              if (index == 0) {
                                                return true;
                                              }
                                              --index;
                                            }
                                            return false;
                                          },
                                          base::OwnedRef(index)))
                       .SetDescription(base::StringPrintf(
                           "NameDescendantViewByType<%s>( \"%s\" %zu )",
                           V::kViewClassName, name.data(), index)));
}

// static
template <typename F, typename V>
// NOLINTNEXTLINE(readability/casting)
  requires ui::test::internal::HasSignature<F, bool(V*)>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::CheckView(
    ElementSpecifier view,
    F&& check) {
  return CheckView(view, std::forward<F>(check), true);
}

// static
template <typename F, typename M, typename R, typename V>
  requires ui::test::internal::HasSignature<F, R(V*)>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::CheckView(
    ElementSpecifier view,
    F&& function,
    M&& matcher) {
  StepBuilder builder;
  builder.SetDescription("CheckView()");
  ui::test::internal::SpecifyElement(builder, view);
  using MatcherType = ui::test::internal::MatcherTypeFor<R>;
  builder.SetStartCallback(base::BindOnce(
      [](base::OnceCallback<R(V*)> function,
         testing::Matcher<MatcherType> matcher,
         ui::InteractionSequence* seq, ui::TrackedElement* el) {
        if (!ui::test::internal::MatchAndExplain(
                "CheckView()", matcher,
                MatcherType(std::move(function).Run(AsView<V>(el))))) {
          seq->FailForTesting();
        }
      },
      ui::test::internal::MaybeBind(std::forward<F>(function)),
      testing::Matcher<MatcherType>(std::forward<M>(matcher))));
  return builder;
}

// static
template <typename V, typename R, typename M>
  requires internal::IsView<V>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::CheckViewProperty(
    ElementSpecifier view,
    R (V::*property)() const,
    M&& matcher) {
  StepBuilder builder;
  builder.SetDescription("CheckViewProperty()");
  ui::test::internal::SpecifyElement(builder, view);
  using MatcherType = ui::test::internal::MatcherTypeFor<R>;
  builder.SetStartCallback(base::BindOnce(
      [](R (V::*property)() const, testing::Matcher<MatcherType> matcher,
         ui::InteractionSequence* seq, ui::TrackedElement* el) {
        if (!ui::test::internal::MatchAndExplain(
                "CheckViewProperty()", matcher,
                MatcherType((AsView<V>(el)->*property)()))) {
          seq->FailForTesting();
        }
      },
      property, testing::Matcher<MatcherType>(std::forward<M>(matcher))));
  return builder;
}

// static
template <typename V, typename R, typename M>
  requires internal::IsView<V>
ui::test::InteractiveTestApi::MultiStep
InteractiveViewsTestApi::WaitForViewPropertyCallback(
    ElementSpecifier view,
    R (V::*property)() const,
    base::CallbackListSubscription (V::*add_listener)(
        ui::metadata::PropertyChangedCallback),
    M&& matcher,
    ui::CustomElementEventType event_type) {
  // Need to make this ref-counted to ensure it lives long enough to actually
  // listen for the state change.
  using RefCountedSubscription =
      scoped_refptr<base::RefCountedData<base::CallbackListSubscription>>;
  RefCountedSubscription subscription =
      base::MakeRefCounted<RefCountedSubscription::element_type>();

  // The first step will check the property, and either immediately send the
  // event or install the observer that will send the event when the state
  // achieves the correct value.
  using MatcherType = ui::test::internal::MatcherTypeFor<R>;
  auto observe_property = base::BindOnce(
      [](RefCountedSubscription subscription, R (V::*property)() const,
         base::CallbackListSubscription (V::*add_listener)(
             ui::metadata::PropertyChangedCallback),
         ui::CustomElementEventType event_type,
         testing::Matcher<MatcherType> matcher,
         ui::TrackedElement* el) {
        auto* const view = AsView<V>(el);
        if (matcher.Matches(MatcherType((view->*property)()))) {
          // Property is already in the desired state, send event immediately.
          ui::ElementTracker::GetFrameworkDelegate()->NotifyCustomEvent(
              el, event_type);
        } else {
          // Watch the property for a value that satisfies the matcher.
          subscription->data = (view->*add_listener)(base::BindRepeating(
              [](V* view, R (V::*property)() const,
                 ui::CustomElementEventType event_type,
                 testing::Matcher<MatcherType> matcher) {
                if (matcher.Matches(MatcherType((view->*property)()))) {
                  ElementTrackerViews::GetInstance()->NotifyCustomEvent(
                      event_type, view);
                }
              },
              view, property, event_type, std::move(matcher)));
        }
      },
      subscription, property, add_listener, event_type,
      testing::Matcher<MatcherType>(std::forward<M>(matcher)));

  auto steps = Steps(
      AfterShow(view, std::move(observe_property)).SetMustRemainVisible(true),
      AfterEvent(view, event_type, [subscription]() {
        // Need to reference subscription by value so that it is
        // not discarded until this step runs or the sequence
        // fails.
        subscription->data = base::CallbackListSubscription();
      }));
  AddDescriptionPrefix(
      steps, base::StrCat({"WaitForProperty( ", event_type.GetName(), ", )"}));
  return steps;
}

// Waits for a property named `Property` to have a value that matches `matcher`
// on View `view` of View class `Class`. Convenience method for
// `InteractiveViewsTestApi::WaitForClassPropertyCallback`.
//
// Do not use with the "Visible" property; use `WaitForShow()` or
// `WaitForHide()` instead.
#define WaitForViewProperty(view, Class, Property, matcher)                    \
  []() {                                                                       \
    DEFINE_MACRO_CUSTOM_ELEMENT_EVENT_TYPE(__FILE__, __LINE__,                 \
                                           kWaitFor##Property##Event);         \
    return WaitForViewPropertyCallback((view), &Class::Get##Property,          \
                                       &Class::Add##Property##ChangedCallback, \
                                       (matcher), kWaitFor##Property##Event);  \
  }()

template <typename T, typename V, typename C>
  requires internal::IsView<V> &&
           ui::test::internal::HasSignature<C, T(const V*)>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::PollView(
    ui::test::StateIdentifier<PollingViewObserver<T, V>> id,
    ui::ElementIdentifier view_id,
    C&& callback,
    base::TimeDelta polling_interval) {
  using Cb = PollingViewObserver<T, V>::PollViewCallback;
  Cb cb = ui::test::internal::MaybeBindRepeating(std::forward<C>(callback));
  auto step =
      WithElement(ui::test::internal::kInteractiveTestPivotElementId,
                  base::BindOnce(
                      [](InteractiveViewsTestApi* api, ui::ElementIdentifier id,
                         ui::ElementIdentifier view_id, Cb callback,
                         base::TimeDelta polling_interval,
                         ui::InteractionSequence* seq, ui::TrackedElement* el) {
                        api->test_impl().AddStateObserver(
                            id, el->context(),
                            std::make_unique<PollingViewObserver<T, V>>(
                                view_id,
                                seq->IsCurrentStepInAnyContextForTesting()
                                    ? std::nullopt
                                    : std::make_optional(el->context()),
                                std::move(callback), polling_interval));
                      },
                      base::Unretained(this), id.identifier(), view_id, cb,
                      polling_interval));
  step.SetDescription(
      base::StringPrintf("PollView(%s)", view_id.GetName().c_str()));
  return step;
}

template <typename R, typename V, typename T>
  requires internal::IsView<V>
ui::InteractionSequence::StepBuilder InteractiveViewsTestApi::PollViewProperty(
    ui::test::StateIdentifier<PollingViewPropertyObserver<T, V>> id,
    ui::ElementIdentifier view_id,
    R (V::*property)() const,
    base::TimeDelta polling_interval) {
  auto step = WithElement(
      ui::test::internal::kInteractiveTestPivotElementId,
      base::BindOnce(
          [](InteractiveViewsTestApi* api, ui::ElementIdentifier id,
             ui::ElementIdentifier view_id, R (V::*property)() const,
             base::TimeDelta polling_interval, ui::InteractionSequence* seq,
             ui::TrackedElement* el) {
            api->test_impl().AddStateObserver(
                id, el->context(),
                std::make_unique<PollingViewPropertyObserver<T, V>>(
                    view_id,
                    seq->IsCurrentStepInAnyContextForTesting()
                        ? std::nullopt
                        : std::make_optional(el->context()),
                    property, polling_interval));
          },
          base::Unretained(this), id.identifier(), view_id, property,
          polling_interval));
  step.SetDescription(
      base::StringPrintf("PollViewProperty(%s)", view_id.GetName().c_str()));
  return step;
}

}  // namespace views::test
#endif  // UI_VIEWS_INTERACTION_INTERACTIVE_VIEWS_TEST_H_