/
TestEventService.h
72 lines (55 loc) · 2.41 KB
/
TestEventService.h
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
// The TestEventService allows to verify sequence of test events.
// The events are logged by the TestEventService::LogEvent method.
// They are observed by the TestEventService::ObserveEvents method which blocks current thread.
// Each test must call TestEventService::Initialize to reset the service state.
#include <JSValue.h>
#include <winrt/base.h>
#include <mutex>
#include <queue>
#include <set>
#include <string_view>
namespace ReactNativeIntegrationTests {
using namespace winrt::Microsoft::ReactNative;
struct TestEvent {
TestEvent() = default;
template <typename TName, typename TValue>
TestEvent(TName &&name, TValue &&value) : EventName(std::forward<TName>(name)), Value(std::forward<TValue>(value)) {}
std::string EventName;
JSValue Value;
};
// Ordered test notifications
struct TestEventService {
// Sets the service to the initial state.
static void Initialize() noexcept;
// Logs new event in the queue and notifies queue observers to check it.
static void LogEvent(std::string_view eventName, JSValue &&value) noexcept;
// Logs new event for value types that need an explicit call to JSValue constructor.
template <typename TValue>
static void LogEvent(std::string_view eventName, TValue &&value) noexcept {
LogEvent(eventName, JSValue{std::forward<TValue>(value)});
}
// Blocks current thread and observes all incoming events in the queue until we see them all.
static void ObserveEvents(winrt::array_view<const TestEvent> expectedEvents) noexcept;
private:
static std::mutex s_mutex; // to synchronize access to the fields below
static std::condition_variable s_cv; // signals about new event
static std::queue<TestEvent> s_eventQueue; // the event queue
static bool s_hasNewEvent;
};
// Unordered test events
struct TestNotificationService {
// Sets the service to the initial state.
static void Initialize() noexcept;
// Set a new notification.
static void Set(std::string_view eventName) noexcept;
// Blocks current thread and waits until expected event appears.
static void Wait(std::string_view eventName) noexcept;
private:
static std::mutex s_mutex; // to synchronize access to the fields below
static std::condition_variable s_cv; // signals about new event
static std::set<std::string, std::less<>> s_events; // a set of events
};
} // namespace ReactNativeIntegrationTests