Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: services/shape_detection/barcode_detection_impl_mac_unittest.mm

Issue 2690253002: RELAND: Shape detection unittests mac (QR and Face) in GPU bots (Closed)
Patch Set: Adding --use-gpu-in-tests so tests are only run on GPU-bots Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/command_line.h"
8 #include "base/mac/mac_util.h"
9 #include "base/mac/scoped_cftyperef.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/mac/sdk_forward_declarations.h"
12 #include "base/run_loop.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gl/gl_switches.h"
16
17 namespace shape_detection {
18
19 ACTION_P(RunClosure, closure) {
20 closure.Run();
21 }
22
23 class BarcodeDetectionImplMacTest : public ::testing::Test {
24 public:
25 ~BarcodeDetectionImplMacTest() override = default;
26
27 void DetectCallback(std::vector<mojom::BarcodeDetectionResultPtr> results) {
28 Detection(results.size(), results.empty() ? "" : results[0]->raw_value);
29 }
30 MOCK_METHOD2(Detection, void(size_t, const std::string&));
31
32 BarcodeDetectionImplMac impl_;
33 const base::MessageLoop message_loop_;
34 };
35
36 TEST_F(BarcodeDetectionImplMacTest, CreateAndDestroy) {}
37
38 // This test generates a single QR code and scans it back.
39 TEST_F(BarcodeDetectionImplMacTest, ScanOneBarcode) {
40 // Barcode detection needs at least MAC OS X 10.10, and GPU infrastructure.
41 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
42 switches::kUseGpuInTests) ||
43 !base::mac::IsAtLeastOS10_10()) {
44 return;
45 }
46
47 const std::string kInfoString = "https://www.chromium.org";
48 // Generate a QR code image as a CIImage by using |qr_code_generator|.
49 NSData* const qr_code_data =
50 [[NSString stringWithUTF8String:kInfoString.c_str()]
51 dataUsingEncoding:NSISOLatin1StringEncoding];
52 // TODO(mcasas): Consider using other generator types (e.g.
53 // CI{AztecCode,Code128Barcode,PDF417Barcode}Generator) when the minimal OS X
54 // is upgraded to 10.10+ (https://crbug.com/624049).
55 CIFilter* qr_code_generator =
56 [CIFilter filterWithName:@"CIQRCodeGenerator"];
57 [qr_code_generator setValue:qr_code_data forKey:@"inputMessage"];
58
59 // [CIImage outputImage] is available in macOS 10.10+. Could be added to
60 // sdk_forward_declarations.h but seems hardly worth it.
61 EXPECT_TRUE([qr_code_generator respondsToSelector:@selector(outputImage)]);
62 CIImage* qr_code_image =
63 [qr_code_generator performSelector:@selector(outputImage)];
64
65 const gfx::Size size([qr_code_image extent].size.width,
66 [qr_code_image extent].size.height);
67 const int num_bytes = size.GetArea() * 4 /* bytes per pixel */;
68
69 base::scoped_nsobject<CIContext> context([[CIContext alloc] init]);
70
71 base::ScopedCFTypeRef<CGImageRef> cg_image(
72 [context createCGImage:qr_code_image fromRect:[qr_code_image extent]]);
73 EXPECT_EQ(static_cast<size_t>(size.width()), CGImageGetWidth(cg_image));
74 EXPECT_EQ(static_cast<size_t>(size.height()), CGImageGetHeight(cg_image));
75
76 base::ScopedCFTypeRef<CFDataRef> raw_cg_image_data(
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);
91
92 base::RunLoop run_loop;
93 base::Closure quit_closure = run_loop.QuitClosure();
94 // Send the image Detect() and expect the response in callback.
95 EXPECT_CALL(*this, Detection(1, kInfoString))
96 .WillOnce(RunClosure(quit_closure));
97 impl_.Detect(std::move(handle), size.width(), size.height(),
98 base::Bind(&BarcodeDetectionImplMacTest::DetectCallback,
99 base::Unretained(this)));
100
101 run_loop.Run();
102 }
103
104 } // shape_detection namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698