File: paint_vector_icon_unittest.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (283 lines) | stat: -rw-r--r-- 11,434 bytes parent folder | download
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
// 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.

#include "ui/gfx/paint_vector_icon.h"

#include <gtest/gtest.h>
#include <vector>

#include "base/i18n/rtl.h"
#include "cc/paint/paint_record.h"
#include "cc/paint/paint_recorder.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/vector_icon_types.h"

namespace gfx {

namespace {

SkColor GetColorAtTopLeft(const Canvas& canvas) {
  return canvas.GetBitmap().getColor(0, 0);
}

class MockCanvas : public SkCanvas {
 public:
  MockCanvas(int width, int height) : SkCanvas(width, height) {}

  // SkCanvas overrides:
  void onDrawPath(const SkPath& path, const SkPaint& paint) override {
    paths_.push_back(path);
  }

  const std::vector<SkPath>& paths() const { return paths_; }

 private:
  std::vector<SkPath> paths_;

  DISALLOW_COPY_AND_ASSIGN(MockCanvas);
};

// Tests that a relative move to command (R_MOVE_TO) after a close command
// (CLOSE) uses the correct starting point. See crbug.com/697497
TEST(VectorIconTest, RelativeMoveToAfterClose) {
  cc::PaintRecorder recorder;
  Canvas canvas(recorder.beginRecording(100, 100), 1.0f);

  const PathElement elements[] = {
      MOVE_TO, 4, 5, LINE_TO, 10, 11, CLOSE,
      // This move should use (4, 5) as the start point rather than (10, 11).
      R_MOVE_TO, 20, 21, R_LINE_TO, 50, 51};
  const VectorIconRep rep_list[] = {{elements, arraysize(elements)}};
  const VectorIcon icon = {rep_list, 1u};

  PaintVectorIcon(&canvas, icon, 100, SK_ColorMAGENTA);
  sk_sp<cc::PaintRecord> record = recorder.finishRecordingAsPicture();

  MockCanvas mock(100, 100);
  record->Playback(&mock);

  ASSERT_EQ(1U, mock.paths().size());
  SkPoint last_point;
  EXPECT_TRUE(mock.paths()[0].getLastPt(&last_point));
  EXPECT_EQ(SkIntToScalar(74), last_point.x());
  EXPECT_EQ(SkIntToScalar(77), last_point.y());
}

TEST(VectorIconTest, FlipsInRtl) {
  // Set the locale to a rtl language otherwise FLIPS_IN_RTL will do nothing.
  base::i18n::SetICUDefaultLocale("he");
  ASSERT_TRUE(base::i18n::IsRTL());

  const int canvas_size = 20;
  const SkColor color = SK_ColorWHITE;

  Canvas canvas(gfx::Size(canvas_size, canvas_size), 1.0f, true);

  // Create a 20x20 square icon which has FLIPS_IN_RTL, and CANVAS_DIMENSIONS
  // are twice as large as |canvas|.
  const PathElement elements[] = {CANVAS_DIMENSIONS,
                                  2 * canvas_size,
                                  FLIPS_IN_RTL,
                                  MOVE_TO,
                                  10,
                                  10,
                                  R_H_LINE_TO,
                                  20,
                                  R_V_LINE_TO,
                                  20,
                                  R_H_LINE_TO,
                                  -20,
                                  CLOSE};
  const VectorIconRep rep_list[] = {{elements, arraysize(elements)}};
  const VectorIcon icon = {rep_list, 1u};
  PaintVectorIcon(&canvas, icon, canvas_size, color);

  // Count the number of pixels in the canvas.
  auto bitmap = canvas.GetBitmap();
  int colored_pixel_count = 0;
  for (int i = 0; i < bitmap.width(); ++i) {
    for (int j = 0; j < bitmap.height(); ++j) {
      if (bitmap.getColor(i, j) == color)
        colored_pixel_count++;
    }
  }

  // Verify that the amount of colored pixels on the canvas bitmap should be a
  // quarter of the original icon, since each side should be scaled down by a
  // factor of two.
  EXPECT_EQ(100, colored_pixel_count);
}

TEST(VectorIconTest, CorrectSizePainted) {
  // Create a set of 5 icons reps, sized {48, 32, 24, 20, 16} for the test icon.
  // Color each of them differently so they can be differentiated (the parts of
  // an icon painted with PATH_COLOR_ARGB will not be overwritten by the color
  // provided to it at creation time).
  // SK_ColorRED.
  const PathElement elements48[] = {CANVAS_DIMENSIONS,
                                    48,
                                    PATH_COLOR_ARGB,
                                    0xFF,
                                    0xFF,
                                    0x00,
                                    0x00,
                                    MOVE_TO,
                                    0,
                                    0,
                                    H_LINE_TO,
                                    48,
                                    V_LINE_TO,
                                    48,
                                    H_LINE_TO,
                                    0,
                                    V_LINE_TO,
                                    0,
                                    CLOSE};
  // SK_ColorGREEN.
  const PathElement elements32[] = {CANVAS_DIMENSIONS,
                                    32,
                                    PATH_COLOR_ARGB,
                                    0xFF,
                                    0x00,
                                    0xFF,
                                    0x00,
                                    MOVE_TO,
                                    0,
                                    0,
                                    H_LINE_TO,
                                    32,
                                    V_LINE_TO,
                                    32,
                                    H_LINE_TO,
                                    0,
                                    V_LINE_TO,
                                    0,
                                    CLOSE};
  // SK_ColorBLUE.
  const PathElement elements24[] = {CANVAS_DIMENSIONS,
                                    24,
                                    PATH_COLOR_ARGB,
                                    0xFF,
                                    0x00,
                                    0x00,
                                    0xFF,
                                    MOVE_TO,
                                    0,
                                    0,
                                    H_LINE_TO,
                                    24,
                                    V_LINE_TO,
                                    24,
                                    H_LINE_TO,
                                    0,
                                    V_LINE_TO,
                                    0,
                                    CLOSE};
  // SK_ColorYELLOW.
  const PathElement elements20[] = {CANVAS_DIMENSIONS,
                                    20,
                                    PATH_COLOR_ARGB,
                                    0xFF,
                                    0xFF,
                                    0xFF,
                                    0x00,
                                    MOVE_TO,
                                    0,
                                    0,
                                    H_LINE_TO,
                                    20,
                                    V_LINE_TO,
                                    20,
                                    H_LINE_TO,
                                    0,
                                    V_LINE_TO,
                                    0,
                                    CLOSE};
  // SK_ColorCYAN.
  const PathElement elements16[] = {CANVAS_DIMENSIONS,
                                    16,
                                    PATH_COLOR_ARGB,
                                    0xFF,
                                    0x00,
                                    0xFF,
                                    0xFF,
                                    MOVE_TO,
                                    0,
                                    0,
                                    H_LINE_TO,
                                    16,
                                    V_LINE_TO,
                                    16,
                                    H_LINE_TO,
                                    0,
                                    V_LINE_TO,
                                    0,
                                    CLOSE};
  // VectorIconReps are always sorted in descending order of size.
  const VectorIconRep rep_list[] = {{elements48, arraysize(elements48)},
                                    {elements32, arraysize(elements32)},
                                    {elements24, arraysize(elements24)},
                                    {elements20, arraysize(elements20)},
                                    {elements16, arraysize(elements16)}};
  const VectorIcon icon = {rep_list, 5u};

  // Test exact sizes paint the correctly sized icon, including the largest and
  // smallest icon.
  Canvas canvas_100(gfx::Size(100, 100), 1.0, true);
  PaintVectorIcon(&canvas_100, icon, 48, SK_ColorBLACK);
  EXPECT_EQ(SK_ColorRED, GetColorAtTopLeft(canvas_100));
  PaintVectorIcon(&canvas_100, icon, 32, SK_ColorBLACK);
  EXPECT_EQ(SK_ColorGREEN, GetColorAtTopLeft(canvas_100));
  PaintVectorIcon(&canvas_100, icon, 16, SK_ColorBLACK);
  EXPECT_EQ(SK_ColorCYAN, GetColorAtTopLeft(canvas_100));

  // Only the largest icon may be upscaled to a size larger than what it was
  // designed for.
  PaintVectorIcon(&canvas_100, icon, 50, SK_ColorBLACK);
  EXPECT_EQ(SK_ColorRED, GetColorAtTopLeft(canvas_100));

  // All other icons may never be upscaled.
  PaintVectorIcon(&canvas_100, icon, 27, SK_ColorBLACK);
  EXPECT_EQ(SK_ColorGREEN, GetColorAtTopLeft(canvas_100));
  PaintVectorIcon(&canvas_100, icon, 8, SK_ColorBLACK);
  EXPECT_EQ(SK_ColorCYAN, GetColorAtTopLeft(canvas_100));

  // Test icons at a scale factor < 100%, still with an exact size, paint the
  // correctly sized icon.
  Canvas canvas_75(gfx::Size(100, 100), 0.75, true);
  PaintVectorIcon(&canvas_75, icon, 32, SK_ColorBLACK);  // 32 * 0.75 = 24.
  EXPECT_EQ(SK_ColorBLUE, GetColorAtTopLeft(canvas_75));

  // Test icons at a scale factor > 100%, still with an exact size, paint the
  // correctly sized icon.
  Canvas canvas_125(gfx::Size(100, 100), 1.25, true);
  PaintVectorIcon(&canvas_125, icon, 16, SK_ColorBLACK);  // 16 * 1.25 = 20.
  EXPECT_EQ(SK_ColorYELLOW, GetColorAtTopLeft(canvas_125));

  // Inexact sizes at scale factors < 100%.
  PaintVectorIcon(&canvas_75, icon, 12, SK_ColorBLACK);  // 12 * 0.75 = 9.
  EXPECT_EQ(SK_ColorCYAN, GetColorAtTopLeft(canvas_75));
  PaintVectorIcon(&canvas_75, icon, 28, SK_ColorBLACK);  // 28 * 0.75 = 21.
  EXPECT_EQ(SK_ColorBLUE, GetColorAtTopLeft(canvas_75));

  // Inexact sizes at scale factors > 100%.
  PaintVectorIcon(&canvas_125, icon, 12, SK_ColorBLACK);  // 12 * 1.25 = 15.
  EXPECT_EQ(SK_ColorCYAN, GetColorAtTopLeft(canvas_125));
  PaintVectorIcon(&canvas_125, icon, 28, SK_ColorBLACK);  // 28 * 1.25 = 35.
  EXPECT_EQ(SK_ColorRED, GetColorAtTopLeft(canvas_125));

  // Painting without a requested size will default to the smallest icon rep.
  PaintVectorIcon(&canvas_100, icon, SK_ColorBLACK);
  EXPECT_EQ(SK_ColorCYAN, GetColorAtTopLeft(canvas_100));
  // But doing this in another scale factor should assume the smallest icon rep
  // size, then scale it up by the DSF.
  PaintVectorIcon(&canvas_125, icon, SK_ColorBLACK);  // 16 * 1.25 = 20.
  EXPECT_EQ(SK_ColorYELLOW, GetColorAtTopLeft(canvas_125));
}

}  // namespace

}  // namespace gfx