[go: nahoru, domu]

blob: 898164555f2d4019c48dc5a40d1caa2c54d7629a [file] [log] [blame]
mcasas9eba8ff2017-03-06 02:58:291// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "services/shape_detection/text_detection_impl_mac.h"
6
7#include "base/command_line.h"
8#include "base/mac/mac_util.h"
9#include "base/mac/scoped_cftyperef.h"
10#include "base/mac/scoped_nsobject.h"
11#include "base/mac/sdk_forward_declarations.h"
Alexander Timin4f9c35c2018-11-01 20:15:2012#include "base/message_loop/message_loop.h"
mcasas9eba8ff2017-03-06 02:58:2913#include "base/run_loop.h"
14#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
junwei.fu31eb1a52017-06-02 07:35:5616#include "third_party/skia/include/utils/mac/SkCGUtils.h"
mcasas9eba8ff2017-03-06 02:58:2917#include "ui/gl/gl_switches.h"
18
19namespace shape_detection {
20
21ACTION_P(RunClosure, closure) {
22 closure.Run();
23}
24
25class TextDetectionImplMacTest : public ::testing::Test {
26 public:
27 ~TextDetectionImplMacTest() override = default;
28
29 void DetectCallback(std::vector<mojom::TextDetectionResultPtr> results) {
30 // CIDetectorTypeText doesn't return the decoded text, juts bounding boxes.
31 Detection(results.size());
32 }
33 MOCK_METHOD1(Detection, void(size_t));
34
erikchend2b3c762017-07-18 00:12:4835 API_AVAILABLE(macosx(10.11)) std::unique_ptr<TextDetectionImplMac> impl_;
mcasas9eba8ff2017-03-06 02:58:2936 const base::MessageLoop message_loop_;
37};
38
39TEST_F(TextDetectionImplMacTest, CreateAndDestroy) {}
40
41// This test generates an image with a single text line and scans it back.
42TEST_F(TextDetectionImplMacTest, ScanOnce) {
43 // Text detection needs at least MAC OS X 10.11, and GPU infrastructure.
44 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
45 switches::kUseGpuInTests) ||
46 !base::mac::IsAtLeastOS10_11()) {
47 return;
48 }
49
erikchend2b3c762017-07-18 00:12:4850 if (@available(macOS 10.11, *)) {
51 impl_.reset(new TextDetectionImplMac);
52 base::ScopedCFTypeRef<CGColorSpaceRef> rgb_colorspace(
53 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
mcasas9eba8ff2017-03-06 02:58:2954
erikchend2b3c762017-07-18 00:12:4855 const int width = 200;
56 const int height = 50;
57 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
58 nullptr, width, height, 8 /* bitsPerComponent */,
59 width * 4 /* rowBytes */, rgb_colorspace,
60 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
mcasas9eba8ff2017-03-06 02:58:2961
erikchend2b3c762017-07-18 00:12:4862 // Draw a white background.
63 CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
64 CGContextFillRect(context, CGRectMake(0.0, 0.0, width, height));
mcasas9eba8ff2017-03-06 02:58:2965
erikchend2b3c762017-07-18 00:12:4866 // Create a line of Helvetica 16 text, and draw it in the |context|.
67 base::scoped_nsobject<NSFont> helvetica(
68 [NSFont fontWithName:@"Helvetica" size:16]);
69 NSDictionary* attributes = [NSDictionary
70 dictionaryWithObjectsAndKeys:helvetica, kCTFontAttributeName, nil];
mcasas9eba8ff2017-03-06 02:58:2971
erikchend2b3c762017-07-18 00:12:4872 base::scoped_nsobject<NSAttributedString> info([[NSAttributedString alloc]
73 initWithString:@"https://www.chromium.org"
74 attributes:attributes]);
mcasas9eba8ff2017-03-06 02:58:2975
erikchend2b3c762017-07-18 00:12:4876 base::ScopedCFTypeRef<CTLineRef> line(
77 CTLineCreateWithAttributedString((CFAttributedStringRef)info.get()));
mcasas9eba8ff2017-03-06 02:58:2978
erikchend2b3c762017-07-18 00:12:4879 CGContextSetTextPosition(context, 10.0, height / 2.0);
80 CTLineDraw(line, context);
mcasas9eba8ff2017-03-06 02:58:2981
erikchend2b3c762017-07-18 00:12:4882 // Extract a CGImage and its raw pixels from |context|.
83 base::ScopedCFTypeRef<CGImageRef> cg_image(
84 CGBitmapContextCreateImage(context));
85 EXPECT_EQ(static_cast<size_t>(width), CGImageGetWidth(cg_image));
86 EXPECT_EQ(static_cast<size_t>(height), CGImageGetHeight(cg_image));
mcasas9eba8ff2017-03-06 02:58:2987
erikchend2b3c762017-07-18 00:12:4888 SkBitmap bitmap;
89 ASSERT_TRUE(SkCreateBitmapFromCGImage(&bitmap, cg_image));
mcasas9eba8ff2017-03-06 02:58:2990
erikchend2b3c762017-07-18 00:12:4891 base::RunLoop run_loop;
92 base::Closure quit_closure = run_loop.QuitClosure();
93 // Send the image to Detect() and expect the response in callback.
94 EXPECT_CALL(*this, Detection(1)).WillOnce(RunClosure(quit_closure));
95 impl_->Detect(bitmap, base::Bind(&TextDetectionImplMacTest::DetectCallback,
96 base::Unretained(this)));
mcasas9eba8ff2017-03-06 02:58:2997
erikchend2b3c762017-07-18 00:12:4898 run_loop.Run();
99 }
mcasas9eba8ff2017-03-06 02:58:29100}
101
102} // shape_detection namespace