yx_only_office_flutter/example/integration_test/viewer_test.dart

126 lines
4.8 KiB
Dart

import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:yx_only_office_flutter/yx_only_office_flutter.dart';
Future<void> main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
// await dotenv.load(fileName: ".env"); // Temporarily disabled for testing
testWidgets('Renders OnlyOffice viewer with public document', (WidgetTester tester) async {
const serverUrl = 'https://document.23544.com/';
// final jwt = dotenv.env['ONLYOFFICE_JWT'];
const jwtSecret = '6Yr6DGoVV3ACS6GtVgdH453mXxLftd6Q'; // Hardcoded for testing
const documentUrl = 'https://filesamples.com/samples/document/docx/sample3.docx';
final documentKey = OnlyOfficeDocument.generateKeyFromUrl(documentUrl);
final configPayload = OnlyOfficeConfig(
documentType: 'word',
document: OnlyOfficeDocument(title: 'Sample Document', url: documentUrl, fileType: 'docx', key: documentKey),
editorConfig: const OnlyOfficeEditorConfig(
user: OnlyOfficeUser(id: 'test-user-id', name: 'Test User'),
mode: 'view',
),
);
final jwt = JWT(configPayload.toJson());
final token = jwt.sign(SecretKey(jwtSecret));
final config = configPayload.copyWith(token: token);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: YxOnlyOfficeViewer(
serverUrl: serverUrl,
config: config,
onError: (error) {
debugPrint('OnlyOffice Error: $error');
},
errorBuilder: (context, error) => Center(child: Text('Error loading document: $error')),
),
),
),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pumpAndSettle(const Duration(seconds: 15));
expect(find.byType(CircularProgressIndicator), findsNothing);
expect(find.byType(YxOnlyOfficeViewer), findsOneWidget);
});
testWidgets('Renders OnlyOffice viewer with PPTX document', (WidgetTester tester) async {
const serverUrl = 'https://document.23544.com/';
// final jwt = dotenv.env['ONLYOFFICE_JWT'];
const jwtSecret = '6Yr6DGoVV3ACS6GtVgdH453mXxLftd6Q'; // Hardcoded for testing
const documentUrl = 'https://quanxue-oa.oss-cn-chengdu.aliyuncs.com/20250815/1755244744547.pptx';
final documentKey = OnlyOfficeDocument.generateKeyFromUrl(documentUrl);
final configPayload = OnlyOfficeConfig(
documentType: 'slide',
document: OnlyOfficeDocument(title: 'Sample PPTX', url: documentUrl, fileType: 'pptx', key: documentKey),
editorConfig: const OnlyOfficeEditorConfig(
user: OnlyOfficeUser(id: 'test-user-id-pptx', name: 'Test User PPTX'),
mode: 'view',
),
);
final jwt = JWT(configPayload.toJson());
final token = jwt.sign(SecretKey(jwtSecret));
final config = configPayload.copyWith(token: token);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: YxOnlyOfficeViewer(serverUrl: serverUrl, config: config),
),
),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pumpAndSettle(const Duration(seconds: 15));
expect(find.byType(CircularProgressIndicator), findsNothing);
expect(find.byType(YxOnlyOfficeViewer), findsOneWidget);
});
testWidgets('Renders OnlyOffice viewer with new DOCX document', (WidgetTester tester) async {
const serverUrl = 'https://document.23544.com/';
// final jwt = dotenv.env['ONLYOFFICE_JWT'];
const jwtSecret = '6Yr6DGoVV3ACS6GtVgdH453mXxLftd6Q'; // Hardcoded for testing
const documentUrl = 'https://quanxue-oa.oss-cn-chengdu.aliyuncs.com/20250905/1757052622898.docx';
final documentKey = OnlyOfficeDocument.generateKeyFromUrl(documentUrl);
final configPayload = OnlyOfficeConfig(
documentType: 'word',
document: OnlyOfficeDocument(title: 'Sample DOCX', url: documentUrl, fileType: 'docx', key: documentKey),
editorConfig: const OnlyOfficeEditorConfig(
user: OnlyOfficeUser(id: 'test-user-id-docx', name: 'Test User DOCX'),
mode: 'view',
),
);
final jwt = JWT(configPayload.toJson());
final token = jwt.sign(SecretKey(jwtSecret));
final config = configPayload.copyWith(token: token);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: YxOnlyOfficeViewer(serverUrl: serverUrl, config: config),
),
),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pumpAndSettle(const Duration(seconds: 15));
expect(find.byType(CircularProgressIndicator), findsNothing);
expect(find.byType(YxOnlyOfficeViewer), findsOneWidget);
});
}