OLD | NEW |
(Empty) | |
| 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" |
| 12 #include "base/run_loop.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/gl/gl_switches.h" |
| 16 |
| 17 namespace shape_detection { |
| 18 |
| 19 ACTION_P(RunClosure, closure) { |
| 20 closure.Run(); |
| 21 } |
| 22 |
| 23 class TextDetectionImplMacTest : public ::testing::Test { |
| 24 public: |
| 25 ~TextDetectionImplMacTest() override = default; |
| 26 |
| 27 void DetectCallback(std::vector<mojom::TextDetectionResultPtr> results) { |
| 28 // CIDetectorTypeText doesn't return the decoded text, juts bounding boxes. |
| 29 Detection(results.size()); |
| 30 } |
| 31 MOCK_METHOD1(Detection, void(size_t)); |
| 32 |
| 33 TextDetectionImplMac impl_; |
| 34 const base::MessageLoop message_loop_; |
| 35 }; |
| 36 |
| 37 TEST_F(TextDetectionImplMacTest, CreateAndDestroy) {} |
| 38 |
| 39 // This test generates an image with a single text line and scans it back. |
| 40 TEST_F(TextDetectionImplMacTest, ScanOnce) { |
| 41 // Text detection needs at least MAC OS X 10.11, and GPU infrastructure. |
| 42 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 43 switches::kUseGpuInTests) || |
| 44 !base::mac::IsAtLeastOS10_11()) { |
| 45 return; |
| 46 } |
| 47 |
| 48 base::ScopedCFTypeRef<CGColorSpaceRef> rgb_colorspace( |
| 49 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| 50 |
| 51 const int width = 200; |
| 52 const int height = 50; |
| 53 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate( |
| 54 nullptr, width, height, 8 /* bitsPerComponent */, |
| 55 width * 4 /* rowBytes */, rgb_colorspace, |
| 56 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host)); |
| 57 |
| 58 // Draw a white background. |
| 59 CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); |
| 60 CGContextFillRect(context, CGRectMake(0.0, 0.0, width, height)); |
| 61 |
| 62 // Create a line of Helvetica 16 text, and draw it in the |context|. |
| 63 base::scoped_nsobject<NSFont> helvetica( |
| 64 [NSFont fontWithName:@"Helvetica" size:16]); |
| 65 NSDictionary* attributes = [NSDictionary |
| 66 dictionaryWithObjectsAndKeys:helvetica, kCTFontAttributeName, nil]; |
| 67 |
| 68 base::scoped_nsobject<NSAttributedString> info([[NSAttributedString alloc] |
| 69 initWithString:@"https://www.chromium.org" |
| 70 attributes:attributes]); |
| 71 |
| 72 base::ScopedCFTypeRef<CTLineRef> line( |
| 73 CTLineCreateWithAttributedString((CFAttributedStringRef)info.get())); |
| 74 |
| 75 CGContextSetTextPosition(context, 10.0, height / 2.0); |
| 76 CTLineDraw(line, context); |
| 77 |
| 78 // Extract a CGImage and its raw pixels from |context|. |
| 79 base::ScopedCFTypeRef<CGImageRef> cg_image( |
| 80 CGBitmapContextCreateImage(context)); |
| 81 EXPECT_EQ(static_cast<size_t>(width), CGImageGetWidth(cg_image)); |
| 82 EXPECT_EQ(static_cast<size_t>(height), CGImageGetHeight(cg_image)); |
| 83 |
| 84 base::ScopedCFTypeRef<CFDataRef> raw_cg_image_data( |
| 85 CGDataProviderCopyData(CGImageGetDataProvider(cg_image))); |
| 86 EXPECT_TRUE(CFDataGetBytePtr(raw_cg_image_data)); |
| 87 const int num_bytes = width * height * 4; |
| 88 EXPECT_EQ(num_bytes, CFDataGetLength(raw_cg_image_data)); |
| 89 |
| 90 // Generate a new ScopedSharedBufferHandle of the aproppriate size, map it and |
| 91 // copy the generated text image pixels into it. |
| 92 auto handle = mojo::SharedBufferHandle::Create(num_bytes); |
| 93 ASSERT_TRUE(handle->is_valid()); |
| 94 |
| 95 mojo::ScopedSharedBufferMapping mapping = handle->Map(num_bytes); |
| 96 ASSERT_TRUE(mapping); |
| 97 |
| 98 memcpy(mapping.get(), CFDataGetBytePtr(raw_cg_image_data), num_bytes); |
| 99 |
| 100 base::RunLoop run_loop; |
| 101 base::Closure quit_closure = run_loop.QuitClosure(); |
| 102 // Send the image to Detect() and expect the response in callback. |
| 103 EXPECT_CALL(*this, Detection(1)).WillOnce(RunClosure(quit_closure)); |
| 104 impl_.Detect(std::move(handle), width, height, |
| 105 base::Bind(&TextDetectionImplMacTest::DetectCallback, |
| 106 base::Unretained(this))); |
| 107 |
| 108 run_loop.Run(); |
| 109 } |
| 110 |
| 111 } // shape_detection namespace |
OLD | NEW |