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 | |
| 13 | #include "chrome/knowledge/handwriting/validate.pb.h" |
| 14 | #include "ml/handwriting.h" |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 15 | #include "ml/handwriting_path.h" |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 16 | |
| 17 | namespace ml { |
| 18 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 19 | using chromeos::machine_learning::mojom::HandwritingRecognizerSpec; |
| 20 | using chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr; |
| 21 | |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 22 | TEST(HandwritingLibraryTest, CanLoadLibrary) { |
| 23 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
| 24 | #ifdef ML_SUPPORT_HANDWRITING |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 25 | #if __has_feature(address_sanitizer) |
| 26 | EXPECT_EQ(instance->GetStatus(), |
| 27 | ml::HandwritingLibrary::Status::kNotSupported); |
| 28 | #else |
| 29 | EXPECT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk); |
| 30 | #endif |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 31 | #else |
| 32 | EXPECT_EQ(instance->GetStatus(), |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 33 | ml::HandwritingLibrary::Status::kNotSupported); |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 34 | #endif |
| 35 | } |
| 36 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 37 | // Tests each supported language against a file of labeled requests. |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 38 | TEST(HandwritingLibraryTest, ExampleRequest) { |
| 39 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 40 | // Nothing to test on an unsupported platform. |
| 41 | if (instance->GetStatus() == ml::HandwritingLibrary::Status::kNotSupported) { |
| 42 | return; |
| 43 | } |
| 44 | |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 45 | ASSERT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk); |
| 46 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 47 | const std::vector<std::string> languages = {"en", "gesture_in_context"}; |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 48 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 49 | for (const auto& language : languages) { |
| 50 | HandwritingRecognizerSpecPtr spec = |
| 51 | HandwritingRecognizerSpec::New(language); |
| 52 | |
| 53 | HandwritingRecognizer const recognizer = |
| 54 | instance->CreateHandwritingRecognizer(); |
| 55 | const chrome_knowledge::HandwritingRecognizerModelPaths paths = |
| 56 | GetModelPaths(spec.Clone()).value(); |
| 57 | chrome_knowledge::HandwritingRecognizerOptions options; |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 58 | ASSERT_TRUE( |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 59 | instance->LoadHandwritingRecognizer(recognizer, options, paths)); |
| 60 | |
| 61 | chrome_knowledge::HandwritingRecognizerLabeledRequests test_data; |
| 62 | std::string buf; |
| 63 | ASSERT_TRUE(base::ReadFileToString( |
| 64 | base::FilePath(GetLabeledRequestsPathForTesting(spec.Clone())), &buf)); |
| 65 | ASSERT_TRUE(test_data.ParseFromString(buf)); |
| 66 | ASSERT_GT(test_data.labeled_requests().size(), 0); |
| 67 | for (auto const& request : test_data.labeled_requests()) { |
| 68 | chrome_knowledge::HandwritingRecognizerResult result; |
| 69 | ASSERT_TRUE(instance->RecognizeHandwriting(recognizer, request.request(), |
| 70 | &result)); |
| 71 | ASSERT_GT(result.candidates().size(), 0); |
| 72 | EXPECT_EQ(result.candidates(0).text(), request.label()); |
| 73 | } |
| 74 | instance->DestroyHandwritingRecognizer(recognizer); |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 75 | } |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 76 | } |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 77 | |
| 78 | } // namespace ml |