mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 1 | // 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 Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 13 | #include "base/run_loop.h" |
| 14 | #include "testing/gmock/include/gmock/gmock.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
junwei.fu | 31eb1a5 | 2017-06-02 07:35:56 | [diff] [blame] | 16 | #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 17 | #include "ui/gl/gl_switches.h" |
| 18 | |
| 19 | namespace shape_detection { |
| 20 | |
| 21 | ACTION_P(RunClosure, closure) { |
| 22 | closure.Run(); |
| 23 | } |
| 24 | |
| 25 | class 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 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 35 | API_AVAILABLE(macosx(10.11)) std::unique_ptr<TextDetectionImplMac> impl_; |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 36 | const base::MessageLoop message_loop_; |
| 37 | }; |
| 38 | |
| 39 | TEST_F(TextDetectionImplMacTest, CreateAndDestroy) {} |
| 40 | |
| 41 | // This test generates an image with a single text line and scans it back. |
| 42 | TEST_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 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 50 | if (@available(macOS 10.11, *)) { |
| 51 | impl_.reset(new TextDetectionImplMac); |
| 52 | base::ScopedCFTypeRef<CGColorSpaceRef> rgb_colorspace( |
| 53 | CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 54 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 55 | 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)); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 61 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 62 | // 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)); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 65 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 66 | // 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]; |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 71 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 72 | base::scoped_nsobject<NSAttributedString> info([[NSAttributedString alloc] |
| 73 | initWithString:@"https://www.chromium.org" |
| 74 | attributes:attributes]); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 75 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 76 | base::ScopedCFTypeRef<CTLineRef> line( |
| 77 | CTLineCreateWithAttributedString((CFAttributedStringRef)info.get())); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 78 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 79 | CGContextSetTextPosition(context, 10.0, height / 2.0); |
| 80 | CTLineDraw(line, context); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 81 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 82 | // 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)); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 87 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 88 | SkBitmap bitmap; |
| 89 | ASSERT_TRUE(SkCreateBitmapFromCGImage(&bitmap, cg_image)); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 90 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 91 | 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))); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 97 | |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 98 | run_loop.Run(); |
| 99 | } |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // shape_detection namespace |