document_with_link_test.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:appflowy_backend/protobuf/flowy-folder2/protobuf.dart';
  2. import 'package:appflowy_editor/appflowy_editor.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. import 'package:integration_test/integration_test.dart';
  7. import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
  8. import '../util/util.dart';
  9. void main() {
  10. IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  11. group('test editing link in document', () {
  12. late MockUrlLauncher mock;
  13. setUp(() {
  14. mock = MockUrlLauncher();
  15. UrlLauncherPlatform.instance = mock;
  16. });
  17. testWidgets('insert/edit/open link', (tester) async {
  18. await tester.initializeAppFlowy();
  19. await tester.tapGoButton();
  20. // create a new document
  21. await tester.createNewPageWithName(
  22. ViewLayoutPB.Document,
  23. );
  24. // tap the first line of the document
  25. await tester.editor.tapLineOfEditorAt(0);
  26. // insert a inline page
  27. const link = 'AppFlowy';
  28. await tester.ime.insertText(link);
  29. await tester.editor.updateSelection(
  30. Selection.single(path: [0], startOffset: 0, endOffset: link.length),
  31. );
  32. // tap the link button
  33. final linkButton = find.byTooltip(
  34. 'Link',
  35. );
  36. await tester.tapButton(linkButton);
  37. expect(find.text('Add your link', findRichText: true), findsOneWidget);
  38. // input the link
  39. const url = 'https://appflowy.io';
  40. final textField = find.byWidgetPredicate(
  41. (widget) => widget is TextField && widget.decoration!.hintText == 'URL',
  42. );
  43. await tester.enterText(textField, url);
  44. await tester.testTextInput.receiveAction(TextInputAction.done);
  45. await tester.pumpAndSettle();
  46. // single-click the link menu to show the menu
  47. await tester.tapButton(find.text(link, findRichText: true));
  48. expect(find.text('Open link', findRichText: true), findsOneWidget);
  49. expect(find.text('Copy link', findRichText: true), findsOneWidget);
  50. expect(find.text('Remove link', findRichText: true), findsOneWidget);
  51. // double-click the link menu to open the link
  52. mock
  53. ..setLaunchExpectations(
  54. url: url,
  55. useSafariVC: false,
  56. useWebView: false,
  57. universalLinksOnly: false,
  58. enableJavaScript: true,
  59. enableDomStorage: true,
  60. headers: <String, String>{},
  61. webOnlyWindowName: null,
  62. launchMode: PreferredLaunchMode.platformDefault,
  63. )
  64. ..setResponse(true);
  65. await tester.simulateKeyEvent(LogicalKeyboardKey.escape);
  66. await tester.doubleTapAt(
  67. tester.getTopLeft(find.text(link, findRichText: true)).translate(5, 5),
  68. );
  69. expect(mock.canLaunchCalled, isTrue);
  70. expect(mock.launchCalled, isTrue);
  71. });
  72. });
  73. }