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/barcode_detection_impl_mac.h" | |
6 | |
7 #include "base/mac/mac_util.h" | |
8 #include "base/mac/scoped_cftyperef.h" | |
9 #include "base/mac/scoped_nsobject.h" | |
10 #include "base/mac/sdk_forward_declarations.h" | |
11 #include "base/run_loop.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace shape_detection { | |
16 | |
17 ACTION_P(RunClosure, closure) { | |
18 closure.Run(); | |
19 } | |
20 | |
21 class BarcodeDetectionImplMacTest : public ::testing::Test { | |
22 public: | |
23 ~BarcodeDetectionImplMacTest() override = default; | |
24 | |
25 void DetectCallback(std::vector<mojom::BarcodeDetectionResultPtr> results) { | |
26 Detection(results.size(), results.empty() ? "" : results[0]->raw_value); | |
27 } | |
28 MOCK_METHOD2(Detection, void(size_t, const std::string&)); | |
29 | |
30 BarcodeDetectionImplMac impl_; | |
31 const base::MessageLoop message_loop_; | |
32 }; | |
33 | |
34 TEST_F(BarcodeDetectionImplMacTest, CreateAndDestroy) {} | |
35 | |
36 // This test generates a single QR code and scans it back. | |
37 TEST_F(BarcodeDetectionImplMacTest, ScanOneBarcode) { | |
38 // Barcode detection needs at least MAC OS X 10.10. | |
39 if (!base::mac::IsAtLeastOS10_10()) | |
40 return; | |
41 | |
42 const std::string kInfoString = "https://www.chromium.org"; | |
43 // Generate a QR code image as a CIImage by using |qr_code_generator|. | |
44 NSData* const qr_code_data = | |
45 [[NSString stringWithUTF8String:kInfoString.c_str()] | |
46 dataUsingEncoding:NSISOLatin1StringEncoding]; | |
47 // TODO(mcasas): Consider using other generator types (e.g. | |
48 // CI{AztecCode,Code128Barcode,PDF417Barcode}Generator) when the minimal OS X | |
49 // is upgraded to 10.10+ (https://crbug.com/624049). | |
50 CIFilter* qr_code_generator = | |
51 [CIFilter filterWithName:@"CIQRCodeGenerator"]; | |
52 [qr_code_generator setValue:qr_code_data forKey:@"inputMessage"]; | |
53 | |
54 // [CIImage outputImage] is available in macOS 10.10+. Could be added to | |
55 // sdk_forward_declarations.h but seems hardly worth it. | |
56 EXPECT_TRUE([qr_code_generator respondsToSelector:@selector(outputImage)]); | |
57 CIImage* qr_code_image = | |
58 [qr_code_generator performSelector:@selector(outputImage)]; | |
59 | |
60 const gfx::Size size([qr_code_image extent].size.width, | |
61 [qr_code_image extent].size.height); | |
62 const int num_bytes = size.GetArea() * 4 /* bytes per pixel */; | |
63 | |
64 base::scoped_nsobject<CIContext> context([[CIContext alloc] init]); | |
65 | |
66 base::ScopedCFTypeRef<CGImageRef> cg_image( | |
67 [context createCGImage:qr_code_image fromRect:[qr_code_image extent]]); | |
68 EXPECT_EQ(static_cast<size_t>(size.width()), CGImageGetWidth(cg_image)); | |
69 EXPECT_EQ(static_cast<size_t>(size.height()), CGImageGetHeight(cg_image)); | |
70 | |
71 base::ScopedCFTypeRef<CFDataRef> raw_cg_image_data( | |
72 CGDataProviderCopyData(CGImageGetDataProvider(cg_image))); | |
73 EXPECT_TRUE(CFDataGetBytePtr(raw_cg_image_data)); | |
74 EXPECT_EQ(num_bytes, CFDataGetLength(raw_cg_image_data)); | |
75 | |
76 // Generate a new ScopedSharedBufferHandle of the aproppriate size, map it and | |
77 // copy the generated qr code image pixels into it. | |
78 mojo::ScopedSharedBufferHandle handle = | |
79 mojo::SharedBufferHandle::Create(num_bytes); | |
80 ASSERT_TRUE(handle->is_valid()); | |
81 | |
82 mojo::ScopedSharedBufferMapping mapping = handle->Map(num_bytes); | |
83 ASSERT_TRUE(mapping); | |
84 | |
85 memcpy(mapping.get(), CFDataGetBytePtr(raw_cg_image_data), num_bytes); | |
86 | |
87 base::RunLoop run_loop; | |
88 base::Closure quit_closure = run_loop.QuitClosure(); | |
89 // Send the image Detect() and expect the response in callback. | |
90 EXPECT_CALL(*this, Detection(1, kInfoString)) | |
91 .WillOnce(RunClosure(quit_closure)); | |
92 impl_.Detect(std::move(handle), size.width(), size.height(), | |
93 base::Bind(&BarcodeDetectionImplMacTest::DetectCallback, | |
94 base::Unretained(this))); | |
95 | |
96 run_loop.Run(); | |
97 } | |
98 | |
99 } // shape_detection namespace | |
OLD | NEW |