image_rtl_test.dart 24 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6
import 'dart:typed_data';
7
import 'dart:ui' as ui show Image, ImageByteFormat;
Ian Hickson's avatar
Ian Hickson committed
8 9 10 11 12 13 14 15 16 17

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

import '../rendering/mock_canvas.dart';

class TestImageProvider extends ImageProvider<TestImageProvider> {
  @override
  Future<TestImageProvider> obtainKey(ImageConfiguration configuration) {
18
    return SynchronousFuture<TestImageProvider>(this);
Ian Hickson's avatar
Ian Hickson committed
19 20 21 22
  }

  @override
  ImageStreamCompleter load(TestImageProvider key) {
23 24
    return OneFrameImageStreamCompleter(
      SynchronousFuture<ImageInfo>(ImageInfo(image: TestImage()))
Ian Hickson's avatar
Ian Hickson committed
25 26 27 28
    );
  }
}

29
class TestImage implements ui.Image {
Ian Hickson's avatar
Ian Hickson committed
30 31 32 33 34 35
  @override
  int get width => 16;

  @override
  int get height => 9;

36 37
  @override
  void dispose() { }
38 39

  @override
40
  Future<ByteData> toByteData({ ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba }) async {
41
    throw UnsupportedError('Cannot encode test image');
42
  }
Ian Hickson's avatar
Ian Hickson committed
43 44 45 46 47
}

void main() {
  testWidgets('DecorationImage RTL with alignment topEnd and match', (WidgetTester tester) async {
    await tester.pumpWidget(
48
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
49
        textDirection: TextDirection.rtl,
50 51
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
52 53
            width: 100.0,
            height: 50.0,
54 55 56
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
57
                alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
58 59 60 61 62 63 64
                repeat: ImageRepeat.repeatX,
                matchTextDirection: true,
              ),
            ),
          ),
        ),
      ),
65
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
66 67 68
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
69
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
Ian Hickson's avatar
Ian Hickson committed
70 71 72
      ..translate(x: 50.0, y: 0.0)
      ..scale(x: -1.0, y: 1.0)
      ..translate(x: -50.0, y: 0.0)
Dan Field's avatar
Dan Field committed
73 74 75 76 77 78 79
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(-12.0, 0.0, 4.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(4.0, 0.0, 20.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(20.0, 0.0, 36.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(36.0, 0.0, 52.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(52.0, 0.0, 68.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(68.0, 0.0, 84.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 0.0, 100.0, 9.0))
80
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
81 82 83 84 85 86
    );
    expect(find.byType(Container), isNot(paints..scale()..scale()));
  });

  testWidgets('DecorationImage LTR with alignment topEnd (and pointless match)', (WidgetTester tester) async {
    await tester.pumpWidget(
87
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
88
        textDirection: TextDirection.ltr,
89 90
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
91 92
            width: 100.0,
            height: 50.0,
93 94 95
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
96
                alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
97 98 99 100 101 102 103
                repeat: ImageRepeat.repeatX,
                matchTextDirection: true,
              ),
            ),
          ),
        ),
      ),
104
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
105 106 107
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
108 109 110 111 112 113 114 115
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(-12.0, 0.0, 4.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(4.0, 0.0, 20.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(20.0, 0.0, 36.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(36.0, 0.0, 52.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(52.0, 0.0, 68.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(68.0, 0.0, 84.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 0.0, 100.0, 9.0))
116
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
117 118 119 120 121 122
    );
    expect(find.byType(Container), isNot(paints..scale()));
  });

  testWidgets('DecorationImage RTL with alignment topEnd', (WidgetTester tester) async {
    await tester.pumpWidget(
123
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
124
        textDirection: TextDirection.rtl,
125 126
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
127 128
            width: 100.0,
            height: 50.0,
129 130 131
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
132
                alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
133 134 135 136 137 138
                repeat: ImageRepeat.repeatX,
              ),
            ),
          ),
        ),
      ),
139
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
140 141 142
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
143 144 145 146 147 148 149 150
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(16.0, 0.0, 32.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(32.0, 0.0, 48.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(48.0, 0.0, 64.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(64.0, 0.0, 80.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(80.0, 0.0, 96.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(96.0, 0.0, 112.0, 9.0))
151
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
152 153 154 155 156 157
    );
    expect(find.byType(Container), isNot(paints..scale()));
  });

  testWidgets('DecorationImage LTR with alignment topEnd', (WidgetTester tester) async {
    await tester.pumpWidget(
158
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
159
        textDirection: TextDirection.ltr,
160 161
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
162 163
            width: 100.0,
            height: 50.0,
164 165 166
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
167
                alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
168 169 170 171 172 173
                repeat: ImageRepeat.repeatX,
              ),
            ),
          ),
        ),
      ),
174
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
175 176 177
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
178 179 180 181 182 183 184 185
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(-12.0, 0.0, 4.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(4.0, 0.0, 20.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(20.0, 0.0, 36.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(36.0, 0.0, 52.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(52.0, 0.0, 68.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(68.0, 0.0, 84.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 0.0, 100.0, 9.0))
186
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
187 188 189 190 191 192
    );
    expect(find.byType(Container), isNot(paints..scale()));
  });

  testWidgets('DecorationImage RTL with alignment center-right and match', (WidgetTester tester) async {
    await tester.pumpWidget(
193
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
194
        textDirection: TextDirection.rtl,
195 196
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
197 198
            width: 100.0,
            height: 50.0,
199 200 201
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
202
                alignment: Alignment.centerRight,
Ian Hickson's avatar
Ian Hickson committed
203 204 205 206 207 208
                matchTextDirection: true,
              ),
            ),
          ),
        ),
      ),
209
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
210 211 212 213 214 215
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
      ..translate(x: 50.0, y: 0.0)
      ..scale(x: -1.0, y: 1.0)
      ..translate(x: -50.0, y: 0.0)
Dan Field's avatar
Dan Field committed
216
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(0.0, 20.5, 16.0, 29.5))
217
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
218 219 220 221 222 223 224
    );
    expect(find.byType(Container), isNot(paints..scale()..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('DecorationImage RTL with alignment center-right and no match', (WidgetTester tester) async {
    await tester.pumpWidget(
225
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
226
        textDirection: TextDirection.rtl,
227 228
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
229 230
            width: 100.0,
            height: 50.0,
231 232 233
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
234
                alignment: Alignment.centerRight,
Ian Hickson's avatar
Ian Hickson committed
235 236 237 238 239
              ),
            ),
          ),
        ),
      ),
240
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
241 242 243
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
244
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 20.5, 100.0, 29.5)),
Ian Hickson's avatar
Ian Hickson committed
245 246 247 248 249 250 251
    );
    expect(find.byType(Container), isNot(paints..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('DecorationImage LTR with alignment center-right and match', (WidgetTester tester) async {
    await tester.pumpWidget(
252
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
253
        textDirection: TextDirection.ltr,
254 255
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
256 257
            width: 100.0,
            height: 50.0,
258 259 260
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
261
                alignment: Alignment.centerRight,
262
                matchTextDirection: true,
Ian Hickson's avatar
Ian Hickson committed
263 264 265 266 267
              ),
            ),
          ),
        ),
      ),
268
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
269 270 271
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
272
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 20.5, 100.0, 29.5)),
Ian Hickson's avatar
Ian Hickson committed
273 274 275 276 277 278 279
    );
    expect(find.byType(Container), isNot(paints..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('DecorationImage LTR with alignment center-right and no match', (WidgetTester tester) async {
    await tester.pumpWidget(
280
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
281
        textDirection: TextDirection.ltr,
282 283
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
284 285
            width: 100.0,
            height: 50.0,
286 287 288
            decoration: BoxDecoration(
              image: DecorationImage(
                image: TestImageProvider(),
289
                alignment: Alignment.centerRight,
290
                matchTextDirection: true,
Ian Hickson's avatar
Ian Hickson committed
291 292 293 294 295
              ),
            ),
          ),
        ),
      ),
296
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
297 298 299
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
300
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 20.5, 100.0, 29.5)),
Ian Hickson's avatar
Ian Hickson committed
301 302 303 304 305 306 307
    );
    expect(find.byType(Container), isNot(paints..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('Image RTL with alignment topEnd and match', (WidgetTester tester) async {
    await tester.pumpWidget(
308
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
309
        textDirection: TextDirection.rtl,
310 311
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
312 313
            width: 100.0,
            height: 50.0,
314 315
            child: Image(
              image: TestImageProvider(),
316
              alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
317 318 319 320 321 322
              repeat: ImageRepeat.repeatX,
              matchTextDirection: true,
            ),
          ),
        ),
      ),
323
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
324 325 326
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
327
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
Ian Hickson's avatar
Ian Hickson committed
328 329 330
      ..translate(x: 50.0, y: 0.0)
      ..scale(x: -1.0, y: 1.0)
      ..translate(x: -50.0, y: 0.0)
Dan Field's avatar
Dan Field committed
331 332 333 334 335 336 337
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(-12.0, 0.0, 4.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(4.0, 0.0, 20.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(20.0, 0.0, 36.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(36.0, 0.0, 52.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(52.0, 0.0, 68.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(68.0, 0.0, 84.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 0.0, 100.0, 9.0))
338
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
339 340 341 342 343 344
    );
    expect(find.byType(Container), isNot(paints..scale()..scale()));
  });

  testWidgets('Image LTR with alignment topEnd (and pointless match)', (WidgetTester tester) async {
    await tester.pumpWidget(
345
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
346
        textDirection: TextDirection.ltr,
347 348
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
349 350
            width: 100.0,
            height: 50.0,
351 352
            child: Image(
              image: TestImageProvider(),
353
              alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
354 355 356 357 358 359
              repeat: ImageRepeat.repeatX,
              matchTextDirection: true,
            ),
          ),
        ),
      ),
360
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
361 362 363
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
364 365 366 367 368 369 370 371
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(-12.0, 0.0, 4.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(4.0, 0.0, 20.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(20.0, 0.0, 36.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(36.0, 0.0, 52.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(52.0, 0.0, 68.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(68.0, 0.0, 84.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 0.0, 100.0, 9.0))
372
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
373 374 375 376 377 378
    );
    expect(find.byType(Container), isNot(paints..scale()));
  });

  testWidgets('Image RTL with alignment topEnd', (WidgetTester tester) async {
    await tester.pumpWidget(
379
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
380
        textDirection: TextDirection.rtl,
381 382
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
383 384
            width: 100.0,
            height: 50.0,
385 386
            child: Image(
              image: TestImageProvider(),
387
              alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
388 389 390 391 392
              repeat: ImageRepeat.repeatX,
            ),
          ),
        ),
      ),
393
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
394 395 396
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
397 398 399 400 401 402 403 404
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(16.0, 0.0, 32.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(32.0, 0.0, 48.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(48.0, 0.0, 64.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(64.0, 0.0, 80.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(80.0, 0.0, 96.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(96.0, 0.0, 112.0, 9.0))
405
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
406 407 408 409 410 411
    );
    expect(find.byType(Container), isNot(paints..scale()));
  });

  testWidgets('Image LTR with alignment topEnd', (WidgetTester tester) async {
    await tester.pumpWidget(
412
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
413
        textDirection: TextDirection.ltr,
414 415
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
416 417
            width: 100.0,
            height: 50.0,
418 419
            child: Image(
              image: TestImageProvider(),
420
              alignment: AlignmentDirectional.topEnd,
Ian Hickson's avatar
Ian Hickson committed
421 422 423 424 425
              repeat: ImageRepeat.repeatX,
            ),
          ),
        ),
      ),
426
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
427 428 429
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
430 431 432 433 434 435 436 437
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 50.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(-12.0, 0.0, 4.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(4.0, 0.0, 20.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(20.0, 0.0, 36.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(36.0, 0.0, 52.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(52.0, 0.0, 68.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(68.0, 0.0, 84.0, 9.0))
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 0.0, 100.0, 9.0))
438
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
439 440 441 442 443 444
    );
    expect(find.byType(Container), isNot(paints..scale()));
  });

  testWidgets('Image RTL with alignment center-right and match', (WidgetTester tester) async {
    await tester.pumpWidget(
445
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
446
        textDirection: TextDirection.rtl,
447 448
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
449 450
            width: 100.0,
            height: 50.0,
451 452
            child: Image(
              image: TestImageProvider(),
453
              alignment: Alignment.centerRight,
Ian Hickson's avatar
Ian Hickson committed
454 455 456 457 458
              matchTextDirection: true,
            ),
          ),
        ),
      ),
459
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
460 461 462 463 464 465
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
      ..translate(x: 50.0, y: 0.0)
      ..scale(x: -1.0, y: 1.0)
      ..translate(x: -50.0, y: 0.0)
Dan Field's avatar
Dan Field committed
466
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(0.0, 20.5, 16.0, 29.5))
467
      ..restore(),
Ian Hickson's avatar
Ian Hickson committed
468 469 470 471 472 473 474
    );
    expect(find.byType(Container), isNot(paints..scale()..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('Image RTL with alignment center-right and no match', (WidgetTester tester) async {
    await tester.pumpWidget(
475
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
476
        textDirection: TextDirection.rtl,
477 478
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
479 480
            width: 100.0,
            height: 50.0,
481 482
            child: Image(
              image: TestImageProvider(),
483
              alignment: Alignment.centerRight,
Ian Hickson's avatar
Ian Hickson committed
484 485 486 487
            ),
          ),
        ),
      ),
488
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
489 490 491
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
492
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 20.5, 100.0, 29.5)),
Ian Hickson's avatar
Ian Hickson committed
493 494 495 496 497 498 499
    );
    expect(find.byType(Container), isNot(paints..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('Image LTR with alignment center-right and match', (WidgetTester tester) async {
    await tester.pumpWidget(
500
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
501
        textDirection: TextDirection.ltr,
502 503
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
504 505
            width: 100.0,
            height: 50.0,
506 507
            child: Image(
              image: TestImageProvider(),
508
              alignment: Alignment.centerRight,
509
              matchTextDirection: true,
Ian Hickson's avatar
Ian Hickson committed
510 511 512 513
            ),
          ),
        ),
      ),
514
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
515 516 517
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
518
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 20.5, 100.0, 29.5)),
Ian Hickson's avatar
Ian Hickson committed
519 520 521 522 523 524 525
    );
    expect(find.byType(Container), isNot(paints..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('Image LTR with alignment center-right and no match', (WidgetTester tester) async {
    await tester.pumpWidget(
526
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
527
        textDirection: TextDirection.ltr,
528 529
        child: Center(
          child: Container(
Ian Hickson's avatar
Ian Hickson committed
530 531
            width: 100.0,
            height: 50.0,
532 533
            child: Image(
              image: TestImageProvider(),
534
              alignment: Alignment.centerRight,
535
              matchTextDirection: true,
Ian Hickson's avatar
Ian Hickson committed
536 537 538 539
            ),
          ),
        ),
      ),
540
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
541 542 543
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    expect(find.byType(Container), paints
Dan Field's avatar
Dan Field committed
544
      ..drawImageRect(source: const Rect.fromLTRB(0.0, 0.0, 16.0, 9.0), destination: const Rect.fromLTRB(84.0, 20.5, 100.0, 29.5)),
Ian Hickson's avatar
Ian Hickson committed
545 546 547 548 549 550 551
    );
    expect(find.byType(Container), isNot(paints..scale()));
    expect(find.byType(Container), isNot(paints..drawImageRect()..drawImageRect()));
  });

  testWidgets('Image - Switch needing direction', (WidgetTester tester) async {
    await tester.pumpWidget(
552
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
553
        textDirection: TextDirection.ltr,
554 555
        child: Image(
          image: TestImageProvider(),
556
          alignment: Alignment.centerRight,
Ian Hickson's avatar
Ian Hickson committed
557 558 559
          matchTextDirection: false,
        ),
      ),
560
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
561 562 563
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    await tester.pumpWidget(
564
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
565
        textDirection: TextDirection.ltr,
566 567
        child: Image(
          image: TestImageProvider(),
568
          alignment: AlignmentDirectional.centerEnd,
Ian Hickson's avatar
Ian Hickson committed
569 570 571
          matchTextDirection: true,
        ),
      ),
572
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
573 574 575
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
    await tester.pumpWidget(
576
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
577
        textDirection: TextDirection.ltr,
578 579
        child: Image(
          image: TestImageProvider(),
580
          alignment: Alignment.centerRight,
Ian Hickson's avatar
Ian Hickson committed
581 582 583
          matchTextDirection: false,
        ),
      ),
584
      Duration.zero,
Ian Hickson's avatar
Ian Hickson committed
585 586 587
      EnginePhase.layout, // so that we don't try to paint the fake images
    );
  });
588
}