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