Aria 3.2.6
C++23 MVVM framework — reactive, coroutine-first, ABI-layered
Loading...
Searching...
No Matches
fake_adapter.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Reusable in-memory adapter for application and adapter contract tests.
3#pragma once
4
6#include "aria/detail/typed_signal.hpp"
7
8#include <cstdint>
9#include <memory>
10#include <string>
11
12namespace aria::binding::testing {
13
14namespace testing_detail {
15
17template <class T>
18[[nodiscard]] T& downcast(IView& v) { // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast)
19 return static_cast<T&>(v);
20}
21
22} // namespace testing_detail
23
26struct FakeView : public IView {
27 std::string text;
28 bool flag = false;
29 int integer = 0;
30 std::int64_t i64 = 0;
31 std::uint64_t u64 = 0;
32 float f32 = 0.0F;
33 double number = 0.0;
34 bool visible = true;
35 bool enabled = true;
36
37 ::aria::detail::TypedSignal<std::string> sig_text;
38 ::aria::detail::TypedSignal<bool> sig_bool;
39 ::aria::detail::TypedSignal<int> sig_int;
40 ::aria::detail::TypedSignal<std::int64_t> sig_int64;
41 ::aria::detail::TypedSignal<std::uint64_t> sig_uint64;
42 ::aria::detail::TypedSignal<float> sig_float;
43 ::aria::detail::TypedSignal<double> sig_double;
44 ::aria::detail::TypedSignal<> sig_click;
46 [[nodiscard]] std::string_view kind() const noexcept override { return "fake"; }
47};
48
50class FakeAdapter : public IViewAdapter {
51public:
52 [[nodiscard]] std::string_view platform_name() const noexcept override { return "Fake"; }
53
54 void set_text(IView& v, std::string_view t) override {
56 std::string s(t);
57 if (fv.text != s) {
58 fv.text = std::move(s);
59 fv.sig_text.emit(fv.text);
60 }
61 }
62 std::string get_text(IView& v) override { return testing_detail::downcast<FakeView>(v).text; }
63 ::aria::Subscription on_text_changed(IView& v, std::function<void(std::string_view)> cb) override {
64 return testing_detail::downcast<FakeView>(v).sig_text.connect(
65 [cb = std::move(cb)](const std::string& s) { cb(s); });
66 }
67
68 void set_bool(IView& v, bool b) override {
70 if (fv.flag != b) { fv.flag = b; fv.sig_bool.emit(b); }
71 }
72 bool get_bool(IView& v) override { return testing_detail::downcast<FakeView>(v).flag; }
73 ::aria::Subscription on_bool_changed(IView& v, std::function<void(bool)> cb) override {
74 return testing_detail::downcast<FakeView>(v).sig_bool.connect(std::move(cb));
75 }
76
77 void set_int(IView& v, int n) override {
79 if (fv.integer != n) { fv.integer = n; fv.sig_int.emit(n); }
80 }
81 int get_int(IView& v) override { return testing_detail::downcast<FakeView>(v).integer; }
82 ::aria::Subscription on_int_changed(IView& v, std::function<void(int)> cb) override {
83 return testing_detail::downcast<FakeView>(v).sig_int.connect(std::move(cb));
84 }
85
86 void set_int64(IView& v, std::int64_t n) override {
88 if (fv.i64 != n) { fv.i64 = n; fv.sig_int64.emit(n); }
89 }
90 std::int64_t get_int64(IView& v) override { return testing_detail::downcast<FakeView>(v).i64; }
92 std::function<void(std::int64_t)> cb) override {
93 return testing_detail::downcast<FakeView>(v).sig_int64.connect(std::move(cb));
94 }
95
96 void set_uint64(IView& v, std::uint64_t n) override {
98 if (fv.u64 != n) { fv.u64 = n; fv.sig_uint64.emit(n); }
99 }
100 std::uint64_t get_uint64(IView& v) override { return testing_detail::downcast<FakeView>(v).u64; }
102 std::function<void(std::uint64_t)> cb) override {
103 return testing_detail::downcast<FakeView>(v).sig_uint64.connect(std::move(cb));
104 }
105
106 void set_float(IView& v, float f) override {
108 if (fv.f32 != f) { fv.f32 = f; fv.sig_float.emit(f); }
109 }
110 float get_float(IView& v) override { return testing_detail::downcast<FakeView>(v).f32; }
111 ::aria::Subscription on_float_changed(IView& v, std::function<void(float)> cb) override {
112 return testing_detail::downcast<FakeView>(v).sig_float.connect(std::move(cb));
113 }
114
115 void set_double(IView& v, double d) override {
117 if (fv.number != d) { fv.number = d; fv.sig_double.emit(d); }
118 }
119 double get_double(IView& v) override { return testing_detail::downcast<FakeView>(v).number; }
120 ::aria::Subscription on_double_changed(IView& v, std::function<void(double)> cb) override {
121 return testing_detail::downcast<FakeView>(v).sig_double.connect(std::move(cb));
122 }
123
124 void set_visible(IView& v, bool b) override { testing_detail::downcast<FakeView>(v).visible = b; }
125 void set_enabled(IView& v, bool b) override { testing_detail::downcast<FakeView>(v).enabled = b; }
126
127 ::aria::Subscription on_click(IView& v, std::function<void()> cb) override {
128 return testing_detail::downcast<FakeView>(v).sig_click.connect(
129 [cb = std::move(cb)] { cb(); });
130 }
131
133 static void user_type(FakeView& v, std::string text) {
134 v.text = std::move(text);
135 v.sig_text.emit(v.text);
136 }
137
138 static void user_click(FakeView& v) { v.sig_click.emit(); }
140 static void user_toggle(FakeView& v, bool b) {
141 v.flag = b;
142 v.sig_bool.emit(b);
143 }
144};
145
146} // namespace aria::binding::testing
RAII handle to a single subscription.
Definition subscription.hpp:44
Abstract platform adapter — knows how to read/write/observe widgets.
Definition view_adapter.hpp:80
Abstract platform widget reference.
Definition view_adapter.hpp:28
Adapter that talks to FakeView.
Definition fake_adapter.hpp:50
::aria::Subscription on_uint64_changed(IView &v, std::function< void(std::uint64_t)> cb) override
Definition fake_adapter.hpp:101
void set_enabled(IView &v, bool b) override
Definition fake_adapter.hpp:125
static void user_type(FakeView &v, std::string text)
Test helper: simulate user typing.
Definition fake_adapter.hpp:133
::aria::Subscription on_double_changed(IView &v, std::function< void(double)> cb) override
Definition fake_adapter.hpp:120
::aria::Subscription on_int64_changed(IView &v, std::function< void(std::int64_t)> cb) override
Definition fake_adapter.hpp:91
void set_bool(IView &v, bool b) override
Definition fake_adapter.hpp:68
std::int64_t get_int64(IView &v) override
Definition fake_adapter.hpp:90
std::string get_text(IView &v) override
Definition fake_adapter.hpp:62
std::uint64_t get_uint64(IView &v) override
Definition fake_adapter.hpp:100
void set_visible(IView &v, bool b) override
Definition fake_adapter.hpp:124
::aria::Subscription on_text_changed(IView &v, std::function< void(std::string_view)> cb) override
Definition fake_adapter.hpp:63
double get_double(IView &v) override
Definition fake_adapter.hpp:119
float get_float(IView &v) override
Definition fake_adapter.hpp:110
::aria::Subscription on_float_changed(IView &v, std::function< void(float)> cb) override
Definition fake_adapter.hpp:111
void set_int(IView &v, int n) override
Definition fake_adapter.hpp:77
void set_float(IView &v, float f) override
Definition fake_adapter.hpp:106
bool get_bool(IView &v) override
Definition fake_adapter.hpp:72
void set_uint64(IView &v, std::uint64_t n) override
Definition fake_adapter.hpp:96
void set_text(IView &v, std::string_view t) override
Definition fake_adapter.hpp:54
::aria::Subscription on_click(IView &v, std::function< void()> cb) override
Definition fake_adapter.hpp:127
void set_double(IView &v, double d) override
Definition fake_adapter.hpp:115
::aria::Subscription on_int_changed(IView &v, std::function< void(int)> cb) override
Definition fake_adapter.hpp:82
std::string_view platform_name() const noexcept override
Definition fake_adapter.hpp:52
static void user_toggle(FakeView &v, bool b)
Test helper: simulate user toggling.
Definition fake_adapter.hpp:140
static void user_click(FakeView &v)
Test helper: simulate user clicking.
Definition fake_adapter.hpp:138
::aria::Subscription on_bool_changed(IView &v, std::function< void(bool)> cb) override
Definition fake_adapter.hpp:73
int get_int(IView &v) override
Definition fake_adapter.hpp:81
void set_int64(IView &v, std::int64_t n) override
Definition fake_adapter.hpp:86
Definition fake_adapter.hpp:14
T & downcast(IView &v)
The fake adapter only ever receives its own view; the downcast is the point.
Definition fake_adapter.hpp:18
Definition adapter_conformance.hpp:83
Fake widget that holds string/bool/int/int64/uint64/float/double state and exposes one signal per typ...
Definition fake_adapter.hpp:26
::aria::detail::TypedSignal sig_click
Definition fake_adapter.hpp:43
std::string text
Definition fake_adapter.hpp:27
std::int64_t i64
Definition fake_adapter.hpp:30
bool flag
Definition fake_adapter.hpp:28
::aria::detail::TypedSignal< double > sig_double
Definition fake_adapter.hpp:43
int integer
Definition fake_adapter.hpp:29
::aria::detail::TypedSignal< bool > sig_bool
Definition fake_adapter.hpp:38
std::uint64_t u64
Definition fake_adapter.hpp:31
::aria::detail::TypedSignal< std::string > sig_text
Definition fake_adapter.hpp:37
::aria::detail::TypedSignal< float > sig_float
Definition fake_adapter.hpp:42
::aria::detail::TypedSignal< std::int64_t > sig_int64
Definition fake_adapter.hpp:40
std::string_view kind() const noexcept override
Definition fake_adapter.hpp:45
::aria::detail::TypedSignal< std::uint64_t > sig_uint64
Definition fake_adapter.hpp:41
bool visible
Definition fake_adapter.hpp:34
::aria::detail::TypedSignal< int > sig_int
Definition fake_adapter.hpp:39
bool enabled
Definition fake_adapter.hpp:35
double number
Definition fake_adapter.hpp:33
float f32
Definition fake_adapter.hpp:32