Skip to content

Commit 9e0e832

Browse files
authored
Use static method for fake instance (#262)
* Swap `Analytics.test` for `Analytics.fake` * Remove default constructor for `FakeAnalytics`
1 parent ea51acc commit 9e0e832

File tree

11 files changed

+141
-138
lines changed

11 files changed

+141
-138
lines changed

pkgs/unified_analytics/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- Exposing new method for `FakeAnalytics.sendPendingErrorEvents` to send error events on command
77
- Added `Event.fromJson` static method to generate instance of `Event` from JSON
88
- Remove unused parameters `measurementId` and `apiSecret` from the `Analytics.test` constructor
9+
- Remove `Analytics.test` factory constructor in favor of `Analytics.fake` static method to return a `FakeAnalytics` instance
10+
- Remove `FakeAnalytics` default constructor in favor of `Analytics.fake`
911

1012
## 5.8.8
1113

pkgs/unified_analytics/example/serving_surveys.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'package:unified_analytics/unified_analytics.dart';
1414

1515
/// This example code is intended to only be used as guidance for
1616
/// clients using this package. Clients using this package should avoid
17-
/// the use of the [Analytics.test] constructor.
17+
/// the use of the [Analytics.fake] static method.
1818
///
1919
/// It was used in this example file so that the real [FileSystem] was swapped
2020
/// out for a [MemoryFileSystem] so that repeated runs of this script yield
@@ -36,7 +36,7 @@ void main() async {
3636
// send events after its first run; this instance won't be used below
3737
//
3838
// ignore: invalid_use_of_visible_for_testing_member
39-
final initialAnalytics = Analytics.test(
39+
final initialAnalytics = Analytics.fake(
4040
tool: DashTool.flutterTool,
4141
homeDirectory: home,
4242
dartVersion: 'dartVersion',
@@ -47,7 +47,7 @@ void main() async {
4747
initialAnalytics.clientShowedMessage();
4848

4949
// ignore: invalid_use_of_visible_for_testing_member
50-
analytics = Analytics.test(
50+
analytics = Analytics.fake(
5151
tool: DashTool.flutterTool,
5252
homeDirectory: home,
5353
dartVersion: 'dartVersion',

pkgs/unified_analytics/lib/src/analytics.dart

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -184,51 +184,6 @@ abstract class Analytics {
184184
);
185185
}
186186

187-
/// Factory constructor to return the [AnalyticsImpl] class with a
188-
/// [MemoryFileSystem] to use for testing.
189-
@visibleForTesting
190-
factory Analytics.test({
191-
required DashTool tool,
192-
required Directory homeDirectory,
193-
required String dartVersion,
194-
required FileSystem fs,
195-
required DevicePlatform platform,
196-
String? flutterChannel,
197-
String? flutterVersion,
198-
String? clientIde,
199-
String? enabledFeatures,
200-
SurveyHandler? surveyHandler,
201-
GAClient? gaClient,
202-
int toolsMessageVersion = kToolsMessageVersion,
203-
String toolsMessage = kToolsMessage,
204-
}) {
205-
final firstRun = runInitialization(homeDirectory: homeDirectory, fs: fs);
206-
207-
return FakeAnalytics(
208-
tool: tool,
209-
homeDirectory: homeDirectory,
210-
flutterChannel: flutterChannel,
211-
toolsMessageVersion: toolsMessageVersion,
212-
flutterVersion: flutterVersion,
213-
dartVersion: dartVersion,
214-
platform: platform,
215-
fs: fs,
216-
surveyHandler: surveyHandler ??
217-
FakeSurveyHandler.fromList(
218-
dismissedSurveyFile: fs.file(p.join(
219-
homeDirectory.path,
220-
kDartToolDirectoryName,
221-
kDismissedSurveyFileName,
222-
)),
223-
initializedSurveys: [],
224-
),
225-
gaClient: gaClient ?? const FakeGAClient(),
226-
clientIde: clientIde,
227-
enabledFeatures: enabledFeatures,
228-
firstRun: firstRun,
229-
);
230-
}
231-
232187
/// The shared identifier for Flutter and Dart related tooling using
233188
/// package:unified_analytics.
234189
String get clientId;
@@ -333,6 +288,53 @@ abstract class Analytics {
333288
///
334289
/// The snooze period is defined by the [Survey.snoozeForMinutes] field.
335290
void surveyShown(Survey survey);
291+
292+
/// Returns an instance of [FakeAnalytics] which can be used in tests to check
293+
/// for certain [Event] instances within [FakeAnalytics.sentEvents].
294+
@visibleForTesting
295+
static FakeAnalytics fake({
296+
required DashTool tool,
297+
required Directory homeDirectory,
298+
required String dartVersion,
299+
required MemoryFileSystem fs,
300+
String? flutterChannel,
301+
String? flutterVersion,
302+
String? clientIde,
303+
String? enabledFeatures,
304+
SurveyHandler? surveyHandler,
305+
GAClient? gaClient,
306+
DevicePlatform platform = DevicePlatform.linux,
307+
int toolsMessageVersion = kToolsMessageVersion,
308+
String toolsMessage = kToolsMessage,
309+
bool enableAsserts = true,
310+
}) {
311+
final firstRun = runInitialization(homeDirectory: homeDirectory, fs: fs);
312+
313+
return FakeAnalytics._(
314+
tool: tool,
315+
homeDirectory: homeDirectory,
316+
flutterChannel: flutterChannel,
317+
toolsMessageVersion: toolsMessageVersion,
318+
flutterVersion: flutterVersion,
319+
dartVersion: dartVersion,
320+
platform: platform,
321+
fs: fs,
322+
surveyHandler: surveyHandler ??
323+
FakeSurveyHandler.fromList(
324+
dismissedSurveyFile: fs.file(p.join(
325+
homeDirectory.path,
326+
kDartToolDirectoryName,
327+
kDismissedSurveyFileName,
328+
)),
329+
initializedSurveys: [],
330+
),
331+
gaClient: gaClient ?? const FakeGAClient(),
332+
clientIde: clientIde,
333+
enabledFeatures: enabledFeatures,
334+
firstRun: firstRun,
335+
enableAsserts: enableAsserts,
336+
);
337+
}
336338
}
337339

338340
class AnalyticsImpl implements Analytics {
@@ -763,7 +765,7 @@ class FakeAnalytics extends AnalyticsImpl {
763765
final List<Event> sentEvents = [];
764766

765767
/// Class to use when you want to see which events were sent
766-
FakeAnalytics({
768+
FakeAnalytics._({
767769
required super.tool,
768770
required super.homeDirectory,
769771
required super.dartVersion,

pkgs/unified_analytics/test/error_handler_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:unified_analytics/src/enums.dart';
1313
import 'package:unified_analytics/unified_analytics.dart';
1414

1515
void main() {
16-
late FileSystem fs;
16+
late MemoryFileSystem fs;
1717
late Directory home;
1818
late FakeAnalytics initializationAnalytics;
1919
late FakeAnalytics analytics;
@@ -41,7 +41,7 @@ void main() {
4141

4242
// This is the first analytics instance that will be used to demonstrate
4343
// that events will not be sent with the first run of analytics
44-
initializationAnalytics = Analytics.test(
44+
initializationAnalytics = Analytics.fake(
4545
tool: initialTool,
4646
homeDirectory: home,
4747
flutterChannel: flutterChannel,
@@ -51,7 +51,7 @@ void main() {
5151
dartVersion: dartVersion,
5252
fs: fs,
5353
platform: platform,
54-
) as FakeAnalytics;
54+
);
5555
expect(initializationAnalytics.shouldShowMessage, true);
5656
initializationAnalytics.clientShowedMessage();
5757
expect(initializationAnalytics.shouldShowMessage, false);
@@ -61,7 +61,7 @@ void main() {
6161
//
6262
// This instance should have the same parameters as the one above for
6363
// [initializationAnalytics]
64-
analytics = Analytics.test(
64+
analytics = Analytics.fake(
6565
tool: initialTool,
6666
homeDirectory: home,
6767
flutterChannel: flutterChannel,
@@ -72,7 +72,7 @@ void main() {
7272
fs: fs,
7373
platform: platform,
7474
clientIde: clientIde,
75-
) as FakeAnalytics;
75+
);
7676
analytics.clientShowedMessage();
7777

7878
// The files that should have been generated that will be used for tests
@@ -93,7 +93,7 @@ void main() {
9393
expect(analytics.telemetryEnabled, false);
9494
expect(sessionFile.readAsStringSync(), isEmpty);
9595

96-
final secondAnalytics = Analytics.test(
96+
final secondAnalytics = Analytics.fake(
9797
tool: initialTool,
9898
homeDirectory: home,
9999
flutterChannel: flutterChannel,
@@ -104,7 +104,7 @@ void main() {
104104
fs: fs,
105105
platform: platform,
106106
clientIde: clientIde,
107-
) as FakeAnalytics;
107+
);
108108
expect(sessionFile.readAsStringSync(), isEmpty);
109109
expect(secondAnalytics.telemetryEnabled, false);
110110

pkgs/unified_analytics/test/events_with_fake_test.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void main() {
1818
// are being sent when invoking methods on the `Analytics` instance
1919

2020
late FakeAnalytics fakeAnalytics;
21-
late FileSystem fs;
21+
late MemoryFileSystem fs;
2222
late Directory homeDirectory;
2323
late File dismissedSurveyFile;
2424

@@ -58,7 +58,7 @@ void main() {
5858
kDismissedSurveyFileName,
5959
));
6060

61-
final initialAnalytics = Analytics.test(
61+
final initialAnalytics = Analytics.fake(
6262
tool: DashTool.flutterTool,
6363
homeDirectory: homeDirectory,
6464
dartVersion: 'dartVersion',
@@ -72,7 +72,7 @@ void main() {
7272
// the first run
7373
withClock(Clock.fixed(DateTime(2022, 3, 3)), () {
7474
final toolsMessageVersion = kToolsMessageVersion;
75-
fakeAnalytics = FakeAnalytics(
75+
fakeAnalytics = Analytics.fake(
7676
tool: DashTool.flutterTool,
7777
homeDirectory: homeDirectory,
7878
dartVersion: 'dartVersion',
@@ -83,7 +83,6 @@ void main() {
8383
dismissedSurveyFile: dismissedSurveyFile,
8484
initializedSurveys: [testSurvey],
8585
),
86-
firstRun: false,
8786
);
8887
});
8988
});

pkgs/unified_analytics/test/legacy_analytics_test.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:unified_analytics/src/enums.dart';
1212
import 'package:unified_analytics/unified_analytics.dart';
1313

1414
void main() {
15-
late FileSystem fs;
15+
late MemoryFileSystem fs;
1616
late Directory home;
1717
late Analytics analytics;
1818

@@ -49,7 +49,7 @@ void main() {
4949

5050
// The main analytics instance, other instances can be spawned within tests
5151
// to test how to instances running together work
52-
analytics = Analytics.test(
52+
analytics = Analytics.fake(
5353
tool: initialTool,
5454
homeDirectory: home,
5555
flutterChannel: flutterChannel,
@@ -80,7 +80,7 @@ void main() {
8080

8181
// The main analytics instance, other instances can be spawned within tests
8282
// to test how to instances running together work
83-
analytics = Analytics.test(
83+
analytics = Analytics.fake(
8484
tool: initialTool,
8585
homeDirectory: home,
8686
flutterChannel: flutterChannel,
@@ -110,7 +110,7 @@ void main() {
110110

111111
// The main analytics instance, other instances can be spawned within tests
112112
// to test how to instances running together work
113-
analytics = Analytics.test(
113+
analytics = Analytics.fake(
114114
tool: initialTool,
115115
homeDirectory: home,
116116
flutterChannel: flutterChannel,
@@ -140,7 +140,7 @@ void main() {
140140

141141
// The main analytics instance, other instances can be spawned within tests
142142
// to test how to instances running together work
143-
analytics = Analytics.test(
143+
analytics = Analytics.fake(
144144
tool: initialTool,
145145
homeDirectory: home,
146146
flutterChannel: flutterChannel,
@@ -174,7 +174,7 @@ void main() {
174174

175175
// The main analytics instance, other instances can be spawned within tests
176176
// to test how to instances running together work
177-
analytics = Analytics.test(
177+
analytics = Analytics.fake(
178178
tool: initialTool,
179179
homeDirectory: home,
180180
flutterChannel: flutterChannel,
@@ -208,7 +208,7 @@ void main() {
208208

209209
// The main analytics instance, other instances can be spawned within tests
210210
// to test how to instances running together work
211-
analytics = Analytics.test(
211+
analytics = Analytics.fake(
212212
tool: initialTool,
213213
homeDirectory: home,
214214
flutterChannel: flutterChannel,
@@ -240,7 +240,7 @@ NOT VALID JSON
240240

241241
// The main analytics instance, other instances can be spawned within tests
242242
// to test how to instances running together work
243-
analytics = Analytics.test(
243+
analytics = Analytics.fake(
244244
tool: initialTool,
245245
homeDirectory: home,
246246
flutterChannel: flutterChannel,
@@ -276,7 +276,7 @@ NOT VALID JSON
276276

277277
// The main analytics instance, other instances can be spawned within tests
278278
// to test how to instances running together work
279-
analytics = Analytics.test(
279+
analytics = Analytics.fake(
280280
tool: initialTool,
281281
homeDirectory: home,
282282
flutterChannel: flutterChannel,
@@ -308,7 +308,7 @@ NOT VALID JSON
308308

309309
// The main analytics instance, other instances can be spawned within tests
310310
// to test how to instances running together work
311-
analytics = Analytics.test(
311+
analytics = Analytics.fake(
312312
tool: initialTool,
313313
homeDirectory: home,
314314
flutterChannel: flutterChannel,

pkgs/unified_analytics/test/log_handler_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'package:unified_analytics/unified_analytics.dart';
1515
void main() {
1616
late FakeAnalytics analytics;
1717
late Directory homeDirectory;
18-
late FileSystem fs;
18+
late MemoryFileSystem fs;
1919
late File logFile;
2020

2121
final testEvent = Event.hotReloadTime(timeMs: 10);
@@ -30,7 +30,7 @@ void main() {
3030
));
3131

3232
// Create the initialization analytics instance to onboard the tool
33-
final initializationAnalytics = Analytics.test(
33+
final initializationAnalytics = Analytics.fake(
3434
tool: DashTool.flutterTool,
3535
homeDirectory: homeDirectory,
3636
dartVersion: 'dartVersion',
@@ -41,13 +41,13 @@ void main() {
4141

4242
// This instance is free to send events since the instance above
4343
// has confirmed that the client has shown the message
44-
analytics = Analytics.test(
44+
analytics = Analytics.fake(
4545
tool: DashTool.flutterTool,
4646
homeDirectory: homeDirectory,
4747
dartVersion: 'dartVersion',
4848
fs: fs,
4949
platform: DevicePlatform.macos,
50-
) as FakeAnalytics;
50+
);
5151
});
5252

5353
test('Ensure that log file is created', () {

0 commit comments

Comments
 (0)