forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome_provider_languages_test.dart
More file actions
162 lines (123 loc) · 5.79 KB
/
Copy pathhome_provider_languages_test.dart
File metadata and controls
162 lines (123 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:omi/backend/preferences.dart';
import 'package:omi/providers/home_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// The picker list moved to the backend. What matters is which source wins.
void main() {
setUp(() async {
SharedPreferences.setMockInitialValues({});
await SharedPreferencesUtil.init();
});
group('source of the picker list', () {
test('falls back to the bundled list before anything is fetched', () {
final provider = HomeProvider();
addTearDown(provider.dispose);
// A first run with no network must still offer languages.
expect(provider.availableLanguages, isNotEmpty);
expect(provider.availableLanguages['English'], 'en');
});
test('a cached list from a previous session wins over the bundled copy', () async {
SharedPreferencesUtil().cachedAvailableLanguages = jsonEncode({'Swahili': 'sw', 'English': 'en'});
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => null);
// Swahili is not in the bundled copy — proving the cache is being read.
expect(provider.availableLanguages['Swahili'], 'sw');
});
test('a corrupt cache falls back instead of emptying the picker', () async {
SharedPreferencesUtil().cachedAvailableLanguages = 'not json';
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => null);
expect(provider.availableLanguages['English'], 'en');
});
test('an empty cached map is ignored', () async {
SharedPreferencesUtil().cachedAvailableLanguages = jsonEncode(<String, String>{});
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => null);
expect(provider.availableLanguages, isNotEmpty);
});
});
group('the served list wins', () {
test('over the bundled copy', () async {
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => {'Swahili': 'sw'});
expect(provider.availableLanguages, {'Swahili': 'sw'});
expect(provider.availableLanguages.containsKey('Japanese'), isFalse);
});
test('over a cached list, and replaces the cache', () async {
SharedPreferencesUtil().cachedAvailableLanguages = jsonEncode({'Klingon': 'tlh'});
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => {'Welsh': 'cy'});
expect(provider.availableLanguages, {'Welsh': 'cy'});
expect(jsonDecode(SharedPreferencesUtil().cachedAvailableLanguages), {'Welsh': 'cy'});
});
test('a null fetch keeps the cached list and does not clear the cache', () async {
SharedPreferencesUtil().cachedAvailableLanguages = jsonEncode({'Swahili': 'sw'});
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => null);
expect(provider.availableLanguages, {'Swahili': 'sw'});
expect(jsonDecode(SharedPreferencesUtil().cachedAvailableLanguages), {'Swahili': 'sw'});
});
test('a throwing fetch falls back to the bundled copy', () async {
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => throw Exception('offline'));
expect(provider.availableLanguages['English'], 'en');
});
test('an empty served list is ignored rather than emptying the picker', () async {
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => <String, String>{});
expect(provider.availableLanguages['English'], 'en');
});
});
group('a sign-out mid-request', () {
test('does not install the list it was fetching', () async {
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadLanguagesThenSetupPrimary(fetch: () async {
provider.clearUserData(); // signs out while the request is in flight
return {'Swahili': 'sw'};
});
expect(provider.availableLanguages.containsKey('Swahili'), isFalse);
expect(provider.availableLanguages['English'], 'en');
});
test('leaves the cache untouched', () async {
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadLanguagesThenSetupPrimary(fetch: () async {
provider.clearUserData();
return {'Swahili': 'sw'};
});
expect(SharedPreferencesUtil().cachedAvailableLanguages, isEmpty);
});
});
group('getLanguageName', () {
test('resolves a code the current list carries', () {
final provider = HomeProvider();
addTearDown(provider.dispose);
expect(provider.getLanguageName('en'), 'English');
});
test('falls back to the bundled name for a code the server list dropped', () async {
// The server list changes without an app release, so a stored preference
// can outlive the list that offered it.
SharedPreferencesUtil().cachedAvailableLanguages = jsonEncode({'English': 'en'});
final provider = HomeProvider();
addTearDown(provider.dispose);
await provider.loadAvailableLanguages(fetch: () async => null);
expect(provider.getLanguageName('ja'), 'Japanese');
});
test('returns the code itself for one nothing knows about', () {
// Previously threw a StateError from firstWhere.
final provider = HomeProvider();
addTearDown(provider.dispose);
expect(provider.getLanguageName('zz'), 'zz');
});
});
}