| 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 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 // TODO(xianglu): Enable other platforms with support. https://crbug.com/646083 | 14 // TODO(xianglu): Enable other platforms support. https://crbug.com/646083 |
| 15 #if defined(OS_ANDROID) | 15 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 16 #define MAYBE_ShapeDetectionBrowserTest ShapeDetectionBrowserTest | 16 #define MAYBE_ShapeDetectionBrowserTest ShapeDetectionBrowserTest |
| 17 #else | 17 #else |
| 18 #define MAYBE_ShapeDetectionBrowserTest DISABLED_ShapeDetectionBrowserTest | 18 #define MAYBE_ShapeDetectionBrowserTest DISABLED_ShapeDetectionBrowserTest |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char kFaceDetectionTestHtml[] = "/media/face_detection_test.html"; | 23 const char kFaceDetectionTestHtml[] = "/media/face_detection_test.html"; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 // This class contains content_browsertests for Shape Detection API, which | 27 // This class contains content_browsertests for Shape Detection API, which |
| 28 // allows for generating bounding boxes for faces on still images.. | 28 // allows for generating bounding boxes in still images.. |
| 29 class MAYBE_ShapeDetectionBrowserTest : public ContentBrowserTest { | 29 class MAYBE_ShapeDetectionBrowserTest : public ContentBrowserTest { |
| 30 public: | 30 public: |
| 31 void SetUpCommandLine(base::CommandLine* command_line) override { | 31 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 32 // Specific flag to enable ShapeDetection and DOMRect API. | 32 // Flag to enable ShapeDetection and DOMRect API. |
| 33 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 33 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 34 switches::kEnableBlinkFeatures, "ShapeDetection, GeometryInterfaces"); | 34 switches::kEnableBlinkFeatures, "ShapeDetection, GeometryInterfaces"); |
| 35 } | 35 } |
| 36 | 36 |
| 37 protected: | 37 protected: |
| 38 void RunDetectFacesOnImageUrl( | 38 void RunDetectFacesOnImageUrl( |
| 39 const std::string& image_path, | 39 const std::string& image_path, |
| 40 const std::vector<std::vector<float>>& expected_results) { | 40 const std::vector<std::vector<float>>& expected_results) { |
| 41 ASSERT_TRUE(embedded_test_server()->Start()); | 41 ASSERT_TRUE(embedded_test_server()->Start()); |
| 42 | 42 |
| 43 const GURL html_url(embedded_test_server()->GetURL(kFaceDetectionTestHtml)); | 43 const GURL html_url(embedded_test_server()->GetURL(kFaceDetectionTestHtml)); |
| 44 const GURL image_url(embedded_test_server()->GetURL(image_path)); | 44 const GURL image_url(embedded_test_server()->GetURL(image_path)); |
| 45 NavigateToURL(shell(), html_url); | 45 NavigateToURL(shell(), html_url); |
| 46 const std::string js_command = | 46 const std::string js_command = |
| 47 "detectFacesOnImageUrl('" + image_url.spec() + "')"; | 47 "detectFacesOnImageUrl('" + image_url.spec() + "')"; |
| 48 std::string response_string; | 48 std::string response_string; |
| 49 ASSERT_TRUE( | 49 ASSERT_TRUE( |
| 50 ExecuteScriptAndExtractString(shell(), js_command, &response_string)); | 50 ExecuteScriptAndExtractString(shell(), js_command, &response_string)); |
| 51 | 51 |
| 52 base::StringTokenizer outer_tokenizer(response_string, "#"); | 52 base::StringTokenizer outer_tokenizer(response_string, "#"); |
| 53 std::vector<std::vector<float>> results; | 53 std::vector<std::vector<float>> results; |
| 54 while (outer_tokenizer.GetNext()) { | 54 while (outer_tokenizer.GetNext()) { |
| 55 std::string s = outer_tokenizer.token().c_str(); | 55 std::string s = outer_tokenizer.token().c_str(); |
| 56 std::vector<float> res; | 56 std::vector<float> res; |
| 57 base::StringTokenizer inner_tokenizer(s, ","); | 57 base::StringTokenizer inner_tokenizer(s, ","); |
| 58 while (inner_tokenizer.GetNext()) | 58 while (inner_tokenizer.GetNext()) |
| 59 res.push_back(atof(inner_tokenizer.token().c_str())); | 59 res.push_back(atof(inner_tokenizer.token().c_str())); |
| 60 results.push_back(res); | 60 results.push_back(res); |
| 61 } | 61 } |
| 62 ASSERT_EQ(expected_results.size(), results.size()) << "Number of faces"; |
| 62 | 63 |
| 63 ASSERT_EQ(expected_results.size(), results.size()) << "Number of faces"; | |
| 64 for (size_t face_id = 0; face_id < results.size(); ++face_id) { | 64 for (size_t face_id = 0; face_id < results.size(); ++face_id) { |
| 65 const std::vector<float> expected_result = expected_results[face_id]; | 65 const std::vector<float> expected_result = expected_results[face_id]; |
| 66 const std::vector<float> result = results[face_id]; | 66 const std::vector<float> result = results[face_id]; |
| 67 for (size_t i = 0; i < 4; ++i) | 67 for (size_t i = 0; i < 4; ++i) |
| 68 EXPECT_NEAR(expected_result[i], result[i], 0.1) << "At index " << i; | 68 EXPECT_NEAR(expected_result[i], result[i], 2) << "At index " << i; |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 IN_PROC_BROWSER_TEST_F(MAYBE_ShapeDetectionBrowserTest, | 73 IN_PROC_BROWSER_TEST_F(MAYBE_ShapeDetectionBrowserTest, |
| 74 DetectFacesOnImageWithNoFaces) { | 74 DetectFacesOnImageWithNoFaces) { |
| 75 const std::string image_path = "/blank.jpg"; | 75 const std::string image_path = "/blank.jpg"; |
| 76 const std::vector<std::vector<float>> expected_empty_results; | 76 const std::vector<std::vector<float>> expected_empty_results; |
| 77 RunDetectFacesOnImageUrl(image_path, expected_empty_results); | 77 RunDetectFacesOnImageUrl(image_path, expected_empty_results); |
| 78 } | 78 } |
| 79 | 79 |
| 80 IN_PROC_BROWSER_TEST_F(MAYBE_ShapeDetectionBrowserTest, | 80 IN_PROC_BROWSER_TEST_F(MAYBE_ShapeDetectionBrowserTest, |
| 81 DetectFacesOnImageWithOneFace) { | 81 DetectFacesOnImageWithOneFace) { |
| 82 const std::string image_path = "/single_face.jpg"; | 82 const std::string image_path = "/single_face.jpg"; |
| 83 std::vector<std::vector<float>> expected_results; |
| 84 #if defined(OS_ANDROID) |
| 83 const std::vector<float> expected_result = {68.640625, 102.96875, 171.5625, | 85 const std::vector<float> expected_result = {68.640625, 102.96875, 171.5625, |
| 84 171.5625}; | 86 171.5625}; |
| 85 const std::vector<std::vector<float>> expected_results = {expected_result}; | 87 expected_results.push_back(expected_result); |
| 88 #elif defined(OS_MACOSX) |
| 89 const std::vector<float> expected_result = {0, 93, 290, 290}; |
| 90 expected_results.push_back(expected_result); |
| 91 #endif |
| 92 |
| 86 RunDetectFacesOnImageUrl(image_path, expected_results); | 93 RunDetectFacesOnImageUrl(image_path, expected_results); |
| 87 } | 94 } |
| 88 | 95 |
| 89 } // namespace content | 96 } // namespace content |
| OLD | NEW |