Chromium Code Reviews| Index: content/browser/shapedetection/shapedetection_browsertest.cc |
| diff --git a/content/browser/shapedetection/shapedetection_browsertest.cc b/content/browser/shapedetection/shapedetection_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2771c12299a3aeba590851c400f8e639dd8211f7 |
| --- /dev/null |
| +++ b/content/browser/shapedetection/shapedetection_browsertest.cc |
| @@ -0,0 +1,95 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/command_line.h" |
| +#include "base/strings/string_tokenizer.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "content/public/test/content_browser_test.h" |
| +#include "content/public/test/content_browser_test_utils.h" |
| + |
| +namespace content { |
| + |
| +// TODO(xianglu): Enable other platforms with support. https://crbug.com/646083 |
| +#if defined(OS_ANDROID) |
| +#define MAYBE_DetectFacesOnImageWithNoFaces DetectFacesOnImageWithNoFaces |
| +#define MAYBE_DetectFacesOnImageWithOneFace DetectFacesOnImageWithOneFace |
| +#else |
| +#define MAYBE_DetectFacesOnImageWithNoFaces DISABLED_DetectFacesOnImageWithNoFaces |
| +#define MAYBE_DetectFacesOnImageWithOneFace DISABLED_DetectFacesOnImageWithOneFace |
| +#endif |
| + |
| +namespace { |
| + |
| +const char kFaceDetectionTestHtml[] = "/media/face_detection_test.html"; |
| + |
| +} // namespace |
| + |
| +// This class contains content_browsertests for Shape Detection API, which |
| +// allows for generating bounding boxes for faces on still images.. |
| +class ShapeDetectionBrowserTest : public ContentBrowserTest { |
| + public: |
| + void SetUpCommandLine(base::CommandLine* command_line) override { |
| + // Specific flag to enable ShapeDetection and DOMRect API. |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kEnableBlinkFeatures, "ShapeDetection, GeometryInterfaces"); |
| + } |
| + |
| + protected: |
| + void RunDetectFacesOnImageUrl( |
| + const std::string& image_path, |
| + const std::vector<std::vector<float>>& expected_results) { |
| + ASSERT_TRUE(embedded_test_server()->Start()); |
| + |
| + const GURL html_url(embedded_test_server()->GetURL(kFaceDetectionTestHtml)); |
| + const GURL image_url(embedded_test_server()->GetURL(image_path)); |
| + NavigateToURL(shell(), html_url); |
| + const std::string js_command = |
| + "detectFacesOnImageUrl('" + image_url.spec() + "')"; |
| + std::string response_string; |
| + ASSERT_TRUE( |
| + ExecuteScriptAndExtractString(shell(), js_command, &response_string)); |
| + |
| + base::StringTokenizer outer_tokenizer(response_string, "#"); |
| + std::vector<std::vector<float>> results; |
| + while (outer_tokenizer.GetNext()) { |
| + std::string s = outer_tokenizer.token().c_str(); |
| + std::vector<float> res; |
| + base::StringTokenizer inner_tokenizer(s, ","); |
| + while (inner_tokenizer.GetNext()) { |
| + res.push_back(atof(inner_tokenizer.token().c_str())); |
| + } |
|
mcasas
2016/10/19 23:41:11
nit: no need for curly brackets {} for one line bo
xianglu
2016/10/20 17:58:48
Done.
|
| + results.push_back(res); |
| + } |
| + |
| + ASSERT_EQ(expected_results.size(), results.size()); |
|
mcasas
2016/10/19 23:41:11
Good!
|
| + for (size_t face_id = 0; face_id < results.size(); face_id++) { |
| + std::vector<float> expected_result = expected_results[face_id]; |
| + std::vector<float> result = results[face_id]; |
| + for (size_t pram_idx = 0; pram_idx < 4; pram_idx++) { |
|
mcasas
2016/10/19 23:41:11
Micro-nit: try to get used to preincrement
the in
xianglu
2016/10/20 17:58:47
Done.
|
| + // TODO(xianglu): Always verify if EXPECT_FLOAT_EQ is sufficient for |
| + // different platforms. |
|
mcasas
2016/10/19 23:41:11
You can also consider using e.g.
EXPECT_NEAR(expec
xianglu
2016/10/20 17:58:48
Done.
|
| + EXPECT_FLOAT_EQ(expected_result[pram_idx], result[pram_idx]); |
|
Reilly Grant (use Gerrit)
2016/10/19 23:48:40
Tip: Add << " at index " << param_index; to the en
xianglu
2016/10/20 17:58:48
Done.
|
| + } |
| + } |
| + } |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ShapeDetectionBrowserTest, |
| + MAYBE_DetectFacesOnImageWithNoFaces) { |
| + const std::string image_path = "/blank.jpg"; |
| + std::vector<std::vector<float>> expected_results; |
| + RunDetectFacesOnImageUrl(image_path, expected_results); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ShapeDetectionBrowserTest, |
| + MAYBE_DetectFacesOnImageWithOneFace) { |
| + const std::string image_path = "/single_face.jpg"; |
| + std::vector<float> expected_result = {68.640625, 102.96875, 171.5625, |
| + 171.5625}; |
|
mcasas
2016/10/19 23:41:11
nit: const :)
xianglu
2016/10/20 17:58:48
Done.
|
| + std::vector<std::vector<float>> expected_results = {expected_result}; |
| + RunDetectFacesOnImageUrl(image_path, expected_results); |
| +} |
| + |
| +} // namespace content |