Skip to content

Commit 0c8323e

Browse files
authored
test: Integration test (#21)
1 parent ad7e91f commit 0c8323e

File tree

7 files changed

+147
-46
lines changed

7 files changed

+147
-46
lines changed

.github/workflows/android-test.yaml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Test-Android
2+
on:
3+
push:
4+
branches:
5+
- 'main'
6+
pull_request:
7+
branches:
8+
- '*'
9+
jobs:
10+
android-test:
11+
runs-on: ubuntu-latest
12+
13+
env:
14+
FLUTTER_CHANNEL: stable
15+
FLUTTER_VERSION: 3.22.2
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
21+
- run: echo API_KEY=${{ secrets.TEST_API_KEY }} > example/.env
22+
23+
- name: Set up Flutter
24+
uses: subosito/flutter-action@v2
25+
with:
26+
flutter-version: ${{ env.FLUTTER_VERSION }}
27+
channel: ${{ env.FLUTTER_CHANNEL }}
28+
29+
# This step enables KVM (Kernel-based Virtual Machine).
30+
# KVM is a virtualization module in the Linux kernel that allows the
31+
# kernel to function as a hypervisor. This is necessary for running
32+
# virtual machines on the host system.
33+
- name: Enable KVM
34+
run: |
35+
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
36+
sudo udevadm control --reload-rules
37+
sudo udevadm trigger --name-match=kvm
38+
39+
- name: run android tests
40+
uses: reactivecircus/android-emulator-runner@v2
41+
with:
42+
api-level: 29
43+
script: cd example && flutter drive --driver=test_drive/integration_test.dart --target=test/widget_test.dart

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
working-directory: example
3737
run: flutter pub get
3838

39+
- run: echo API_KEY=${{ secrets.TEST_API_KEY }} > example/.env
3940
- name: Build Android
4041
working-directory: example
4142
run: flutter build apk --release
@@ -55,4 +56,3 @@ jobs:
5556
- name: Build iOS
5657
working-directory: example
5758
run: flutter build ios --release --no-codesign
58-

.github/workflows/ios-test.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Test-iOS
2+
on:
3+
push:
4+
branches:
5+
- 'main'
6+
pull_request:
7+
branches:
8+
- '*'
9+
jobs:
10+
ios-test:
11+
runs-on: macos-latest
12+
13+
env:
14+
FLUTTER_CHANNEL: stable
15+
FLUTTER_VERSION: 3.22.2
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
21+
- name: fetch submodules
22+
run: git submodule update --init --recursive
23+
24+
- name: Copy iOS
25+
working-directory: ios/Classes
26+
run: cp -r confidence-sdk/Sources/Confidence .
27+
28+
- name: Remove the submodule
29+
working-directory: ios/Classes
30+
run: rm -rf confidence-sdk
31+
32+
- name: Remove git submodule
33+
working-directory: ios/Classes
34+
run: git rm confidence-sdk
35+
36+
- uses: futureware-tech/simulator-action@v3
37+
with:
38+
model: 'iPhone 15'
39+
40+
- name: Set up Flutter
41+
uses: subosito/flutter-action@v2
42+
with:
43+
flutter-version: ${{ env.FLUTTER_VERSION }}
44+
channel: ${{ env.FLUTTER_CHANNEL }}
45+
46+
- run: echo API_KEY=${{ secrets.TEST_API_KEY }} > example/.env
47+
- run: cd example && flutter drive --driver=test_drive/integration_test.dart --target=test/widget_test.dart

example/lib/main.dart

+25-23
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@ import 'dart:async';
33
import 'package:confidence_flutter_sdk/confidence_flutter_sdk.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter/services.dart';
6+
import 'package:flutter_dotenv/flutter_dotenv.dart';
67

78
void main() {
8-
runApp(const MyApp());
9+
runApp(MyApp());
910
}
1011

1112
class MyApp extends StatefulWidget {
12-
const MyApp({super.key});
13+
MyApp({super.key});
14+
final Completer<void> _initCompleter = Completer<void>();
15+
16+
Future<void> initDone() async {
17+
return _initCompleter.future;
18+
}
1319

1420
@override
15-
State<MyApp> createState() => _MyAppState();
21+
// ignore: no_logic_in_create_state
22+
State<MyApp> createState() => _MyAppState(_initCompleter);
1623
}
1724

1825
class _MyAppState extends State<MyApp> {
1926
String _object = 'Unknown';
2027
String _message = 'Unknown';
21-
bool _enabled = false;
2228
final _confidenceFlutterSdkPlugin = ConfidenceFlutterSdk();
29+
final Completer<void> initCompleter;
30+
31+
_MyAppState(this.initCompleter);
2332

2433
@override
2534
void initState() {
@@ -30,23 +39,19 @@ class _MyAppState extends State<MyApp> {
3039
// Platform messages are asynchronous, so we initialize in an async method.
3140
Future<void> initPlatformState() async {
3241
String message;
33-
bool enabled;
3442
String object;
3543
// Platform messages may fail, so we use a try/catch PlatformException.
3644
// We also handle the message potentially returning null.
3745
try {
38-
await _confidenceFlutterSdkPlugin.setup("API_KEY");
39-
if(await _confidenceFlutterSdkPlugin.isStorageEmpty()) {
40-
await _confidenceFlutterSdkPlugin.fetchAndActivate();
41-
} else {
42-
await _confidenceFlutterSdkPlugin.activateAndFetchAsync();
43-
}
44-
await _confidenceFlutterSdkPlugin.putContext("Yo", "Hello");
46+
await dotenv.load(fileName: ".env");
47+
await _confidenceFlutterSdkPlugin.setup(dotenv.env["API_KEY"]!);
48+
await _confidenceFlutterSdkPlugin.putContext("targeting_key", "random");
49+
await _confidenceFlutterSdkPlugin.putContext("user", <String, dynamic>{"country": "SE"});
50+
await _confidenceFlutterSdkPlugin.fetchAndActivate();
4551
object =
46-
(await _confidenceFlutterSdkPlugin.getObject("hawkflag", <String, dynamic>{})).toString();
52+
(await _confidenceFlutterSdkPlugin.getObject("kotlin-test-flag", <String, dynamic>{})).toString();
4753
message =
48-
(await _confidenceFlutterSdkPlugin.getString("hawkflag.message", "default")).toString();
49-
enabled = await _confidenceFlutterSdkPlugin.getBool("hawkflag.enabled", false);
54+
(await _confidenceFlutterSdkPlugin.getInt("kotlin-test-flag.my-integer", 0)).toString();
5055
final data = {
5156
'screen': 'home',
5257
"my_bool": false,
@@ -59,7 +64,6 @@ class _MyAppState extends State<MyApp> {
5964
_confidenceFlutterSdkPlugin.flush();
6065
} on PlatformException {
6166
message = 'Failed to get platform version.';
62-
enabled = false;
6367
object = 'Failed to get object.';
6468
}
6569

@@ -69,10 +73,10 @@ class _MyAppState extends State<MyApp> {
6973
if (!mounted) return;
7074

7175
setState(() {
72-
_enabled = enabled;
7376
_message = message;
7477
_object = object;
7578
});
79+
initCompleter.complete();
7680
}
7781

7882
@override
@@ -84,19 +88,17 @@ class _MyAppState extends State<MyApp> {
8488
),
8589
body: Center(
8690
child: ListView.builder(
87-
itemCount: 3,
91+
itemCount: 2,
8892
itemBuilder: (context, index) {
8993
var title = "";
9094
switch (index) {
9195
case 0:
92-
title = 'Message: $_message\n';
96+
title = _message;
9397
case 1:
94-
title = 'Enabled: $_enabled\n';
95-
case 2:
96-
title = 'Object: \n$_object\n';
98+
title = _object;
9799
}
98100
return ListTile(
99-
title: Text('Evaluation -> $title\n'),
101+
title: Text('$title\n'),
100102
);
101103
},
102104
),

example/pubspec.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ environment:
1616
dependencies:
1717
flutter:
1818
sdk: flutter
19+
flutter_dotenv: ^5.0.2
1920

2021
confidence_flutter_sdk:
2122
# When depending on this package from a real application you should use:
@@ -47,7 +48,8 @@ dev_dependencies:
4748

4849
# The following section is specific to Flutter packages.
4950
flutter:
50-
51+
assets:
52+
- .env
5153
# The following line ensures that the Material Icons font is
5254
# included with your application, so that you can use the icons in
5355
# the material Icons class.
@@ -82,4 +84,4 @@ flutter:
8284
# weight: 700
8385
#
8486
# For details regarding fonts from package dependencies,
85-
# see https://flutter.dev/custom-fonts/#from-packages
87+
# see https://flutter.dev/custom-fonts/#from-packages

example/test/widget_test.dart

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
// This is a basic Flutter widget test.
2-
//
3-
// To perform an interaction with a widget in your test, use the WidgetTester
4-
// utility in the flutter_test package. For example, you can send tap and scroll
5-
// gestures. You can also use WidgetTester to find child widgets in the widget
6-
// tree, read text, and verify that the values of widget properties are correct.
7-
8-
import 'package:flutter/material.dart';
1+
import 'package:flutter/cupertino.dart';
92
import 'package:flutter_test/flutter_test.dart';
10-
113
import 'package:confidence_flutter_sdk_example/main.dart';
4+
import 'package:integration_test/integration_test.dart';
125

136
void main() {
7+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
148
testWidgets('Verify Platform version', (WidgetTester tester) async {
159
// Build our app and trigger a frame.
16-
await tester.pumpWidget(const MyApp());
17-
18-
// Verify that platform version is retrieved.
19-
expect(
20-
find.byWidgetPredicate(
21-
(Widget widget) => widget is Text &&
22-
widget.data!.startsWith('Running on:'),
23-
),
24-
findsOneWidget,
25-
);
26-
});
10+
MyApp myApp = MyApp();
11+
await tester.pumpWidget(myApp);
12+
await myApp.initDone();
13+
// expect a list item with text evaluation exist
14+
await tester.pump();
15+
final textWidgets = find.byType(Text);
16+
int count = 0;
17+
textWidgets.evaluate().forEach((element) {
18+
if(count == 0) {
19+
final textWidget = element.widget as Text;
20+
final string = textWidget.data?.trim() ?? "";
21+
expect(string, "4");
22+
}
23+
if(count == 1) {
24+
final textWidget = element.widget as Text;
25+
final string = textWidget.data?.trim() ?? "";
26+
expect(string, "{my-integer: 4}");
27+
}
28+
count++;
29+
});
30+
});
2731
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'package:integration_test/integration_test_driver.dart';
2+
3+
Future<void> main() => integrationDriver();

0 commit comments

Comments
 (0)