1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef MockWebSpeechRecognizer_h
27#define MockWebSpeechRecognizer_h
28
29#include "TestCommon.h"
30#include "public/platform/WebNonCopyable.h"
31#include "public/testing/WebTask.h"
32#include "public/web/WebSpeechRecognizer.h"
33#include <deque>
34#include <vector>
35
36namespace blink {
37class WebSpeechRecognitionHandle;
38class WebSpeechRecognitionParams;
39class WebSpeechRecognizerClient;
40}
41
42namespace WebTestRunner {
43
44class WebTestDelegate;
45
46class MockWebSpeechRecognizer : public blink::WebSpeechRecognizer, public blink::WebNonCopyable {
47public:
48    MockWebSpeechRecognizer();
49    ~MockWebSpeechRecognizer();
50
51    void setDelegate(WebTestDelegate*);
52
53    // WebSpeechRecognizer implementation:
54    virtual void start(const blink::WebSpeechRecognitionHandle&, const blink::WebSpeechRecognitionParams&, blink::WebSpeechRecognizerClient*) OVERRIDE;
55    virtual void stop(const blink::WebSpeechRecognitionHandle&, blink::WebSpeechRecognizerClient*) OVERRIDE;
56    virtual void abort(const blink::WebSpeechRecognitionHandle&, blink::WebSpeechRecognizerClient*) OVERRIDE;
57
58    // Methods accessed by layout tests:
59    void addMockResult(const blink::WebString& transcript, float confidence);
60    void setError(const blink::WebString& error, const blink::WebString& message);
61    bool wasAborted() const { return m_wasAborted; }
62
63    // Methods accessed from Task objects:
64    blink::WebSpeechRecognizerClient* client() { return m_client; }
65    blink::WebSpeechRecognitionHandle& handle() { return m_handle; }
66    WebTaskList* taskList() { return &m_taskList; }
67
68    class Task {
69    public:
70        Task(MockWebSpeechRecognizer* recognizer) : m_recognizer(recognizer) { }
71        virtual ~Task() { }
72        virtual void run() = 0;
73    protected:
74        MockWebSpeechRecognizer* m_recognizer;
75    };
76
77private:
78    void startTaskQueue();
79    void clearTaskQueue();
80
81    WebTaskList m_taskList;
82    blink::WebSpeechRecognitionHandle m_handle;
83    blink::WebSpeechRecognizerClient* m_client;
84    std::vector<blink::WebString> m_mockTranscripts;
85    std::vector<float> m_mockConfidences;
86    bool m_wasAborted;
87
88    // Queue of tasks to be run.
89    std::deque<Task*> m_taskQueue;
90    bool m_taskQueueRunning;
91
92    WebTestDelegate* m_delegate;
93
94    // Task for stepping the queue.
95    class StepTask : public WebMethodTask<MockWebSpeechRecognizer> {
96    public:
97        StepTask(MockWebSpeechRecognizer* object) : WebMethodTask<MockWebSpeechRecognizer>(object) { }
98        virtual void runIfValid() OVERRIDE;
99    };
100};
101
102}
103
104#endif // MockWebSpeechRecognizer_h
105