chromium/services/image_annotation/annotator_unittest.cc

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/memory/ptr_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "components/manta/anchovy/anchovy_provider.h"
#include "components/manta/manta_status.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/accessibility/accessibility_features.h"
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <cstring>
#include <optional>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "net/base/net_errors.h"
#include "net/http/http_status_code.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "services/data_decoder/public/mojom/json_parser.mojom.h"
#include "services/image_annotation/annotator.h"
#include "services/image_annotation/image_annotation_metrics.h"
#include "services/image_annotation/public/mojom/image_annotation.mojom.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/url_loader.mojom-shared.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace image_annotation {

namespace {

Bucket;
ElementsAre;
Eq;
IsEmpty;
SizeIs;
UnorderedElementsAre;

MATCHER_P3(AnnotatorEq, type, score, text, "") {}

constexpr char kTestServerUrl[] =;
constexpr char kLangsServerUrl[] =;

// Example image URLs.

constexpr char kImage1Url[] =;
constexpr char kImage2Url[] =;
constexpr char kImage3Url[] =;

// Example server requests / responses.

// Template for a request for a single image.
constexpr char kTemplateRequest[] =;

// Batch request for |kImage1Url|, |kImage2Url| and |kImage3Url|.
constexpr char kBatchRequest[] =;

// Successful OCR text extraction for |kImage1Url| with no descriptions.
constexpr char kOcrSuccessResponse[] =;

// Batch response containing successful annotations for |kImage1Url| and
// |kImage2Url|, and a failure for |kImage3Url|.
//
// The results also appear "out of order" (i.e. image 2 comes before image 1).
constexpr char kBatchResponse[] =;

constexpr base::TimeDelta kThrottle =;

// The minimum dimension required for description annotation.
constexpr int32_t kDescDim =;

// The description language to use in tests that don't exercise
// language-handling logic.
constexpr char kDescLang[] =;

// An image processor that holds and exposes the callbacks it is passed.
class TestImageProcessor : public mojom::ImageProcessor {};

// A class that supports test URL loading for the "server" use case: where
// all request URLs have the same prefix and differ only in suffix and body
// content.
class TestServerURLLoaderFactory {};

// Returns a "canonically" formatted version of a JSON string by parsing and
// then rewriting it.
std::string ReformatJson(const std::string& in) {}

// Receives the result of an annotation request and writes the result data into
// the given variables.
void ReportResult(std::optional<mojom::AnnotateImageError>* const error,
                  std::vector<mojom::Annotation>* const annotations,
                  mojom::AnnotateImageResultPtr result) {}

class TestAnnotatorClient : public Annotator::Client {};

}  // namespace

// Test that annotation works for one client, and that the cache is populated.
TEST(AnnotatorTest, OcrSuccessAndCache) {}

// Test that description annotations are successfully returned.
TEST(AnnotatorTest, DescriptionSuccess) {}

// Test that the specialized OCR result takes precedence.
TEST(AnnotatorTest, DoubleOcrResult) {}

// Test that HTTP failure is gracefully handled.
TEST(AnnotatorTest, HttpError) {}

// Test that backend failure is gracefully handled.
TEST(AnnotatorTest, BackendError) {}

// Test that partial results are returned if the OCR backend fails.
TEST(AnnotatorTest, OcrBackendError) {}

// Test that partial results are returned if the description backend fails.
TEST(AnnotatorTest, DescriptionBackendError) {}

// Test that server failure (i.e. nonsense response) is gracefully handled.
TEST(AnnotatorTest, ServerError) {}

// Test that adult content returns an error.
TEST(AnnotatorTest, AdultError) {}

// Test that work is reassigned if a processor fails.
TEST(AnnotatorTest, ProcessorFails) {}

// Test a case that was previously buggy: when one client requests annotations,
// then fails local processing, then another client makes the same request.
TEST(AnnotatorTest, ProcessorFailedPreviously) {}

// Test that work is reassigned if processor dies.
TEST(AnnotatorTest, ProcessorDies) {}

// Test that multiple concurrent requests are handled in the same batch.
TEST(AnnotatorTest, ConcurrentSameBatch) {}

// Test that multiple concurrent requests are handled in separate batches.
TEST(AnnotatorTest, ConcurrentSeparateBatches) {}

// Test that work is not duplicated if it is already ongoing.
TEST(AnnotatorTest, DuplicateWork) {}

// Test that the description engine is not requested for images that violate
// model policy (i.e. are too small or have too-high an aspect ratio).
TEST(AnnotatorTest, DescPolicy) {}

// Test that description language preferences are sent to the server.
TEST(AnnotatorTest, DescLanguage) {}

// 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) {}

// Test that the specified API key is sent, but only to Google-associated server
// domains.
TEST(AnnotatorTest, ApiKey) {}

// Tests that the Annotator computes a reasonable preferred language
// based on the page language, top languages, accept languages, and
// server languages.
TEST(AnnotatorTest, ComputePreferredLanguage) {}

TEST(AnnotatorTest, FetchServerLanguages) {}

// If the server langs don't contain English, they're ignored.
TEST(AnnotatorTest, ServerLanguagesMustContainEnglish) {}

// Alternative Routing Tests.

class FakeAnchovyProvider : public manta::AnchovyProvider {};

void RunAnchovyAnnotatorTest(
    std::unique_ptr<manta::AnchovyProvider> fake_provider,
    std::vector<mojom::Annotation>* annotations) {}

void SimpleAnchovySuccessTest(std::string str_type,
                              mojom::AnnotationType expected_type) {}

TEST(AnnotatorTest, EmptyResultIfDictIsEmpty) {}

TEST(AnnotatorTest, EmptyResultIfListIsEmpty) {}

TEST(AnnotatorTest, AnchovySuccessMultiple) {}

TEST(AnnotatorTest, AnchovySuccessOCR) {}

TEST(AnnotatorTest, AnchovySuccessCaption) {}

TEST(AnnotatorTest, AnchovySuccessLabel) {}

TEST(AnnotatorTest, CrashIfNoText) {}

TEST(AnnotatorTest, CrashIfNoAnchovyProvider) {}

}  // namespace image_annotation