File: watermark_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,479 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/enterprise/watermarking/watermark.h"

#include <vector>

#include "base/path_service.h"
#include "cc/paint/skia_paint_canvas.h"
#include "cc/test/pixel_comparator.h"
#include "cc/test/pixel_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"

namespace enterprise_watermark {

namespace {

struct WatermarkParams {
  WatermarkParams(SkColor color, const std::string& file_name)
      : color(color), file_name(file_name) {}

  SkColor color;
  std::string file_name;
};

class WatermarkTest : public testing::Test,
                      public testing::WithParamInterface<WatermarkParams> {};

}  // namespace

// Temporary code needed to generate png files:
//
// #include "base/files/file_util.h"
// #include "ui/gfx/codec/png_codec.h"
//
// std::optional<std::vector<uint8_t>> data =
//   gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true);
// base::WriteFile(path, data.value());

// The consensus for pixel gold tests is that we create a single test for a
// single platform. In this case, this is to avoid issues with platform-specific
// font rendering.
// TODO(b/356446812): add reference images for other platforms.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_PageRenderedWithWatermark PageRenderedWithWatermark
#else
#define MAYBE_PageRenderedWithWatermark DISABLED_PageRenderedWithWatermark
#endif

TEST_P(WatermarkTest, MAYBE_PageRenderedWithWatermark) {
  const int kWidth = 200;
  const int kHeight = 200;
  constexpr SkColor kFillColor = SkColorSetARGB(0xb, 0x00, 0x00, 0x00);
  constexpr SkColor kOutlineColor = SkColorSetARGB(0x11, 0xff, 0xff, 0xff);

  const std::string kWatermarkText = "Private! Confidential";

  WatermarkBlock watermark_block =
      DrawWatermarkToPaintRecord(kWatermarkText, kFillColor, kOutlineColor);
  sk_sp<SkPicture> picture = watermark_block.record.ToSkPicture(
      SkRect::MakeWH(watermark_block.width, watermark_block.height));

  // Create bitmap-backed SkCanvas
  SkBitmap bitmap;
  bitmap.allocN32Pixels(kWidth, kHeight);
  SkCanvas canvas(bitmap);
  canvas.clear(GetParam().color);
  DrawWatermark(&canvas, picture.get(), watermark_block.width,
                watermark_block.height, SkSize::Make(kWidth, kHeight));

  base::FilePath path =
      base::PathService::CheckedGet(base::DIR_SRC_TEST_DATA_ROOT);
  path = path.AppendASCII("components")
             .AppendASCII("test")
             .AppendASCII("data")
             .AppendASCII("enterprise")
             .AppendASCII(GetParam().file_name);

  ASSERT_TRUE(cc::MatchesPNGFile(bitmap, path, cc::ExactPixelComparator()));

  // Test the cc::PaintRecord overload produces the same result
  canvas.clear(GetParam().color);
  cc::SkiaPaintCanvas skia_canvas(&canvas);
  DrawWatermark(&skia_canvas, &watermark_block.record, watermark_block.width,
                watermark_block.height, SkSize::Make(kWidth, kHeight));
  ASSERT_TRUE(cc::MatchesPNGFile(bitmap, path, cc::ExactPixelComparator()));
}

INSTANTIATE_TEST_SUITE_P(
    All,
    WatermarkTest,
    testing::Values(WatermarkParams(SkColorSetRGB(0x22, 0x22, 0x22),
                                    "watermark_dark.png"),
                    WatermarkParams(SkColorSetRGB(0xff, 0xff, 0xff),
                                    "watermark_light.png")));

}  // namespace enterprise_watermark