Curtis McMullan | 70ece0a | 2021-01-11 15:21:06 +1100 | [diff] [blame] | 1 | // Copyright 2021 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 <gtest/gtest.h> |
| 6 | |
| 7 | #include "ml/text_suggestions.h" |
| 8 | #include "ml/util.h" |
| 9 | |
| 10 | namespace ml { |
| 11 | namespace { |
| 12 | |
| 13 | TEST(TextSuggestionsTest, CanLoadLibrary) { |
| 14 | auto* const instance = ml::TextSuggestions::GetInstance(); |
| 15 | if (IsAsan()) { |
| 16 | EXPECT_FALSE(ml::TextSuggestions::IsTextSuggestionsSupported()); |
| 17 | EXPECT_EQ(instance->GetStatus(), |
| 18 | ml::TextSuggestions::Status::kNotSupported); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | if (ml::TextSuggestions::IsTextSuggestionsSupported()) { |
| 23 | EXPECT_EQ(instance->GetStatus(), ml::TextSuggestions::Status::kOk); |
| 24 | } else { |
| 25 | EXPECT_EQ(instance->GetStatus(), |
| 26 | ml::TextSuggestions::Status::kNotSupported); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | TEST(TextSuggestionsText, ExampleRequest) { |
| 31 | auto* const instance = ml::TextSuggestions::GetInstance(); |
| 32 | if (instance->GetStatus() == ml::TextSuggestions::Status::kNotSupported) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | ASSERT_EQ(instance->GetStatus(), TextSuggestions::Status::kOk); |
| 37 | |
| 38 | TextSuggester const suggester = instance->CreateTextSuggester(); |
| 39 | instance->LoadTextSuggester(suggester); |
| 40 | |
Curtis McMullan | 39ffedf | 2021-05-07 10:05:58 +1000 | [diff] [blame^] | 41 | // TODO(crbug/1146266): Add prediction test case after next sharedlib uprev |
Curtis McMullan | 70ece0a | 2021-01-11 15:21:06 +1100 | [diff] [blame] | 42 | chrome_knowledge::TextSuggesterRequest request; |
| 43 | request.set_text("How are y"); |
Curtis McMullan | 39ffedf | 2021-05-07 10:05:58 +1000 | [diff] [blame^] | 44 | request.set_suggestion_mode( |
| 45 | chrome_knowledge::RequestSuggestionMode::SUGGESTION_MODE_COMPLETION); |
Curtis McMullan | 70ece0a | 2021-01-11 15:21:06 +1100 | [diff] [blame] | 46 | |
| 47 | chrome_knowledge::NextWordCompletionCandidate* candidate = |
| 48 | request.add_next_word_candidates(); |
| 49 | candidate->set_text("you"); |
| 50 | candidate->set_normalized_score(-1.0f); |
| 51 | |
| 52 | chrome_knowledge::TextSuggesterResult result; |
| 53 | instance->GenerateSuggestions(suggester, request, &result); |
| 54 | |
| 55 | ASSERT_GT(result.candidates_size(), 0); |
| 56 | EXPECT_EQ(result.candidates(0).has_multi_word(), true); |
| 57 | EXPECT_EQ(result.candidates(0).multi_word().text(), "you doing"); |
| 58 | EXPECT_FLOAT_EQ(result.candidates(0).multi_word().normalized_score(), |
| 59 | -0.680989f); |
| 60 | |
| 61 | instance->DestroyTextSuggester(suggester); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | } // namespace ml |