Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 1 | // 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> |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 7 | #include <vector> |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 8 | |
| 9 | #include <base/files/file_path.h> |
| 10 | #include <base/files/file_util.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Honglin Yu | cfb1930 | 2020-08-17 17:13:14 +1000 | [diff] [blame^] | 13 | #include "chrome/knowledge/handwriting/handwriting_validate.pb.h" |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 14 | #include "ml/handwriting.h" |
| 15 | |
| 16 | namespace ml { |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 17 | namespace { |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 18 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 19 | using chromeos::machine_learning::mojom::HandwritingRecognizerSpec; |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 20 | |
| 21 | constexpr char kLabeledRequestPathEn[] = |
| 22 | "/build/share/libhandwriting/handwriting_labeled_requests.pb"; |
| 23 | constexpr char kLabeledRequestPathGesture[] = |
| 24 | "/build/share/libhandwriting/gesture_labeled_requests.pb"; |
| 25 | |
| 26 | } // namespace |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 27 | |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 28 | TEST(HandwritingLibraryTest, CanLoadLibrary) { |
| 29 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 30 | 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 Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 55 | } |
| 56 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 57 | // Tests each supported language against a file of labeled requests. |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 58 | TEST(HandwritingLibraryTest, ExampleRequest) { |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 59 | // Nothing to test on an unsupported platform. |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 60 | if (!ml::HandwritingLibrary::IsHandwritingLibraryUnitTestSupported()) { |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 64 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 65 | ASSERT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk); |
| 66 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 67 | const std::vector<std::string> languages = {"en", "gesture_in_context"}; |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 68 | const std::vector<std::string> labeled_request_paths = { |
| 69 | kLabeledRequestPathEn, kLabeledRequestPathGesture}; |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 70 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 71 | for (int i = 0; i < languages.size(); ++i) { |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 72 | HandwritingRecognizer const recognizer = |
| 73 | instance->CreateHandwritingRecognizer(); |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 74 | ASSERT_TRUE(instance->LoadHandwritingRecognizer( |
| 75 | recognizer, HandwritingRecognizerSpec::New(languages[i]))); |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 76 | |
| 77 | chrome_knowledge::HandwritingRecognizerLabeledRequests test_data; |
| 78 | std::string buf; |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 79 | ASSERT_TRUE( |
| 80 | base::ReadFileToString(base::FilePath(labeled_request_paths[i]), &buf)); |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 81 | 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 Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 91 | } |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 92 | } |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 93 | |
| 94 | } // namespace ml |