Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 5 | #include "base/command_line.h" |
| 6 | #include "base/strings/string_tokenizer.h" |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 7 | #include "build/build_config.h" |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 8 | #include "content/public/common/content_switches.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 9 | #include "content/public/test/browser_test.h" |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 10 | #include "content/public/test/browser_test_utils.h" |
| 11 | #include "content/public/test/content_browser_test.h" |
| 12 | #include "content/public/test/content_browser_test_utils.h" |
| 13 | #include "ui/gl/gl_switches.h" |
| 14 | |
| 15 | using base::CommandLine; |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | const char kShapeDetectionTestHtml[] = "/media/shape_detection_test.html"; |
| 22 | |
| 23 | struct TestParameters { |
| 24 | const std::string detector_name; |
| 25 | const std::string image_path; |
Taiyo Mizuhashi | 5ef7635 | 2024-07-11 19:56:37 | [diff] [blame] | 26 | const std::vector<std::vector<float>> expected_bounding_boxes; |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 27 | } const kTestParameters[] = { |
Taiyo Mizuhashi | 5ef7635 | 2024-07-11 19:56:37 | [diff] [blame] | 28 | {"FaceDetector", "/blank.jpg", {}}, |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 29 | {"FaceDetector", |
| 30 | "/single_face.jpg", |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 31 | #if BUILDFLAG(IS_ANDROID) |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 32 | {{23, 20, 42, 42}} |
| 33 | #else |
| 34 | {{23, 26, 42, 42}} |
| 35 | #endif |
| 36 | }, |
| 37 | }; |
| 38 | |
| 39 | std::ostream& operator<<(std::ostream& out, |
| 40 | const struct TestParameters& parameters) { |
| 41 | out << parameters.detector_name << " running on: " << parameters.image_path; |
| 42 | return out; |
| 43 | } |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | // This class contains content_browsertests for Shape Detection API, which |
| 48 | // detect features (Faces, QR/Barcodes, etc) in still or moving images. |
| 49 | class ShapeDetectionBrowserTest |
| 50 | : public ContentBrowserTest, |
| 51 | public ::testing::WithParamInterface<struct TestParameters> { |
| 52 | public: |
| 53 | void SetUpCommandLine(base::CommandLine* command_line) override { |
Reilly Grant | 57ae4c5 | 2020-02-15 00:40:35 | [diff] [blame] | 54 | // Enable FaceDetector since it is still experimental. |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 55 | CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
Reilly Grant | 57ae4c5 | 2020-02-15 00:40:35 | [diff] [blame] | 56 | switches::kEnableBlinkFeatures, "FaceDetector"); |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | protected: |
| 60 | void RunDetectShapesOnImage( |
| 61 | const std::string& detector_name, |
| 62 | const std::string& image_path, |
| 63 | const std::vector<std::vector<float>>& expected_bounding_boxes) { |
| 64 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 65 | |
| 66 | const GURL html_url( |
| 67 | embedded_test_server()->GetURL(kShapeDetectionTestHtml)); |
| 68 | const GURL image_url(embedded_test_server()->GetURL(image_path)); |
Alex Moshchuk | aeb20fe3 | 2019-09-25 17:40:01 | [diff] [blame] | 69 | EXPECT_TRUE(NavigateToURL(shell(), html_url)); |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 70 | const std::string js_command = "detectShapesOnImageUrl('" + detector_name + |
| 71 | "', '" + image_url.spec() + "')"; |
Chris Fredrickson | b854bbf | 2023-03-27 17:27:44 | [diff] [blame] | 72 | std::string response_string = EvalJs(shell(), js_command).ExtractString(); |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 73 | |
| 74 | base::StringTokenizer outer_tokenizer(response_string, "#"); |
| 75 | std::vector<std::vector<float>> detected_bounding_boxes; |
| 76 | while (outer_tokenizer.GetNext()) { |
| 77 | std::string s = outer_tokenizer.token().c_str(); |
| 78 | std::vector<float> bounding_box; |
| 79 | base::StringTokenizer inner_tokenizer(s, ","); |
| 80 | while (inner_tokenizer.GetNext()) |
| 81 | bounding_box.push_back(atof(inner_tokenizer.token().c_str())); |
| 82 | detected_bounding_boxes.push_back(bounding_box); |
| 83 | } |
| 84 | |
| 85 | ASSERT_EQ(expected_bounding_boxes.size(), detected_bounding_boxes.size()); |
| 86 | for (size_t shape_id = 0; shape_id < detected_bounding_boxes.size(); |
| 87 | ++shape_id) { |
| 88 | const auto expected_bounding_box = expected_bounding_boxes[shape_id]; |
| 89 | const auto detected_bounding_box = detected_bounding_boxes[shape_id]; |
| 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 | } |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | |
Alison Gale | 53c77f6 | 2024-04-22 15:16:27 | [diff] [blame] | 98 | // TODO(crbug.com/41282827): Enable the test on other platforms. |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 99 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 100 | #define MAYBE_DetectShapesInImage DetectShapesInImage |
| 101 | #else |
| 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); |
| 112 | } |
| 113 | |
Ilia Samsonov | 1ad9ea3 | 2019-11-25 14:48:00 | [diff] [blame] | 114 | INSTANTIATE_TEST_SUITE_P(All, |
Victor Costan | aa7b61f | 2019-02-13 07:57:57 | [diff] [blame] | 115 | ShapeDetectionBrowserTest, |
| 116 | testing::ValuesIn(kTestParameters)); |
mcasas | 04e8ca7 | 2017-03-01 03:19:19 | [diff] [blame] | 117 | |
| 118 | } // namespace content |