OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle
r.h" | 5 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle
r.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "chrome/browser/media/router/mock_media_router.h" | 8 #include "chrome/browser/media/router/mock_media_router.h" |
9 #include "chrome/browser/media/router/mojo/media_router_mojo_test.h" | 9 #include "chrome/browser/media/router/mojo/media_router_mojo_test.h" |
10 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 web_ui_.reset(); | 161 web_ui_.reset(); |
162 BrowserWithTestWindowTest::TearDown(); | 162 BrowserWithTestWindowTest::TearDown(); |
163 } | 163 } |
164 | 164 |
165 const std::string& provider_extension_id() const { | 165 const std::string& provider_extension_id() const { |
166 return provider_extension_id_; | 166 return provider_extension_id_; |
167 } | 167 } |
168 | 168 |
169 // Gets the call data for the function call made to |web_ui_|. There needs | 169 // Gets the call data for the function call made to |web_ui_|. There needs |
170 // to be one call made, and its function name must be |function_name|. | 170 // to be one call made, and its function name must be |function_name|. |
171 const content::TestWebUI::CallData& GetCallData( | 171 const base::Value* GetCallData(const std::string& function_name) { |
172 const std::string& function_name) { | 172 CHECK(1u == web_ui_->call_data().size()); |
173 EXPECT_EQ(1u, web_ui_->call_data().size()); | |
174 const content::TestWebUI::CallData& call_data = *web_ui_->call_data()[0]; | 173 const content::TestWebUI::CallData& call_data = *web_ui_->call_data()[0]; |
175 EXPECT_EQ(function_name, call_data.function_name()); | 174 CHECK(function_name == call_data.function_name()); |
176 | 175 return call_data.arg1(); |
177 return call_data; | |
178 } | 176 } |
179 | 177 |
180 // Gets the dictionary passed into a call to the |web_ui_| as the argument. | 178 // Gets the dictionary passed into a call to the |web_ui_| as the argument. |
181 // There needs to be one call made, and its function name must be | 179 // There needs to be one call made, and its function name must be |
182 // |function_name|. | 180 // |function_name|. |
183 const base::DictionaryValue* ExtractDictFromCallArg( | 181 const base::DictionaryValue* ExtractDictFromCallArg( |
184 const std::string& function_name) { | 182 const std::string& function_name) { |
185 const content::TestWebUI::CallData& call_data = GetCallData(function_name); | |
186 const base::Value* arg1 = call_data.arg1(); | |
187 const base::DictionaryValue* dict_value = nullptr; | 183 const base::DictionaryValue* dict_value = nullptr; |
188 CHECK(arg1->GetAsDictionary(&dict_value)); | 184 CHECK(GetCallData(function_name)->GetAsDictionary(&dict_value)); |
| 185 return dict_value; |
| 186 } |
189 | 187 |
190 return dict_value; | 188 // Gets the list passed into a call to the |web_ui_| as the argument. |
| 189 // There needs to be one call made, and its function name must be |
| 190 // |function_name|. |
| 191 const base::ListValue* ExtractListFromCallArg( |
| 192 const std::string& function_name) { |
| 193 const base::ListValue* list_value = nullptr; |
| 194 CHECK(GetCallData(function_name)->GetAsList(&list_value)); |
| 195 return list_value; |
191 } | 196 } |
192 | 197 |
193 // Gets the first element of the list passed in as the argument to a call to | 198 // Gets the first element of the list passed in as the argument to a call to |
194 // the |web_ui_| as a dictionary. There needs to be one call made, and its | 199 // the |web_ui_| as a dictionary. There needs to be one call made, and its |
195 // function name must be |function_name|. | 200 // function name must be |function_name|. |
196 const base::DictionaryValue* ExtractDictFromListFromCallArg( | 201 const base::DictionaryValue* ExtractDictFromListFromCallArg( |
197 const std::string& function_name) { | 202 const std::string& function_name) { |
198 const content::TestWebUI::CallData& call_data = GetCallData(function_name); | |
199 const base::Value* arg1 = call_data.arg1(); | |
200 const base::ListValue* list_value = nullptr; | 203 const base::ListValue* list_value = nullptr; |
201 CHECK(arg1->GetAsList(&list_value)); | 204 CHECK(GetCallData(function_name)->GetAsList(&list_value)); |
202 const base::DictionaryValue* dict_value = nullptr; | 205 const base::DictionaryValue* dict_value = nullptr; |
203 CHECK(list_value->GetDictionary(0, &dict_value)); | 206 CHECK(list_value->GetDictionary(0, &dict_value)); |
204 | |
205 return dict_value; | 207 return dict_value; |
206 } | 208 } |
207 | 209 |
208 protected: | 210 protected: |
209 std::unique_ptr<content::TestWebUI> web_ui_; | 211 std::unique_ptr<content::TestWebUI> web_ui_; |
210 std::unique_ptr<MockMediaRouterUI> mock_media_router_ui_; | 212 std::unique_ptr<MockMediaRouterUI> mock_media_router_ui_; |
211 std::unique_ptr<TestMediaRouterWebUIMessageHandler> handler_; | 213 std::unique_ptr<TestMediaRouterWebUIMessageHandler> handler_; |
212 const std::string provider_extension_id_; | 214 const std::string provider_extension_id_; |
213 }; | 215 }; |
214 | 216 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 EXPECT_FALSE(GetBooleanFromDict(route_value, "canJoin")); | 377 EXPECT_FALSE(GetBooleanFromDict(route_value, "canJoin")); |
376 | 378 |
377 int actual_current_cast_mode = -1; | 379 int actual_current_cast_mode = -1; |
378 EXPECT_FALSE( | 380 EXPECT_FALSE( |
379 route_value->GetInteger("currentCastMode", &actual_current_cast_mode)); | 381 route_value->GetInteger("currentCastMode", &actual_current_cast_mode)); |
380 std::string custom_controller_path; | 382 std::string custom_controller_path; |
381 EXPECT_FALSE( | 383 EXPECT_FALSE( |
382 route_value->GetString("customControllerPath", &custom_controller_path)); | 384 route_value->GetString("customControllerPath", &custom_controller_path)); |
383 } | 385 } |
384 | 386 |
| 387 TEST_F(MediaRouterWebUIMessageHandlerTest, SetCastModesList) { |
| 388 CastModeSet cast_modes({MediaCastMode::DEFAULT, MediaCastMode::TAB_MIRROR, |
| 389 MediaCastMode::DESKTOP_MIRROR}); |
| 390 handler_->UpdateCastModes(cast_modes, "www.host.com", MediaCastMode::DEFAULT); |
| 391 const base::ListValue* set_cast_mode_list = |
| 392 ExtractListFromCallArg("media_router.ui.setCastModeList"); |
| 393 |
| 394 const base::DictionaryValue* cast_mode = nullptr; |
| 395 size_t index = 0; |
| 396 for (auto i = cast_modes.begin(); i != cast_modes.end(); i++) { |
| 397 CHECK(set_cast_mode_list->GetDictionary(index++, &cast_mode)); |
| 398 EXPECT_EQ(static_cast<int>(*i), GetIntegerFromDict(cast_mode, "type")); |
| 399 EXPECT_EQ(MediaCastModeToDescription(*i, "www.host.com"), |
| 400 GetStringFromDict(cast_mode, "description")); |
| 401 EXPECT_EQ("www.host.com", GetStringFromDict(cast_mode, "host")); |
| 402 EXPECT_EQ(*i == MediaCastMode::DEFAULT, |
| 403 GetBooleanFromDict(cast_mode, "isForced")); |
| 404 } |
| 405 } |
| 406 |
385 TEST_F(MediaRouterWebUIMessageHandlerTest, UpdateMediaRouteStatus) { | 407 TEST_F(MediaRouterWebUIMessageHandlerTest, UpdateMediaRouteStatus) { |
386 MediaStatus status; | 408 MediaStatus status; |
387 status.title = "test title"; | 409 status.title = "test title"; |
388 status.description = "test description"; | 410 status.description = "test description"; |
389 status.can_play_pause = true; | 411 status.can_play_pause = true; |
390 status.can_set_volume = true; | 412 status.can_set_volume = true; |
391 status.is_paused = true; | 413 status.is_paused = true; |
392 status.duration = base::TimeDelta::FromSeconds(90); | 414 status.duration = base::TimeDelta::FromSeconds(90); |
393 status.current_time = base::TimeDelta::FromSeconds(80); | 415 status.current_time = base::TimeDelta::FromSeconds(80); |
394 status.volume = 0.9; | 416 status.volume = 0.9; |
(...skipping 20 matching lines...) Expand all Loading... |
415 } | 437 } |
416 | 438 |
417 TEST_F(MediaRouterWebUIMessageHandlerTest, OnCreateRouteResponseReceived) { | 439 TEST_F(MediaRouterWebUIMessageHandlerTest, OnCreateRouteResponseReceived) { |
418 MediaRoute route = CreateRoute(); | 440 MediaRoute route = CreateRoute(); |
419 bool incognito = false; | 441 bool incognito = false; |
420 route.set_incognito(incognito); | 442 route.set_incognito(incognito); |
421 | 443 |
422 EXPECT_CALL(*mock_media_router_ui_, GetRouteProviderExtensionId()) | 444 EXPECT_CALL(*mock_media_router_ui_, GetRouteProviderExtensionId()) |
423 .WillOnce(ReturnRef(provider_extension_id())); | 445 .WillOnce(ReturnRef(provider_extension_id())); |
424 handler_->OnCreateRouteResponseReceived(route.media_sink_id(), &route); | 446 handler_->OnCreateRouteResponseReceived(route.media_sink_id(), &route); |
425 const content::TestWebUI::CallData& call_data = | 447 |
426 GetCallData("media_router.ui.onCreateRouteResponseReceived"); | 448 const content::TestWebUI::CallData& call_data = *web_ui_->call_data()[0]; |
427 const base::Value* sink_id_value = nullptr; | 449 EXPECT_EQ("media_router.ui.onCreateRouteResponseReceived", |
| 450 call_data.function_name()); |
| 451 std::string sink_id_value; |
428 ASSERT_TRUE(call_data.arg1()->GetAsString(&sink_id_value)); | 452 ASSERT_TRUE(call_data.arg1()->GetAsString(&sink_id_value)); |
429 EXPECT_EQ(route.media_sink_id(), sink_id_value->GetString()); | 453 EXPECT_EQ(route.media_sink_id(), sink_id_value); |
430 | 454 |
431 const base::DictionaryValue* route_value = nullptr; | 455 const base::DictionaryValue* route_value = nullptr; |
432 ASSERT_TRUE(call_data.arg2()->GetAsDictionary(&route_value)); | 456 ASSERT_TRUE(call_data.arg2()->GetAsDictionary(&route_value)); |
433 EXPECT_EQ(route.media_route_id(), GetStringFromDict(route_value, "id")); | 457 EXPECT_EQ(route.media_route_id(), GetStringFromDict(route_value, "id")); |
434 EXPECT_EQ(route.media_sink_id(), GetStringFromDict(route_value, "sinkId")); | 458 EXPECT_EQ(route.media_sink_id(), GetStringFromDict(route_value, "sinkId")); |
435 EXPECT_EQ(route.description(), GetStringFromDict(route_value, "description")); | 459 EXPECT_EQ(route.description(), GetStringFromDict(route_value, "description")); |
436 EXPECT_EQ(route.is_local(), GetBooleanFromDict(route_value, "isLocal")); | 460 EXPECT_EQ(route.is_local(), GetBooleanFromDict(route_value, "isLocal")); |
437 std::string expected_path = base::StringPrintf( | 461 std::string expected_path = base::StringPrintf( |
438 "%s://%s/%s", extensions::kExtensionScheme, | 462 "%s://%s/%s", extensions::kExtensionScheme, |
439 kProviderExtensionIdForTesting, kControllerPathForTesting); | 463 kProviderExtensionIdForTesting, kControllerPathForTesting); |
440 EXPECT_EQ(expected_path, | 464 EXPECT_EQ(expected_path, |
441 GetStringFromDict(route_value, "customControllerPath")); | 465 GetStringFromDict(route_value, "customControllerPath")); |
442 | 466 |
443 bool route_for_display = false; | 467 bool route_for_display = false; |
444 ASSERT_TRUE(call_data.arg3()->GetAsBoolean(&route_for_display)); | 468 ASSERT_TRUE(call_data.arg3()->GetAsBoolean(&route_for_display)); |
445 EXPECT_TRUE(route_for_display); | 469 EXPECT_TRUE(route_for_display); |
446 } | 470 } |
447 | 471 |
448 TEST_F(MediaRouterWebUIMessageHandlerTest, | 472 TEST_F(MediaRouterWebUIMessageHandlerTest, |
449 OnCreateRouteResponseReceivedIncognito) { | 473 OnCreateRouteResponseReceivedIncognito) { |
450 handler_->set_incognito_for_test(true); | 474 handler_->set_incognito_for_test(true); |
451 MediaRoute route = CreateRoute(); | 475 MediaRoute route = CreateRoute(); |
452 bool incognito = true; | 476 bool incognito = true; |
453 route.set_incognito(incognito); | 477 route.set_incognito(incognito); |
454 | 478 |
455 EXPECT_CALL(*mock_media_router_ui_, GetRouteProviderExtensionId()).WillOnce( | 479 EXPECT_CALL(*mock_media_router_ui_, GetRouteProviderExtensionId()).WillOnce( |
456 ReturnRef(provider_extension_id())); | 480 ReturnRef(provider_extension_id())); |
457 handler_->OnCreateRouteResponseReceived(route.media_sink_id(), &route); | 481 handler_->OnCreateRouteResponseReceived(route.media_sink_id(), &route); |
458 const content::TestWebUI::CallData& call_data = | 482 |
459 GetCallData("media_router.ui.onCreateRouteResponseReceived"); | 483 const content::TestWebUI::CallData& call_data = *web_ui_->call_data()[0]; |
460 const base::Value* sink_id_value = nullptr; | 484 EXPECT_EQ("media_router.ui.onCreateRouteResponseReceived", |
| 485 call_data.function_name()); |
| 486 std::string sink_id_value; |
461 ASSERT_TRUE(call_data.arg1()->GetAsString(&sink_id_value)); | 487 ASSERT_TRUE(call_data.arg1()->GetAsString(&sink_id_value)); |
462 EXPECT_EQ(route.media_sink_id(), sink_id_value->GetString()); | 488 EXPECT_EQ(route.media_sink_id(), sink_id_value); |
463 | 489 |
464 const base::DictionaryValue* route_value = nullptr; | 490 const base::DictionaryValue* route_value = nullptr; |
465 ASSERT_TRUE(call_data.arg2()->GetAsDictionary(&route_value)); | 491 ASSERT_TRUE(call_data.arg2()->GetAsDictionary(&route_value)); |
466 EXPECT_EQ(route.media_route_id(), GetStringFromDict(route_value, "id")); | 492 EXPECT_EQ(route.media_route_id(), GetStringFromDict(route_value, "id")); |
467 EXPECT_EQ(route.media_sink_id(), GetStringFromDict(route_value, "sinkId")); | 493 EXPECT_EQ(route.media_sink_id(), GetStringFromDict(route_value, "sinkId")); |
468 EXPECT_EQ(route.description(), GetStringFromDict(route_value, "description")); | 494 EXPECT_EQ(route.description(), GetStringFromDict(route_value, "description")); |
469 EXPECT_EQ(route.is_local(), GetBooleanFromDict(route_value, "isLocal")); | 495 EXPECT_EQ(route.is_local(), GetBooleanFromDict(route_value, "isLocal")); |
470 | 496 |
471 std::string actual_path; | 497 std::string actual_path; |
472 EXPECT_FALSE(route_value->GetString("customControllerPath", &actual_path)); | 498 EXPECT_FALSE(route_value->GetString("customControllerPath", &actual_path)); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 | 678 |
653 TEST_F(MediaRouterWebUIMessageHandlerTest, OnRouteControllerInvalidated) { | 679 TEST_F(MediaRouterWebUIMessageHandlerTest, OnRouteControllerInvalidated) { |
654 handler_->OnRouteControllerInvalidated(); | 680 handler_->OnRouteControllerInvalidated(); |
655 EXPECT_EQ(1u, web_ui_->call_data().size()); | 681 EXPECT_EQ(1u, web_ui_->call_data().size()); |
656 const content::TestWebUI::CallData& call_data = *web_ui_->call_data()[0]; | 682 const content::TestWebUI::CallData& call_data = *web_ui_->call_data()[0]; |
657 EXPECT_EQ("media_router.ui.onRouteControllerInvalidated", | 683 EXPECT_EQ("media_router.ui.onRouteControllerInvalidated", |
658 call_data.function_name()); | 684 call_data.function_name()); |
659 } | 685 } |
660 | 686 |
661 } // namespace media_router | 687 } // namespace media_router |
OLD | NEW |