|
| 1 | +// Copyright 2022 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:cloud_firestore/cloud_firestore.dart'; |
| 6 | +import 'package:cloud_firestore_platform_interface/src/method_channel/method_channel_firestore.dart'; |
| 7 | +import 'package:cloud_firestore_platform_interface/src/method_channel/method_channel_query.dart'; |
| 8 | +import 'package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart'; |
| 9 | +import 'package:firebase_core/firebase_core.dart'; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | +import 'package:flutter/services.dart'; |
| 12 | + |
| 13 | +import './mock.dart'; |
| 14 | + |
| 15 | +int kCount = 4; |
| 16 | + |
| 17 | +void main() { |
| 18 | + setupCloudFirestoreMocks(); |
| 19 | + MethodChannelFirebaseFirestore.channel = const MethodChannel( |
| 20 | + 'plugins.flutter.io/firebase_firestore', |
| 21 | + StandardMethodCodec(AggregateQueryMessageCodec()), |
| 22 | + ); |
| 23 | + |
| 24 | + MethodChannelFirebaseFirestore.channel.setMockMethodCallHandler((call) async { |
| 25 | + if (call.method == 'AggregateQuery#count') { |
| 26 | + return { |
| 27 | + 'count': kCount, |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + return null; |
| 32 | + }); |
| 33 | + |
| 34 | + FirebaseFirestore? firestore; |
| 35 | + |
| 36 | + group('$AggregateQuery', () { |
| 37 | + setUpAll(() async { |
| 38 | + await Firebase.initializeApp(); |
| 39 | + firestore = FirebaseFirestore.instance; |
| 40 | + }); |
| 41 | + |
| 42 | + test('returns the correct `AggregateQuerySnapshot` with correct `count`', |
| 43 | + () async { |
| 44 | + Query query = firestore!.collection('flutter-tests'); |
| 45 | + AggregateQuery aggregateQuery = query.count(); |
| 46 | + |
| 47 | + expect(query, aggregateQuery.query); |
| 48 | + AggregateQuerySnapshot snapshot = await aggregateQuery.get(); |
| 49 | + |
| 50 | + expect(snapshot.count, equals(kCount)); |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +class AggregateQueryMessageCodec extends FirestoreMessageCodec { |
| 56 | + /// Constructor. |
| 57 | + const AggregateQueryMessageCodec(); |
| 58 | + static const int _kFirestoreInstance = 144; |
| 59 | + static const int _kFirestoreQuery = 145; |
| 60 | + static const int _kFirestoreSettings = 146; |
| 61 | + |
| 62 | + @override |
| 63 | + Object? readValueOfType(int type, ReadBuffer buffer) { |
| 64 | + switch (type) { |
| 65 | + // The following cases are only used by unit tests, and not by actual application |
| 66 | + // code paths. |
| 67 | + case _kFirestoreInstance: |
| 68 | + String appName = readValue(buffer)! as String; |
| 69 | + readValue(buffer); |
| 70 | + final FirebaseApp app = Firebase.app(appName); |
| 71 | + return MethodChannelFirebaseFirestore(app: app); |
| 72 | + case _kFirestoreQuery: |
| 73 | + Map<dynamic, dynamic> values = |
| 74 | + readValue(buffer)! as Map<dynamic, dynamic>; |
| 75 | + final FirebaseApp app = Firebase.app(); |
| 76 | + return MethodChannelQuery( |
| 77 | + MethodChannelFirebaseFirestore(app: app), |
| 78 | + values['path'], |
| 79 | + ); |
| 80 | + case _kFirestoreSettings: |
| 81 | + readValue(buffer); |
| 82 | + return const Settings(); |
| 83 | + default: |
| 84 | + return super.readValueOfType(type, buffer); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments