OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/shape_detection/barcode_detection_impl_mac.h" | 5 #include "services/shape_detection/barcode_detection_impl_mac.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
9 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
11 #include "base/mac/sdk_forward_declarations.h" | 11 #include "base/mac/sdk_forward_declarations.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
15 #include "ui/gl/gl_switches.h" | 16 #include "ui/gl/gl_switches.h" |
16 | 17 |
17 namespace shape_detection { | 18 namespace shape_detection { |
18 | 19 |
19 ACTION_P(RunClosure, closure) { | 20 ACTION_P(RunClosure, closure) { |
20 closure.Run(); | 21 closure.Run(); |
21 } | 22 } |
22 | 23 |
23 class BarcodeDetectionImplMacTest : public ::testing::Test { | 24 class BarcodeDetectionImplMacTest : public ::testing::Test { |
24 public: | 25 public: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 [qr_code_generator setValue:qr_code_data forKey:@"inputMessage"]; | 58 [qr_code_generator setValue:qr_code_data forKey:@"inputMessage"]; |
58 | 59 |
59 // [CIImage outputImage] is available in macOS 10.10+. Could be added to | 60 // [CIImage outputImage] is available in macOS 10.10+. Could be added to |
60 // sdk_forward_declarations.h but seems hardly worth it. | 61 // sdk_forward_declarations.h but seems hardly worth it. |
61 EXPECT_TRUE([qr_code_generator respondsToSelector:@selector(outputImage)]); | 62 EXPECT_TRUE([qr_code_generator respondsToSelector:@selector(outputImage)]); |
62 CIImage* qr_code_image = | 63 CIImage* qr_code_image = |
63 [qr_code_generator performSelector:@selector(outputImage)]; | 64 [qr_code_generator performSelector:@selector(outputImage)]; |
64 | 65 |
65 const gfx::Size size([qr_code_image extent].size.width, | 66 const gfx::Size size([qr_code_image extent].size.width, |
66 [qr_code_image extent].size.height); | 67 [qr_code_image extent].size.height); |
67 const int num_bytes = size.GetArea() * 4 /* bytes per pixel */; | |
68 | 68 |
69 base::scoped_nsobject<CIContext> context([[CIContext alloc] init]); | 69 base::scoped_nsobject<CIContext> context([[CIContext alloc] init]); |
70 | 70 |
71 base::ScopedCFTypeRef<CGImageRef> cg_image( | 71 base::ScopedCFTypeRef<CGImageRef> cg_image( |
72 [context createCGImage:qr_code_image fromRect:[qr_code_image extent]]); | 72 [context createCGImage:qr_code_image fromRect:[qr_code_image extent]]); |
73 EXPECT_EQ(static_cast<size_t>(size.width()), CGImageGetWidth(cg_image)); | 73 EXPECT_EQ(static_cast<size_t>(size.width()), CGImageGetWidth(cg_image)); |
74 EXPECT_EQ(static_cast<size_t>(size.height()), CGImageGetHeight(cg_image)); | 74 EXPECT_EQ(static_cast<size_t>(size.height()), CGImageGetHeight(cg_image)); |
75 | 75 |
76 base::ScopedCFTypeRef<CFDataRef> raw_cg_image_data( | 76 SkBitmap bitmap; |
77 CGDataProviderCopyData(CGImageGetDataProvider(cg_image))); | 77 ASSERT_TRUE(SkCreateBitmapFromCGImage(&bitmap, cg_image)); |
78 EXPECT_TRUE(CFDataGetBytePtr(raw_cg_image_data)); | |
79 EXPECT_EQ(num_bytes, CFDataGetLength(raw_cg_image_data)); | |
80 | |
81 // Generate a new ScopedSharedBufferHandle of the aproppriate size, map it and | |
82 // copy the generated qr code image pixels into it. | |
83 mojo::ScopedSharedBufferHandle handle = | |
84 mojo::SharedBufferHandle::Create(num_bytes); | |
85 ASSERT_TRUE(handle->is_valid()); | |
86 | |
87 mojo::ScopedSharedBufferMapping mapping = handle->Map(num_bytes); | |
88 ASSERT_TRUE(mapping); | |
89 | |
90 memcpy(mapping.get(), CFDataGetBytePtr(raw_cg_image_data), num_bytes); | |
91 | 78 |
92 base::RunLoop run_loop; | 79 base::RunLoop run_loop; |
93 base::Closure quit_closure = run_loop.QuitClosure(); | 80 base::Closure quit_closure = run_loop.QuitClosure(); |
94 // Send the image Detect() and expect the response in callback. | 81 // Send the image Detect() and expect the response in callback. |
95 EXPECT_CALL(*this, Detection(1, kInfoString)) | 82 EXPECT_CALL(*this, Detection(1, kInfoString)) |
96 .WillOnce(RunClosure(quit_closure)); | 83 .WillOnce(RunClosure(quit_closure)); |
97 impl_.Detect(std::move(handle), size.width(), size.height(), | 84 impl_.Detect(bitmap, base::Bind(&BarcodeDetectionImplMacTest::DetectCallback, |
98 base::Bind(&BarcodeDetectionImplMacTest::DetectCallback, | 85 base::Unretained(this))); |
99 base::Unretained(this))); | |
100 | 86 |
101 run_loop.Run(); | 87 run_loop.Run(); |
102 } | 88 } |
103 | 89 |
104 } // shape_detection namespace | 90 } // shape_detection namespace |
OLD | NEW |