Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/file_path.h" | |
| 7 #include "chrome/browser/browser.h" | |
| 8 #include "chrome/browser/speech/speech_input_dispatcher_host.h" | |
| 9 #include "chrome/browser/speech/speech_input_manager.h" | |
| 10 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 11 #include "chrome/common/chrome_switches.h" | |
| 12 #include "chrome/test/in_process_browser_test.h" | |
| 13 #include "chrome/test/ui_test_utils.h" | |
| 14 | |
| 15 namespace speech_input { | |
| 16 class FakeSpeechInputManager; | |
| 17 } | |
| 18 | |
| 19 // This class does not need to be refcounted (typically done by PostTask) since | |
| 20 // it will outlive the test and gets released only when the test shuts down. | |
| 21 // Disabling refcounting here saves a bit of unnecessary code and the factory | |
| 22 // method can return a plain pointer below as required by the real code. | |
| 23 DISABLE_RUNNABLE_METHOD_REFCOUNT(speech_input::FakeSpeechInputManager); | |
| 24 | |
| 25 namespace speech_input { | |
| 26 | |
| 27 const char* kTestResult = "Pictures of the moon"; | |
| 28 | |
| 29 class FakeSpeechInputManager : public SpeechInputManager { | |
| 30 public: | |
| 31 explicit FakeSpeechInputManager(Listener* listener) | |
| 32 : render_view_id_(0), | |
| 33 listener_(listener) { | |
| 34 } | |
| 35 | |
| 36 // SpeechInputManager methods. | |
| 37 void StartRecognition(int render_view_id) { | |
| 38 EXPECT_EQ(0, render_view_id_); | |
| 39 render_view_id_ = render_view_id; | |
| 40 // Give the fake result in a short while. | |
| 41 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, | |
| 42 &FakeSpeechInputManager::SetFakeRecognitionResult)); | |
| 43 } | |
| 44 void CancelRecognition(int render_view_id) { | |
| 45 EXPECT_EQ(render_view_id_, render_view_id); | |
| 46 render_view_id_ = 0; | |
| 47 } | |
| 48 void StopRecording(int render_view_id) { | |
| 49 EXPECT_EQ(render_view_id_, render_view_id); | |
| 50 // Nothing to do here since we aren't really recording. | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 void SetFakeRecognitionResult() { | |
| 55 if (render_view_id_) { // Do a check in case we were cancelled.. | |
| 56 listener_->DidCompleteRecording(render_view_id_); | |
| 57 listener_->SetRecognitionResult(render_view_id_, ASCIIToUTF16(kTestResult) ); | |
|
bulach
2010/08/04 19:29:05
>80cols
| |
| 58 listener_->DidCompleteRecognition(render_view_id_); | |
| 59 render_view_id_ = 0; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 int render_view_id_; | |
| 64 Listener* listener_; | |
| 65 }; | |
| 66 | |
| 67 // Factory method. | |
| 68 SpeechInputManager* fakeManagerFactory(SpeechInputManager::Listener* listener) { | |
| 69 return new FakeSpeechInputManager(listener); | |
| 70 } | |
| 71 | |
| 72 class SpeechInputBrowserTest : public InProcessBrowserTest { | |
| 73 public: | |
| 74 // InProcessBrowserTest methods | |
| 75 virtual void SetUpCommandLine(CommandLine* command_line) { | |
| 76 command_line->AppendSwitch(switches::kEnableSpeechInput); | |
| 77 } | |
| 78 | |
| 79 GURL testUrl(const FilePath::CharType* filename) { | |
| 80 const FilePath kTestDir(FILE_PATH_LITERAL("speech")); | |
| 81 return ui_test_utils::GetTestUrl(kTestDir, FilePath(filename)); | |
| 82 } | |
| 83 }; | |
| 84 | |
| 85 IN_PROC_BROWSER_TEST_F(SpeechInputBrowserTest, TestBasicRecognition) { | |
| 86 // Inject the fake manager factory so that the test result is returned to the | |
| 87 // web page. | |
| 88 SpeechInputDispatcherHost::set_manager_factory(&fakeManagerFactory); | |
| 89 | |
| 90 // The test page starts speech recognition and waits to receive the above | |
| 91 // defined test string as the result. Once it receives the result it either | |
| 92 // navigates to #pass or #fail depending on the result. | |
| 93 GURL test_url = testUrl(FILE_PATH_LITERAL("basic_recognition.html")); | |
| 94 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), | |
| 95 test_url, | |
| 96 2); | |
| 97 | |
| 98 // Check that the page got the result it expected. | |
| 99 EXPECT_EQ("pass", browser()->GetSelectedTabContents()->GetURL().ref()); | |
| 100 } | |
| 101 | |
| 102 } // namespace speech_input | |
| OLD | NEW |