blob: 0b4ee353bf533aca2d0efcefd18659d87799137b [file] [log] [blame]
Honglin Yu0c4760a2020-04-18 20:53:31 +10001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <fstream>
6#include <string>
charleszhao05c5a4a2020-06-09 16:49:54 +10007#include <vector>
Honglin Yu0c4760a2020-04-18 20:53:31 +10008
9#include <base/files/file_path.h>
10#include <base/files/file_util.h>
11#include <gtest/gtest.h>
12
Honglin Yucfb19302020-08-17 17:13:14 +100013#include "chrome/knowledge/handwriting/handwriting_validate.pb.h"
Honglin Yu0c4760a2020-04-18 20:53:31 +100014#include "ml/handwriting.h"
Honglin Yu3e360a02020-08-27 10:30:01 +100015#include "ml/util.h"
Honglin Yu0c4760a2020-04-18 20:53:31 +100016
17namespace ml {
Charles Zhaoc882eb02020-07-27 10:02:35 +100018namespace {
Honglin Yu0c4760a2020-04-18 20:53:31 +100019
charleszhao05c5a4a2020-06-09 16:49:54 +100020using chromeos::machine_learning::mojom::HandwritingRecognizerSpec;
Charles Zhaoc882eb02020-07-27 10:02:35 +100021
22constexpr char kLabeledRequestPathEn[] =
23 "/build/share/libhandwriting/handwriting_labeled_requests.pb";
24constexpr char kLabeledRequestPathGesture[] =
25 "/build/share/libhandwriting/gesture_labeled_requests.pb";
26
27} // namespace
charleszhao05c5a4a2020-06-09 16:49:54 +100028
Honglin Yu0c4760a2020-04-18 20:53:31 +100029TEST(HandwritingLibraryTest, CanLoadLibrary) {
30 auto* const instance = ml::HandwritingLibrary::GetInstance();
Honglin Yu3e360a02020-08-27 10:30:01 +100031 if (IsAsan()) {
Charles Zhaoc882eb02020-07-27 10:02:35 +100032 EXPECT_FALSE(ml::HandwritingLibrary::IsHandwritingLibrarySupported());
33 EXPECT_FALSE(
34 ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported());
35 EXPECT_EQ(instance->GetStatus(),
36 ml::HandwritingLibrary::Status::kNotSupported);
37 return;
38 }
39
40 if (ml::HandwritingLibrary::IsUseLibHandwritingEnabled()) {
41 EXPECT_TRUE(ml::HandwritingLibrary::IsHandwritingLibrarySupported());
42 EXPECT_TRUE(
43 ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported());
44 EXPECT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk);
45 return;
46 }
47
48 if (ml::HandwritingLibrary::IsUseLibHandwritingDlcEnabled()) {
49 EXPECT_TRUE(ml::HandwritingLibrary::IsHandwritingLibrarySupported());
50 EXPECT_FALSE(
51 ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported());
52 EXPECT_EQ(instance->GetStatus(),
53 ml::HandwritingLibrary::Status::kLoadLibraryFailed);
54 return;
55 }
Honglin Yu0c4760a2020-04-18 20:53:31 +100056}
57
charleszhao05c5a4a2020-06-09 16:49:54 +100058// Tests each supported language against a file of labeled requests.
Honglin Yu0c4760a2020-04-18 20:53:31 +100059TEST(HandwritingLibraryTest, ExampleRequest) {
charleszhao17777f92020-04-23 12:53:11 +100060 // Nothing to test on an unsupported platform.
Charles Zhaoc882eb02020-07-27 10:02:35 +100061 if (!ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported()) {
charleszhao17777f92020-04-23 12:53:11 +100062 return;
63 }
64
Charles Zhaoc882eb02020-07-27 10:02:35 +100065 auto* const instance = ml::HandwritingLibrary::GetInstance();
Honglin Yu0c4760a2020-04-18 20:53:31 +100066 ASSERT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk);
67
charleszhao05c5a4a2020-06-09 16:49:54 +100068 const std::vector<std::string> languages = {"en", "gesture_in_context"};
Charles Zhaoc882eb02020-07-27 10:02:35 +100069 const std::vector<std::string> labeled_request_paths = {
70 kLabeledRequestPathEn, kLabeledRequestPathGesture};
Honglin Yu0c4760a2020-04-18 20:53:31 +100071
Charles Zhaoc882eb02020-07-27 10:02:35 +100072 for (int i = 0; i < languages.size(); ++i) {
charleszhao05c5a4a2020-06-09 16:49:54 +100073 HandwritingRecognizer const recognizer =
74 instance->CreateHandwritingRecognizer();
Honglin Yu0f5b21d2021-04-06 23:39:04 +100075 ASSERT_TRUE(instance->LoadHandwritingRecognizer(recognizer, languages[i]));
charleszhao05c5a4a2020-06-09 16:49:54 +100076
77 chrome_knowledge::HandwritingRecognizerLabeledRequests test_data;
78 std::string buf;
Charles Zhaoc882eb02020-07-27 10:02:35 +100079 ASSERT_TRUE(
80 base::ReadFileToString(base::FilePath(labeled_request_paths[i]), &buf));
charleszhao05c5a4a2020-06-09 16:49:54 +100081 ASSERT_TRUE(test_data.ParseFromString(buf));
82 ASSERT_GT(test_data.labeled_requests().size(), 0);
83 for (auto const& request : test_data.labeled_requests()) {
84 chrome_knowledge::HandwritingRecognizerResult result;
85 ASSERT_TRUE(instance->RecognizeHandwriting(recognizer, request.request(),
86 &result));
87 ASSERT_GT(result.candidates().size(), 0);
88 EXPECT_EQ(result.candidates(0).text(), request.label());
89 }
90 instance->DestroyHandwritingRecognizer(recognizer);
Honglin Yu0c4760a2020-04-18 20:53:31 +100091 }
Honglin Yu0c4760a2020-04-18 20:53:31 +100092}
Honglin Yu0c4760a2020-04-18 20:53:31 +100093
94} // namespace ml