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
|
// Copyright 2016 The Chromium Authors
// 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/platform/testing/picture_matchers.h"
#include <utility>
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/skia_conversions.h"
namespace blink {
namespace {
class DrawsRectangleCanvas : public SkCanvas {
public:
DrawsRectangleCanvas()
: SkCanvas(800, 600),
save_count_(0),
alpha_(255),
alpha_save_layer_count_(-1) {}
const Vector<RectWithColor>& RectsWithColor() const { return rects_; }
void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
SkPoint quad[4];
getTotalMatrix().mapRectToQuad(quad, rect);
SkRect device_rect;
device_rect.setBounds(quad, 4);
SkIRect device_clip_bounds;
gfx::RectF clipped_rect;
if (getDeviceClipBounds(&device_clip_bounds) &&
device_rect.intersect(SkRect::Make(device_clip_bounds)))
clipped_rect = gfx::SkRectToRectF(device_rect);
unsigned paint_alpha = static_cast<unsigned>(paint.getAlpha());
SkPaint paint_with_alpha(paint);
paint_with_alpha.setAlpha(static_cast<U8CPU>(alpha_ * paint_alpha / 255));
// TODO(https://crbug.com/1351544): This class should use SkColor4f.
Color color = Color::FromSkColor(paint_with_alpha.getColor());
rects_.emplace_back(clipped_rect, color);
SkCanvas::onDrawRect(rect, paint);
}
SkCanvas::SaveLayerStrategy getSaveLayerStrategy(
const SaveLayerRec& rec) override {
save_count_++;
unsigned layer_alpha = static_cast<unsigned>(rec.fPaint->getAlpha());
if (layer_alpha < 255) {
DCHECK_EQ(alpha_save_layer_count_, -1);
alpha_save_layer_count_ = save_count_;
alpha_ = layer_alpha;
}
return SkCanvas::getSaveLayerStrategy(rec);
}
void willSave() override {
save_count_++;
SkCanvas::willSave();
}
void willRestore() override {
DCHECK_GT(save_count_, 0);
if (alpha_save_layer_count_ == save_count_) {
alpha_ = 255;
alpha_save_layer_count_ = -1;
}
save_count_--;
SkCanvas::willRestore();
}
private:
Vector<RectWithColor> rects_;
int save_count_;
unsigned alpha_;
int alpha_save_layer_count_;
};
class DrawsRectanglesMatcher
: public testing::MatcherInterface<const SkPicture&> {
public:
DrawsRectanglesMatcher(const Vector<RectWithColor>& rects_with_color)
: rects_with_color_(rects_with_color) {}
bool MatchAndExplain(const SkPicture& picture,
testing::MatchResultListener* listener) const override {
DrawsRectangleCanvas canvas;
picture.playback(&canvas);
const auto& actual_rects = canvas.RectsWithColor();
if (actual_rects.size() != rects_with_color_.size()) {
*listener << "which draws " << actual_rects.size() << " rects";
return false;
}
for (unsigned index = 0; index < actual_rects.size(); index++) {
const auto& actual_rect_with_color = actual_rects[index];
const auto& expect_rect_with_color = rects_with_color_[index];
if (gfx::ToEnclosingRect(actual_rect_with_color.rect) !=
gfx::ToEnclosingRect(expect_rect_with_color.rect) ||
actual_rect_with_color.color != expect_rect_with_color.color) {
if (listener->IsInterested()) {
*listener << "at index " << index << " which draws "
<< actual_rect_with_color.rect.ToString() << " with color "
<< actual_rect_with_color.color.SerializeAsCSSColor()
<< "\n";
}
return false;
}
}
return true;
}
void DescribeTo(::std::ostream* os) const override {
*os << "\n";
for (unsigned index = 0; index < rects_with_color_.size(); index++) {
const auto& rect_with_color = rects_with_color_[index];
*os << "at index " << index << " rect draws "
<< rect_with_color.rect.ToString() << " with color "
<< rect_with_color.color.SerializeAsCSSColor() << "\n";
}
}
private:
const Vector<RectWithColor> rects_with_color_;
};
} // namespace
testing::Matcher<const SkPicture&> DrawsRectangle(const gfx::RectF& rect,
Color color) {
Vector<RectWithColor> rects_with_color;
rects_with_color.push_back(RectWithColor(rect, color));
return testing::MakeMatcher(new DrawsRectanglesMatcher(rects_with_color));
}
testing::Matcher<const SkPicture&> DrawsRectangles(
const Vector<RectWithColor>& rects_with_color) {
return testing::MakeMatcher(new DrawsRectanglesMatcher(rects_with_color));
}
} // namespace blink
|