Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / speech / speech_recognition_browsertest.cc
blobceb0ff1a05fad419fb4ff2aa8d0ffc0a29f65d94
1 // Copyright 2015 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/strings/utf_string_conversions.h"
6 #include "chrome/browser/speech/chrome_speech_recognition_manager_delegate.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/test/browser_test_utils.h"
14 #include "content/public/test/fake_speech_recognition_manager.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
17 using content::FakeSpeechRecognitionManager;
18 using content::WebContents;
20 namespace speech {
22 class ChromeSpeechRecognitionTest : public InProcessBrowserTest {
23 public:
24 ChromeSpeechRecognitionTest() {}
25 ~ChromeSpeechRecognitionTest() override {}
27 void SetUp() override {
28 // SpeechRecognition test specific SetUp.
29 fake_speech_recognition_manager_.reset(
30 new content::FakeSpeechRecognitionManager());
31 fake_speech_recognition_manager_->set_should_send_fake_response(true);
32 // Inject the fake manager factory so that the test result is returned to
33 // the web page.
34 content::SpeechRecognitionManager::SetManagerForTesting(
35 fake_speech_recognition_manager_.get());
37 InProcessBrowserTest::SetUp();
40 void TearDown() override {
41 content::SpeechRecognitionManager::SetManagerForTesting(NULL);
42 fake_speech_recognition_manager_->SetDelegate(NULL);
43 InProcessBrowserTest::TearDown();
46 protected:
47 scoped_ptr<content::FakeSpeechRecognitionManager>
48 fake_speech_recognition_manager_;
50 private:
51 DISALLOW_COPY_AND_ASSIGN(ChromeSpeechRecognitionTest);
54 class SpeechWebContentsObserver : public content::WebContentsObserver {
55 public:
56 explicit SpeechWebContentsObserver(WebContents* web_contents)
57 : WebContentsObserver(web_contents),
58 render_view_host_changed_(false),
59 web_contents_destroyed_(false) {}
60 ~SpeechWebContentsObserver() override {}
62 // content::WebContentsObserver overrides.
63 void RenderViewHostChanged(content::RenderViewHost* old_host,
64 content::RenderViewHost* new_host) override {
65 render_view_host_changed_ = true;
67 void WebContentsDestroyed() override { web_contents_destroyed_ = true; }
69 bool web_contents_destroyed() { return web_contents_destroyed_; }
70 bool render_view_host_changed() { return render_view_host_changed_; }
72 private:
73 bool render_view_host_changed_;
74 bool web_contents_destroyed_;
76 DISALLOW_COPY_AND_ASSIGN(SpeechWebContentsObserver);
79 // Tests that ChromeSpeechRecognitionManagerDelegate works properly
80 // when a WebContents goes away (WCO::WebContentsDestroyed) or the RVH
81 // changes within a WebContents (WCO::RenderViewHostChanged).
82 IN_PROC_BROWSER_TEST_F(ChromeSpeechRecognitionTest, BasicTearDown) {
83 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
84 net::SpawnedTestServer::SSLOptions ssl_options;
85 net::SpawnedTestServer https_server(
86 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
87 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
88 ASSERT_TRUE(https_server.Start());
90 GURL http_url =
91 embedded_test_server()->GetURL("/speech/web_speech_test.html");
92 GURL https_url(https_server.GetURL("files/speech/web_speech_test.html"));
94 scoped_ptr<ChromeSpeechRecognitionManagerDelegate> delegate(
95 new ChromeSpeechRecognitionManagerDelegate());
96 static_cast<content::FakeSpeechRecognitionManager*>(
97 fake_speech_recognition_manager_.get())->SetDelegate(delegate.get());
99 ui_test_utils::NavigateToURL(browser(), http_url);
100 WebContents* web_contents =
101 browser()->tab_strip_model()->GetActiveWebContents();
102 EXPECT_TRUE(web_contents);
103 SpeechWebContentsObserver speech_contents_observer(web_contents);
105 base::string16 success_title(base::ASCIIToUTF16("PASS"));
106 base::string16 failure_title(base::ASCIIToUTF16("FAIL"));
108 const char kRetriveTranscriptScript[] =
109 "window.domAutomationController.send(window.getFirstTranscript())";
110 const char kExpectedTranscript[] = "Pictures of the moon";
113 content::TitleWatcher title_watcher(web_contents, success_title);
114 title_watcher.AlsoWaitForTitle(failure_title);
115 EXPECT_TRUE(
116 content::ExecuteScript(web_contents, "testSpeechRecognition()"));
117 EXPECT_EQ(success_title, title_watcher.WaitAndGetTitle());
119 std::string output;
120 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
121 web_contents, kRetriveTranscriptScript, &output));
122 EXPECT_EQ(kExpectedTranscript, output);
125 // Navigating to an https page will force RVH change within
126 // |web_contents|, results in WCO::RenderViewHostChanged().
127 ui_test_utils::NavigateToURL(browser(), https_url);
129 EXPECT_TRUE(speech_contents_observer.render_view_host_changed());
132 content::TitleWatcher title_watcher(web_contents, success_title);
133 title_watcher.AlsoWaitForTitle(failure_title);
134 EXPECT_TRUE(
135 content::ExecuteScript(web_contents, "testSpeechRecognition()"));
136 EXPECT_EQ(success_title, title_watcher.WaitAndGetTitle());
138 std::string output;
139 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
140 web_contents, kRetriveTranscriptScript, &output));
141 EXPECT_EQ(kExpectedTranscript, output);
144 // Close the tab to so that we see WCO::WebContentsDestroyed().
145 chrome::CloseTab(browser());
146 EXPECT_TRUE(speech_contents_observer.web_contents_destroyed());
149 } // namespace speech