blob: ae712612e34f837b5d7a46b65b568aaebb1ed6d7 [file] [log] [blame]
Devon Carew26ceabe2014-12-11 11:44:53 -08001// Copyright (c) 2014, the Dart 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
5library usage.impl_test;
6
Devon Carew590f3432015-10-17 14:00:33 -07007import 'package:test/test.dart';
Devon Carew26ceabe2014-12-11 11:44:53 -08008import 'package:usage/src/usage_impl.dart';
Devon Carewe328cc22014-12-18 13:10:45 -08009
10import 'src/common.dart';
Devon Carew26ceabe2014-12-11 11:44:53 -080011
Mike Fairhurst43af9e62018-02-19 15:00:30 -080012void main() => defineTests();
Devon Carew590f3432015-10-17 14:00:33 -070013
Devon Carew26ceabe2014-12-11 11:44:53 -080014void defineTests() {
15 group('ThrottlingBucket', () {
16 test('can send', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080017 var bucket = ThrottlingBucket(20);
Devon Carew26ceabe2014-12-11 11:44:53 -080018 expect(bucket.removeDrop(), true);
19 });
20
21 test('doesn\'t send too many', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080022 var bucket = ThrottlingBucket(20);
23 for (var i = 0; i < 20; i++) {
Devon Carew26ceabe2014-12-11 11:44:53 -080024 expect(bucket.removeDrop(), true);
25 }
26 expect(bucket.removeDrop(), false);
27 });
28 });
29
Devon Carewe328cc22014-12-18 13:10:45 -080030 group('AnalyticsImpl', () {
Devon Carewc7dd6822017-06-22 19:55:46 -070031 test('trackingId', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080032 var mock = createMock();
Devon Carewc7dd6822017-06-22 19:55:46 -070033 expect(mock.trackingId, isNotNull);
34 });
35
36 test('applicationName', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080037 var mock = createMock();
Devon Carewc7dd6822017-06-22 19:55:46 -070038 expect(mock.applicationName, isNotNull);
39 });
40
41 test('applicationVersion', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080042 var mock = createMock();
Devon Carewc7dd6822017-06-22 19:55:46 -070043 expect(mock.applicationVersion, isNotNull);
44 });
45
Devon Carewe328cc22014-12-18 13:10:45 -080046 test('respects disabled', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080047 var mock = createMock();
Devon Carewd6a97072016-04-25 22:15:44 -070048 mock.enabled = false;
Devon Carewe328cc22014-12-18 13:10:45 -080049 mock.sendException('FooBar exception');
Devon Carewd6a97072016-04-25 22:15:44 -070050 expect(mock.enabled, false);
Devon Carewe328cc22014-12-18 13:10:45 -080051 expect(mock.mockPostHandler.sentValues, isEmpty);
52 });
53
Devon Carewd6a97072016-04-25 22:15:44 -070054 test('firstRun', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080055 var mock = createMock();
Devon Carewd6a97072016-04-25 22:15:44 -070056 expect(mock.firstRun, true);
57 mock = createMock(props: {'firstRun': false});
58 expect(mock.firstRun, false);
Devon Carewe328cc22014-12-18 13:10:45 -080059 });
60
61 test('setSessionValue', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080062 var mock = createMock();
Devon Carewe328cc22014-12-18 13:10:45 -080063 mock.sendScreenView('foo');
64 hasnt(mock.last, 'val');
65 mock.setSessionValue('val', 'ue');
66 mock.sendScreenView('bar');
67 has(mock.last, 'val');
Devon Carew9243d312014-12-18 13:20:20 -080068 mock.setSessionValue('val', null);
69 mock.sendScreenView('baz');
70 hasnt(mock.last, 'val');
Devon Carewe328cc22014-12-18 13:10:45 -080071 });
Devon Carew7d1ae962015-01-27 11:23:07 -080072
73 test('waitForLastPing', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080074 var mock = createMock();
Devon Carew7d1ae962015-01-27 11:23:07 -080075 mock.sendScreenView('foo');
76 mock.sendScreenView('bar');
77 mock.sendScreenView('baz');
Nate Bosch2075fe92019-12-09 13:28:45 -080078 return mock.waitForLastPing(timeout: Duration(milliseconds: 100));
Devon Carew7d1ae962015-01-27 11:23:07 -080079 });
Yegor Jbanov021664e2017-06-15 12:37:01 -070080
81 group('clientId', () {
82 test('is available immediately', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080083 var mock = createMock();
Yegor Jbanov021664e2017-06-15 12:37:01 -070084 expect(mock.clientId, isNotEmpty);
85 });
86
87 test('is memoized', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080088 var mock = createMock();
Yegor Jbanov021664e2017-06-15 12:37:01 -070089 final value1 = mock.clientId;
90 final value2 = mock.clientId;
91 expect(value1, isNotEmpty);
92 expect(value1, value2);
93 });
94
95 test('is stored in properties', () {
Nate Boschc2051dd2020-01-13 13:15:22 -080096 var mock = createMock();
Yegor Jbanov021664e2017-06-15 12:37:01 -070097 expect(mock.properties['clientId'], isNull);
98 final value = mock.clientId;
99 expect(mock.properties['clientId'], value);
100 });
101 });
Devon Carewe328cc22014-12-18 13:10:45 -0800102 });
103
Devon Carewe328cc22014-12-18 13:10:45 -0800104 group('postEncode', () {
105 test('simple', () {
Nate Boschc2051dd2020-01-13 13:15:22 -0800106 var map = <String, dynamic>{'foo': 'bar', 'baz': 'qux norf'};
Devon Carewe328cc22014-12-18 13:10:45 -0800107 expect(postEncode(map), 'foo=bar&baz=qux%20norf');
Devon Carew26ceabe2014-12-11 11:44:53 -0800108 });
109 });
110}