1 /*
2 * Copyright 2017 Google, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "render_test.h"
18
19 #include <string>
20
21 #include "flutter/fml/logging.h"
22 #include "third_party/skia/include/core/SkImageEncoder.h"
23 #include "third_party/skia/include/core/SkStream.h"
24 #include "txt/asset_font_manager.h"
25 #include "txt/font_collection.h"
26 #include "txt_test_utils.h"
27
28 namespace txt {
29
RenderTest()30 RenderTest::RenderTest()
31 : snapshots_(0), font_collection_(txt::GetTestFontCollection()) {}
32
33 RenderTest::~RenderTest() = default;
34
GetCanvas()35 SkCanvas* RenderTest::GetCanvas() {
36 return canvas_ == nullptr ? nullptr : canvas_.get();
37 }
38
GetNextSnapshotName()39 std::string RenderTest::GetNextSnapshotName() {
40 const auto& test_info =
41 ::testing::UnitTest::GetInstance()->current_test_info();
42
43 std::stringstream stream;
44 stream << "snapshots/" << test_info->test_case_name() << "_"
45 << test_info->name();
46 stream << "_" << ++snapshots_ << ".png";
47
48 return stream.str();
49 }
50
Snapshot()51 bool RenderTest::Snapshot() {
52 if (!canvas_ || !bitmap_) {
53 return false;
54 }
55 std::string snapshot_dir = "snapshots";
56 int error = 0;
57 // _WIN32 defined by Windows Visual compiler.
58 #if defined(_WIN32)
59 // Handle windows path creation.
60 error = _mkdir(snapshot_dir.c_str());
61 #else
62 // Handle non-windows path creation with Unix permissions.
63 mode_t permissions = 0733;
64 error = mkdir(snapshot_dir.c_str(), permissions);
65 #endif
66 if (error > 0) {
67 FML_LOG(ERROR) << "'snapshot/' Directory not available and could not be "
68 "created. Please create manually to save snapshot.";
69 return false;
70 }
71 auto file_name = GetNextSnapshotName();
72 SkFILEWStream file(file_name.c_str());
73 return SkEncodeImage(&file, *bitmap_, SkEncodedImageFormat::kPNG, 100);
74 }
75
GetTestCanvasWidth() const76 size_t RenderTest::GetTestCanvasWidth() const {
77 return 1000;
78 }
79
GetTestCanvasHeight() const80 size_t RenderTest::GetTestCanvasHeight() const {
81 return 600;
82 }
83
SetUp()84 void RenderTest::SetUp() {
85 bitmap_ = std::make_unique<SkBitmap>();
86 bitmap_->allocN32Pixels(GetTestCanvasWidth(), GetTestCanvasHeight());
87 canvas_ = std::make_unique<SkCanvas>(*bitmap_);
88 canvas_->clear(SK_ColorWHITE);
89 }
90
GetTestFontCollection() const91 std::shared_ptr<FontCollection> RenderTest::GetTestFontCollection() const {
92 return font_collection_;
93 }
94
TearDown()95 void RenderTest::TearDown() {
96 canvas_ = nullptr;
97 bitmap_ = nullptr;
98 }
99
100 } // namespace txt
101