[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / test / media_router / media_router_integration_browsertest.cc
blob73b7507a6a61c51a359a076116642928df315ca9
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 "chrome/test/media_router/media_router_integration_browsertest.h"
7 #include "base/files/file_util.h"
8 #include "base/json/json_file_value_serializer.h"
9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h"
11 #include "base/path_service.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "content/public/test/test_navigation_observer.h"
20 #include "content/public/test/test_utils.h"
21 #include "net/base/filename_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace {
25 // The path relative to <chromium src>/out/<build config> for media router
26 // browser test resources.
27 const base::FilePath::StringPieceType kResourcePath = FILE_PATH_LITERAL(
28 "media_router/browser_test_resources/");
29 } // namespace
32 namespace media_router {
34 MediaRouterIntegrationBrowserTest::MediaRouterIntegrationBrowserTest() {
37 MediaRouterIntegrationBrowserTest::~MediaRouterIntegrationBrowserTest() {
40 void MediaRouterIntegrationBrowserTest::ExecuteJavaScriptAPI(
41 content::WebContents* web_contents,
42 const std::string& script) {
43 std::string result;
45 ASSERT_TRUE(
46 content::ExecuteScriptAndExtractString(web_contents, script, &result));
48 // Read the test result, the test result set by javascript is a
49 // JSON string with the following format:
50 // {"passed": "<true/false>", "errorMessage": "<error_message>"}
51 scoped_ptr<base::Value> value =
52 base::JSONReader::Read(result, base::JSON_ALLOW_TRAILING_COMMAS);
54 // Convert to dictionary.
55 base::DictionaryValue* dict_value = nullptr;
56 ASSERT_TRUE(value->GetAsDictionary(&dict_value));
58 // Extract the fields.
59 bool passed = false;
60 ASSERT_TRUE(dict_value->GetBoolean("passed", &passed));
61 std::string error_message;
62 ASSERT_TRUE(dict_value->GetString("errorMessage", &error_message));
64 EXPECT_TRUE(passed) << error_message;
67 void MediaRouterIntegrationBrowserTest::OpenTestPage(
68 base::FilePath::StringPieceType file_name) {
69 base::FilePath full_path = GetResourceFile(file_name);
70 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(full_path));
73 void MediaRouterIntegrationBrowserTest::OpenTestPageInNewTab(
74 base::FilePath::StringPieceType file_name) {
75 base::FilePath full_path = GetResourceFile(file_name);
76 ui_test_utils::NavigateToURLWithDisposition(
77 browser(), net::FilePathToFileURL(full_path), NEW_FOREGROUND_TAB,
78 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
81 void MediaRouterIntegrationBrowserTest::ChooseSink(
82 content::WebContents* web_contents,
83 const std::string& sink_id) {
84 MediaRouterDialogControllerImpl* controller =
85 MediaRouterDialogControllerImpl::GetOrCreateForWebContents(web_contents);
86 content::WebContents* dialog_contents = controller->GetMediaRouterDialog();
87 ASSERT_TRUE(dialog_contents);
88 std::string script = base::StringPrintf(
89 "window.document.getElementById('media-router-container')."
90 "showOrCreateRoute_({'id': '%s', 'name': ''}, null)",
91 sink_id.c_str());
92 ASSERT_TRUE(content::ExecuteScript(dialog_contents, script));
95 void MediaRouterIntegrationBrowserTest::SetTestData(
96 base::FilePath::StringPieceType test_data_file) {
97 base::FilePath full_path = GetResourceFile(test_data_file);
98 JSONFileValueDeserializer deserializer(full_path);
99 int error_code = 0;
100 std::string error_message;
101 scoped_ptr<base::Value> value(
102 deserializer.Deserialize(&error_code, &error_message));
103 CHECK(value.get()) << "Deserialize failed: " << error_message;
104 std::string test_data_str;
105 ASSERT_TRUE(base::JSONWriter::Write(*value, &test_data_str));
106 ExecuteScriptInBackgroundPageNoWait(
107 extension_id_,
108 base::StringPrintf("localStorage['testdata'] = '%s'",
109 test_data_str.c_str()));
112 base::FilePath MediaRouterIntegrationBrowserTest::GetResourceFile(
113 base::FilePath::StringPieceType relative_path) const {
114 base::FilePath base_dir;
115 // ASSERT_TRUE can only be used in void returning functions.
116 // Use CHECK instead in non-void returning functions.
117 CHECK(PathService::Get(base::DIR_MODULE, &base_dir));
118 base::FilePath full_path =
119 base_dir.Append(kResourcePath).Append(relative_path);
120 CHECK(PathExists(full_path));
121 return full_path;
124 IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_Basic) {
125 OpenTestPage(FILE_PATH_LITERAL("basic_test.html"));
126 content::WebContents* web_contents =
127 browser()->tab_strip_model()->GetActiveWebContents();
128 ASSERT_TRUE(web_contents);
129 ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();");
130 content::TestNavigationObserver test_navigation_observer(web_contents, 1);
131 test_navigation_observer.StartWatchingNewWebContents();
132 ExecuteJavaScriptAPI(web_contents, "startSession();");
133 test_navigation_observer.Wait();
134 ChooseSink(web_contents, "id1");
135 ExecuteJavaScriptAPI(web_contents, "checkSession();");
136 Wait(base::TimeDelta::FromSeconds(5));
137 ExecuteJavaScriptAPI(web_contents, "stopSession();");
140 IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest,
141 MANUAL_Fail_No_Provider) {
142 SetTestData(FILE_PATH_LITERAL("no_provider.json"));
143 OpenTestPage(FILE_PATH_LITERAL("no_provider.html"));
144 content::WebContents* web_contents =
145 browser()->tab_strip_model()->GetActiveWebContents();
146 ASSERT_TRUE(web_contents);
147 ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();");
148 content::TestNavigationObserver test_navigation_observer(web_contents, 1);
149 test_navigation_observer.StartWatchingNewWebContents();
150 ExecuteJavaScriptAPI(web_contents, "startSession();");
151 test_navigation_observer.Wait();
152 ChooseSink(web_contents, "id1");
153 ExecuteJavaScriptAPI(web_contents, "checkSessionFailedToStart();");
156 IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest,
157 MANUAL_Fail_Create_Route) {
158 SetTestData(FILE_PATH_LITERAL("fail_create_route.json"));
159 OpenTestPage(FILE_PATH_LITERAL("fail_create_route.html"));
160 content::WebContents* web_contents =
161 browser()->tab_strip_model()->GetActiveWebContents();
162 ASSERT_TRUE(web_contents);
163 ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();");
164 content::TestNavigationObserver test_navigation_observer(web_contents, 1);
165 test_navigation_observer.StartWatchingNewWebContents();
166 ExecuteJavaScriptAPI(web_contents, "startSession();");
167 test_navigation_observer.Wait();
168 ChooseSink(web_contents, "id1");
169 ExecuteJavaScriptAPI(web_contents, "checkSessionFailedToStart();");
172 IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_JoinSession) {
173 OpenTestPage(FILE_PATH_LITERAL("basic_test.html"));
174 content::WebContents* web_contents =
175 browser()->tab_strip_model()->GetActiveWebContents();
176 ASSERT_TRUE(web_contents);
177 ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();");
178 content::TestNavigationObserver test_navigation_observer(web_contents, 1);
179 test_navigation_observer.StartWatchingNewWebContents();
180 ExecuteJavaScriptAPI(web_contents, "startSession();");
181 test_navigation_observer.Wait();
182 ChooseSink(web_contents, "id1");
183 ExecuteJavaScriptAPI(web_contents, "checkSession();");
184 std::string session_id;
185 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
186 web_contents, "window.domAutomationController.send(startedSession.id)",
187 &session_id));
189 OpenTestPageInNewTab(FILE_PATH_LITERAL("basic_test.html"));
190 content::WebContents* new_web_contents =
191 browser()->tab_strip_model()->GetActiveWebContents();
192 ASSERT_TRUE(new_web_contents);
193 ASSERT_NE(web_contents, new_web_contents);
194 ExecuteJavaScriptAPI(
195 new_web_contents,
196 base::StringPrintf("joinSession('%s');", session_id.c_str()));
197 std::string joined_session_id;
198 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
199 new_web_contents, "window.domAutomationController.send(joinedSession.id)",
200 &joined_session_id));
201 ASSERT_EQ(session_id, joined_session_id);
204 } // namespace media_router