|
| 1 | +// Copyright 2019, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:firebase_analytics/firebase_analytics.dart'; |
| 6 | +import 'package:firebase_core/firebase_core.dart'; |
| 7 | +import 'package:flutter/foundation.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:tests/firebase_options.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + group('firebase_analytics', () { |
| 13 | + setUpAll(() async { |
| 14 | + await Firebase.initializeApp( |
| 15 | + options: DefaultFirebaseOptions.currentPlatform, |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + test('logEvent', () async { |
| 20 | + await expectLater( |
| 21 | + FirebaseAnalytics.instance.logEvent(name: 'testing'), |
| 22 | + completes, |
| 23 | + ); |
| 24 | + |
| 25 | + AnalyticsEventItem analyticsEventItem = AnalyticsEventItem( |
| 26 | + affiliation: 'affil', |
| 27 | + coupon: 'coup', |
| 28 | + creativeName: 'creativeName', |
| 29 | + creativeSlot: 'creativeSlot', |
| 30 | + discount: 2.22, |
| 31 | + index: 3, |
| 32 | + itemBrand: 'itemBrand', |
| 33 | + itemCategory: 'itemCategory', |
| 34 | + itemCategory2: 'itemCategory2', |
| 35 | + itemCategory3: 'itemCategory3', |
| 36 | + itemCategory4: 'itemCategory4', |
| 37 | + itemCategory5: 'itemCategory5', |
| 38 | + itemId: 'itemId', |
| 39 | + itemListId: 'itemListId', |
| 40 | + itemListName: 'itemListName', |
| 41 | + itemName: 'itemName', |
| 42 | + itemVariant: 'itemVariant', |
| 43 | + locationId: 'locationId', |
| 44 | + price: 9.99, |
| 45 | + currency: 'USD', |
| 46 | + promotionId: 'promotionId', |
| 47 | + promotionName: 'promotionName', |
| 48 | + quantity: 1, |
| 49 | + ); |
| 50 | + // test custom event |
| 51 | + await expectLater( |
| 52 | + FirebaseAnalytics.instance.logEvent( |
| 53 | + name: 'testing-parameters', |
| 54 | + parameters: { |
| 55 | + 'foo': 'bar', |
| 56 | + 'baz': 500, |
| 57 | + 'items': [analyticsEventItem], |
| 58 | + }, |
| 59 | + ), |
| 60 | + completes, |
| 61 | + ); |
| 62 | + // test 2 reserved events |
| 63 | + await expectLater( |
| 64 | + FirebaseAnalytics.instance.logAdImpression( |
| 65 | + adPlatform: 'foo', |
| 66 | + adSource: 'bar', |
| 67 | + adFormat: 'baz', |
| 68 | + adUnitName: 'foo', |
| 69 | + currency: 'bar', |
| 70 | + value: 100, |
| 71 | + ), |
| 72 | + completes, |
| 73 | + ); |
| 74 | + |
| 75 | + await expectLater( |
| 76 | + FirebaseAnalytics.instance.logPurchase( |
| 77 | + currency: 'foo', |
| 78 | + coupon: 'bar', |
| 79 | + value: 200, |
| 80 | + items: [analyticsEventItem], |
| 81 | + tax: 10, |
| 82 | + shipping: 23, |
| 83 | + transactionId: 'bar', |
| 84 | + affiliation: 'baz', |
| 85 | + ), |
| 86 | + completes, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + test( |
| 91 | + 'setSessionTimeoutDuration', |
| 92 | + () async { |
| 93 | + if (kIsWeb) { |
| 94 | + await expectLater( |
| 95 | + FirebaseAnalytics.instance |
| 96 | + .setSessionTimeoutDuration(const Duration(milliseconds: 5000)), |
| 97 | + throwsA(isA<UnimplementedError>()), |
| 98 | + ); |
| 99 | + } else { |
| 100 | + await expectLater( |
| 101 | + FirebaseAnalytics.instance |
| 102 | + .setSessionTimeoutDuration(const Duration(milliseconds: 5000)), |
| 103 | + completes, |
| 104 | + ); |
| 105 | + } |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + test('setAnalyticsCollectionEnabled', () async { |
| 110 | + await expectLater( |
| 111 | + FirebaseAnalytics.instance.setAnalyticsCollectionEnabled(true), |
| 112 | + completes, |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + test('setUserId', () async { |
| 117 | + await expectLater( |
| 118 | + FirebaseAnalytics.instance.setUserId(id: 'foo'), |
| 119 | + completes, |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + test('setCurrentScreen', () async { |
| 124 | + await expectLater( |
| 125 | + FirebaseAnalytics.instance.setCurrentScreen(screenName: 'screen-name'), |
| 126 | + completes, |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + test('setUserProperty', () async { |
| 131 | + await expectLater( |
| 132 | + FirebaseAnalytics.instance.setUserProperty(name: 'foo', value: 'bar'), |
| 133 | + completes, |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + test( |
| 138 | + 'resetAnalyticsData', |
| 139 | + () async { |
| 140 | + if (kIsWeb) { |
| 141 | + await expectLater( |
| 142 | + FirebaseAnalytics.instance.resetAnalyticsData(), |
| 143 | + throwsA(isA<UnimplementedError>()), |
| 144 | + ); |
| 145 | + } else { |
| 146 | + await expectLater( |
| 147 | + FirebaseAnalytics.instance.resetAnalyticsData(), |
| 148 | + completes, |
| 149 | + ); |
| 150 | + } |
| 151 | + }, |
| 152 | + ); |
| 153 | + |
| 154 | + test( |
| 155 | + 'setConsent', |
| 156 | + () async { |
| 157 | + if (kIsWeb) { |
| 158 | + await expectLater( |
| 159 | + FirebaseAnalytics.instance.setConsent( |
| 160 | + analyticsStorageConsentGranted: false, |
| 161 | + adStorageConsentGranted: true, |
| 162 | + ), |
| 163 | + throwsA(isA<UnimplementedError>()), |
| 164 | + ); |
| 165 | + } else { |
| 166 | + await expectLater( |
| 167 | + FirebaseAnalytics.instance.setConsent( |
| 168 | + analyticsStorageConsentGranted: false, |
| 169 | + adStorageConsentGranted: true, |
| 170 | + ), |
| 171 | + completes, |
| 172 | + ); |
| 173 | + } |
| 174 | + }, |
| 175 | + ); |
| 176 | + |
| 177 | + test( |
| 178 | + 'setDefaultEventParameters', |
| 179 | + () async { |
| 180 | + if (kIsWeb) { |
| 181 | + await expectLater( |
| 182 | + FirebaseAnalytics.instance |
| 183 | + .setDefaultEventParameters({'default': 'parameters'}), |
| 184 | + throwsA(isA<UnimplementedError>()), |
| 185 | + ); |
| 186 | + } else { |
| 187 | + await expectLater( |
| 188 | + FirebaseAnalytics.instance |
| 189 | + .setDefaultEventParameters({'default': 'parameters'}), |
| 190 | + completes, |
| 191 | + ); |
| 192 | + } |
| 193 | + }, |
| 194 | + ); |
| 195 | + }); |
| 196 | +} |
0 commit comments