blob: 81871aeeadea82bdbd1a70caeb048b176d707c1e [file] [log] [blame]
Dana Frieda9e97492023-02-11 02:22:211// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_BASE_INTERACTION_INTERACTION_SEQUENCE_TEST_UTIL_H_
6#define UI_BASE_INTERACTION_INTERACTION_SEQUENCE_TEST_UTIL_H_
7
Dana Fried2d40c372024-11-07 23:03:508#include "base/test/bind.h"
Dana Frieda9e97492023-02-11 02:22:219#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
11#include "ui/base/interaction/interaction_sequence.h"
12
13namespace ui::test {
14
15template <typename M1,
16 typename M2,
17 typename M3,
18 typename M4,
19 typename M5,
20 typename M6 = decltype(testing::_),
21 typename M7 = decltype(testing::_)>
22auto SequenceAbortedMatcher(M1 step_idx_matcher,
23 M2 element_matcher,
24 M3 id_matcher,
25 M4 step_type_matcher,
26 M5 aborted_reason_matcher,
27 M6 step_description_matcher = testing::_,
28 M7 subsequence_failures_matcher = testing::_) {
29 return testing::AllOf(
30 testing::Field(&InteractionSequence::AbortedData::step_index,
31 (step_idx_matcher)),
32 testing::Field(&InteractionSequence::AbortedData::element,
33 (element_matcher)),
34 testing::Field(&InteractionSequence::AbortedData::element_id,
35 (id_matcher)),
36 testing::Field(&InteractionSequence::AbortedData::step_type,
37 (step_type_matcher)),
38 testing::Field(&InteractionSequence::AbortedData::aborted_reason,
39 (aborted_reason_matcher)),
40 testing::Field(&InteractionSequence::AbortedData::step_description,
41 (step_description_matcher)),
42 testing::Field(&InteractionSequence::AbortedData::subsequence_failures,
43 (subsequence_failures_matcher)));
Dana Fried8b3f47542024-08-13 16:39:0644
45// When waiting for mock callbacks in a run loop, post the trigger this far in
46// advance to allow any existing processing to finish. Needs to be long enough
47// that it won't be overtaken on slower machines.
48//
49// Note that this is Probably Bad and is only being used because we're trying to
50// coordinate between external signals and step callbacks without triggering the
51// external signals from the step callbacks.
52//
53// In the future we might want to rewrite these tests to do it other ways.
54#define _EXPECT_ASYNC_POST_HELPER(Block) \
55 base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( \
56 FROM_HERE, base::BindLambdaForTesting([&]() { Block; }), \
57 base::Milliseconds(100))
58
59#define _EXPECT_ASYNC_CALL_HELPER(Name, Call) \
60 EXPECT_CALL(Name, Call).WillOnce([&]() { \
61 if (++__call_count >= __kExpectedCount) { \
62 __run_loop.Quit(); \
63 } \
64 })
65
66#define EXPECT_ASYNC_CALL_IN_SCOPE(Name, Call, Block) \
67 { \
68 static constexpr int __kExpectedCount = 1; \
69 int __call_count = 0; \
70 base::RunLoop __run_loop(base::RunLoop::Type::kNestableTasksAllowed); \
71 _EXPECT_ASYNC_CALL_HELPER(Name, Call); \
72 _EXPECT_ASYNC_POST_HELPER(Block); \
73 __run_loop.Run(); \
74 EXPECT_CALL(Name, Run).Times(0); \
75 }
76
77#define EXPECT_ASYNC_CALLS_IN_SCOPE_2(Name1, Call1, Name2, Call2, Block) \
78 { \
79 static constexpr int __kExpectedCount = 2; \
80 int __call_count = 0; \
81 base::RunLoop __run_loop(base::RunLoop::Type::kNestableTasksAllowed); \
82 _EXPECT_ASYNC_CALL_HELPER(Name1, Call1); \
83 _EXPECT_ASYNC_CALL_HELPER(Name2, Call2); \
84 _EXPECT_ASYNC_POST_HELPER(Block); \
85 __run_loop.Run(); \
86 EXPECT_CALL(Name2, Run).Times(0); \
87 EXPECT_CALL(Name1, Run).Times(0); \
88 }
89
90#define EXPECT_ASYNC_CALLS_IN_SCOPE_3(Name1, Call1, Name2, Call2, Name3, \
91 Call3, Block) \
92 { \
93 static constexpr int __kExpectedCount = 3; \
94 int __call_count = 0; \
95 base::RunLoop __run_loop(base::RunLoop::Type::kNestableTasksAllowed); \
96 _EXPECT_ASYNC_CALL_HELPER(Name1, Call1); \
97 _EXPECT_ASYNC_CALL_HELPER(Name2, Call2); \
98 _EXPECT_ASYNC_CALL_HELPER(Name3, Call3); \
99 _EXPECT_ASYNC_POST_HELPER(Block); \
100 __run_loop.Run(); \
101 EXPECT_CALL(Name3, Run).Times(0); \
102 EXPECT_CALL(Name2, Run).Times(0); \
103 EXPECT_CALL(Name1, Run).Times(0); \
104 }
Dana Frieda9e97492023-02-11 02:22:21105}
106
107} // namespace ui::test
108
109#endif // UI_BASE_INTERACTION_INTERACTION_SEQUENCE_TEST_UTIL_H_