Skip to content

Commit 01b00a9

Browse files
rubennortefacebook-github-bot
authored andcommitted
Add tests for performance.eventCounts (#52463)
Summary: Changelog: [internal] This adds Fantom tests for `performance.eventCounts`. Reviewed By: huntie Differential Revision: D77860881
1 parent 84d8529 commit 01b00a9

6 files changed

Lines changed: 73 additions & 0 deletions

File tree

packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,8 @@ void NativePerformance::setCurrentTimeStampForTesting(
464464
forcedCurrentTimeStamp_ = ts;
465465
}
466466

467+
void NativePerformance::clearEventCountsForTesting(jsi::Runtime& /*rt*/) {
468+
PerformanceEntryReporter::getInstance()->clearEventCounts();
469+
}
470+
467471
} // namespace facebook::react

packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class NativePerformance : public NativePerformanceCxxSpec<NativePerformance> {
199199
#pragma mark - Testing
200200

201201
void setCurrentTimeStampForTesting(jsi::Runtime& rt, HighResTimeStamp ts);
202+
void clearEventCountsForTesting(jsi::Runtime& rt);
202203

203204
private:
204205
std::optional<HighResTimeStamp> forcedCurrentTimeStamp_;

packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ PerformanceMeasure PerformanceEntryReporter::reportMeasure(
213213
return entry;
214214
}
215215

216+
void PerformanceEntryReporter::clearEventCounts() {
217+
eventCounts_.clear();
218+
}
219+
216220
std::optional<HighResTimeStamp> PerformanceEntryReporter::getMarkTime(
217221
const std::string& markName) const {
218222
std::shared_lock lock(buffersMutex_);

packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class PerformanceEntryReporter {
8181
return eventCounts_;
8282
}
8383

84+
void clearEventCounts();
85+
8486
std::optional<HighResTimeStamp> getMarkTime(
8587
const std::string& markName) const;
8688

packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13+
import type Performance from 'react-native/src/private/webapis/performance/Performance';
1314
import type {PerformanceObserverEntryList} from 'react-native/src/private/webapis/performance/PerformanceObserver';
1415

16+
import NativePerformance from '../specs/NativePerformance';
1517
import * as Fantom from '@react-native/fantom';
1618
import nullthrows from 'nullthrows';
1719
import {useState} from 'react';
@@ -22,6 +24,8 @@ import {PerformanceObserver} from 'react-native/src/private/webapis/performance/
2224

2325
setUpPerformanceObserver();
2426

27+
declare var performance: Performance;
28+
2529
function sleep(ms: number) {
2630
const end = performance.now() + ms;
2731
while (performance.now() < end) {}
@@ -187,4 +191,61 @@ describe('Event Timing API', () => {
187191

188192
expect(entry.interactionId).toBeGreaterThanOrEqual(0);
189193
});
194+
195+
it('reports number of dispatched events via performance.eventCounts', () => {
196+
NativePerformance?.clearEventCountsForTesting?.();
197+
198+
const root = Fantom.createRoot();
199+
Fantom.runTask(() => {
200+
root.render(<View />);
201+
});
202+
203+
const element = nullthrows(root.document.documentElement.firstElementChild);
204+
205+
expect(performance.eventCounts).not.toBeInstanceOf(Map);
206+
207+
// FIXME: this isn't spec compliant, as the map should be prepopulated with
208+
// all the supported event names mapped to 0.
209+
expect(performance.eventCounts.size).toBe(0);
210+
expect([...performance.eventCounts.entries()]).toEqual([]);
211+
const initialForEachCallback = jest.fn();
212+
performance.eventCounts.forEach(initialForEachCallback);
213+
expect(initialForEachCallback.mock.calls).toEqual([]);
214+
expect([...performance.eventCounts.keys()]).toEqual([]);
215+
expect([...performance.eventCounts.values()]).toEqual([]);
216+
217+
Fantom.dispatchNativeEvent(element, 'click');
218+
Fantom.dispatchNativeEvent(element, 'click');
219+
Fantom.dispatchNativeEvent(element, 'click');
220+
221+
Fantom.dispatchNativeEvent(element, 'pointerDown');
222+
Fantom.dispatchNativeEvent(element, 'pointerUp');
223+
224+
expect(performance.eventCounts.size).toBe(3);
225+
expect(performance.eventCounts.get('click')).toBe(3);
226+
expect(performance.eventCounts.get('pointerdown')).toBe(1);
227+
expect(performance.eventCounts.get('pointerup')).toBe(1);
228+
229+
expect([...performance.eventCounts.entries()]).toEqual([
230+
['pointerup', 1],
231+
['pointerdown', 1],
232+
['click', 3],
233+
]);
234+
235+
const forEachCallback = jest.fn();
236+
performance.eventCounts.forEach(forEachCallback);
237+
expect(forEachCallback.mock.calls).toEqual([
238+
[1, 'pointerup', performance.eventCounts],
239+
[1, 'pointerdown', performance.eventCounts],
240+
[3, 'click', performance.eventCounts],
241+
]);
242+
243+
expect([...performance.eventCounts.keys()]).toEqual([
244+
'pointerup',
245+
'pointerdown',
246+
'click',
247+
]);
248+
249+
expect([...performance.eventCounts.values()]).toEqual([1, 1, 3]);
250+
});
190251
});

packages/react-native/src/private/webapis/performance/specs/NativePerformance.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export interface Spec extends TurboModule {
107107
+getSupportedPerformanceEntryTypes?: () => $ReadOnlyArray<RawPerformanceEntryType>;
108108

109109
+setCurrentTimeStampForTesting?: (timeStamp: number) => void;
110+
+clearEventCountsForTesting?: () => void;
110111
}
111112

112113
export default (TurboModuleRegistry.get<Spec>('NativePerformanceCxx'): ?Spec);

0 commit comments

Comments
 (0)