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> |
| 7 | |
| 8 | #include <base/files/file_path.h> |
| 9 | #include <base/files/file_util.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | #include "chrome/knowledge/handwriting/validate.pb.h" |
| 13 | #include "ml/handwriting.h" |
| 14 | |
| 15 | namespace ml { |
| 16 | |
| 17 | TEST(HandwritingLibraryTest, CanLoadLibrary) { |
| 18 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
| 19 | #ifdef ML_SUPPORT_HANDWRITING |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame^] | 20 | #if __has_feature(address_sanitizer) |
| 21 | EXPECT_EQ(instance->GetStatus(), |
| 22 | ml::HandwritingLibrary::Status::kNotSupported); |
| 23 | #else |
| 24 | EXPECT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk); |
| 25 | #endif |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 26 | #else |
| 27 | EXPECT_EQ(instance->GetStatus(), |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame^] | 28 | ml::HandwritingLibrary::Status::kNotSupported); |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 29 | #endif |
| 30 | } |
| 31 | |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 32 | TEST(HandwritingLibraryTest, ExampleRequest) { |
| 33 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame^] | 34 | // Nothing to test on an unsupported platform. |
| 35 | if (instance->GetStatus() == ml::HandwritingLibrary::Status::kNotSupported) { |
| 36 | return; |
| 37 | } |
| 38 | |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 39 | ASSERT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk); |
| 40 | |
| 41 | HandwritingRecognizer const recognizer = |
| 42 | instance->CreateHandwritingRecognizer(); |
| 43 | chrome_knowledge::HandwritingRecognizerModelPaths paths; |
| 44 | paths.set_reco_model_path( |
| 45 | "/opt/google/chrome/ml_models/handwriting/latin_indy.tflite"); |
| 46 | paths.set_seg_model_path( |
| 47 | "/opt/google/chrome/ml_models/handwriting/latin_indy_seg.tflite"); |
| 48 | paths.set_conf_model_path( |
| 49 | "/opt/google/chrome/ml_models/handwriting/latin_indy_conf.tflite"); |
| 50 | paths.set_fst_lm_path( |
| 51 | "/opt/google/chrome/ml_models/handwriting/latin_indy.compact.fst"); |
| 52 | paths.set_recospec_path( |
| 53 | "/opt/google/chrome/ml_models/handwriting/latin_indy.pb"); |
| 54 | chrome_knowledge::HandwritingRecognizerOptions options; |
| 55 | ASSERT_TRUE(instance->LoadHandwritingRecognizer(recognizer, options, paths)); |
| 56 | |
| 57 | chrome_knowledge::HandwritingRecognizerLabeledRequests test_data; |
| 58 | std::string buf; |
| 59 | ASSERT_TRUE(base::ReadFileToString( |
| 60 | base::FilePath("/build/share/libhandwriting/correct_labeled_requests.pb"), |
| 61 | &buf)); |
| 62 | ASSERT_TRUE(test_data.ParseFromString(buf)); |
| 63 | ASSERT_GT(test_data.labeled_requests().size(), 0); |
| 64 | for (auto const& request : test_data.labeled_requests()) { |
| 65 | chrome_knowledge::HandwritingRecognizerResult result; |
| 66 | ASSERT_TRUE( |
| 67 | instance->RecognizeHandwriting(recognizer, request.request(), &result)); |
| 68 | ASSERT_GT(result.candidates().size(), 0); |
| 69 | EXPECT_EQ(result.candidates(0).text(), request.label()); |
| 70 | } |
| 71 | instance->DestroyHandwritingRecognizer(recognizer); |
| 72 | } |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 73 | |
| 74 | } // namespace ml |