Skip to content

Commit 75778b2

Browse files
committed
implement unit testing for new added features
1 parent a2535b9 commit 75778b2

8 files changed

+401
-4
lines changed

lib/Pages/torrent_screen.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class TorrentScreen extends StatefulWidget {
2323
_TorrentScreenState createState() => _TorrentScreenState();
2424
}
2525

26-
List<String> trackerURIsList = [];
27-
2826
class _TorrentScreenState extends State<TorrentScreen> {
2927
String keyword = '';
3028

lib/Provider/filter_provider.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ class FilterProvider extends ChangeNotifier {
1616
List<String> statusList = [];
1717
var mapStatus = {};
1818
String tagSelected = '';
19-
List<String> tagList = [];
2019
Map<String, dynamic> mapTags = {'Untagged': 0};
2120
String trackerURISelected = '';
22-
List<String> trackerURIsList = [];
2321
var trackersSizeList = [];
2422
var tagsSizeList = [];
2523
var maptrackerURIs = {};

test/api_provider_unit_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flood_mobile/Provider/api_provider.dart';
22
import 'package:flutter_test/flutter_test.dart';
33

44
void main() {
5+
TestWidgetsFlutterBinding.ensureInitialized();
56
late ApiProvider sut;
67

78
setUp(() {

test/client_provider_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flood_mobile/Model/client_settings_model.dart';
2+
import 'package:flood_mobile/Provider/client_provider.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
late ClientSettingsProvider sut;
7+
late ClientSettingsModel newClientSettings;
8+
9+
setUp(() {
10+
sut = ClientSettingsProvider();
11+
newClientSettings = ClientSettingsModel(
12+
dht: false,
13+
dhtPort: 1234,
14+
directoryDefault: 'path/to/directory',
15+
networkHttpMaxOpen: 10,
16+
networkLocalAddress: ['192.168.0.1', '192.168.0.2'],
17+
networkMaxOpenFiles: 100,
18+
networkPortOpen: true,
19+
networkPortRandom: false,
20+
networkPortRange: '6881-6889',
21+
piecesHashOnCompletion: true,
22+
piecesMemoryMax: 512,
23+
protocolPex: true,
24+
throttleGlobalDownSpeed: 1024,
25+
throttleGlobalUpSpeed: 2048,
26+
throttleMaxDownloads: 5,
27+
throttleMaxDownloadsGlobal: 10,
28+
throttleMaxPeersNormal: 50,
29+
throttleMaxPeersSeed: 100,
30+
throttleMaxUploads: 3,
31+
throttleMaxUploadsGlobal: 6,
32+
throttleMinPeersNormal: 10,
33+
throttleMinPeersSeed: 20,
34+
trackersNumWant: 30,
35+
);
36+
});
37+
38+
test(
39+
"initial values are correct",
40+
() {
41+
sut.setClientSettings(newClientSettings);
42+
expect(sut.clientSettings, newClientSettings);
43+
},
44+
);
45+
}

test/color_provider_unit_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:flood_mobile/Components/onboarding/data/onboard_page_data.dart';
2+
import 'package:flood_mobile/Provider/color_provider.dart';
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
6+
void main() {
7+
group('ColorProvider', () {
8+
late ColorProvider colorProvider;
9+
10+
setUp(() {
11+
colorProvider = ColorProvider();
12+
});
13+
14+
test(
15+
'Initial color should match the first accent color from the onboard data',
16+
() {
17+
expect(colorProvider.color, equals(onboardData[0].accentColor));
18+
});
19+
20+
test('Setting a new color should update the color value', () {
21+
const newColor = Colors.blue;
22+
colorProvider.color = newColor;
23+
expect(colorProvider.color, equals(newColor));
24+
});
25+
});
26+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:flood_mobile/Provider/filter_provider.dart';
3+
4+
void main() {
5+
group('FilterProvider', () {
6+
late FilterProvider filterProvider;
7+
8+
setUp(() {
9+
filterProvider = FilterProvider();
10+
});
11+
12+
test('setFilterSelected should update filterStatus', () {
13+
final newFilterStatus = FilterValue.active;
14+
expect(filterProvider.filterStatus, equals(FilterValue.all));
15+
filterProvider.setFilterSelected(newFilterStatus);
16+
expect(filterProvider.filterStatus, equals(newFilterStatus));
17+
});
18+
19+
test('setmapStatus should update mapStatus', () {
20+
final newMapStatus = {'status1': true, 'status2': false};
21+
expect(filterProvider.mapStatus, isEmpty);
22+
filterProvider.setmapStatus(newMapStatus);
23+
expect(filterProvider.mapStatus, equals(newMapStatus));
24+
});
25+
26+
test('setstatusList should update statusList', () {
27+
final newStatusList = ['status1', 'status2', 'status3'];
28+
expect(filterProvider.statusList, isEmpty);
29+
filterProvider.setstatusList(newStatusList);
30+
expect(filterProvider.statusList, equals(newStatusList));
31+
});
32+
33+
test('setTagSelected should update tagSelected and notify listeners', () {
34+
final newTagSelected = 'New Tag';
35+
expect(filterProvider.tagSelected, isEmpty);
36+
filterProvider.setTagSelected(newTagSelected);
37+
expect(filterProvider.tagSelected, equals(newTagSelected));
38+
});
39+
40+
test('setmapTags should update mapTags', () {
41+
final newMapTags = {'tag1': 0, 'tag2': 1, 'tag3': 2};
42+
expect(filterProvider.mapTags, equals({'Untagged': 0}));
43+
filterProvider.setmapTags(newMapTags);
44+
expect(filterProvider.mapTags, equals(newMapTags));
45+
});
46+
47+
test('setTagsSizeList should update tagsSizeList', () {
48+
final newTagsSizeList = [10, 20, 30];
49+
expect(filterProvider.tagsSizeList, isEmpty);
50+
filterProvider.setTagsSizeList(newTagsSizeList);
51+
expect(filterProvider.tagsSizeList, equals(newTagsSizeList));
52+
});
53+
54+
test(
55+
'settrackerURISelected should update trackerURISelected and notify listeners',
56+
() {
57+
final newTrackerURISelected = 'New Tracker URI';
58+
expect(filterProvider.trackerURISelected, isEmpty);
59+
filterProvider.settrackerURISelected(newTrackerURISelected);
60+
expect(filterProvider.trackerURISelected, equals(newTrackerURISelected));
61+
});
62+
63+
test('setmaptrackerURIs should update maptrackerURIs', () {
64+
final newMaptrackerURIs = {'uri1': true, 'uri2': false};
65+
expect(filterProvider.maptrackerURIs, isEmpty);
66+
filterProvider.setmaptrackerURIs(newMaptrackerURIs);
67+
expect(filterProvider.maptrackerURIs, equals(newMaptrackerURIs));
68+
});
69+
70+
test('setTrackersSizeList should update trackersSizeList', () {
71+
final newTrackersSizeList = [100, 200, 300];
72+
expect(filterProvider.trackersSizeList, isEmpty);
73+
filterProvider.setTrackersSizeList(newTrackersSizeList);
74+
expect(filterProvider.trackersSizeList, equals(newTrackersSizeList));
75+
});
76+
});
77+
}

test/graph_provider_unit_test.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import 'package:flood_mobile/Provider/graph_provider.dart';
2+
import 'package:flood_mobile/Provider/home_provider.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
group('GraphProvider', () {
7+
late GraphProvider graphProvider;
8+
9+
setUp(() {
10+
graphProvider = GraphProvider();
11+
});
12+
13+
test('Initial upload graph data should have 30 items with speed 0.00', () {
14+
expect(graphProvider.uploadGraphData.length, equals(30));
15+
16+
for (final item in graphProvider.uploadGraphData) {
17+
expect(item.speed, equals(0.00));
18+
}
19+
});
20+
21+
test('Initial download graph data should have 30 items with speed 0.00',
22+
() {
23+
expect(graphProvider.downloadGraphData.length, equals(30));
24+
25+
for (final item in graphProvider.downloadGraphData) {
26+
expect(item.speed, equals(0.00));
27+
}
28+
});
29+
30+
test('Initial fake time should be 31', () {
31+
expect(graphProvider.fakeTime, equals(31));
32+
});
33+
34+
test('Initial show chart status should be false', () {
35+
expect(graphProvider.showChart, isFalse);
36+
});
37+
38+
test('Changing chart status should toggle the show chart status', () {
39+
final initialStatus = graphProvider.showChart;
40+
41+
graphProvider.changChartStatus();
42+
43+
expect(graphProvider.showChart, isNot(equals(initialStatus)));
44+
45+
graphProvider.changChartStatus();
46+
47+
expect(graphProvider.showChart, equals(initialStatus));
48+
});
49+
50+
test('updateDataSource should update the graph data correctly', () {
51+
final graphProvider = GraphProvider();
52+
final homeProvider = HomeProvider();
53+
homeProvider.upSpeed = '100 KB/s';
54+
homeProvider.downSpeed = '200 KB/s';
55+
56+
graphProvider.updateDataSource(homeProvider);
57+
58+
final uploadGraphData = graphProvider.uploadGraphData;
59+
final downloadGraphData = graphProvider.downloadGraphData;
60+
61+
// Check if the last entry in uploadGraphData is updated correctly
62+
expect(uploadGraphData.last.speed, 100.0);
63+
expect(uploadGraphData.last.second, 31);
64+
65+
// Check if the last entry in downloadGraphData is updated correctly
66+
expect(downloadGraphData.last.speed, 200.0);
67+
expect(downloadGraphData.last.second, 31);
68+
69+
// Check if the first entry in uploadGraphData is removed
70+
expect(uploadGraphData.length, 30);
71+
expect(uploadGraphData.first.speed, 0.0);
72+
expect(uploadGraphData.first.second, 2);
73+
74+
// Check if the first entry in downloadGraphData is removed
75+
expect(downloadGraphData.length, 30);
76+
expect(downloadGraphData.first.speed, 0.0);
77+
expect(downloadGraphData.first.second, 2);
78+
79+
// Check if the fakeTime is incremented
80+
expect(graphProvider.fakeTime, 32);
81+
});
82+
});
83+
}

0 commit comments

Comments
 (0)