16#include <gtest/gtest.h>
17#include <AUI/Test/UI/Assertion/Empty.h>
18#include <AUI/View/AViewContainer.h>
19#include "UITestUtil.h"
24 bool mIncludeInvisibleViews =
false;
29 template<
typename T,
typename =
int>
30 struct ignores_visibility : std::false_type { };
33 struct ignores_visibility<T, decltype((T::IGNORE_VISIBILITY::value, 0))> : T::IGNORE_VISIBILITY { };
35 template<
class Assertion>
36 void performHintChecks(
const char* msg,
ASet<
_<AView>>& set) {
38 if constexpr (ignores_visibility<Assertion>::value) {
42 if (msg ==
nullptr) std::cout <<
"Assertion message is empty";
43 if constexpr(!std::is_same_v<Assertion, empty>) {
44 if (set.empty()) std::cout <<
"UIMatcher is empty so check is not performed";
54 if (current() ==
this) {
55 currentImpl() =
nullptr;
68 SCOPED_TRACE(
"no views selected");
75 mIncludeInvisibleViews =
true;
79 template<
class Action>
82 if (set.empty()) std::cout <<
"UIMatcher is empty so action is not performed";
112 auto mySet = toSet();
113 auto targets = matcher.toSet();
115 if (targets.size() != 1) {
116 throw AException(
"expected to match one view, matched {}"_format(mySet.size()));
119 throw AException(
"findNearestTo requires at least one element to match");
122 auto nearestToView = (*targets.begin());
123 auto nearestToPoint = glm::vec2(nearestToView->getPositionInWindow() + nearestToView->getSize());
124 auto target = std::min_element(mySet.begin(), mySet.end(), [&](
const _<AView>& lhs,
const _<AView>& rhs) {
125 float dst1 = glm::distance2(nearestToPoint, glm::vec2(lhs->getCenterPointInWindow()));
126 float dst2 = glm::distance2(nearestToPoint, glm::vec2(rhs->getCenterPointInWindow()));
129 EXPECT_TRUE(target != mySet.end());
131 class ToOneMatcher:
public IMatcher {
133 explicit ToOneMatcher(
_<AView> view) : mView(std::move(view)) {}
135 bool matches(
const _<AView>& view)
override {
136 return view == mView;
142 return { _new<ToOneMatcher>(std::move(*target)) };
146 template<
class Assertion>
147 UIMatcher& check(Assertion&& assertion,
const char* msg =
"no msg") {
148 mIncludeInvisibleViews = ignores_visibility<Assertion>::value;
152 performHintChecks<Assertion>(msg, set);
153 for (
auto& s : set) {
162 struct ParentMatcher:
public IMatcher {
166 ParentMatcher(
const _<IMatcher>& childMatcher) : childMatcher(childMatcher) {}
168 bool matches(
const _<AView>& view)
override {
169 if (
auto container = _cast<AViewContainer>(view)) {
170 for (
const auto& childView : container) {
171 if (childMatcher->matches(childView))
return true;
177 return { _new<ParentMatcher>(mMatcher) };
183 struct ChildMatcher:
public IMatcher {
187 ChildMatcher(
const _<IMatcher>& childMatcher) : childMatcher(childMatcher) {}
189 bool matches(
const _<AView>& view)
override {
194 return { _new<ChildMatcher>(mMatcher) };
199 template<
class BinaryOperator>
200 struct BinaryOperatorMatcher:
public IMatcher {
207 bool matches(
const _<AView>& view)
override {
208 return BinaryOperator()(lhs->matches(view), rhs->matches(view));
216 bool operator()(
bool lhs,
bool rhs)
const {
220 return { _new<BinaryOperatorMatcher<compare_or>>(mMatcher, matcher.mMatcher) };
225 bool operator()(
bool lhs,
bool rhs)
const {
229 return { _new<BinaryOperatorMatcher<compare_and>>(mMatcher, matcher.mMatcher) };
Abstract AUI exception.
Definition: AException.h:29
A std::set with AUI extensions.
Definition: ASet.h:25
static AStacktrace capture(unsigned skipFrames=0, unsigned maxFrames=128) noexcept
Creates stacktrace of the current thread.
Definition: AStacktraceImpl.cpp:156
Definition: IMatcher.h:18
Definition: UIMatcher.h:21
UIMatcher findNearestTo(UIMatcher matcher)
Finds the nearest view to the specified one.
Definition: UIMatcher.h:111
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
static _< T > fake(T *raw)
Definition: SharedPtrTypes.h:376