File: on_device_translation_service_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (166 lines) | stat: -rw-r--r-- 6,614 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/services/on_device_translation/on_device_translation_service.h"

#include "base/command_line.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "components/services/on_device_translation/public/cpp/features.h"
#include "components/services/on_device_translation/public/mojom/on_device_translation_service.mojom.h"
#include "components/services/on_device_translation/public/mojom/translator.mojom.h"
#include "components/services/on_device_translation/test/test_util.h"
#include "components/services/on_device_translation/translate_kit_client.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace on_device_translation {
namespace {

class OnDeviceTranslationServiceTest : public testing::Test {
 public:
  OnDeviceTranslationServiceTest()
      : service_impl_(OnDeviceTranslationService::CreateForTesting(
            service_remote_.BindNewPipeAndPassReceiver(),
            TranslateKitClient::CreateForTest(GetMockLibraryPath()))) {}
  ~OnDeviceTranslationServiceTest() override = default;

  OnDeviceTranslationServiceTest(const OnDeviceTranslationServiceTest&) =
      delete;
  OnDeviceTranslationServiceTest& operator=(
      const OnDeviceTranslationServiceTest&) = delete;

 protected:
  // Sends a config to the service and returns a fake file operation proxy that
  // needs to be kept alive for the duration of the test.
  std::unique_ptr<FakeFileOperationProxy, base::OnTaskRunnerDeleter> SendConfig(
      const std::vector<std::pair<std::string, std::string>>& packages,
      const std::vector<TestFile>& files) {
    auto config = mojom::OnDeviceTranslationServiceConfig::New();

    for (const auto& pair : packages) {
      config->packages.push_back(mojom::OnDeviceTranslationLanguagePackage::New(
          pair.first, pair.second));
    }
    auto file_operation_proxy = FakeFileOperationProxy::Create(
        config->file_operation_proxy.InitWithNewPipeAndPassReceiver(), files);
    service_remote_->SetServiceConfig(std::move(config));
    return file_operation_proxy;
  }

  // Returns true if the service can translate the given language pair.
  bool CanTranslate(const std::string& source_lang,
                    const std::string& target_lang) {
    bool result_out = false;
    base::RunLoop run_loop;
    service_remote_->CanTranslate(source_lang, target_lang,
                                  base::BindLambdaForTesting([&](bool result) {
                                    result_out = result;
                                    run_loop.Quit();
                                  }));
    run_loop.Run();
    return result_out;
  }

  // Returns a remote to a translator for the given language pair or an invalid
  // remote if the language pair is not supported.
  mojo::Remote<mojom::Translator> CreateTranslator(
      const std::string& source_lang,
      const std::string& target_lang) {
    mojo::Remote<mojom::Translator> translator_remote;
    base::RunLoop run_loop;
    bool succeeded = false;
    service_remote_->CreateTranslator(
        source_lang, target_lang,
        translator_remote.BindNewPipeAndPassReceiver(),
        base::BindLambdaForTesting([&](mojom::CreateTranslatorResult result) {
          succeeded = result == mojom::CreateTranslatorResult::kSuccess;
          run_loop.Quit();
        }));
    run_loop.Run();
    return succeeded ? std::move(translator_remote)
                     : mojo::Remote<mojom::Translator>();
  }

 private:
  base::test::TaskEnvironment task_environment_;
  mojo::Remote<mojom::OnDeviceTranslationService> service_remote_;
  std::unique_ptr<OnDeviceTranslationService> service_impl_;
};

// Tests that the CanTranslate method returns true for a language pair that is
// supported by the service.
TEST_F(OnDeviceTranslationServiceTest, CanTranslateSuccess) {
  auto file_operation_proxy =
      SendConfig({{"en", "ja"}}, {
                                     {"0/dict.dat", "En to Ja - "},
                                 });
  EXPECT_TRUE(CanTranslate("en", "ja"));
}

// Tests that the CanTranslate method returns false for a language pair that is
// not supported by the service.
TEST_F(OnDeviceTranslationServiceTest, CanTranslateFaulure) {
  auto file_operation_proxy =
      SendConfig({{"en", "ja"}}, {
                                     {"0/dict.dat", "En to Ja - "},
                                 });
  EXPECT_FALSE(CanTranslate("en", "es"));
}

// Tests that the CreateTranslator method returns a valid remote to a translator
// for a language pair that is supported by the service. And the translator
// should be able to translate a text.
TEST_F(OnDeviceTranslationServiceTest, CreateTranslatorSuccess) {
  auto file_operation_proxy =
      SendConfig({{"en", "ja"}}, {
                                     {"0/dict.dat", "En to Ja - "},
                                 });
  mojo::Remote<mojom::Translator> translator_remote =
      CreateTranslator("en", "ja");
  ASSERT_TRUE(translator_remote);

  base::RunLoop run_loop;
  translator_remote->Translate(
      "test",
      base::BindLambdaForTesting([&](const std::optional<std::string>& output) {
        EXPECT_EQ(output, "En to Ja - test");
        run_loop.Quit();
      }));
  run_loop.Run();
}

// Tests that the CreateTranslator method returns a null remote when the
// language pair is not supported by the service.
TEST_F(OnDeviceTranslationServiceTest, CreateTranslatorFaulure) {
  auto file_operation_proxy =
      SendConfig({{"en", "ja"}}, {
                                     {"0/dict.dat", "En to Ja - "},
                                 });
  EXPECT_FALSE(CreateTranslator("en", "es"));
}

// Tests that the Translate method returns an empty string when the translator
// fails to translate the text.
TEST_F(OnDeviceTranslationServiceTest, TranslateFailure) {
  auto file_operation_proxy =
      SendConfig({{"en", "ja"}}, {
                                     {"0/dict.dat", "En to Ja - "},
                                 });
  mojo::Remote<mojom::Translator> translator_remote =
      CreateTranslator("en", "ja");
  ASSERT_TRUE(translator_remote);

  base::RunLoop run_loop;
  translator_remote->Translate(
      "SIMULATE_ERROR",
      base::BindLambdaForTesting([&](const std::optional<std::string>& output) {
        EXPECT_FALSE(output.has_value());
        run_loop.Quit();
      }));
  run_loop.Run();
}

}  // namespace
}  // namespace on_device_translation