-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoda_proto_mojom_conversion_test.cc
144 lines (118 loc) · 5.58 KB
/
soda_proto_mojom_conversion_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <gtest/gtest.h>
#include <string>
#include <utility>
#include "ml/soda_proto_mojom_conversion.h"
namespace ml {
using speech::soda::chrome::SodaResponse;
TEST(SodaProtoMojomConversionTest, AudioLevelsTest) {
SodaResponse response;
response.set_soda_type(SodaResponse::AUDIO_LEVEL);
response.mutable_audio_level_info()->set_audio_level(0.1);
response.mutable_audio_level_info()->set_rms(0.3);
auto actual_audio_mojom = internal::AudioLevelEventFromProto(response);
auto expected_audio_mojom =
chromeos::machine_learning::mojom::AudioLevelEvent::New();
expected_audio_mojom->rms = 0.3;
expected_audio_mojom->audio_level = 0.1;
EXPECT_TRUE(actual_audio_mojom.Equals(expected_audio_mojom));
// now for the full mojom
auto actual_mojom = SpeechRecognizerEventFromProto(response);
chromeos::machine_learning::mojom::SpeechRecognizerEventPtr expected_mojom =
chromeos::machine_learning::mojom::SpeechRecognizerEvent::New();
expected_mojom->set_audio_event(std::move(expected_audio_mojom));
EXPECT_TRUE(actual_mojom.Equals(expected_mojom));
// Let's check the other tests.
EXPECT_FALSE(IsStopSodaResponse(response));
EXPECT_FALSE(IsStartSodaResponse(response));
EXPECT_FALSE(IsShutdownSodaResponse(response));
}
TEST(SodaProtoMojomConversionTest, PartialResultsTest) {
SodaResponse response;
response.set_soda_type(SodaResponse::RECOGNITION);
auto* rec = response.mutable_recognition_result();
rec->add_hypothesis("first hyp");
rec->add_hypothesis("second hyp");
rec->set_result_type(speech::soda::chrome::SodaRecognitionResult::PARTIAL);
auto expected_rec_mojom =
chromeos::machine_learning::mojom::PartialResult::New();
expected_rec_mojom->partial_text.push_back("first hyp");
expected_rec_mojom->partial_text.push_back("second hyp");
auto actual_rec_mojom = internal::PartialResultFromProto(response);
EXPECT_TRUE(actual_rec_mojom.Equals(expected_rec_mojom));
// now for the full mojom
auto actual_mojom = SpeechRecognizerEventFromProto(response);
chromeos::machine_learning::mojom::SpeechRecognizerEventPtr expected_mojom =
chromeos::machine_learning::mojom::SpeechRecognizerEvent::New();
expected_mojom->set_partial_result(std::move(actual_rec_mojom));
EXPECT_TRUE(actual_mojom.Equals(expected_mojom));
// Let's check the other tests.
EXPECT_FALSE(IsStopSodaResponse(response));
EXPECT_FALSE(IsStartSodaResponse(response));
EXPECT_FALSE(IsShutdownSodaResponse(response));
}
TEST(SodaProtoMojomConversionTest, FinalResultsTest) {
SodaResponse response;
response.set_soda_type(SodaResponse::RECOGNITION);
auto* rec = response.mutable_recognition_result();
rec->add_hypothesis("first hypo");
rec->add_hypothesis("second hypo");
rec->set_result_type(speech::soda::chrome::SodaRecognitionResult::FINAL);
auto expected_rec_mojom =
chromeos::machine_learning::mojom::FinalResult::New();
expected_rec_mojom->final_hypotheses.push_back("first hypo");
expected_rec_mojom->final_hypotheses.push_back("second hypo");
auto actual_rec_mojom = internal::FinalResultFromProto(response);
EXPECT_TRUE(actual_rec_mojom.Equals(expected_rec_mojom));
// now for the full mojom
auto actual_mojom = SpeechRecognizerEventFromProto(response);
chromeos::machine_learning::mojom::SpeechRecognizerEventPtr expected_mojom =
chromeos::machine_learning::mojom::SpeechRecognizerEvent::New();
expected_mojom->set_final_result(std::move(actual_rec_mojom));
EXPECT_TRUE(actual_mojom.Equals(expected_mojom));
// Let's check the other tests.
EXPECT_FALSE(IsStopSodaResponse(response));
EXPECT_FALSE(IsStartSodaResponse(response));
EXPECT_FALSE(IsShutdownSodaResponse(response));
}
TEST(SodaProtoMojomConversionTest, EndpointTest) {
SodaResponse response;
response.set_soda_type(SodaResponse::ENDPOINT);
auto* end = response.mutable_endpoint_event();
end->set_endpoint_type(
speech::soda::chrome::SodaEndpointEvent::END_OF_SPEECH);
auto expected_end_mojom =
chromeos::machine_learning::mojom::EndpointerEvent::New();
expected_end_mojom->endpointer_type =
chromeos::machine_learning::mojom::EndpointerType::END_OF_SPEECH;
auto actual_end_mojom = internal::EndpointerEventFromProto(response);
EXPECT_TRUE(actual_end_mojom.Equals(expected_end_mojom));
// now for the full mojom
auto actual_mojom = SpeechRecognizerEventFromProto(response);
chromeos::machine_learning::mojom::SpeechRecognizerEventPtr expected_mojom =
chromeos::machine_learning::mojom::SpeechRecognizerEvent::New();
expected_mojom->set_endpointer_event(std::move(actual_end_mojom));
EXPECT_TRUE(actual_mojom.Equals(expected_mojom));
// Let's check the other tests.
EXPECT_FALSE(IsStopSodaResponse(response));
EXPECT_FALSE(IsStartSodaResponse(response));
EXPECT_FALSE(IsShutdownSodaResponse(response));
}
TEST(SodaProtoMojomConversionTest, BooleanFunctionTest) {
SodaResponse response;
response.set_soda_type(SodaResponse::STOP);
EXPECT_TRUE(IsStopSodaResponse(response));
EXPECT_FALSE(IsStartSodaResponse(response));
EXPECT_FALSE(IsShutdownSodaResponse(response));
response.set_soda_type(SodaResponse::START);
EXPECT_FALSE(IsStopSodaResponse(response));
EXPECT_TRUE(IsStartSodaResponse(response));
EXPECT_FALSE(IsShutdownSodaResponse(response));
response.set_soda_type(SodaResponse::SHUTDOWN);
EXPECT_FALSE(IsStopSodaResponse(response));
EXPECT_FALSE(IsStartSodaResponse(response));
EXPECT_TRUE(IsShutdownSodaResponse(response));
}
} // namespace ml