summaryrefslogtreecommitdiffstats
path: root/chrome/browser/speech/speech_recognition_request_unittest.cc
blob: dcf0e9b52664fd26373c9506ce2933eac1fa52d2 (plain)
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
// Copyright (c) 2010 The Chromium 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 "base/utf_string_conversions.h"
#include "chrome/browser/speech/speech_recognition_request.h"
#include "chrome/common/net/url_request_context_getter.h"
#include "chrome/common/net/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace speech_input {

class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate,
                                     public testing::Test {
 public:
  SpeechRecognitionRequestTest() : error_(false) { }

  // Creates a speech recognition request and invokes it's URL fetcher delegate
  // with the given test data.
  void CreateAndTestRequest(bool success, const std::string& http_response);

  // SpeechRecognitionRequestDelegate methods.
  virtual void SetRecognitionResult(bool error, const string16& result) {
    error_ = error;
    result_ = result;
  }

  // testing::Test methods.
  virtual void SetUp() {
    URLFetcher::set_factory(&url_fetcher_factory_);
  }

  virtual void TearDown() {
    URLFetcher::set_factory(NULL);
  }

 protected:
  TestURLFetcherFactory url_fetcher_factory_;
  bool error_;
  string16 result_;
};

void SpeechRecognitionRequestTest::CreateAndTestRequest(
    bool success, const std::string& http_response) {
  SpeechRecognitionRequest request(NULL, GURL(""), this);
  request.Send(std::string(), std::string());
  TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
  ASSERT_TRUE(fetcher);
  URLRequestStatus status;
  status.set_status(success ? URLRequestStatus::SUCCESS :
                              URLRequestStatus::FAILED);
  fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(),
                                          status, success ? 200 : 500,
                                          ResponseCookies(),
                                          http_response);
  // Parsed response will be available in result_.
}

TEST_F(SpeechRecognitionRequestTest, BasicTest) {
  // Normal success case with one result.
  CreateAndTestRequest(true,
      "{\"hypotheses\":[{\"utterance\":\"123456\",\"confidence\":0.9}]}");
  EXPECT_FALSE(error_);
  EXPECT_EQ(ASCIIToUTF16("123456"), result_);

  // Normal success case with multiple results.
  CreateAndTestRequest(true,
      "{\"hypotheses\":[{\"utterance\":\"hello\",\"confidence\":0.9},"
      "{\"utterance\":\"123456\",\"confidence\":0.5}]}");
  EXPECT_FALSE(error_);
  EXPECT_EQ(ASCIIToUTF16("hello"), result_);

  // Http failure case.
  CreateAndTestRequest(false, "");
  EXPECT_TRUE(error_);
  EXPECT_EQ(ASCIIToUTF16(""), result_);

  // Malformed JSON case.
  CreateAndTestRequest(true, "{\"hypotheses\":[{\"unknownkey\":\"hello\"}]}");
  EXPECT_TRUE(error_);
  EXPECT_EQ(ASCIIToUTF16(""), result_);
}

}  // namespace speech_input