blob: f29ff801fd1187c00d42255b93b7d181c9d5073b [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2016 The Chromium Authors
mcasas04e8ca72017-03-01 03:19:192// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
mcasas04e8ca72017-03-01 03:19:195#include "base/command_line.h"
6#include "base/strings/string_tokenizer.h"
Xiaohan Wang1ecfd002022-01-19 22:33:107#include "build/build_config.h"
mcasas04e8ca72017-03-01 03:19:198#include "content/public/common/content_switches.h"
Peter Kasting919ce652020-05-07 10:22:369#include "content/public/test/browser_test.h"
mcasas04e8ca72017-03-01 03:19:1910#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
15using base::CommandLine;
16
17namespace content {
18
19namespace {
20
21const char kShapeDetectionTestHtml[] = "/media/shape_detection_test.html";
22
23struct TestParameters {
24 const std::string detector_name;
25 const std::string image_path;
Taiyo Mizuhashi5ef76352024-07-11 19:56:3726 const std::vector<std::vector<float>> expected_bounding_boxes;
mcasas04e8ca72017-03-01 03:19:1927} const kTestParameters[] = {
Taiyo Mizuhashi5ef76352024-07-11 19:56:3728 {"FaceDetector", "/blank.jpg", {}},
mcasas04e8ca72017-03-01 03:19:1929 {"FaceDetector",
30 "/single_face.jpg",
Xiaohan Wang1ecfd002022-01-19 22:33:1031#if BUILDFLAG(IS_ANDROID)
mcasas04e8ca72017-03-01 03:19:1932 {{23, 20, 42, 42}}
33#else
34 {{23, 26, 42, 42}}
35#endif
36 },
37};
38
39std::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.
49class ShapeDetectionBrowserTest
50 : public ContentBrowserTest,
51 public ::testing::WithParamInterface<struct TestParameters> {
52 public:
53 void SetUpCommandLine(base::CommandLine* command_line) override {
Reilly Grant57ae4c52020-02-15 00:40:3554 // Enable FaceDetector since it is still experimental.
mcasas04e8ca72017-03-01 03:19:1955 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
Reilly Grant57ae4c52020-02-15 00:40:3556 switches::kEnableBlinkFeatures, "FaceDetector");
mcasas04e8ca72017-03-01 03:19:1957 }
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 Moshchukaeb20fe32019-09-25 17:40:0169 EXPECT_TRUE(NavigateToURL(shell(), html_url));
mcasas04e8ca72017-03-01 03:19:1970 const std::string js_command = "detectShapesOnImageUrl('" + detector_name +
71 "', '" + image_url.spec() + "')";
Chris Fredricksonb854bbf2023-03-27 17:27:4472 std::string response_string = EvalJs(shell(), js_command).ExtractString();
mcasas04e8ca72017-03-01 03:19:1973
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 Gale53c77f62024-04-22 15:16:2798// TODO(crbug.com/41282827): Enable the test on other platforms.
Xiaohan Wang1ecfd002022-01-19 22:33:1099#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC)
mcasas04e8ca72017-03-01 03:19:19100#define MAYBE_DetectShapesInImage DetectShapesInImage
101#else
102#define MAYBE_DetectShapesInImage DISABLED_DetectShapesInImage
103#endif
104
105IN_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 Samsonov1ad9ea32019-11-25 14:48:00114INSTANTIATE_TEST_SUITE_P(All,
Victor Costanaa7b61f2019-02-13 07:57:57115 ShapeDetectionBrowserTest,
116 testing::ValuesIn(kTestParameters));
mcasas04e8ca72017-03-01 03:19:19117
118} // namespace content