summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/nine_piece_image_grid_test.cc
blob: f8e51fa6e97fcd698f0c13b0d7e8731f163bac89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2014 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.

#include "third_party/blink/renderer/core/paint/nine_piece_image_grid.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/css_gradient_value.h"
#include "third_party/blink/renderer/core/style/nine_piece_image.h"
#include "third_party/blink/renderer/core/style/style_generated_image.h"
#include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"

namespace blink {
namespace {

class NinePieceImageGridTest : public RenderingTest {
 public:
  NinePieceImageGridTest() = default;

  StyleImage* GeneratedImage() {
    auto* gradient = MakeGarbageCollected<cssvalue::

                                              CSSLinearGradientValue>(
        nullptr, nullptr, nullptr, nullptr, nullptr, cssvalue::kRepeating);
    return MakeGarbageCollected<StyleGeneratedImage>(*gradient);
  }
};

TEST_F(NinePieceImageGridTest, NinePieceImagePainting_NoDrawables) {
  NinePieceImage nine_piece;
  nine_piece.SetImage(GeneratedImage());

  FloatSize image_size(100, 100);
  IntRect border_image_area(0, 0, 100, 100);
  IntRectOutsets border_widths(0, 0, 0, 0);

  NinePieceImageGrid grid =
      NinePieceImageGrid(nine_piece, image_size, FloatSize(1, 1), 1,
                         border_image_area, border_widths);
  for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
    NinePieceImageGrid::NinePieceDrawInfo draw_info =
        grid.GetNinePieceDrawInfo(piece);
    EXPECT_FALSE(draw_info.is_drawable);
  }
}

TEST_F(NinePieceImageGridTest, NinePieceImagePainting_AllDrawable) {
  NinePieceImage nine_piece;
  nine_piece.SetImage(GeneratedImage());
  nine_piece.SetImageSlices(LengthBox(10, 10, 10, 10));
  nine_piece.SetFill(true);

  FloatSize image_size(100, 100);
  IntRect border_image_area(0, 0, 100, 100);
  IntRectOutsets border_widths(10, 10, 10, 10);

  NinePieceImageGrid grid =
      NinePieceImageGrid(nine_piece, image_size, FloatSize(1, 1), 1,
                         border_image_area, border_widths);
  for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
    NinePieceImageGrid::NinePieceDrawInfo draw_info =
        grid.GetNinePieceDrawInfo(piece);
    EXPECT_TRUE(draw_info.is_drawable);
  }
}

TEST_F(NinePieceImageGridTest, NinePieceImagePainting_NoFillMiddleNotDrawable) {
  NinePieceImage nine_piece;
  nine_piece.SetImage(GeneratedImage());
  nine_piece.SetImageSlices(LengthBox(10, 10, 10, 10));
  nine_piece.SetFill(false);  // default

  FloatSize image_size(100, 100);
  IntRect border_image_area(0, 0, 100, 100);
  IntRectOutsets border_widths(10, 10, 10, 10);

  NinePieceImageGrid grid =
      NinePieceImageGrid(nine_piece, image_size, FloatSize(1, 1), 1,
                         border_image_area, border_widths);
  for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
    NinePieceImageGrid::NinePieceDrawInfo draw_info =
        grid.GetNinePieceDrawInfo(piece);
    if (piece != kMiddlePiece)
      EXPECT_TRUE(draw_info.is_drawable);
    else
      EXPECT_FALSE(draw_info.is_drawable);
  }
}

TEST_F(NinePieceImageGridTest, NinePieceImagePainting_TopLeftDrawable) {
  NinePieceImage nine_piece;
  nine_piece.SetImage(GeneratedImage());
  nine_piece.SetImageSlices(LengthBox(10, 10, 10, 10));

  FloatSize image_size(100, 100);
  IntRect border_image_area(0, 0, 100, 100);

  const struct {
    IntRectOutsets border_widths;
    bool expected_is_drawable;
  } test_cases[] = {
      {IntRectOutsets(0, 0, 0, 0), false},
      {IntRectOutsets(10, 0, 0, 0), false},
      {IntRectOutsets(0, 0, 0, 10), false},
      {IntRectOutsets(10, 0, 0, 10), true},
  };

  for (const auto& test_case : test_cases) {
    NinePieceImageGrid grid =
        NinePieceImageGrid(nine_piece, image_size, FloatSize(1, 1), 1,
                           border_image_area, test_case.border_widths);
    for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
      NinePieceImageGrid::NinePieceDrawInfo draw_info =
          grid.GetNinePieceDrawInfo(piece);
      if (piece == kTopLeftPiece)
        EXPECT_EQ(draw_info.is_drawable, test_case.expected_is_drawable);
    }
  }
}

TEST_F(NinePieceImageGridTest, NinePieceImagePainting_ScaleDownBorder) {
  NinePieceImage nine_piece;
  nine_piece.SetImage(GeneratedImage());
  nine_piece.SetImageSlices(LengthBox(10, 10, 10, 10));

  FloatSize image_size(100, 100);
  IntRect border_image_area(0, 0, 100, 100);
  IntRectOutsets border_widths(10, 10, 10, 10);

  // Set border slices wide enough so that the widths are scaled
  // down and corner pieces cover the entire border image area.
  nine_piece.SetBorderSlices(BorderImageLengthBox(6));

  NinePieceImageGrid grid =
      NinePieceImageGrid(nine_piece, image_size, FloatSize(1, 1), 1,
                         border_image_area, border_widths);
  for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
    NinePieceImageGrid::NinePieceDrawInfo draw_info =
        grid.GetNinePieceDrawInfo(piece);
    if (draw_info.is_corner_piece)
      EXPECT_EQ(draw_info.destination.Size(), FloatSize(50, 50));
    else
      EXPECT_TRUE(draw_info.destination.Size().IsEmpty());
  }

  // Like above, but also make sure to get a scale-down factor that requires
  // rounding to pick the larger value on one of the edges. (A 1:3, 2:3 split.)
  BorderImageLength top_left(10);
  BorderImageLength bottom_right(20);
  nine_piece.SetBorderSlices(
      BorderImageLengthBox(top_left, bottom_right, bottom_right, top_left));
  grid = NinePieceImageGrid(nine_piece, image_size, FloatSize(1, 1), 1,
                            border_image_area, border_widths);
  NinePieceImageGrid::NinePieceDrawInfo draw_info =
      grid.GetNinePieceDrawInfo(kTopLeftPiece);
  EXPECT_EQ(draw_info.destination.Size(), FloatSize(33, 33));
  draw_info = grid.GetNinePieceDrawInfo(kTopRightPiece);
  EXPECT_EQ(draw_info.destination.Size(), FloatSize(67, 33));
  draw_info = grid.GetNinePieceDrawInfo(kBottomLeftPiece);
  EXPECT_EQ(draw_info.destination.Size(), FloatSize(33, 67));
  draw_info = grid.GetNinePieceDrawInfo(kBottomRightPiece);
  EXPECT_EQ(draw_info.destination.Size(), FloatSize(67, 67));

  // Set border slices that overlap in one dimension but not in the other, and
  // where the resulting width in the non-overlapping dimension will round to a
  // larger width.
  BorderImageLength top_bottom(10);
  BorderImageLength left_right(Length::Fixed(11));
  nine_piece.SetBorderSlices(
      BorderImageLengthBox(top_bottom, left_right, top_bottom, left_right));
  grid = NinePieceImageGrid(nine_piece, image_size, FloatSize(1, 1), 1,
                            border_image_area, border_widths);
  NinePieceImageGrid::NinePieceDrawInfo tl_info =
      grid.GetNinePieceDrawInfo(kTopLeftPiece);
  EXPECT_EQ(tl_info.destination.Size(), FloatSize(6, 50));
  // The top-right, bottom-left and bottom-right pieces are the same size as
  // the top-left piece.
  draw_info = grid.GetNinePieceDrawInfo(kTopRightPiece);
  EXPECT_EQ(tl_info.destination.Size(), draw_info.destination.Size());
  draw_info = grid.GetNinePieceDrawInfo(kBottomLeftPiece);
  EXPECT_EQ(tl_info.destination.Size(), draw_info.destination.Size());
  draw_info = grid.GetNinePieceDrawInfo(kBottomRightPiece);
  EXPECT_EQ(tl_info.destination.Size(), draw_info.destination.Size());
}

TEST_F(NinePieceImageGridTest, NinePieceImagePainting) {
  const struct {
    FloatSize image_size;
    IntRect border_image_area;
    IntRectOutsets border_widths;
    bool fill;
    LengthBox image_slices;
    ENinePieceImageRule horizontal_rule;
    ENinePieceImageRule vertical_rule;
    struct {
      bool is_drawable;
      bool is_corner_piece;
      FloatRect destination;
      FloatRect source;
      float tile_scale_horizontal;
      float tile_scale_vertical;
      ENinePieceImageRule horizontal_rule;
      ENinePieceImageRule vertical_rule;
    } pieces[9];
  } test_cases[] = {
      {// Empty border and slices but with fill
       FloatSize(100, 100),
       IntRect(0, 0, 100, 100),
       IntRectOutsets(0, 0, 0, 0),
       true,
       LengthBox(Length::Fixed(0), Length::Fixed(0), Length::Fixed(0),
                 Length::Fixed(0)),
       kStretchImageRule,
       kStretchImageRule,
       {
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(0, 0, 100, 100), FloatRect(0, 0, 100, 100),
            1, 1, kStretchImageRule, kStretchImageRule},
       }},
      {// Single border and fill
       FloatSize(100, 100),
       IntRect(0, 0, 100, 100),
       IntRectOutsets(0, 0, 10, 0),
       true,
       LengthBox(Length::Percent(20), Length::Percent(20), Length::Percent(20),
                 Length::Percent(20)),
       kStretchImageRule,
       kStretchImageRule,
       {
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(0, 90, 100, 10), FloatRect(20, 80, 60, 20),
            0.5, 0.5, kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(0, 0, 100, 90), FloatRect(20, 20, 60, 60),
            1.666667, 1.5, kStretchImageRule, kStretchImageRule},
       }},
      {// All borders, no fill
       FloatSize(100, 100),
       IntRect(0, 0, 100, 100),
       IntRectOutsets(10, 10, 10, 10),
       false,
       LengthBox(Length::Percent(20), Length::Percent(20), Length::Percent(20),
                 Length::Percent(20)),
       kStretchImageRule,
       kStretchImageRule,
       {
           {true, true, FloatRect(0, 0, 10, 10), FloatRect(0, 0, 20, 20), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {true, true, FloatRect(0, 90, 10, 10), FloatRect(0, 80, 20, 20), 1,
            1, kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(0, 10, 10, 80), FloatRect(0, 20, 20, 60),
            0.5, 0.5, kStretchImageRule, kStretchImageRule},
           {true, true, FloatRect(90, 0, 10, 10), FloatRect(80, 0, 20, 20), 1,
            1, kStretchImageRule, kStretchImageRule},
           {true, true, FloatRect(90, 90, 10, 10), FloatRect(80, 80, 20, 20), 1,
            1, kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(90, 10, 10, 80), FloatRect(80, 20, 20, 60),
            0.5, 0.5, kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(10, 0, 80, 10), FloatRect(20, 0, 60, 20),
            0.5, 0.5, kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(10, 90, 80, 10), FloatRect(20, 80, 60, 20),
            0.5, 0.5, kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kStretchImageRule},
       }},
      {// Single border, no fill
       FloatSize(100, 100),
       IntRect(0, 0, 100, 100),
       IntRectOutsets(0, 0, 0, 10),
       false,
       LengthBox(Length::Percent(20), Length::Percent(20), Length::Percent(20),
                 Length::Percent(20)),
       kStretchImageRule,
       kRoundImageRule,
       {
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {true, false, FloatRect(0, 0, 10, 100), FloatRect(0, 20, 20, 60),
            0.5, 0.5, kStretchImageRule, kRoundImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kRoundImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kRoundImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kRoundImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kRoundImageRule},
       }},
      {// All borders but no slices, with fill (stretch horizontally, space
       // vertically)
       FloatSize(100, 100),
       IntRect(0, 0, 100, 100),
       IntRectOutsets(10, 10, 10, 10),
       true,
       LengthBox(Length::Fixed(0), Length::Fixed(0), Length::Fixed(0),
                 Length::Fixed(0)),
       kStretchImageRule,
       kSpaceImageRule,
       {
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kSpaceImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, true, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 1, 1,
            kStretchImageRule, kStretchImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kSpaceImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kSpaceImageRule},
           {false, false, FloatRect(0, 0, 0, 0), FloatRect(0, 0, 0, 0), 0, 0,
            kStretchImageRule, kSpaceImageRule},
           {true, false, FloatRect(10, 10, 80, 80), FloatRect(0, 0, 100, 100),
            0.800000, 1, kStretchImageRule, kSpaceImageRule},
       }},
  };

  for (auto& test_case : test_cases) {
    NinePieceImage nine_piece;
    nine_piece.SetImage(GeneratedImage());
    nine_piece.SetFill(test_case.fill);
    nine_piece.SetImageSlices(test_case.image_slices);
    nine_piece.SetHorizontalRule(
        (ENinePieceImageRule)test_case.horizontal_rule);
    nine_piece.SetVerticalRule((ENinePieceImageRule)test_case.vertical_rule);

    NinePieceImageGrid grid = NinePieceImageGrid(
        nine_piece, test_case.image_size, FloatSize(1, 1), 1,
        test_case.border_image_area, test_case.border_widths);
    for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
      NinePieceImageGrid::NinePieceDrawInfo draw_info =
          grid.GetNinePieceDrawInfo(piece);
      EXPECT_EQ(test_case.pieces[piece].is_drawable, draw_info.is_drawable);
      if (!test_case.pieces[piece].is_drawable)
        continue;

      EXPECT_EQ(test_case.pieces[piece].destination.X(),
                draw_info.destination.X());
      EXPECT_EQ(test_case.pieces[piece].destination.Y(),
                draw_info.destination.Y());
      EXPECT_EQ(test_case.pieces[piece].destination.Width(),
                draw_info.destination.Width());
      EXPECT_EQ(test_case.pieces[piece].destination.Height(),
                draw_info.destination.Height());
      EXPECT_EQ(test_case.pieces[piece].source.X(), draw_info.source.X());
      EXPECT_EQ(test_case.pieces[piece].source.Y(), draw_info.source.Y());
      EXPECT_EQ(test_case.pieces[piece].source.Width(),
                draw_info.source.Width());
      EXPECT_EQ(test_case.pieces[piece].source.Height(),
                draw_info.source.Height());

      if (test_case.pieces[piece].is_corner_piece)
        continue;

      EXPECT_FLOAT_EQ(test_case.pieces[piece].tile_scale_horizontal,
                      draw_info.tile_scale.Width());
      EXPECT_FLOAT_EQ(test_case.pieces[piece].tile_scale_vertical,
                      draw_info.tile_scale.Height());
      EXPECT_EQ(test_case.pieces[piece].horizontal_rule,
                draw_info.tile_rule.horizontal);
      EXPECT_EQ(test_case.pieces[piece].vertical_rule,
                draw_info.tile_rule.vertical);
    }
  }
}

TEST_F(NinePieceImageGridTest, NinePieceImagePainting_Zoomed) {
  NinePieceImage nine_piece;
  nine_piece.SetImage(GeneratedImage());
  // Image slices are specified in CSS pixels.
  nine_piece.SetImageSlices(LengthBox(10, 10, 10, 10));
  nine_piece.SetFill(true);

  FloatSize image_size(50, 50);
  IntRect border_image_area(0, 0, 200, 200);
  IntRectOutsets border_widths(20, 20, 20, 20);

  NinePieceImageGrid grid =
      NinePieceImageGrid(nine_piece, image_size, FloatSize(2, 2), 2,
                         border_image_area, border_widths);
  struct {
    bool is_drawable;
    bool is_corner_piece;
    FloatRect destination;
    FloatRect source;
    float tile_scale_horizontal;
    float tile_scale_vertical;
    ENinePieceImageRule horizontal_rule;
    ENinePieceImageRule vertical_rule;
  } expected_pieces[kMaxPiece] = {
      {true, true, FloatRect(0, 0, 20, 20), FloatRect(0, 0, 20, 20), 0, 0,
       kStretchImageRule, kStretchImageRule},
      {true, true, FloatRect(0, 180, 20, 20), FloatRect(0, 30, 20, 20), 0, 0,
       kStretchImageRule, kStretchImageRule},
      {true, false, FloatRect(0, 20, 20, 160), FloatRect(0, 20, 20, 10), 1, 1,
       kStretchImageRule, kStretchImageRule},
      {true, true, FloatRect(180, 0, 20, 20), FloatRect(30, 0, 20, 20), 0, 0,
       kStretchImageRule, kStretchImageRule},
      {true, true, FloatRect(180, 180, 20, 20), FloatRect(30, 30, 20, 20), 0, 0,
       kStretchImageRule, kStretchImageRule},
      {true, false, FloatRect(180, 20, 20, 160), FloatRect(30, 20, 20, 10), 1,
       1, kStretchImageRule, kStretchImageRule},
      {true, false, FloatRect(20, 0, 160, 20), FloatRect(20, 0, 10, 20), 1, 1,
       kStretchImageRule, kStretchImageRule},
      {true, false, FloatRect(20, 180, 160, 20), FloatRect(20, 30, 10, 20), 1,
       1, kStretchImageRule, kStretchImageRule},
      {true, false, FloatRect(20, 20, 160, 160), FloatRect(20, 20, 10, 10), 16,
       16, kStretchImageRule, kStretchImageRule},
  };

  for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
    NinePieceImageGrid::NinePieceDrawInfo draw_info =
        grid.GetNinePieceDrawInfo(piece);
    EXPECT_TRUE(draw_info.is_drawable);

    const auto& expected = expected_pieces[piece];
    EXPECT_EQ(draw_info.destination, expected.destination);
    EXPECT_EQ(draw_info.source, expected.source);

    if (expected.is_corner_piece)
      continue;

    EXPECT_FLOAT_EQ(draw_info.tile_scale.Width(),
                    expected.tile_scale_horizontal);
    EXPECT_FLOAT_EQ(draw_info.tile_scale.Height(),
                    expected.tile_scale_vertical);
    EXPECT_EQ(draw_info.tile_rule.vertical, expected.vertical_rule);
    EXPECT_EQ(draw_info.tile_rule.horizontal, expected.horizontal_rule);
  }
}

TEST_F(NinePieceImageGridTest, NinePieceImagePainting_ZoomedNarrowSlices) {
  NinePieceImage nine_piece;
  nine_piece.SetImage(GeneratedImage());
  // Image slices are specified in CSS pixels.
  nine_piece.SetImageSlices(LengthBox(1, 1, 1, 1));
  nine_piece.SetFill(true);

  constexpr float zoom = 2.2f;
  FloatSize image_size(3 * zoom, 3 * zoom);
  IntRect border_image_area(0, 0, 220, 220);
  IntRectOutsets border_widths(33, 33, 33, 33);

  NinePieceImageGrid grid =
      NinePieceImageGrid(nine_piece, image_size, FloatSize(zoom, zoom), zoom,
                         border_image_area, border_widths);
  struct {
    bool is_drawable;
    bool is_corner_piece;
    FloatRect destination;
    FloatRect source;
    float tile_scale_horizontal;
    float tile_scale_vertical;
    ENinePieceImageRule horizontal_rule;
    ENinePieceImageRule vertical_rule;
  } expected_pieces[kMaxPiece] = {
      {true, true, FloatRect(0, 0, 33, 33), FloatRect(0, 0, 2.2f, 2.2f), 0, 0,
       kStretchImageRule, kStretchImageRule},
      {true, true, FloatRect(0, 187, 33, 33), FloatRect(0, 4.4f, 2.2f, 2.2f), 0,
       0, kStretchImageRule, kStretchImageRule},
      {true, false, FloatRect(0, 33, 33, 154), FloatRect(0, 2.2f, 2.2f, 2.2f),
       15, 15, kStretchImageRule, kStretchImageRule},
      {true, true, FloatRect(187, 0, 33, 33), FloatRect(4.4f, 0, 2.2f, 2.2f), 0,
       0, kStretchImageRule, kStretchImageRule},
      {true, true, FloatRect(187, 187, 33, 33),
       FloatRect(4.4f, 4.4f, 2.2f, 2.2f), 0, 0, kStretchImageRule,
       kStretchImageRule},
      {true, false, FloatRect(187, 33, 33, 154),
       FloatRect(4.4f, 2.2f, 2.2f, 2.2f), 15, 15, kStretchImageRule,
       kStretchImageRule},
      {true, false, FloatRect(33, 0, 154, 33), FloatRect(2.2f, 0, 2.2f, 2.2f),
       15, 15, kStretchImageRule, kStretchImageRule},
      {true, false, FloatRect(33, 187, 154, 33),
       FloatRect(2.2f, 4.4f, 2.2f, 2.2f), 15, 15, kStretchImageRule,
       kStretchImageRule},
      {true, false, FloatRect(33, 33, 154, 154),
       FloatRect(2.2f, 2.2f, 2.2f, 2.2f), 70, 70, kStretchImageRule,
       kStretchImageRule},
  };

  for (NinePiece piece = kMinPiece; piece < kMaxPiece; ++piece) {
    NinePieceImageGrid::NinePieceDrawInfo draw_info =
        grid.GetNinePieceDrawInfo(piece);
    EXPECT_TRUE(draw_info.is_drawable);

    const auto& expected = expected_pieces[piece];
    EXPECT_FLOAT_EQ(draw_info.destination.X(), expected.destination.X());
    EXPECT_FLOAT_EQ(draw_info.destination.Y(), expected.destination.Y());
    EXPECT_FLOAT_EQ(draw_info.destination.Width(),
                    expected.destination.Width());
    EXPECT_FLOAT_EQ(draw_info.destination.Height(),
                    expected.destination.Height());
    EXPECT_FLOAT_EQ(draw_info.source.X(), expected.source.X());
    EXPECT_FLOAT_EQ(draw_info.source.Y(), expected.source.Y());
    EXPECT_FLOAT_EQ(draw_info.source.Width(), expected.source.Width());
    EXPECT_FLOAT_EQ(draw_info.source.Height(), expected.source.Height());

    if (expected.is_corner_piece)
      continue;

    EXPECT_FLOAT_EQ(draw_info.tile_scale.Width(),
                    expected.tile_scale_horizontal);
    EXPECT_FLOAT_EQ(draw_info.tile_scale.Height(),
                    expected.tile_scale_vertical);
    EXPECT_EQ(draw_info.tile_rule.vertical, expected.vertical_rule);
    EXPECT_EQ(draw_info.tile_rule.horizontal, expected.horizontal_rule);
  }
}

}  // namespace
}  // namespace blink