OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/strings/string_tokenizer.h" | 6 #include "base/strings/string_tokenizer.h" |
7 #include "content/public/common/content_switches.h" | 7 #include "content/public/common/content_switches.h" |
8 #include "content/public/test/browser_test_utils.h" | 8 #include "content/public/test/browser_test_utils.h" |
9 #include "content/public/test/content_browser_test.h" | 9 #include "content/public/test/content_browser_test.h" |
10 #include "content/public/test/content_browser_test_utils.h" | 10 #include "content/public/test/content_browser_test_utils.h" |
11 #include "ui/gl/gl_switches.h" | |
12 | |
13 using base::CommandLine; | |
11 | 14 |
12 namespace content { | 15 namespace content { |
13 | 16 |
14 // TODO(xianglu): Enable other platforms support. https://crbug.com/646083 | |
15 #if defined(OS_ANDROID) | |
16 #define MAYBE_ShapeDetectionBrowserTest ShapeDetectionBrowserTest | |
17 #else | |
18 #define MAYBE_ShapeDetectionBrowserTest DISABLED_ShapeDetectionBrowserTest | |
19 #endif | |
20 | |
21 namespace { | 17 namespace { |
22 | 18 |
23 const char kFaceDetectionTestHtml[] = "/media/face_detection_test.html"; | 19 const char kShapeDetectionTestHtml[] = "/media/shape_detection_test.html"; |
20 | |
21 struct TestParameters { | |
22 const std::string detector_name; | |
23 const std::string image_path; | |
24 const std::vector<std::vector<float>>& expected_bounding_boxes; | |
25 } const kTestParameters[] = { | |
26 {"FaceDetector", "/blank.jpg", {}}, | |
27 {"FaceDetector", | |
28 "/single_face.jpg", | |
29 #if defined(OS_ANDROID) | |
30 {{23, 20, 42, 42}} | |
31 #else | |
32 {{23, 26, 42, 42}} | |
33 #endif | |
34 }, | |
35 }; | |
36 | |
37 std::ostream& operator<<(std::ostream& out, | |
38 const struct TestParameters& parameters) { | |
39 out << parameters.detector_name << " running on: " << parameters.image_path; | |
40 return out; | |
41 } | |
24 | 42 |
25 } // namespace | 43 } // namespace |
26 | 44 |
27 // This class contains content_browsertests for Shape Detection API, which | 45 // This class contains content_browsertests for Shape Detection API, which |
28 // allows for generating bounding boxes in still images. | 46 // detect features (Faces, QR/Barcodes, etc) in still or moving images. |
29 class MAYBE_ShapeDetectionBrowserTest : public ContentBrowserTest { | 47 class ShapeDetectionBrowserTest |
48 : public ContentBrowserTest, | |
49 public ::testing::WithParamInterface<struct TestParameters> { | |
30 public: | 50 public: |
31 void SetUpCommandLine(base::CommandLine* command_line) override { | 51 void SetUpCommandLine(base::CommandLine* command_line) override { |
32 // Flag to enable ShapeDetection and DOMRect API. | 52 // Flags to enable ShapeDetection and DOMRect API. |
33 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 53 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
34 switches::kEnableBlinkFeatures, "ShapeDetection, GeometryInterfaces"); | 54 switches::kEnableBlinkFeatures, "ShapeDetection, GeometryInterfaces"); |
35 } | 55 } |
36 | 56 |
37 protected: | 57 protected: |
38 void RunDetectFacesOnImageUrl( | 58 void RunDetectShapesOnImage( |
59 const std::string& detector_name, | |
39 const std::string& image_path, | 60 const std::string& image_path, |
40 const std::vector<std::vector<float>>& expected_results) { | 61 const std::vector<std::vector<float>>& expected_bounding_boxes) { |
41 ASSERT_TRUE(embedded_test_server()->Start()); | 62 ASSERT_TRUE(embedded_test_server()->Start()); |
42 | 63 |
43 const GURL html_url(embedded_test_server()->GetURL(kFaceDetectionTestHtml)); | 64 const GURL html_url( |
65 embedded_test_server()->GetURL(kShapeDetectionTestHtml)); | |
44 const GURL image_url(embedded_test_server()->GetURL(image_path)); | 66 const GURL image_url(embedded_test_server()->GetURL(image_path)); |
45 NavigateToURL(shell(), html_url); | 67 NavigateToURL(shell(), html_url); |
46 const std::string js_command = | 68 const std::string js_command = "detectShapesOnImageUrl('" + detector_name + |
47 "detectFacesOnImageUrl('" + image_url.spec() + "')"; | 69 "', '" + image_url.spec() + "')"; |
48 std::string response_string; | 70 std::string response_string; |
49 ASSERT_TRUE( | 71 ASSERT_TRUE( |
50 ExecuteScriptAndExtractString(shell(), js_command, &response_string)); | 72 ExecuteScriptAndExtractString(shell(), js_command, &response_string)); |
51 | 73 |
52 base::StringTokenizer outer_tokenizer(response_string, "#"); | 74 base::StringTokenizer outer_tokenizer(response_string, "#"); |
53 std::vector<std::vector<float>> results; | 75 std::vector<std::vector<float>> detected_bounding_boxes = {}; |
Reilly Grant (use Gerrit)
2017/02/28 00:35:11
Why initialize this with {}?
mcasas
2017/02/28 18:10:18
Because if the loop in l.76 is not executed
at all
Reilly Grant (use Gerrit)
2017/02/28 18:49:13
As discussed offline this doesn't make sense to me
mcasas
2017/02/28 21:06:57
Removed.
| |
54 while (outer_tokenizer.GetNext()) { | 76 while (outer_tokenizer.GetNext()) { |
55 std::string s = outer_tokenizer.token().c_str(); | 77 std::string s = outer_tokenizer.token().c_str(); |
56 std::vector<float> res; | 78 std::vector<float> bounding_box; |
57 base::StringTokenizer inner_tokenizer(s, ","); | 79 base::StringTokenizer inner_tokenizer(s, ","); |
58 while (inner_tokenizer.GetNext()) | 80 while (inner_tokenizer.GetNext()) |
59 res.push_back(atof(inner_tokenizer.token().c_str())); | 81 bounding_box.push_back(atof(inner_tokenizer.token().c_str())); |
60 results.push_back(res); | 82 detected_bounding_boxes.push_back(bounding_box); |
61 } | 83 } |
62 | 84 |
63 ASSERT_EQ(expected_results.size(), results.size()) << "Number of faces"; | 85 ASSERT_EQ(expected_bounding_boxes.size(), detected_bounding_boxes.size()); |
64 for (size_t face_id = 0; face_id < results.size(); ++face_id) { | 86 for (size_t shape_id = 0; shape_id < detected_bounding_boxes.size(); |
65 const std::vector<float> expected_result = expected_results[face_id]; | 87 ++shape_id) { |
66 const std::vector<float> result = results[face_id]; | 88 const auto expected_bounding_box = expected_bounding_boxes[shape_id]; |
67 for (size_t i = 0; i < 4; ++i) | 89 const auto detected_bounding_box = detected_bounding_boxes[shape_id]; |
68 EXPECT_NEAR(expected_result[i], result[i], 2) << "At index " << i; | 90 for (size_t i = 0; i < 4; ++i) { |
91 EXPECT_NEAR(expected_bounding_box[i], detected_bounding_box[i], 2) | |
92 << ", index " << i; | |
93 } | |
69 } | 94 } |
70 } | 95 } |
71 }; | 96 }; |
72 | 97 |
73 IN_PROC_BROWSER_TEST_F(MAYBE_ShapeDetectionBrowserTest, | 98 // TODO(xianglu): Enable the test on other platforms. https://crbug.com/659138 |
Reilly Grant (use Gerrit)
2017/02/28 00:35:11
nit: TODO(https://crbug.com/659138)
mcasas
2017/02/28 18:10:18
Done.
| |
74 DetectFacesOnImageWithNoFaces) { | 99 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
75 const std::string image_path = "/blank.jpg"; | 100 #define MAYBE_DetectShapesInImage DetectShapesInImage |
76 const std::vector<std::vector<float>> expected_empty_results; | 101 #else |
77 RunDetectFacesOnImageUrl(image_path, expected_empty_results); | 102 #define MAYBE_DetectShapesInImage DISABLED_DetectShapesInImage |
103 #endif | |
104 | |
105 IN_PROC_BROWSER_TEST_P(ShapeDetectionBrowserTest, MAYBE_DetectShapesInImage) { | |
106 // Face detection needs GPU infrastructure. | |
107 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseGpuInTests)) | |
108 return; | |
109 | |
110 RunDetectShapesOnImage(GetParam().detector_name, GetParam().image_path, | |
111 GetParam().expected_bounding_boxes); | |
78 } | 112 } |
79 | 113 |
80 IN_PROC_BROWSER_TEST_F(MAYBE_ShapeDetectionBrowserTest, | 114 INSTANTIATE_TEST_CASE_P(, |
81 DetectFacesOnImageWithOneFace) { | 115 ShapeDetectionBrowserTest, |
82 const std::string image_path = "/single_face.jpg"; | 116 testing::ValuesIn(kTestParameters)); |
83 #if defined(OS_ANDROID) | |
84 const std::vector<std::vector<float>> expected_results{ | |
85 {68.640625, 102.96875, 171.5625, 171.5625}}; | |
86 #else | |
87 const std::vector<std::vector<float>> expected_results; | |
88 #endif | |
89 | |
90 RunDetectFacesOnImageUrl(image_path, expected_results); | |
91 } | |
92 | 117 |
93 } // namespace content | 118 } // namespace content |
OLD | NEW |