blob: 3afa4adecb17f227ffcc75cc7f77ca97fef7b949 [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"
15
16namespace ml {
Charles Zhaoc882eb02020-07-27 10:02:35 +100017namespace {
Honglin Yu0c4760a2020-04-18 20:53:31 +100018
charleszhao05c5a4a2020-06-09 16:49:54 +100019using chromeos::machine_learning::mojom::HandwritingRecognizerSpec;
Charles Zhaoc882eb02020-07-27 10:02:35 +100020
21constexpr char kLabeledRequestPathEn[] =
22 "/build/share/libhandwriting/handwriting_labeled_requests.pb";
23constexpr char kLabeledRequestPathGesture[] =
24 "/build/share/libhandwriting/gesture_labeled_requests.pb";
25
26} // namespace
charleszhao05c5a4a2020-06-09 16:49:54 +100027
Honglin Yu0c4760a2020-04-18 20:53:31 +100028TEST(HandwritingLibraryTest, CanLoadLibrary) {
29 auto* const instance = ml::HandwritingLibrary::GetInstance();
Charles Zhaoc882eb02020-07-27 10:02:35 +100030 if (ml::HandwritingLibrary::IsAsan()) {
31 EXPECT_FALSE(ml::HandwritingLibrary::IsHandwritingLibrarySupported());
32 EXPECT_FALSE(
33 ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported());
34 EXPECT_EQ(instance->GetStatus(),
35 ml::HandwritingLibrary::Status::kNotSupported);
36 return;
37 }
38
39 if (ml::HandwritingLibrary::IsUseLibHandwritingEnabled()) {
40 EXPECT_TRUE(ml::HandwritingLibrary::IsHandwritingLibrarySupported());
41 EXPECT_TRUE(
42 ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported());
43 EXPECT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk);
44 return;
45 }
46
47 if (ml::HandwritingLibrary::IsUseLibHandwritingDlcEnabled()) {
48 EXPECT_TRUE(ml::HandwritingLibrary::IsHandwritingLibrarySupported());
49 EXPECT_FALSE(
50 ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported());
51 EXPECT_EQ(instance->GetStatus(),
52 ml::HandwritingLibrary::Status::kLoadLibraryFailed);
53 return;
54 }
Honglin Yu0c4760a2020-04-18 20:53:31 +100055}
56
charleszhao05c5a4a2020-06-09 16:49:54 +100057// Tests each supported language against a file of labeled requests.
Honglin Yu0c4760a2020-04-18 20:53:31 +100058TEST(HandwritingLibraryTest, ExampleRequest) {
charleszhao17777f92020-04-23 12:53:11 +100059 // Nothing to test on an unsupported platform.
Charles Zhaoc882eb02020-07-27 10:02:35 +100060 if (!ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported()) {
charleszhao17777f92020-04-23 12:53:11 +100061 return;
62 }
63
Charles Zhaoc882eb02020-07-27 10:02:35 +100064 auto* const instance = ml::HandwritingLibrary::GetInstance();
Honglin Yu0c4760a2020-04-18 20:53:31 +100065 ASSERT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk);
66
charleszhao05c5a4a2020-06-09 16:49:54 +100067 const std::vector<std::string> languages = {"en", "gesture_in_context"};
Charles Zhaoc882eb02020-07-27 10:02:35 +100068 const std::vector<std::string> labeled_request_paths = {
69 kLabeledRequestPathEn, kLabeledRequestPathGesture};
Honglin Yu0c4760a2020-04-18 20:53:31 +100070
Charles Zhaoc882eb02020-07-27 10:02:35 +100071 for (int i = 0; i < languages.size(); ++i) {
charleszhao05c5a4a2020-06-09 16:49:54 +100072 HandwritingRecognizer const recognizer =
73 instance->CreateHandwritingRecognizer();
Charles Zhaoc882eb02020-07-27 10:02:35 +100074 ASSERT_TRUE(instance->LoadHandwritingRecognizer(
75 recognizer, HandwritingRecognizerSpec::New(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