zefyr

scope_test.dart 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. import 'package:zefyr/zefyr.dart';
  7. void main() {
  8. group('$ZefyrScope', () {
  9. ZefyrScope scope;
  10. setUp(() {
  11. WidgetsFlutterBinding.ensureInitialized();
  12. final doc = NotusDocument();
  13. scope = ZefyrScope.editable(
  14. controller: ZefyrController(doc),
  15. imageDelegate: ZefyrDefaultImageDelegate(),
  16. focusNode: FocusNode(),
  17. focusScope: FocusScopeNode(),
  18. );
  19. });
  20. test('it notifies on image delegate update', () {
  21. bool notified = false;
  22. scope.addListener(() {
  23. notified = true;
  24. });
  25. final delegate = ZefyrDefaultImageDelegate();
  26. scope.imageDelegate = delegate;
  27. expect(notified, isTrue);
  28. notified = false;
  29. scope.imageDelegate = delegate;
  30. expect(notified, isFalse);
  31. });
  32. test('it notifies on controller update', () {
  33. bool notified = false;
  34. scope.addListener(() {
  35. notified = true;
  36. });
  37. final controller = ZefyrController(NotusDocument());
  38. scope.controller = controller;
  39. expect(notified, isTrue);
  40. notified = false;
  41. scope.controller = controller;
  42. expect(notified, isFalse);
  43. });
  44. test('it notifies on focus node update', () {
  45. bool notified = false;
  46. scope.addListener(() {
  47. notified = true;
  48. });
  49. final focusNode = FocusNode();
  50. scope.focusNode = focusNode;
  51. expect(notified, isTrue);
  52. notified = false;
  53. scope.focusNode = focusNode;
  54. expect(notified, isFalse);
  55. });
  56. test('it notifies on selection changes but not text changes', () {
  57. bool notified = false;
  58. scope.addListener(() {
  59. notified = true;
  60. });
  61. scope.controller.replaceText(0, 0, 'Hello');
  62. expect(notified, isFalse);
  63. scope.controller.updateSelection(TextSelection.collapsed(offset: 4));
  64. expect(notified, isTrue);
  65. });
  66. });
  67. }