1 // Copyright 2013 The Chromium 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.
5 #include "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/browser/web_contents/web_contents_impl.h"
13 #include "content/public/browser/notification_types.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/speech_recognition_error.h"
17 #include "content/public/common/speech_recognition_result.h"
18 #include "content/public/common/url_constants.h"
19 #include "content/public/test/content_browser_test.h"
20 #include "content/public/test/content_browser_test_utils.h"
21 #include "content/public/test/fake_speech_recognition_manager.h"
22 #include "content/public/test/test_utils.h"
23 #include "content/shell/browser/shell.h"
24 #include "third_party/WebKit/public/web/WebInputEvent.h"
28 class InputTagSpeechBrowserTest : public ContentBrowserTest {
30 // ContentBrowserTest methods
31 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
32 EXPECT_TRUE(!command_line->HasSwitch(switches::kDisableSpeechInput));
36 void LoadAndStartSpeechRecognitionTest(const char* filename) {
37 // The test page calculates the speech button's coordinate in the page on
38 // load & sets that coordinate in the URL fragment. We send mouse down & up
39 // events at that coordinate to trigger speech recognition.
40 GURL test_url = GetTestUrl("speech", filename);
41 NavigateToURL(shell(), test_url);
43 blink::WebMouseEvent mouse_event;
44 mouse_event.type = blink::WebInputEvent::MouseDown;
45 mouse_event.button = blink::WebMouseEvent::ButtonLeft;
48 mouse_event.clickCount = 1;
49 WebContents* web_contents = shell()->web_contents();
51 WindowedNotificationObserver observer(
52 NOTIFICATION_LOAD_STOP,
53 Source<NavigationController>(&web_contents->GetController()));
54 web_contents->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
55 mouse_event.type = blink::WebInputEvent::MouseUp;
56 web_contents->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
57 fake_speech_recognition_manager_.WaitForRecognitionStarted();
59 // We should wait for a navigation event, raised by the test page JS code
60 // upon the onwebkitspeechchange event, in all cases except when the
61 // speech response is inhibited.
62 if (fake_speech_recognition_manager_.should_send_fake_response())
66 void RunSpeechRecognitionTest(const char* filename) {
67 // The fake speech input manager would receive the speech input
68 // request and return the test string as recognition result. The test page
69 // then sets the URL fragment as 'pass' if it received the expected string.
70 LoadAndStartSpeechRecognitionTest(filename);
72 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
75 // ContentBrowserTest methods.
76 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
77 fake_speech_recognition_manager_.set_should_send_fake_response(true);
78 speech_recognition_manager_ = &fake_speech_recognition_manager_;
80 // Inject the fake manager factory so that the test result is returned to
82 SpeechRecognitionManager::SetManagerForTesting(speech_recognition_manager_);
85 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
86 speech_recognition_manager_ = NULL;
89 FakeSpeechRecognitionManager fake_speech_recognition_manager_;
91 // This is used by the static |fakeManager|, and it is a pointer rather than a
92 // direct instance per the style guide.
93 static SpeechRecognitionManager* speech_recognition_manager_;
96 SpeechRecognitionManager*
97 InputTagSpeechBrowserTest::speech_recognition_manager_ = NULL;
99 // TODO(satish): Once this flakiness has been fixed, add a second test here to
100 // check for sending many clicks in succession to the speech button and verify
101 // that it doesn't cause any crash but works as expected. This should act as the
102 // test for http://crbug.com/59173
104 // TODO(satish): Similar to above, once this flakiness has been fixed add
105 // another test here to check that when speech recognition is in progress and
106 // a renderer crashes, we get a call to
107 // SpeechRecognitionManager::CancelAllRequestsWithDelegate.
109 IN_PROC_BROWSER_TEST_F(InputTagSpeechBrowserTest,
110 DISABLED_TestBasicRecognition) {
111 RunSpeechRecognitionTest("basic_recognition.html");
112 EXPECT_TRUE(fake_speech_recognition_manager_.grammar().empty());
116 IN_PROC_BROWSER_TEST_F(InputTagSpeechBrowserTest, DISABLED_GrammarAttribute) {
117 RunSpeechRecognitionTest("grammar_attribute.html");
118 EXPECT_EQ("http://example.com/grammar.xml",
119 fake_speech_recognition_manager_.grammar());
122 // Flaky on Linux, Windows and Mac http://crbug.com/140765.
123 IN_PROC_BROWSER_TEST_F(InputTagSpeechBrowserTest, DISABLED_TestCancelAll) {
124 // The test checks that the cancel-all callback gets issued when a session
125 // is pending, so don't send a fake response.
126 // We are not expecting a navigation event being raised from the JS of the
127 // test page JavaScript in this case.
128 fake_speech_recognition_manager_.set_should_send_fake_response(false);
130 LoadAndStartSpeechRecognitionTest("basic_recognition.html");
132 // Make the renderer crash. This should trigger
133 // InputTagSpeechDispatcherHost to cancel all pending sessions.
134 NavigateToURL(shell(), GURL(kChromeUICrashURL));
136 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all());
139 } // namespace content