diff options
Diffstat (limited to 'chromium/services/image_annotation')
-rw-r--r-- | chromium/services/image_annotation/annotator.cc | 5 | ||||
-rw-r--r-- | chromium/services/image_annotation/annotator.h | 1 | ||||
-rw-r--r-- | chromium/services/image_annotation/annotator_unittest.cc | 101 |
3 files changed, 106 insertions, 1 deletions
diff --git a/chromium/services/image_annotation/annotator.cc b/chromium/services/image_annotation/annotator.cc index 53de4424f67..4c8b943a70e 100644 --- a/chromium/services/image_annotation/annotator.cc +++ b/chromium/services/image_annotation/annotator.cc @@ -737,8 +737,11 @@ void Annotator::ProcessResults( // |request_infos_|, and this method should only execute once per request // key. const auto request_info_it = request_infos_.find(request_key); - if (request_info_it == request_infos_.end()) + if (request_info_it == request_infos_.end()) { + LOG(ERROR) << "Could not find request key in request_infos_: " + << request_key.first << "," << request_key.second; continue; + } const auto image_result = result_lookup != results.end() ? result_lookup->second.Clone() diff --git a/chromium/services/image_annotation/annotator.h b/chromium/services/image_annotation/annotator.h index 572ba326fc3..56ddf90fc34 100644 --- a/chromium/services/image_annotation/annotator.h +++ b/chromium/services/image_annotation/annotator.h @@ -107,6 +107,7 @@ class Annotator : public mojom::Annotator { FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, ComputePreferredLanguage); FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, FetchServerLanguages); FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, ServerLanguagesMustContainEnglish); + FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, LanguageFallback); // The relevant info for a request from a client feature for a single image. struct ClientRequestInfo { diff --git a/chromium/services/image_annotation/annotator_unittest.cc b/chromium/services/image_annotation/annotator_unittest.cc index 7803095dd5d..40e6b6cc239 100644 --- a/chromium/services/image_annotation/annotator_unittest.cc +++ b/chromium/services/image_annotation/annotator_unittest.cc @@ -2173,6 +2173,107 @@ TEST(AnnotatorTest, DescLanguage) { mojom::AnnotationType::kOcr, 1.0, "2"))); } +// Test that annotation works properly when we need to fall back on a +// different language because the page language isn't available. +TEST(AnnotatorTest, LanguageFallback) { + base::test::TaskEnvironment test_task_env( + base::test::TaskEnvironment::TimeSource::MOCK_TIME); + TestServerURLLoaderFactory test_url_factory( + "https://ia-pa.googleapis.com/v1/"); + data_decoder::test::InProcessDataDecoder in_process_data_decoder; + base::HistogramTester histogram_tester; + + Annotator annotator(GURL(kTestServerUrl), GURL(""), + std::string() /* api_key */, kThrottle, + 1 /* batch_size */, 1.0 /* min_ocr_confidence */, + test_url_factory.AsSharedURLLoaderFactory(), + std::make_unique<TestAnnotatorClient>()); + annotator.server_languages_ = {"en", "it", "fr"}; + + TestImageProcessor processor; + base::Optional<mojom::AnnotateImageError> error; + std::vector<mojom::Annotation> annotations; + + // Send a request in an unsupported language. + annotator.AnnotateImage(kImage1Url, "hu", processor.GetPendingRemote(), + base::BindOnce(&ReportResult, &error, &annotations)); + test_task_env.RunUntilIdle(); + + // Send back image data. + std::move(processor.callbacks()[0]).Run({1, 2, 3}, kDescDim, kDescDim); + processor.callbacks().pop_back(); + test_task_env.RunUntilIdle(); + + // Fast-forward time so that server sends batch. + EXPECT_THAT(test_url_factory.requests(), IsEmpty()); + test_task_env.FastForwardBy(base::TimeDelta::FromSeconds(1)); + + // A single HTTP request for all images should have been sent. + test_url_factory.ExpectRequestAndSimulateResponse( + "annotation", {} /* expected_headers */, ReformatJson(R"( + { + "imageRequests": [ + { + "imageId": "https://www.example.com/image1.jpg en", + "imageBytes": "AQID", + "engineParameters": [ + {"ocrParameters": {}}, + { + "descriptionParameters": { + "preferredLanguages": ["en"] + } + } + ] + } + ] + } + )"), + R"( + { + "results": [ + { + "imageId": "https://www.example.com/image1.jpg en", + "engineResults": [ + { + "status": {}, + "ocrEngine": { + "ocrRegions": [{ + "words": [{ + "detectedText": "1", + "confidenceScore": 1.0 + }] + }] + } + }, + { + "status": {}, + "descriptionEngine": { + "descriptionList": { + "descriptions": [{ + "type": "CAPTION", + "text": "Result in fallback language.", + "score": 1.0 + }] + } + } + } + ] + } + ] + } + )", + net::HTTP_OK); + test_task_env.RunUntilIdle(); + + // Annotator should have called each callback with its corresponding results. + ASSERT_EQ(error, base::nullopt); + EXPECT_THAT( + annotations, + UnorderedElementsAre(AnnotatorEq(mojom::AnnotationType::kOcr, 1.0, "1"), + AnnotatorEq(mojom::AnnotationType::kCaption, 1.0, + "Result in fallback language."))); +} + // Test that the specified API key is sent, but only to Google-associated server // domains. TEST(AnnotatorTest, ApiKey) { |