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