|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/gestures.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter/rendering.dart'; |
| 8 | +import 'package:flutter_api_samples/material/selection_container/selection_container_disabled.0.dart' as example; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + testWidgets('A SelectionContainer.disabled should disable selections', (WidgetTester tester) async { |
| 13 | + await tester.pumpWidget( |
| 14 | + const example.SelectionContainerDisabledExampleApp(), |
| 15 | + ); |
| 16 | + |
| 17 | + expect(find.widgetWithText(AppBar, 'SelectionContainer.disabled Sample'), findsOne); |
| 18 | + |
| 19 | + final RenderParagraph paragraph1 = tester.renderObject<RenderParagraph>( |
| 20 | + find.descendant( |
| 21 | + of: find.text('Selectable text').first, |
| 22 | + matching: find.byType(RichText), |
| 23 | + ), |
| 24 | + ); |
| 25 | + final Rect paragraph1Rect = tester.getRect(find.text('Selectable text').first); |
| 26 | + final TestGesture gesture = await tester.startGesture( |
| 27 | + paragraph1Rect.centerLeft, |
| 28 | + kind: PointerDeviceKind.mouse, |
| 29 | + ); |
| 30 | + addTearDown(gesture.removePointer); |
| 31 | + await tester.pump(); |
| 32 | + |
| 33 | + await gesture.moveTo(paragraph1Rect.center); |
| 34 | + await tester.pump(); |
| 35 | + expect( |
| 36 | + paragraph1.selections.first, |
| 37 | + const TextSelection(baseOffset: 0, extentOffset: 7), |
| 38 | + ); |
| 39 | + |
| 40 | + final RenderParagraph paragraph2 = tester.renderObject<RenderParagraph>( |
| 41 | + find.descendant( |
| 42 | + of: find.text('Non-selectable text'), |
| 43 | + matching: find.byType(RichText), |
| 44 | + ), |
| 45 | + ); |
| 46 | + final Rect paragraph2Rect = tester.getRect(find.text('Non-selectable text')); |
| 47 | + await gesture.moveTo(paragraph2Rect.center); |
| 48 | + // Should select the rest of paragraph 1. |
| 49 | + expect( |
| 50 | + paragraph1.selections.first, |
| 51 | + const TextSelection(baseOffset: 0, extentOffset: 15), |
| 52 | + ); |
| 53 | + // paragraph2 is in a disabled container. |
| 54 | + expect(paragraph2.selections, isEmpty); |
| 55 | + |
| 56 | + final RenderParagraph paragraph3 = tester.renderObject<RenderParagraph>( |
| 57 | + find.descendant( |
| 58 | + of: find.text('Selectable text').last, |
| 59 | + matching: find.byType(RichText), |
| 60 | + ), |
| 61 | + ); |
| 62 | + final Rect paragraph3Rect = tester.getRect(find.text('Selectable text').last); |
| 63 | + await gesture.moveTo(paragraph3Rect.center); |
| 64 | + expect( |
| 65 | + paragraph1.selections.first, |
| 66 | + const TextSelection(baseOffset: 0, extentOffset: 15), |
| 67 | + ); |
| 68 | + expect(paragraph2.selections, isEmpty); |
| 69 | + expect( |
| 70 | + paragraph3.selections.first, |
| 71 | + const TextSelection(baseOffset: 0, extentOffset: 7), |
| 72 | + ); |
| 73 | + |
| 74 | + await gesture.up(); |
| 75 | + }); |
| 76 | +} |
0 commit comments