OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
7 #include "content/browser/speech/speech_recognition_request.h" | 7 #include "content/browser/speech/speech_recognition_request.h" |
8 #include "content/public/common/speech_input_result.h" | 8 #include "content/public/common/speech_recognition_result.h" |
9 #include "content/test/test_url_fetcher_factory.h" | 9 #include "content/test/test_url_fetcher_factory.h" |
10 #include "net/url_request/url_request_context_getter.h" | 10 #include "net/url_request/url_request_context_getter.h" |
11 #include "net/url_request/url_request_status.h" | 11 #include "net/url_request/url_request_status.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace speech_input { | 14 namespace speech { |
15 | 15 |
16 class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate, | 16 class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate, |
17 public testing::Test { | 17 public testing::Test { |
18 public: | 18 public: |
19 SpeechRecognitionRequestTest() { } | 19 SpeechRecognitionRequestTest() { } |
20 | 20 |
21 // Creates a speech recognition request and invokes it's URL fetcher delegate | 21 // Creates a speech recognition request and invokes it's URL fetcher delegate |
22 // with the given test data. | 22 // with the given test data. |
23 void CreateAndTestRequest(bool success, const std::string& http_response); | 23 void CreateAndTestRequest(bool success, const std::string& http_response); |
24 | 24 |
25 // SpeechRecognitionRequestDelegate methods. | 25 // SpeechRecognitionRequestDelegate methods. |
26 virtual void SetRecognitionResult( | 26 virtual void SetRecognitionResult( |
27 const content::SpeechInputResult& result) OVERRIDE { | 27 const content::SpeechRecognitionResult& result) OVERRIDE { |
28 result_ = result; | 28 result_ = result; |
29 } | 29 } |
30 | 30 |
31 protected: | 31 protected: |
32 MessageLoop message_loop_; | 32 MessageLoop message_loop_; |
33 TestURLFetcherFactory url_fetcher_factory_; | 33 TestURLFetcherFactory url_fetcher_factory_; |
34 content::SpeechInputResult result_; | 34 content::SpeechRecognitionResult result_; |
35 }; | 35 }; |
36 | 36 |
37 void SpeechRecognitionRequestTest::CreateAndTestRequest( | 37 void SpeechRecognitionRequestTest::CreateAndTestRequest( |
38 bool success, const std::string& http_response) { | 38 bool success, const std::string& http_response) { |
39 SpeechRecognitionRequest request(NULL, this); | 39 SpeechRecognitionRequest request(NULL, this); |
40 request.Start(std::string(), std::string(), false, std::string(), | 40 request.Start(std::string(), std::string(), false, std::string(), |
41 std::string(), std::string()); | 41 std::string(), std::string()); |
42 request.UploadAudioChunk(std::string(" "), true); | 42 request.UploadAudioChunk(std::string(" "), true); |
43 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 43 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
44 ASSERT_TRUE(fetcher); | 44 ASSERT_TRUE(fetcher); |
45 | 45 |
46 fetcher->set_url(fetcher->GetOriginalURL()); | 46 fetcher->set_url(fetcher->GetOriginalURL()); |
47 net::URLRequestStatus status; | 47 net::URLRequestStatus status; |
48 status.set_status(success ? net::URLRequestStatus::SUCCESS : | 48 status.set_status(success ? net::URLRequestStatus::SUCCESS : |
49 net::URLRequestStatus::FAILED); | 49 net::URLRequestStatus::FAILED); |
50 fetcher->set_status(status); | 50 fetcher->set_status(status); |
51 fetcher->set_response_code(success ? 200 : 500); | 51 fetcher->set_response_code(success ? 200 : 500); |
52 fetcher->SetResponseString(http_response); | 52 fetcher->SetResponseString(http_response); |
53 | 53 |
54 fetcher->delegate()->OnURLFetchComplete(fetcher); | 54 fetcher->delegate()->OnURLFetchComplete(fetcher); |
55 // Parsed response will be available in result_. | 55 // Parsed response will be available in result_. |
56 } | 56 } |
57 | 57 |
58 TEST_F(SpeechRecognitionRequestTest, BasicTest) { | 58 TEST_F(SpeechRecognitionRequestTest, BasicTest) { |
59 // Normal success case with one result. | 59 // Normal success case with one result. |
60 CreateAndTestRequest(true, | 60 CreateAndTestRequest(true, |
61 "{\"status\":0,\"hypotheses\":" | 61 "{\"status\":0,\"hypotheses\":" |
62 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); | 62 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); |
63 EXPECT_EQ(result_.error, content::SPEECH_INPUT_ERROR_NONE); | 63 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE); |
64 EXPECT_EQ(1U, result_.hypotheses.size()); | 64 EXPECT_EQ(1U, result_.hypotheses.size()); |
65 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); | 65 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); |
66 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); | 66 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
67 | 67 |
68 // Normal success case with multiple results. | 68 // Normal success case with multiple results. |
69 CreateAndTestRequest(true, | 69 CreateAndTestRequest(true, |
70 "{\"status\":0,\"hypotheses\":[" | 70 "{\"status\":0,\"hypotheses\":[" |
71 "{\"utterance\":\"hello\",\"confidence\":0.9}," | 71 "{\"utterance\":\"hello\",\"confidence\":0.9}," |
72 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); | 72 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); |
73 EXPECT_EQ(result_.error, content::SPEECH_INPUT_ERROR_NONE); | 73 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE); |
74 EXPECT_EQ(2u, result_.hypotheses.size()); | 74 EXPECT_EQ(2u, result_.hypotheses.size()); |
75 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); | 75 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); |
76 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); | 76 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
77 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); | 77 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); |
78 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); | 78 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); |
79 | 79 |
80 // Zero results. | 80 // Zero results. |
81 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); | 81 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); |
82 EXPECT_EQ(result_.error, content::SPEECH_INPUT_ERROR_NONE); | 82 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE); |
83 EXPECT_EQ(0U, result_.hypotheses.size()); | 83 EXPECT_EQ(0U, result_.hypotheses.size()); |
84 | 84 |
85 // Http failure case. | 85 // Http failure case. |
86 CreateAndTestRequest(false, ""); | 86 CreateAndTestRequest(false, ""); |
87 EXPECT_EQ(result_.error, content::SPEECH_INPUT_ERROR_NETWORK); | 87 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); |
88 EXPECT_EQ(0U, result_.hypotheses.size()); | 88 EXPECT_EQ(0U, result_.hypotheses.size()); |
89 | 89 |
90 // Invalid status case. | 90 // Invalid status case. |
91 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); | 91 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); |
92 EXPECT_EQ(result_.error, content::SPEECH_INPUT_ERROR_NETWORK); | 92 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); |
93 EXPECT_EQ(0U, result_.hypotheses.size()); | 93 EXPECT_EQ(0U, result_.hypotheses.size()); |
94 | 94 |
95 // Server-side error case. | 95 // Server-side error case. |
96 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); | 96 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); |
97 EXPECT_EQ(result_.error, content::SPEECH_INPUT_ERROR_NETWORK); | 97 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); |
98 EXPECT_EQ(0U, result_.hypotheses.size()); | 98 EXPECT_EQ(0U, result_.hypotheses.size()); |
99 | 99 |
100 // Malformed JSON case. | 100 // Malformed JSON case. |
101 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" | 101 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" |
102 "[{\"unknownkey\":\"hello\"}]}"); | 102 "[{\"unknownkey\":\"hello\"}]}"); |
103 EXPECT_EQ(result_.error, content::SPEECH_INPUT_ERROR_NETWORK); | 103 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); |
104 EXPECT_EQ(0U, result_.hypotheses.size()); | 104 EXPECT_EQ(0U, result_.hypotheses.size()); |
105 } | 105 } |
106 | 106 |
107 } // namespace speech_input | 107 } // namespace speech |
OLD | NEW |