Skip to content

Commit 3d2a974

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Define TracingTestBase class for tests
Summary: # Changelog: [Internal] Extracts `startTracing()`, `stopTracing()` helpers from NetworkReporterTest into a re-usable `TracingTestBase` class. Differential Revision: D86027333
1 parent a034841 commit 3d2a974

File tree

2 files changed

+85
-55
lines changed

2 files changed

+85
-55
lines changed

packages/react-native/ReactCommon/jsinspector-modern/tests/NetworkReporterTest.cpp

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#include "JsiIntegrationTest.h"
8+
#include "TracingTest.h"
99
#include "engines/JsiIntegrationTestHermesEngineAdapter.h"
1010

1111
#include <folly/executors/QueuedImmediateExecutor.h>
@@ -31,13 +31,13 @@ struct NetworkReporterTestParams {
3131
*/
3232
template <typename Params>
3333
requires std::convertible_to<Params, NetworkReporterTestParams>
34-
class NetworkReporterTestBase : public JsiIntegrationPortableTestBase<
34+
class NetworkReporterTestBase : public TracingTestBase<
3535
JsiIntegrationTestHermesEngineAdapter,
3636
folly::QueuedImmediateExecutor>,
3737
public WithParamInterface<Params> {
3838
protected:
3939
NetworkReporterTestBase()
40-
: JsiIntegrationPortableTestBase({
40+
: TracingTestBase({
4141
.networkInspectionEnabled = true,
4242
.enableNetworkEventReporting =
4343
WithParamInterface<Params>::GetParam()
@@ -68,58 +68,6 @@ class NetworkReporterTestBase : public JsiIntegrationPortableTestBase<
6868
urlMatcher);
6969
}
7070

71-
void startTracing() {
72-
this->expectMessageFromPage(JsonEq(R"({
73-
"id": 1,
74-
"result": {}
75-
})"));
76-
77-
this->toPage_->sendMessage(R"({
78-
"id": 1,
79-
"method": "Tracing.start"
80-
})");
81-
}
82-
83-
/**
84-
* Helper method to end tracing and collect all trace events from potentially
85-
* multiple chunked Tracing.dataCollected messages.
86-
* \returns A vector containing all collected trace events
87-
*/
88-
std::vector<folly::dynamic> endTracingAndCollectEvents() {
89-
InSequence s;
90-
91-
this->expectMessageFromPage(JsonEq(R"({
92-
"id": 1,
93-
"result": {}
94-
})"));
95-
96-
std::vector<folly::dynamic> allTraceEvents;
97-
98-
EXPECT_CALL(
99-
fromPage(),
100-
onMessage(JsonParsed(AtJsonPtr("/method", "Tracing.dataCollected"))))
101-
.Times(AtLeast(1))
102-
.WillRepeatedly(Invoke([&allTraceEvents](const std::string& message) {
103-
auto parsedMessage = folly::parseJson(message);
104-
auto& events = parsedMessage.at("params").at("value");
105-
allTraceEvents.insert(
106-
allTraceEvents.end(),
107-
std::make_move_iterator(events.begin()),
108-
std::make_move_iterator(events.end()));
109-
}));
110-
111-
this->expectMessageFromPage(JsonParsed(AllOf(
112-
AtJsonPtr("/method", "Tracing.tracingComplete"),
113-
AtJsonPtr("/params/dataLossOccurred", false))));
114-
115-
this->toPage_->sendMessage(R"({
116-
"id": 1,
117-
"method": "Tracing.end"
118-
})");
119-
120-
return allTraceEvents;
121-
}
122-
12371
private:
12472
std::optional<std::string> getScriptUrlById(const std::string& scriptId) {
12573
auto it = scriptUrlsById_.find(scriptId);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include "JsiIntegrationTest.h"
11+
12+
#include <folly/dynamic.h>
13+
#include <folly/json.h>
14+
#include <gmock/gmock.h>
15+
#include <vector>
16+
17+
#include "FollyDynamicMatchers.h"
18+
19+
namespace facebook::react::jsinspector_modern {
20+
21+
/**
22+
* Base test class providing tracing-related test utilities for tests.
23+
*/
24+
template <typename EngineAdapter, typename Executor>
25+
class TracingTestBase : public JsiIntegrationPortableTestBase<EngineAdapter, Executor> {
26+
protected:
27+
using JsiIntegrationPortableTestBase<EngineAdapter, Executor>::JsiIntegrationPortableTestBase;
28+
29+
/**
30+
* Helper method to start tracing via Tracing.start CDP command.
31+
*/
32+
void startTracing()
33+
{
34+
this->expectMessageFromPage(JsonEq(R"({
35+
"id": 1,
36+
"result": {}
37+
})"));
38+
39+
this->toPage_->sendMessage(R"({
40+
"id": 1,
41+
"method": "Tracing.start"
42+
})");
43+
}
44+
45+
/**
46+
* Helper method to end tracing and collect all trace events from potentially
47+
* multiple chunked Tracing.dataCollected messages.
48+
* \returns A vector containing all collected trace events
49+
*/
50+
std::vector<folly::dynamic> endTracingAndCollectEvents()
51+
{
52+
testing::InSequence s;
53+
54+
this->expectMessageFromPage(JsonEq(R"({
55+
"id": 1,
56+
"result": {}
57+
})"));
58+
59+
std::vector<folly::dynamic> allTraceEvents;
60+
61+
EXPECT_CALL(this->fromPage(), onMessage(JsonParsed(AtJsonPtr("/method", "Tracing.dataCollected"))))
62+
.Times(testing::AtLeast(1))
63+
.WillRepeatedly(testing::Invoke([&allTraceEvents](const std::string &message) {
64+
auto parsedMessage = folly::parseJson(message);
65+
auto &events = parsedMessage.at("params").at("value");
66+
allTraceEvents.insert(
67+
allTraceEvents.end(), std::make_move_iterator(events.begin()), std::make_move_iterator(events.end()));
68+
}));
69+
70+
this->expectMessageFromPage(JsonParsed(
71+
testing::AllOf(AtJsonPtr("/method", "Tracing.tracingComplete"), AtJsonPtr("/params/dataLossOccurred", false))));
72+
73+
this->toPage_->sendMessage(R"({
74+
"id": 1,
75+
"method": "Tracing.end"
76+
})");
77+
78+
return allTraceEvents;
79+
}
80+
};
81+
82+
} // namespace facebook::react::jsinspector_modern

0 commit comments

Comments
 (0)