Skip to content

Commit

Permalink
Foreground Background automation (#200)
Browse files Browse the repository at this point in the history
* remove tests from root

* automate android fb

* slash

* triplle message

* finalized version
  • Loading branch information
turtledreams authored Feb 6, 2024
1 parent 969a961 commit 2e6aa82
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ void main() {
config.apm.setAppStartTimestampOverride(1620000000000); // set app start timestamp and it should be ignored
await Countly.initWithConfig(config);

// wait for 5 seconds. Go to background and come back to foreground manually.
// TODO(turtledreams): automate this
print('Waiting for 5 seconds...');
await tester.pump(Duration(seconds: 5));
// go foreground and background
// TODO: this automation is Android only, iOS automation is not supported yet
goBackgroundAndForeground();

Countly.appLoadingFinished(); // this should be ignored

// check if there are no apm related requests in the queue
// should be 2 begin session and 1 end session (because we manually went to background and came back to foreground)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:countly_flutter/countly_flutter.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import '../utils.dart';
Expand All @@ -7,7 +8,7 @@ import '../utils.dart';
/// 2 apm requests should be sent
/// 1 for foreground and 1 for background
/// Currently this test is not automated and F/B actions should be done manually
/// TODO: automate this
/// TODO: automate this for iOS
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

Expand All @@ -16,16 +17,19 @@ void main() {
config.apm.enableForegroundBackgroundTracking();
await Countly.initWithConfig(config);

// wait for 5 seconds. go to background manually
print('Waiting for 5 seconds... Go background');
await tester.pump(Duration(seconds: 5));
// go foreground and background
// TODO: this automation is Android only, iOS automation is not supported yet
FlutterForegroundTask.minimizeApp();
print('waiting for 2 seconds, go to background');
await tester.pump(Duration(seconds: 2));

// foreground apm request should be sent
List<String> apmReqs = await getAndPrintWantedElementsWithParamFromAllQueues('apm');
expect(apmReqs.length, 1);

print('Waiting for 5 seconds... Back to foreground');
await tester.pump(Duration(seconds: 5));
FlutterForegroundTask.launchApp();
print('waiting for 2 seconds, go to foreground');
await tester.pump(Duration(seconds: 2));

// background apm request should be sent
apmReqs = await getAndPrintWantedElementsWithParamFromAllQueues('apm');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ void main() {
// trigger app loaded
await Countly.appLoadingFinished();

// wait for 5 seconds. go to background manually
print('Waiting for 5 seconds...');
await tester.pump(Duration(seconds: 5));
// go foreground and background
// TODO: this automation is Android only, iOS automation is not supported yet
goBackgroundAndForeground();

// check if there is 3 apm related requests in the queue
List<String> apmRequests = await getAndPrintWantedElementsWithParamFromAllQueues('apm');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ void main() {
// trigger app loaded
await Countly.appLoadingFinished();

// wait for 5 seconds. go to background manually
print('Waiting for 5 seconds...');
await tester.pump(Duration(seconds: 5));
// go foreground and background
// TODO: this automation is Android only, iOS automation is not supported yet
goBackgroundAndForeground();

// check if there is 3 apm related requests in the queue
List<String> apmRequests = await getAndPrintWantedElementsWithParamFromAllQueues('apm');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Test that needs manual interaction
Currently as we don't have a way to trigger app foregraound/background action with code, the following tests need manual human interaction to go F/B when waiting log is printed:
For iOS, currently as we don't have a way to trigger app foregraound action with code, the following tests need manual human interaction to go F/B when waiting log is printed:
200, 204, 207A, 207B

## Legacy call (setRecordAppStartTime)
Expand Down
27 changes: 25 additions & 2 deletions example/integration_test/utils.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:flutter_test/flutter_test.dart';

const MethodChannel _channelTest = MethodChannel('countly_flutter');

// Base config options for tests
final String SERVER_URL = 'https://xxx.count.ly';
final String APP_KEY = 'YOUR_APP_KEY';
final String APP_KEY = 'YOUR_APP_KEY'; // change this for ios tests

/// Get request queue from native side (list of strings)
Future<List<String>> getRequestQueue() async {
Expand Down Expand Up @@ -108,3 +108,26 @@ Future<Map<String, dynamic>> getApmParamsFromRequest(String request) async {
Map<String, dynamic> apmParams = json.decode(queryParams['apm']![0]);
return apmParams;
}

/// Go to background and foreground
void goBackgroundAndForeground() {
FlutterForegroundTask.minimizeApp();
if (Platform.isIOS) {
printMessageMultipleTimes('will now go to background, get ready to go foreground manually', 3);
}
sleep(Duration(seconds: 2));
FlutterForegroundTask.launchApp();
if (Platform.isIOS) {
printMessageMultipleTimes('waiting for 3 seconds, now go to foreground', 3);
}
sleep(Duration(seconds: 3));
}

/// Print message x times
/// [String message] - message
/// [int times] - times
void printMessageMultipleTimes(String message, int times) {
for (int i = 0; i < times; i++) {
print(message);
}
}
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter
flutter_foreground_task: 6.0.0+1

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
25 changes: 0 additions & 25 deletions example/test/widget_test.dart

This file was deleted.

19 changes: 0 additions & 19 deletions integration_test/foo/bar_test.dart

This file was deleted.

37 changes: 0 additions & 37 deletions integration_test/test_utility.dart

This file was deleted.

4 changes: 0 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ dependencies:

dev_dependencies:
flutter_lints: ^2.0.1
flutter_test:
sdk: flutter
integration_test:
sdk: flutter

flutter:
plugin:
Expand Down
18 changes: 0 additions & 18 deletions test/countly_flutter_test.dart

This file was deleted.

0 comments on commit 2e6aa82

Please sign in to comment.