File: translate_kit_client_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (273 lines) | stat: -rw-r--r-- 11,726 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// 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/translate_kit_client.h"

#include <tuple>

#include "base/files/scoped_temp_file.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/gtest_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "components/services/on_device_translation/public/mojom/on_device_translation_service.mojom.h"
#include "components/services/on_device_translation/test/test_util.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace on_device_translation {
namespace {

using base::test::ErrorIs;
using mojom::CreateTranslatorResult;

class TranslateKitClientTest : public testing::Test {
 public:
  TranslateKitClientTest()
      : file_operation_proxy_(nullptr, base::OnTaskRunnerDeleter(nullptr)) {}
  ~TranslateKitClientTest() override = default;

 protected:
  // Creates a TranslateKitClient with the given library path, packages, and
  // files.
  std::unique_ptr<TranslateKitClient> CreateClient(
      const base::FilePath& library_path,
      const std::vector<std::pair<std::string, std::string>>& packages,
      const std::vector<TestFile>& files) {
    auto client = TranslateKitClient::CreateForTest(GetMockLibraryPath());
    SetConfig(*client, packages, files);
    return client;
  }

  void SetConfig(
      TranslateKitClient& client,
      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));
    }
    file_operation_proxy_ = FakeFileOperationProxy::Create(
        config->file_operation_proxy.InitWithNewPipeAndPassReceiver(), files);
    client.SetConfig(std::move(config));
  }

 private:
  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<FakeFileOperationProxy, base::OnTaskRunnerDeleter>
      file_operation_proxy_;
};

// Tests that the CanTranslate method returns true for a language pair that is
// supported.
TEST_F(TranslateKitClientTest, CanTranslateMatchingLanguagePack) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                             });
  EXPECT_TRUE(client->CanTranslate("en", "ja"));
}

// Tests that the CanTranslate method returns false for a language pair that is
// not supported.
TEST_F(TranslateKitClientTest, CanTranslateNoMatchingLanguagePack) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                             });
  EXPECT_FALSE(client->CanTranslate("en", "es"));
}

// Tests that the CanTranslate method returns false for a language pair that
// language data is not available.
TEST_F(TranslateKitClientTest, CanTranslateBrokenLanguagePack) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}}, {});
  EXPECT_FALSE(client->CanTranslate("en", "ja"));
}

// Tests that the CanTranslate method crashes if no config is set.
TEST_F(TranslateKitClientTest, CanTranslateNoConfig) {
  auto client = TranslateKitClient::CreateForTest(GetMockLibraryPath());
  EXPECT_CHECK_DEATH_WITH(client->CanTranslate("en", "ja"),
                          "SetConfig must have been called");
}

// Tests that the GetTranslator method returns a valid translator for a language
// pair that is supported, and the translator can be used to translate text.
TEST_F(TranslateKitClientTest, GetTranslatorMatchingLanguagePack) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                             });
  ASSERT_OK_AND_ASSIGN(auto* translator, client->GetTranslator("en", "ja"));
  ASSERT_TRUE(translator);
  // Check that the translator can be used to translate text.
  // Note: the mock library returns the concatenation of the content of
  // "dict.dat" and the input text.
  EXPECT_EQ(translator->Translate("test"), "En to Ja - test");
}

// Tests that the GetTranslator method returns `kErrorFailedToCreateTranslator`
// for a language pair that is not supported.
TEST_F(TranslateKitClientTest, GetTranslatorNoMatchingLanguagePack) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                             });
  EXPECT_THAT(client->GetTranslator("en", "es"),
              ErrorIs(CreateTranslatorResult::kErrorFailedToCreateTranslator));
}

// Tests that the GetTranslator method returns `kErrorFailedToCreateTranslator`
// for a language pair that language data is not available.
TEST_F(TranslateKitClientTest, GetTranslatorBrokenLanguagePack) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}}, {});
  EXPECT_THAT(client->GetTranslator("en", "ja"),
              ErrorIs(CreateTranslatorResult::kErrorFailedToCreateTranslator));
}

// Tests that the GetTranslator method crashes if no config is set.
TEST_F(TranslateKitClientTest, GetTranslatorNoConfig) {
  auto client = TranslateKitClient::CreateForTest(GetMockLibraryPath());
  EXPECT_CHECK_DEATH_WITH(
      { std::ignore = client->GetTranslator("en", "ja"); },
      "SetConfig must have been called");
}

// Tests that SetConfig updates the config of the client, and the new config is
// used to create translators.
TEST_F(TranslateKitClientTest, UpdatingConfig) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                             });
  ASSERT_OK_AND_ASSIGN(auto* translator1, client->GetTranslator("en", "ja"));
  ASSERT_TRUE(translator1);
  EXPECT_EQ(translator1->Translate("test"), "En to Ja - test");
  EXPECT_THAT(client->GetTranslator("en", "es"),
              ErrorIs(CreateTranslatorResult::kErrorFailedToCreateTranslator));

  SetConfig(*client, {{"en", "ja"}, {"en", "es"}},
            {
                {"0/dict.dat", "En to Ja - "},
                {"1/dict.dat", "En to Es - "},
            });

  ASSERT_OK_AND_ASSIGN(auto* translator2, client->GetTranslator("en", "ja"));
  ASSERT_TRUE(translator2);
  EXPECT_EQ(translator2->Translate("test"), "En to Ja - test");

  ASSERT_OK_AND_ASSIGN(auto* translator3, client->GetTranslator("en", "es"));
  ASSERT_TRUE(translator3);
  EXPECT_EQ(translator3->Translate("test"), "En to Es - test");
}

// Tests that two translators can be created for the different language pairs,
// and they can be used to translate text.
TEST_F(TranslateKitClientTest, TwoTranslators) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}, {"en", "es"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                                 {"1/dict.dat", "En to Es - "},
                             });
  ASSERT_OK_AND_ASSIGN(auto* translator1, client->GetTranslator("en", "ja"));
  ASSERT_TRUE(translator1);
  ASSERT_OK_AND_ASSIGN(auto* translator2, client->GetTranslator("en", "es"));
  ASSERT_TRUE(translator2);
  EXPECT_NE(translator1, translator2);
  EXPECT_EQ(translator1->Translate("test"), "En to Ja - test");
  EXPECT_EQ(translator2->Translate("test"), "En to Es - test");
}

// Tests that the same translator is returned for the same language pair, and
// it can be used to translate text.
TEST_F(TranslateKitClientTest, SameTranslator) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}, {"en", "es"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                                 {"1/dict.dat", "En to Es - "},
                             });
  ASSERT_OK_AND_ASSIGN(auto* translator1, client->GetTranslator("en", "ja"));
  ASSERT_TRUE(translator1);
  ASSERT_OK_AND_ASSIGN(auto* translator2, client->GetTranslator("en", "ja"));
  ASSERT_TRUE(translator2);
  EXPECT_EQ(translator1, translator2);
  EXPECT_EQ(translator1->Translate("test"), "En to Ja - test");
  EXPECT_EQ(translator2->Translate("test"), "En to Ja - test");
}

// Tests that the translate method returns nullopt if the library returns an
// error.
TEST_F(TranslateKitClientTest, TranslateFailure) {
  auto client = CreateClient(GetMockLibraryPath(), {{"en", "ja"}},
                             {
                                 {"0/dict.dat", "En to Ja - "},
                             });
  ASSERT_OK_AND_ASSIGN(auto* translator, client->GetTranslator("en", "ja"));
  ASSERT_TRUE(translator);
  EXPECT_EQ(translator->Translate("SIMULATE_ERROR"), std::nullopt);
}

// Tests that the library is loaded successfully.
TEST_F(TranslateKitClientTest, LoadBinary) {
  base::HistogramTester histogram_tester;
  auto client = TranslateKitClient::CreateForTest(GetMockLibraryPath());
  histogram_tester.ExpectBucketCount("AI.Translation.LoadTranslateKitResult",
                                     LoadTranslateKitResult::kSuccess, 1);
}

// Tests the behavior of TranslateKitClient when the library is invalid.
TEST_F(TranslateKitClientTest, InvalidBinary) {
  base::ScopedTempFile invalid_lib;
  ASSERT_TRUE(invalid_lib.Create());

  base::HistogramTester histogram_tester;
  auto client = TranslateKitClient::CreateForTest(invalid_lib.path());
  histogram_tester.ExpectBucketCount("AI.Translation.LoadTranslateKitResult",
                                     LoadTranslateKitResult::kInvalidBinary, 1);

  // Calling following methods should not cause a crash.
  client->SetConfig(mojom::OnDeviceTranslationServiceConfigPtr());
  EXPECT_FALSE(client->CanTranslate("en", "ja"));
  EXPECT_THAT(client->GetTranslator("en", "ja"),
              ErrorIs(CreateTranslatorResult::kErrorInvalidBinary));
}

// Tests the behavior of TranslateKitClient when the library doesn't contain
// any of the methods in the TranslateKit API.
TEST_F(TranslateKitClientTest, InvalidFunctionPointerBinary) {
  base::HistogramTester histogram_tester;
  auto client = TranslateKitClient::CreateForTest(
      GetMockInvalidFunctionPointerLibraryPath());
  histogram_tester.ExpectBucketCount(
      "AI.Translation.LoadTranslateKitResult",
      LoadTranslateKitResult::kInvalidFunctionPointer, 1);

  // Calling following methods should not cause a crash.
  client->SetConfig(mojom::OnDeviceTranslationServiceConfigPtr());
  EXPECT_FALSE(client->CanTranslate("en", "ja"));
  EXPECT_THAT(client->GetTranslator("en", "ja"),
              ErrorIs(CreateTranslatorResult::kErrorInvalidFunctionPointer));
}

// Tests the behavior of TranslateKitClient when the library's
// CreateTranslateKit() method fails.
TEST_F(TranslateKitClientTest, FailingBinary) {
  base::HistogramTester histogram_tester;
  auto client = TranslateKitClient::CreateForTest(GetMockFailingLibraryPath());
  histogram_tester.ExpectBucketCount("AI.Translation.LoadTranslateKitResult",
                                     LoadTranslateKitResult::kSuccess, 1);

  // Calling following methods should not cause a crash.
  client->SetConfig(mojom::OnDeviceTranslationServiceConfigPtr());
  EXPECT_FALSE(client->CanTranslate("en", "ja"));
  EXPECT_THAT(client->GetTranslator("en", "ja"),
              ErrorIs(CreateTranslatorResult::kErrorFailedToInitialize));
}

}  // namespace

}  // namespace on_device_translation