forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_detection_impl_mac_unittest.mm
101 lines (78 loc) · 3.53 KB
/
text_detection_impl_mac_unittest.mm
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/shape_detection/text_detection_impl_mac.h"
#import <AppKit/AppKit.h>
#include <memory>
#include "base/apple/bridging.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/mac/mac_util.h"
#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/utils/mac/SkCGUtils.h"
#include "ui/gl/gl_switches.h"
using base::test::RunOnceClosure;
namespace shape_detection {
class TextDetectionImplMacTest : public ::testing::Test {
public:
~TextDetectionImplMacTest() override = default;
void DetectCallback(std::vector<mojom::TextDetectionResultPtr> results) {
// CIDetectorTypeText doesn't return the decoded text, juts bounding boxes.
Detection(results.size());
}
MOCK_METHOD1(Detection, void(size_t));
std::unique_ptr<TextDetectionImplMac> impl_;
base::test::SingleThreadTaskEnvironment task_environment_;
};
TEST_F(TextDetectionImplMacTest, CreateAndDestroy) {}
// This test generates an image with a single text line and scans it back.
TEST_F(TextDetectionImplMacTest, ScanOnce) {
// Text detection needs GPU infrastructure.
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseGpuInTests)) {
return;
}
impl_ = std::make_unique<TextDetectionImplMac>();
base::apple::ScopedCFTypeRef<CGColorSpaceRef> rgb_colorspace(
CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
const int width = 200;
const int height = 50;
base::apple::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
nullptr, width, height, 8 /* bitsPerComponent */,
width * 4 /* rowBytes */, rgb_colorspace.get(),
uint32_t{kCGImageAlphaPremultipliedFirst} | kCGBitmapByteOrder32Host));
// Draw a white background.
CGContextSetRGBFillColor(context.get(), 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context.get(), CGRectMake(0.0, 0.0, width, height));
// Create a line of Helvetica 16 text, and draw it in the |context|.
NSDictionary* attributes =
@{NSFontAttributeName : [NSFont fontWithName:@"Helvetica" size:16]};
NSAttributedString* info =
[[NSAttributedString alloc] initWithString:@"https://www.chromium.org"
attributes:attributes];
base::apple::ScopedCFTypeRef<CTLineRef> line(
CTLineCreateWithAttributedString(base::apple::NSToCFPtrCast(info)));
CGContextSetTextPosition(context.get(), 10.0, height / 2.0);
CTLineDraw(line.get(), context.get());
// Extract a CGImage and its raw pixels from |context|.
base::apple::ScopedCFTypeRef<CGImageRef> cg_image(
CGBitmapContextCreateImage(context.get()));
EXPECT_EQ(static_cast<size_t>(width), CGImageGetWidth(cg_image.get()));
EXPECT_EQ(static_cast<size_t>(height), CGImageGetHeight(cg_image.get()));
SkBitmap bitmap;
ASSERT_TRUE(SkCreateBitmapFromCGImage(&bitmap, cg_image.get()));
base::RunLoop run_loop;
// Send the image to Detect() and expect the response in callback.
EXPECT_CALL(*this, Detection(1))
.WillOnce(RunOnceClosure(run_loop.QuitClosure()));
impl_->Detect(bitmap,
base::BindOnce(&TextDetectionImplMacTest::DetectCallback,
base::Unretained(this)));
run_loop.Run();
}
} // shape_detection namespace