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" | |
16 #include "ui/gl/gl_switches.h" | 15 #include "ui/gl/gl_switches.h" |
17 | 16 |
18 namespace shape_detection { | 17 namespace shape_detection { |
19 | 18 |
20 ACTION_P(RunClosure, closure) { | 19 ACTION_P(RunClosure, closure) { |
21 closure.Run(); | 20 closure.Run(); |
22 } | 21 } |
23 | 22 |
24 class BarcodeDetectionImplMacTest : public ::testing::Test { | 23 class BarcodeDetectionImplMacTest : public ::testing::Test { |
25 public: | 24 public: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 [qr_code_generator setValue:qr_code_data forKey:@"inputMessage"]; | 57 [qr_code_generator setValue:qr_code_data forKey:@"inputMessage"]; |
59 | 58 |
60 // [CIImage outputImage] is available in macOS 10.10+. Could be added to | 59 // [CIImage outputImage] is available in macOS 10.10+. Could be added to |
61 // sdk_forward_declarations.h but seems hardly worth it. | 60 // sdk_forward_declarations.h but seems hardly worth it. |
62 EXPECT_TRUE([qr_code_generator respondsToSelector:@selector(outputImage)]); | 61 EXPECT_TRUE([qr_code_generator respondsToSelector:@selector(outputImage)]); |
63 CIImage* qr_code_image = | 62 CIImage* qr_code_image = |
64 [qr_code_generator performSelector:@selector(outputImage)]; | 63 [qr_code_generator performSelector:@selector(outputImage)]; |
65 | 64 |
66 const gfx::Size size([qr_code_image extent].size.width, | 65 const gfx::Size size([qr_code_image extent].size.width, |
67 [qr_code_image extent].size.height); | 66 [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 SkBitmap bitmap; | 76 base::ScopedCFTypeRef<CFDataRef> raw_cg_image_data( |
77 ASSERT_TRUE(SkCreateBitmapFromCGImage(&bitmap, cg_image)); | 77 CGDataProviderCopyData(CGImageGetDataProvider(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); |
78 | 91 |
79 base::RunLoop run_loop; | 92 base::RunLoop run_loop; |
80 base::Closure quit_closure = run_loop.QuitClosure(); | 93 base::Closure quit_closure = run_loop.QuitClosure(); |
81 // Send the image Detect() and expect the response in callback. | 94 // Send the image Detect() and expect the response in callback. |
82 EXPECT_CALL(*this, Detection(1, kInfoString)) | 95 EXPECT_CALL(*this, Detection(1, kInfoString)) |
83 .WillOnce(RunClosure(quit_closure)); | 96 .WillOnce(RunClosure(quit_closure)); |
84 impl_.Detect(bitmap, base::Bind(&BarcodeDetectionImplMacTest::DetectCallback, | 97 impl_.Detect(std::move(handle), size.width(), size.height(), |
85 base::Unretained(this))); | 98 base::Bind(&BarcodeDetectionImplMacTest::DetectCallback, |
| 99 base::Unretained(this))); |
86 | 100 |
87 run_loop.Run(); | 101 run_loop.Run(); |
88 } | 102 } |
89 | 103 |
90 } // shape_detection namespace | 104 } // shape_detection namespace |
OLD | NEW |