diff --git a/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance.dart b/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance.dart index e52ad060f62b..3744ea8be573 100644 --- a/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance.dart +++ b/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance.dart @@ -2,6 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:js_interop'; + import 'package:firebase_core_web/firebase_core_web_interop.dart' hide jsify; import 'performance_interop.dart' as performance_interop; @@ -38,45 +40,46 @@ class Performance : super.fromJsObject(jsObject); Trace trace(String traceName) => - Trace.fromJsObject(performance_interop.trace(jsObject, traceName)); + Trace.fromJsObject(performance_interop.trace(jsObject, traceName.toJS)); /// Non-null App for this instance of firestore service. App get app => App.getInstance(jsObject.app); - bool get instrumentationEnabled => jsObject.instrumentationEnabled; - bool get dataCollectionEnabled => jsObject.dataCollectionEnabled; + bool get instrumentationEnabled => jsObject.instrumentationEnabled.toDart; + bool get dataCollectionEnabled => jsObject.dataCollectionEnabled.toDart; } class Trace extends JsObjectWrapper { Trace.fromJsObject(performance_interop.TraceJsImpl jsObject) : super.fromJsObject(jsObject); - String getAttribute(String attr) => jsObject.getAttribute(attr); + String getAttribute(String attr) => jsObject.getAttribute(attr.toJS).toDart; Map getAttributes() { return dartify(jsObject.getAttributes()).cast(); } - int getMetric(String metricName) => jsObject.getMetric(metricName); + int getMetric(String metricName) => + jsObject.getMetric(metricName.toJS).toDartInt; void incrementMetric(String metricName, [int? num]) { if (num != null) { - return jsObject.incrementMetric(metricName, num); + return jsObject.incrementMetric(metricName.toJS, num.toJS); } else { - return jsObject.incrementMetric(metricName); + return jsObject.incrementMetric(metricName.toJS); } } void putMetric(String metricName, int num) { - return jsObject.putMetric(metricName, num); + return jsObject.putMetric(metricName.toJS, num.toJS); } void putAttribute(String attr, String value) { - return jsObject.putAttribute(attr, value); + return jsObject.putAttribute(attr.toJS, value.toJS); } void removeAttribute(String attr) { - return jsObject.removeAttribute(attr); + return jsObject.removeAttribute(attr.toJS); } void start() { @@ -97,8 +100,8 @@ class PerformanceSettings bool? instrumentationEnabled, ]) { final jsObject = performance_interop.PerformanceSettingsJsImpl( - dataCollectionEnabled: dataCollectionEnabled, - instrumentationEnabled: instrumentationEnabled, + dataCollectionEnabled: dataCollectionEnabled?.toJS, + instrumentationEnabled: instrumentationEnabled?.toJS, ); return _expando[jsObject] ??= PerformanceSettings._fromJsObject(jsObject); } @@ -107,13 +110,13 @@ class PerformanceSettings performance_interop.PerformanceSettingsJsImpl jsObject, ) : super.fromJsObject(jsObject); - bool? get dataCollectionEnabled => jsObject.dataCollectionEnabled; + bool? get dataCollectionEnabled => jsObject.dataCollectionEnabled?.toDart; set dataCollectionEnabled(bool? b) { - jsObject.dataCollectionEnabled = b; + jsObject.dataCollectionEnabled = b?.toJS; } - bool? get instrumentationEnabled => jsObject.instrumentationEnabled; + bool? get instrumentationEnabled => jsObject.instrumentationEnabled?.toDart; set instrumentationEnabled(bool? b) { - jsObject.instrumentationEnabled = b; + jsObject.instrumentationEnabled = b?.toJS; } } diff --git a/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance_interop.dart b/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance_interop.dart index fc2264929df8..b2dd22f34478 100644 --- a/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance_interop.dart +++ b/packages/firebase_performance/firebase_performance_web/lib/src/interop/performance_interop.dart @@ -5,62 +5,84 @@ @JS('firebase_performance') library firebase.performance_interop; +import 'dart:js_interop'; + import 'package:firebase_core_web/firebase_core_web_interop.dart'; -import 'package:js/js.dart'; @JS() +@staticInterop external PerformanceJsImpl getPerformance([AppJsImpl? app]); @JS() +@staticInterop external PerformanceJsImpl initializePerformance( AppJsImpl app, [ PerformanceSettingsJsImpl? settings, ]); @JS() -external TraceJsImpl trace(PerformanceJsImpl performance, String traceName); +@staticInterop +external TraceJsImpl trace(PerformanceJsImpl performance, JSString traceName); @JS('Performance') -abstract class PerformanceJsImpl { +@staticInterop +abstract class PerformanceJsImpl {} + +extension PerformanceJsImplExtension on PerformanceJsImpl { external AppJsImpl get app; - external bool dataCollectionEnabled; - external bool instrumentationEnabled; + external JSBoolean dataCollectionEnabled; + external JSBoolean instrumentationEnabled; } @JS('Trace') +@staticInterop @anonymous -class TraceJsImpl { - external String getAttribute(String attr); - external Object getAttributes(); - external int getMetric(String metricName); - external void incrementMetric(String metricName, [int? num]); - external void putMetric(String metricName, int num); - external void putAttribute(String attr, String value); - external void removeAttribute(String attr); +class TraceJsImpl {} + +extension TraceJsImplExtension on TraceJsImpl { + external JSString getAttribute(JSString attr); + external JSAny getAttributes(); + external JSNumber getMetric(JSString metricName); + external void incrementMetric(JSString metricName, [JSNumber? num]); + external void putMetric(JSString metricName, JSNumber num); + external void putAttribute(JSString attr, JSString value); + external void removeAttribute(JSString attr); external void start(); - external void record(int number, int duration, [RecordOptions? options]); + external void record( + JSNumber number, + JSNumber duration, [ + RecordOptions? options, + ]); external void stop(); } @JS() +@staticInterop @anonymous class RecordOptions { + external factory RecordOptions({JSAny? metrics, JSAny? attributes}); +} + +extension RecordOptionsExtension on RecordOptions { /* map of metrics */ - external Object? get metrics; + external JSAny? get metrics; /* map of attributes */ - external Object? get attributes; - external factory RecordOptions({Object? metrics, Object? attributes}); + external JSAny? get attributes; } @JS() +@staticInterop @anonymous class PerformanceSettingsJsImpl { - external bool? get dataCollectionEnabled; - external set dataCollectionEnabled(bool? b); - external bool? get instrumentationEnabled; - external set instrumentationEnabled(bool? b); external factory PerformanceSettingsJsImpl({ - bool? dataCollectionEnabled, - bool? instrumentationEnabled, + JSBoolean? dataCollectionEnabled, + JSBoolean? instrumentationEnabled, }); } + +extension PerformanceSettingsJsImplExtension on PerformanceSettingsJsImpl { + external JSBoolean? get dataCollectionEnabled; + external set dataCollectionEnabled(JSBoolean? b); + external JSBoolean? get instrumentationEnabled; + external set instrumentationEnabled(JSBoolean? b); +} diff --git a/packages/firebase_performance/firebase_performance_web/pubspec.yaml b/packages/firebase_performance/firebase_performance_web/pubspec.yaml index 7f8e4220d928..f51139815bfb 100644 --- a/packages/firebase_performance/firebase_performance_web/pubspec.yaml +++ b/packages/firebase_performance/firebase_performance_web/pubspec.yaml @@ -17,12 +17,12 @@ dependencies: flutter_web_plugins: sdk: flutter js: ^0.6.3 + web: ^0.5.1 + dev_dependencies: - build_runner: ^2.3.3 flutter_test: sdk: flutter - mockito: ^5.0.10 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/packages/firebase_performance/firebase_performance_web/test/firebase_performance_web_test.dart b/packages/firebase_performance/firebase_performance_web/test/firebase_performance_web_test.dart deleted file mode 100644 index 338d910ae3e3..000000000000 --- a/packages/firebase_performance/firebase_performance_web/test/firebase_performance_web_test.dart +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2022, the Chromium project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -@TestOn('chrome') // Uses web-only Flutter SDK - -import 'package:firebase_performance_platform_interface/firebase_performance_platform_interface.dart'; -import 'package:firebase_performance_web/firebase_performance_web.dart'; -import 'package:firebase_performance_web/src/interop/performance.dart'; -import 'package:firebase_performance_web/src/trace.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:mockito/annotations.dart'; -import 'package:mockito/mockito.dart'; - -@GenerateNiceMocks([MockSpec(), MockSpec()]) -import 'firebase_performance_web_test.mocks.dart'; - -void main() { - group('FirebasePerformanceWeb', () { - late FirebasePerformanceWeb firebasePerformancePlatform; - late MockPerformance mockPerformance; - - setUp(() { - mockPerformance = MockPerformance(); - firebasePerformancePlatform = FirebasePerformanceWeb(); - firebasePerformancePlatform.mockDelegate = mockPerformance; - }); - - test('isPerformanceCollectionEnabled', () async { - await expectLater( - firebasePerformancePlatform.setPerformanceCollectionEnabled(true), - completes, - ); - }); - - test('newTrace returns correct trace web platform object', () async { - const testTraceName = 'test_trace'; - final MockTrace mockTrace = MockTrace(); - when(mockPerformance.trace(testTraceName)).thenReturn(mockTrace); - - TracePlatform trace = firebasePerformancePlatform.newTrace(testTraceName); - - expect(trace.runtimeType, TraceWeb); - trace = trace as TraceWeb; - expect(trace.traceDelegate, mockTrace); - verify(mockPerformance.trace(testTraceName)).called(1); - verifyNoMoreInteractions(mockPerformance); - verifyNoMoreInteractions(mockTrace); - }); - }); - group('TraceWeb', () { - late TracePlatform tracePlatform; - late MockTrace mockTrace; - - setUp(() { - mockTrace = MockTrace(); - tracePlatform = TraceWeb(mockTrace); - }); - - test('start', () async { - await tracePlatform.start(); - verify(mockTrace.start()).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('stop', () async { - await tracePlatform.stop(); - verify(mockTrace.stop()).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('incrementMetric', () { - tracePlatform.incrementMetric('counter_name', 33); - verify(mockTrace.incrementMetric('counter_name', 33)).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('setMetric', () { - tracePlatform.setMetric('set_name', 50); - verify(mockTrace.putMetric('set_name', 50)).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('getMetric', () async { - tracePlatform.getMetric('counter_name'); - verify(mockTrace.getMetric('counter_name')).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('putAttribute', () { - tracePlatform.putAttribute('attribute', 'value'); - verify(mockTrace.putAttribute('attribute', 'value')).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('removeAttribute', () { - tracePlatform.removeAttribute('attribute'); - verify(mockTrace.removeAttribute('attribute')).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('getAttribute', () async { - tracePlatform.getAttribute('attribute'); - verify(mockTrace.getAttribute('attribute')).called(1); - verifyNoMoreInteractions(mockTrace); - }); - - test('getAttributes', () async { - when(mockTrace.getAttributes()).thenReturn({}); - tracePlatform.getAttributes(); - verify(mockTrace.getAttributes()).called(1); - verifyNoMoreInteractions(mockTrace); - }); - }); -} diff --git a/packages/firebase_performance/firebase_performance_web/test/firebase_performance_web_test.mocks.dart b/packages/firebase_performance/firebase_performance_web/test/firebase_performance_web_test.mocks.dart deleted file mode 100644 index 591c1d769e64..000000000000 --- a/packages/firebase_performance/firebase_performance_web/test/firebase_performance_web_test.mocks.dart +++ /dev/null @@ -1,242 +0,0 @@ -// Copyright 2021 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. -// Mocks generated by Mockito 5.4.0 from annotations -// in firebase_performance_web/test/firebase_performance_web_test.dart. -// Do not manually edit this file. - -// ignore_for_file: no_leading_underscores_for_library_prefixes -import 'package:firebase_core_web/firebase_core_web_interop.dart' as _i2; -import 'package:firebase_performance_web/src/interop/performance.dart' as _i4; -import 'package:firebase_performance_web/src/interop/performance_interop.dart' - as _i3; -import 'package:mockito/mockito.dart' as _i1; - -// ignore_for_file: type=lint -// ignore_for_file: avoid_redundant_argument_values -// ignore_for_file: avoid_setters_without_getters -// ignore_for_file: comment_references -// ignore_for_file: implementation_imports -// ignore_for_file: invalid_use_of_visible_for_testing_member -// ignore_for_file: prefer_const_constructors -// ignore_for_file: unnecessary_parenthesis -// ignore_for_file: camel_case_types -// ignore_for_file: subtype_of_sealed_class - -class _FakeApp_0 extends _i1.SmartFake implements _i2.App { - _FakeApp_0( - Object parent, - Invocation parentInvocation, - ) : super( - parent, - parentInvocation, - ); -} - -class _FakePerformanceJsImpl_1 extends _i1.SmartFake - implements _i3.PerformanceJsImpl { - _FakePerformanceJsImpl_1( - Object parent, - Invocation parentInvocation, - ) : super( - parent, - parentInvocation, - ); -} - -class _FakeTrace_2 extends _i1.SmartFake implements _i4.Trace { - _FakeTrace_2( - Object parent, - Invocation parentInvocation, - ) : super( - parent, - parentInvocation, - ); -} - -class _FakeTraceJsImpl_3 extends _i1.SmartFake implements _i3.TraceJsImpl { - _FakeTraceJsImpl_3( - Object parent, - Invocation parentInvocation, - ) : super( - parent, - parentInvocation, - ); -} - -/// A class which mocks [Performance]. -/// -/// See the documentation for Mockito's code generation for more information. -class MockPerformance extends _i1.Mock implements _i4.Performance { - @override - _i2.App get app => (super.noSuchMethod( - Invocation.getter(#app), - returnValue: _FakeApp_0( - this, - Invocation.getter(#app), - ), - returnValueForMissingStub: _FakeApp_0( - this, - Invocation.getter(#app), - ), - ) as _i2.App); - @override - bool get instrumentationEnabled => (super.noSuchMethod( - Invocation.getter(#instrumentationEnabled), - returnValue: false, - returnValueForMissingStub: false, - ) as bool); - @override - bool get dataCollectionEnabled => (super.noSuchMethod( - Invocation.getter(#dataCollectionEnabled), - returnValue: false, - returnValueForMissingStub: false, - ) as bool); - @override - _i3.PerformanceJsImpl get jsObject => (super.noSuchMethod( - Invocation.getter(#jsObject), - returnValue: _FakePerformanceJsImpl_1( - this, - Invocation.getter(#jsObject), - ), - returnValueForMissingStub: _FakePerformanceJsImpl_1( - this, - Invocation.getter(#jsObject), - ), - ) as _i3.PerformanceJsImpl); - @override - _i4.Trace trace(String? traceName) => (super.noSuchMethod( - Invocation.method( - #trace, - [traceName], - ), - returnValue: _FakeTrace_2( - this, - Invocation.method( - #trace, - [traceName], - ), - ), - returnValueForMissingStub: _FakeTrace_2( - this, - Invocation.method( - #trace, - [traceName], - ), - ), - ) as _i4.Trace); -} - -/// A class which mocks [Trace]. -/// -/// See the documentation for Mockito's code generation for more information. -class MockTrace extends _i1.Mock implements _i4.Trace { - @override - _i3.TraceJsImpl get jsObject => (super.noSuchMethod( - Invocation.getter(#jsObject), - returnValue: _FakeTraceJsImpl_3( - this, - Invocation.getter(#jsObject), - ), - returnValueForMissingStub: _FakeTraceJsImpl_3( - this, - Invocation.getter(#jsObject), - ), - ) as _i3.TraceJsImpl); - @override - String getAttribute(String? attr) => (super.noSuchMethod( - Invocation.method( - #getAttribute, - [attr], - ), - returnValue: '', - returnValueForMissingStub: '', - ) as String); - @override - Map getAttributes() => (super.noSuchMethod( - Invocation.method( - #getAttributes, - [], - ), - returnValue: {}, - returnValueForMissingStub: {}, - ) as Map); - @override - int getMetric(String? metricName) => (super.noSuchMethod( - Invocation.method( - #getMetric, - [metricName], - ), - returnValue: 0, - returnValueForMissingStub: 0, - ) as int); - @override - void incrementMetric( - String? metricName, [ - int? num, - ]) => - super.noSuchMethod( - Invocation.method( - #incrementMetric, - [ - metricName, - num, - ], - ), - returnValueForMissingStub: null, - ); - @override - void putMetric( - String? metricName, - int? num, - ) => - super.noSuchMethod( - Invocation.method( - #putMetric, - [ - metricName, - num, - ], - ), - returnValueForMissingStub: null, - ); - @override - void putAttribute( - String? attr, - String? value, - ) => - super.noSuchMethod( - Invocation.method( - #putAttribute, - [ - attr, - value, - ], - ), - returnValueForMissingStub: null, - ); - @override - void removeAttribute(String? attr) => super.noSuchMethod( - Invocation.method( - #removeAttribute, - [attr], - ), - returnValueForMissingStub: null, - ); - @override - void start() => super.noSuchMethod( - Invocation.method( - #start, - [], - ), - returnValueForMissingStub: null, - ); - @override - void stop() => super.noSuchMethod( - Invocation.method( - #stop, - [], - ), - returnValueForMissingStub: null, - ); -}