huawei_health_registration_dialog_test.dart
6.58 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import 'package:doublefeel_flutter/app/actions/dialog_action.dart';
import 'dart:async';
import 'package:doublefeel_flutter/core/error/app_error.dart';
import 'package:doublefeel_flutter/core/network/api/harmony_api.dart';
import 'package:doublefeel_flutter/core/result/app_result.dart';
import 'package:doublefeel_flutter/app/modules/home/widgets/today/huawei_health_registration_dialog.dart';
import 'package:doublefeel_flutter/app/modules/webview/controllers/hw_health_auth_webview_controller.dart';
import 'package:doublefeel_flutter/app/routes/app_pages.dart';
import 'package:doublefeel_flutter/core/platform/pigeon_api_facade.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
class _HarmonyApi extends Fake implements HarmonyApi {
final response = Completer<AppResult<void>>();
final submittedCodes = <String>[];
@override
Future<AppResult<void>> postHmAuth(String code) {
submittedCodes.add(code);
return response.future;
}
}
void main() {
const channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.doublefeel_flutter.PlatformHostApi.nativeHandleUrl',
StandardMessageCodec(),
);
testWidgets('registration dialog waits for confirmation', (tester) async {
addTearDown(Get.reset);
DialogAction? confirmed;
await tester.pumpWidget(GetMaterialApp(
home: Scaffold(
body: TextButton(
onPressed: () async {
confirmed = await HuaweiHealthRegistrationDialog.show();
},
child: const Text('show'),
)),
));
await tester.tap(find.text('show'));
await tester.pumpAndSettle();
expect(find.text('该账号暂未注册华为运动健康'), findsOneWidget);
expect(find.text('请注册后再进行授权'), findsOneWidget);
expect(find.byType(Image), findsNothing);
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(confirmed, isNull);
expect(find.text('去注册'), findsOneWidget);
await tester.tap(find.text('去注册'));
await tester.pumpAndSettle();
expect(confirmed, DialogAction.confirm);
expect(find.text('去注册'), findsNothing);
expect(tester.takeException(), isNull);
});
testWidgets('unbind returns a distinct action without registering',
(tester) async {
addTearDown(Get.reset);
DialogAction? action;
await tester.pumpWidget(GetMaterialApp(
home: Scaffold(
body: TextButton(
onPressed: () async {
action = await HuaweiHealthRegistrationDialog.show();
},
child: const Text('show'),
)),
));
await tester.tap(find.text('show'));
await tester.pumpAndSettle();
await tester.tap(find.text('解除绑定该账号'));
await tester.pumpAndSettle();
expect(action, DialogAction.cancel);
expect(find.text('去注册'), findsNothing);
expect(tester.takeException(), isNull);
});
for (final error in [
'3',
'access_denied',
'invalid_state',
'backend_success',
'backend_network_failure',
'backend_server_failure'
]) {
testWidgets('Huawei authorization result $error', (tester) async {
tester.view.physicalSize = const Size(375, 812);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final openedUrls = <Object?>[];
final harmonyApi = _HarmonyApi();
Get.put<HarmonyApi>(harmonyApi);
final isBackendResult = error.startsWith('backend_');
tester.binding.defaultBinaryMessenger
.setMockDecodedMessageHandler<Object?>(
channel,
(message) async {
openedUrls.add((message! as List<Object?>).single);
return [true];
},
);
addTearDown(() {
tester.binding.defaultBinaryMessenger
.setMockDecodedMessageHandler<Object?>(channel, null);
Get.reset();
});
HealthAuthorizationStatus? authorization;
await tester.pumpWidget(GetMaterialApp(
home: Scaffold(
body: TextButton(
onPressed: () async {
authorization = await AppHealthKitHostApi(
appPlatform: AppPigeonPlatform.ohos,
).requestHealthClientAuthorization();
},
child: const Text('authorize'),
),
),
getPages: [
GetPage(
name: Routes.HW_HEALTH_AUTH_WEB_VIEW,
page: () => Scaffold(
body: TextButton(
onPressed: () {
final arguments =
Get.arguments as HwHealthAuthWebviewArguments;
final state = Uri.parse(arguments.authorizationUrl)
.queryParameters['state'];
Get.back(
result: HealthAuthEvent(
state: error == 'invalid_state' ? 'wrong' : state,
code: isBackendResult ? 'test-authorization-code' : null,
error: isBackendResult
? null
: (error == 'invalid_state' ? '3' : error),
));
},
child: const Text('return authorization'),
),
),
),
],
));
await tester.tap(find.text('authorize'));
await tester.pumpAndSettle();
await tester.tap(find.text('return authorization'));
await tester.pumpAndSettle();
if (isBackendResult) {
expect(harmonyApi.submittedCodes, ['test-authorization-code']);
// A valid OAuth code alone must not report authorization success.
expect(authorization, isNull);
expect(openedUrls, isEmpty);
harmonyApi.response.complete(switch (error) {
'backend_success' => const AppSuccess<void>(null),
'backend_network_failure' =>
const AppFailure<void>(AppNetworkError('offline')),
_ => const AppFailure<void>(AppHttpError(statusCode: 500)),
});
await tester.pumpAndSettle();
} else {
expect(harmonyApi.submittedCodes, isEmpty);
}
expect(openedUrls, isEmpty);
// The facade reports the result without displaying UI or opening apps.
expect(find.text('去注册'), findsNothing);
expect(
authorization,
switch (error) {
'3' => HealthAuthorizationStatus.activationRequired,
'backend_success' => HealthAuthorizationStatus.authorized,
_ => HealthAuthorizationStatus.unauthorized,
},
);
expect(tester.takeException(), isNull);
});
}
}