|  | @@ -0,0 +1,110 @@
 | 
	
		
			
			|  | 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/services.dart';
 | 
	
		
			
			|  | 6 | +import 'package:flutter_test/flutter_test.dart';
 | 
	
		
			
			|  | 7 | +import 'package:image_picker/image_picker.dart';
 | 
	
		
			
			|  | 8 | +import 'package:zefyr/zefyr.dart';
 | 
	
		
			
			|  | 9 | +
 | 
	
		
			
			|  | 10 | +import '../testing.dart';
 | 
	
		
			
			|  | 11 | +
 | 
	
		
			
			|  | 12 | +void main() {
 | 
	
		
			
			|  | 13 | +  group('$ZefyrDefaultImageDelegate', () {
 | 
	
		
			
			|  | 14 | +    const MethodChannel channel =
 | 
	
		
			
			|  | 15 | +        const MethodChannel('plugins.flutter.io/image_picker');
 | 
	
		
			
			|  | 16 | +
 | 
	
		
			
			|  | 17 | +    final List<MethodCall> log = <MethodCall>[];
 | 
	
		
			
			|  | 18 | +
 | 
	
		
			
			|  | 19 | +    setUp(() {
 | 
	
		
			
			|  | 20 | +      channel.setMockMethodCallHandler((MethodCall methodCall) async {
 | 
	
		
			
			|  | 21 | +        log.add(methodCall);
 | 
	
		
			
			|  | 22 | +        return '/tmp/test.jpg';
 | 
	
		
			
			|  | 23 | +      });
 | 
	
		
			
			|  | 24 | +      log.clear();
 | 
	
		
			
			|  | 25 | +    });
 | 
	
		
			
			|  | 26 | +
 | 
	
		
			
			|  | 27 | +    test('pick image', () async {
 | 
	
		
			
			|  | 28 | +      final delegate = new ZefyrDefaultImageDelegate();
 | 
	
		
			
			|  | 29 | +      final result = await delegate.pickImage(ImageSource.gallery);
 | 
	
		
			
			|  | 30 | +      expect(result, 'file:///tmp/test.jpg');
 | 
	
		
			
			|  | 31 | +    });
 | 
	
		
			
			|  | 32 | +  });
 | 
	
		
			
			|  | 33 | +
 | 
	
		
			
			|  | 34 | +  group('$ZefyrImage', () {
 | 
	
		
			
			|  | 35 | +    const MethodChannel channel =
 | 
	
		
			
			|  | 36 | +        const MethodChannel('plugins.flutter.io/image_picker');
 | 
	
		
			
			|  | 37 | +
 | 
	
		
			
			|  | 38 | +    final List<MethodCall> log = <MethodCall>[];
 | 
	
		
			
			|  | 39 | +
 | 
	
		
			
			|  | 40 | +    setUp(() {
 | 
	
		
			
			|  | 41 | +      channel.setMockMethodCallHandler((MethodCall methodCall) async {
 | 
	
		
			
			|  | 42 | +        log.add(methodCall);
 | 
	
		
			
			|  | 43 | +        return '/tmp/test.jpg';
 | 
	
		
			
			|  | 44 | +      });
 | 
	
		
			
			|  | 45 | +      log.clear();
 | 
	
		
			
			|  | 46 | +    });
 | 
	
		
			
			|  | 47 | +
 | 
	
		
			
			|  | 48 | +    testWidgets('embed image', (tester) async {
 | 
	
		
			
			|  | 49 | +      final editor = new EditorSandBox(tester: tester);
 | 
	
		
			
			|  | 50 | +      await editor.tapEditor();
 | 
	
		
			
			|  | 51 | +      await editor.tapButtonWithIcon(Icons.photo);
 | 
	
		
			
			|  | 52 | +      await editor.tapButtonWithIcon(Icons.photo_camera);
 | 
	
		
			
			|  | 53 | +      LineNode line = editor.document.root.children.last;
 | 
	
		
			
			|  | 54 | +      expect(line.hasEmbed, isTrue);
 | 
	
		
			
			|  | 55 | +      EmbedNode embed = line.children.single;
 | 
	
		
			
			|  | 56 | +      expect(embed.style.value(NotusAttribute.embed), <String, dynamic>{
 | 
	
		
			
			|  | 57 | +        'type': 'image',
 | 
	
		
			
			|  | 58 | +        'source': 'file:///tmp/test.jpg',
 | 
	
		
			
			|  | 59 | +      });
 | 
	
		
			
			|  | 60 | +      expect(find.byType(ZefyrImage), findsOneWidget);
 | 
	
		
			
			|  | 61 | +    });
 | 
	
		
			
			|  | 62 | +
 | 
	
		
			
			|  | 63 | +    testWidgets('tap on left side of image puts caret before it',
 | 
	
		
			
			|  | 64 | +        (tester) async {
 | 
	
		
			
			|  | 65 | +      final editor = new EditorSandBox(tester: tester);
 | 
	
		
			
			|  | 66 | +      await editor.tapEditor();
 | 
	
		
			
			|  | 67 | +      await editor.tapButtonWithIcon(Icons.photo);
 | 
	
		
			
			|  | 68 | +      await editor.tapButtonWithIcon(Icons.photo_camera);
 | 
	
		
			
			|  | 69 | +      await editor.updateSelection(base: 0, extent: 0);
 | 
	
		
			
			|  | 70 | +
 | 
	
		
			
			|  | 71 | +      await tester.tapAt(tester.getTopLeft(find.byType(ZefyrImage)));
 | 
	
		
			
			|  | 72 | +      LineNode line = editor.document.root.children.last;
 | 
	
		
			
			|  | 73 | +      EmbedNode embed = line.children.single;
 | 
	
		
			
			|  | 74 | +      expect(editor.selection.isCollapsed, isTrue);
 | 
	
		
			
			|  | 75 | +      expect(editor.selection.extentOffset, embed.documentOffset);
 | 
	
		
			
			|  | 76 | +    });
 | 
	
		
			
			|  | 77 | +
 | 
	
		
			
			|  | 78 | +    testWidgets('tap right side of horizontal rule puts caret after it',
 | 
	
		
			
			|  | 79 | +        (tester) async {
 | 
	
		
			
			|  | 80 | +      final editor = new EditorSandBox(tester: tester);
 | 
	
		
			
			|  | 81 | +      await editor.tapEditor();
 | 
	
		
			
			|  | 82 | +      await editor.tapButtonWithIcon(Icons.photo);
 | 
	
		
			
			|  | 83 | +      await editor.tapButtonWithIcon(Icons.photo_camera);
 | 
	
		
			
			|  | 84 | +      await editor.updateSelection(base: 0, extent: 0);
 | 
	
		
			
			|  | 85 | +
 | 
	
		
			
			|  | 86 | +      final img = find.byType(ZefyrImage);
 | 
	
		
			
			|  | 87 | +      final offset = tester.getBottomRight(img) - new Offset(1.0, 1.0);
 | 
	
		
			
			|  | 88 | +      await tester.tapAt(offset);
 | 
	
		
			
			|  | 89 | +      LineNode line = editor.document.root.children.last;
 | 
	
		
			
			|  | 90 | +      EmbedNode embed = line.children.single;
 | 
	
		
			
			|  | 91 | +      expect(editor.selection.isCollapsed, isTrue);
 | 
	
		
			
			|  | 92 | +      expect(editor.selection.extentOffset, embed.documentOffset + 1);
 | 
	
		
			
			|  | 93 | +    });
 | 
	
		
			
			|  | 94 | +
 | 
	
		
			
			|  | 95 | +    testWidgets('selects on long press', (tester) async {
 | 
	
		
			
			|  | 96 | +      final editor = new EditorSandBox(tester: tester);
 | 
	
		
			
			|  | 97 | +      await editor.tapEditor();
 | 
	
		
			
			|  | 98 | +      await editor.tapButtonWithIcon(Icons.photo);
 | 
	
		
			
			|  | 99 | +      await editor.tapButtonWithIcon(Icons.photo_camera);
 | 
	
		
			
			|  | 100 | +      await editor.updateSelection(base: 0, extent: 0);
 | 
	
		
			
			|  | 101 | +
 | 
	
		
			
			|  | 102 | +      final img = find.byType(ZefyrImage);
 | 
	
		
			
			|  | 103 | +      await tester.longPress(img);
 | 
	
		
			
			|  | 104 | +      LineNode line = editor.document.root.children.last;
 | 
	
		
			
			|  | 105 | +      EmbedNode embed = line.children.single;
 | 
	
		
			
			|  | 106 | +      expect(editor.selection.baseOffset, embed.documentOffset);
 | 
	
		
			
			|  | 107 | +      expect(editor.selection.extentOffset, embed.documentOffset + 1);
 | 
	
		
			
			|  | 108 | +    });
 | 
	
		
			
			|  | 109 | +  });
 | 
	
		
			
			|  | 110 | +}
 |