Commit 3d883bec authored by Dominic Mazzoni's avatar Dominic Mazzoni Committed by Commit Bot

Add test for image_annotator language fallback.

This test is a follow-up to this patch: http://crrev.com/c/2226300

It ensures that if the server request uses a different language than the
language passed with the original request, the annotation still succeeds.

Also adds a log to make it more clear if we were ever to hit this case
in the future. Previously it was a silent failure.

AX-relnotes: N/A
Bug: 1090234, 1070505
Change-Id: I6c728a859bfa4c9a182ea4dd446b3b54c1836014
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2233482Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Commit-Queue: Dominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#776131}
parent ac7bfb4b
...@@ -737,8 +737,11 @@ void Annotator::ProcessResults( ...@@ -737,8 +737,11 @@ void Annotator::ProcessResults(
// |request_infos_|, and this method should only execute once per request // |request_infos_|, and this method should only execute once per request
// key. // key.
const auto request_info_it = request_infos_.find(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; continue;
}
const auto image_result = result_lookup != results.end() const auto image_result = result_lookup != results.end()
? result_lookup->second.Clone() ? result_lookup->second.Clone()
......
...@@ -107,6 +107,7 @@ class Annotator : public mojom::Annotator { ...@@ -107,6 +107,7 @@ class Annotator : public mojom::Annotator {
FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, ComputePreferredLanguage); FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, ComputePreferredLanguage);
FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, FetchServerLanguages); FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, FetchServerLanguages);
FRIEND_TEST_ALL_PREFIXES(AnnotatorTest, ServerLanguagesMustContainEnglish); 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. // The relevant info for a request from a client feature for a single image.
struct ClientRequestInfo { struct ClientRequestInfo {
......
...@@ -2173,6 +2173,107 @@ TEST(AnnotatorTest, DescLanguage) { ...@@ -2173,6 +2173,107 @@ TEST(AnnotatorTest, DescLanguage) {
mojom::AnnotationType::kOcr, 1.0, "2"))); 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 // Test that the specified API key is sent, but only to Google-associated server
// domains. // domains.
TEST(AnnotatorTest, ApiKey) { TEST(AnnotatorTest, ApiKey) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment