diff options
author | leilei <leilei@chromium.org> | 2015-08-11 16:49:51 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-11 23:50:34 +0000 |
commit | 90bac4fd03ae9e1863512507df9ab461ff9366ee (patch) | |
tree | c43b765905d9c940d2115a2b5fb7507b26ebe87c /chrome | |
parent | d30ec60a76d4a68540771754195f5e49c01bc57a (diff) | |
download | chromium_src-90bac4fd03ae9e1863512507df9ab461ff9366ee.zip chromium_src-90bac4fd03ae9e1863512507df9ab461ff9366ee.tar.gz chromium_src-90bac4fd03ae9e1863512507df9ab461ff9366ee.tar.bz2 |
Basic integration test case for MR popup dialog.
This test doesn't cover route detail dialog, since I don't know how to
get the object model for route detail dialog, added a TODO for that.
Moreover, it also doesn't cover the failure case, since IssueManager
hasn't been implemented, we should add failure cases to cover it once
IssueManager has been done.
BUG=464227
Review URL: https://codereview.chromium.org/1244993002
Cr-Commit-Position: refs/heads/master@{#342940}
Diffstat (limited to 'chrome')
5 files changed, 215 insertions, 53 deletions
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 3c4361a..dad133a 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -880,6 +880,7 @@ 'test/media_router/media_router_e2e_browsertest.h', 'test/media_router/media_router_integration_browsertest.cc', 'test/media_router/media_router_integration_browsertest.h', + 'test/media_router/media_router_integration_ui_browsertest.cc', 'test/media_router/test_media_sinks_observer.cc', 'test/media_router/test_media_sinks_observer.h', ], diff --git a/chrome/test/media_router/media_router_base_browsertest.cc b/chrome/test/media_router/media_router_base_browsertest.cc index 34a88db..f8da12f 100644 --- a/chrome/test/media_router/media_router_base_browsertest.cc +++ b/chrome/test/media_router/media_router_base_browsertest.cc @@ -24,16 +24,15 @@ const char kExtensionCrx[] = "extension-crx"; const char kExtensionUnpacked[] = "extension-unpacked"; } // namespace + namespace media_router { MediaRouterBaseBrowserTest::MediaRouterBaseBrowserTest() : extension_load_event_(false, false), extension_host_created_(false) { base::CommandLine::ForCurrentProcess()->AppendSwitch( switches::kEnableMediaRouter); - base::CommandLine::ForCurrentProcess()->AppendSwitch( - extensions::switches::kEnableExtensionActionRedesign); base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( - switches::kEnableBlinkFeatures, "Presentation"); + switches::kEnableExperimentalWebPlatformFeatures, "Presentation"); } MediaRouterBaseBrowserTest::~MediaRouterBaseBrowserTest() { @@ -49,13 +48,13 @@ void MediaRouterBaseBrowserTest::TearDown() { } void MediaRouterBaseBrowserTest::SetUpOnMainThread() { + ExtensionBrowserTest::SetUpOnMainThread(); extensions::ProcessManager* process_manager = extensions::ProcessManager::Get(browser()->profile()); DCHECK(process_manager); process_manager->AddObserver(this); InstallAndEnableMRExtension(); extension_load_event_.Wait(); - ExtensionBrowserTest::SetUpOnMainThread(); } void MediaRouterBaseBrowserTest::TearDownOnMainThread() { diff --git a/chrome/test/media_router/media_router_integration_browsertest.cc b/chrome/test/media_router/media_router_integration_browsertest.cc index fbd2498..daaf002 100644 --- a/chrome/test/media_router/media_router_integration_browsertest.cc +++ b/chrome/test/media_router/media_router_integration_browsertest.cc @@ -4,16 +4,20 @@ #include "chrome/test/media_router/media_router_integration_browsertest.h" +#include "base/bind.h" #include "base/files/file_util.h" #include "base/json/json_file_value_serializer.h" #include "base/json/json_reader.h" #include "base/json/json_writer.h" #include "base/path_service.h" #include "base/strings/stringprintf.h" +#include "base/thread_task_runner_handle.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.h" +#include "chrome/common/url_constants.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/test/browser_test_utils.h" #include "content/public/test/test_navigation_observer.h" @@ -21,6 +25,7 @@ #include "net/base/filename_util.h" #include "testing/gtest/include/gtest/gtest.h" + namespace media_router { namespace { @@ -28,6 +33,12 @@ namespace { // browser test resources. const base::FilePath::StringPieceType kResourcePath = FILE_PATH_LITERAL( "media_router/browser_test_resources/"); +// The javascript snippets. +const std::string kCheckSessionScript = "checkSession();"; +const std::string kCheckSessionFailedScript = "checkSessionFailedToStart();"; +const std::string kStartSessionScript = "startSession();"; +const std::string kStopSessionScript = "stopSession()"; +const std::string kWaitDeviceScript = "waitUntilDeviceAvailable();"; void GetStartedSessionId(content::WebContents* web_contents, std::string* session_id) { @@ -44,13 +55,15 @@ MediaRouterIntegrationBrowserTest::MediaRouterIntegrationBrowserTest() { MediaRouterIntegrationBrowserTest::~MediaRouterIntegrationBrowserTest() { } +void MediaRouterIntegrationBrowserTest::TearDownOnMainThread() { + MediaRouterBaseBrowserTest::TearDownOnMainThread(); + test_navigation_observer_.reset(); +} + void MediaRouterIntegrationBrowserTest::ExecuteJavaScriptAPI( content::WebContents* web_contents, const std::string& script) { - std::string result; - - ASSERT_TRUE( - content::ExecuteScriptAndExtractString(web_contents, script, &result)); + std::string result(ExecuteScriptAndExtractString(web_contents, script)); // Read the test result, the test result set by javascript is a // JSON string with the following format: @@ -68,7 +81,7 @@ void MediaRouterIntegrationBrowserTest::ExecuteJavaScriptAPI( std::string error_message; ASSERT_TRUE(dict_value->GetString("errorMessage", &error_message)); - EXPECT_TRUE(passed) << error_message; + ASSERT_TRUE(passed) << error_message; } void MediaRouterIntegrationBrowserTest::OpenTestPage( @@ -85,20 +98,47 @@ void MediaRouterIntegrationBrowserTest::OpenTestPageInNewTab( ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); } +void MediaRouterIntegrationBrowserTest::StartSession( + content::WebContents* web_contents) { + test_navigation_observer_.reset( + new content::TestNavigationObserver(web_contents, 1)); + test_navigation_observer_->StartWatchingNewWebContents(); + ExecuteJavaScriptAPI(web_contents, kStartSessionScript); + test_navigation_observer_->Wait(); + test_navigation_observer_->StopWatchingNewWebContents(); +} + void MediaRouterIntegrationBrowserTest::ChooseSink( content::WebContents* web_contents, - const std::string& sink_id) { - MediaRouterDialogControllerImpl* controller = - MediaRouterDialogControllerImpl::GetOrCreateForWebContents(web_contents); - content::WebContents* dialog_contents = controller->GetMediaRouterDialog(); - ASSERT_TRUE(dialog_contents); + const std::string& sink_id, + const std::string& current_route) { + content::WebContents* dialog_contents = GetMRDialog(web_contents); + std::string route; + if (current_route.empty()) { + route = "null"; + } else { + route = current_route; + std::string script = base::StringPrintf( + "window.document.getElementById('media-router-container')." + "currentRoute_ = %s", route.c_str()); + ASSERT_TRUE(content::ExecuteScript(dialog_contents, script)); + } std::string script = base::StringPrintf( "window.document.getElementById('media-router-container')." - "showOrCreateRoute_({'id': '%s', 'name': ''}, null)", - sink_id.c_str()); + "showOrCreateRoute_({'id': '%s', 'name': ''}, %s)", + sink_id.c_str(), route.c_str()); ASSERT_TRUE(content::ExecuteScript(dialog_contents, script)); } +content::WebContents* MediaRouterIntegrationBrowserTest::GetMRDialog( + content::WebContents* web_contents) { + MediaRouterDialogControllerImpl* controller = + MediaRouterDialogControllerImpl::GetOrCreateForWebContents(web_contents); + content::WebContents* dialog_contents = controller->GetMediaRouterDialog(); + CHECK(dialog_contents); + return dialog_contents; +} + void MediaRouterIntegrationBrowserTest::SetTestData( base::FilePath::StringPieceType test_data_file) { base::FilePath full_path = GetResourceFile(test_data_file); @@ -116,6 +156,21 @@ void MediaRouterIntegrationBrowserTest::SetTestData( test_data_str.c_str())); } +content::WebContents* MediaRouterIntegrationBrowserTest::OpenMRDialog( + content::WebContents* web_contents) { + MediaRouterDialogControllerImpl* controller = + MediaRouterDialogControllerImpl::GetOrCreateForWebContents(web_contents); + test_navigation_observer_.reset( + new content::TestNavigationObserver(web_contents, 1)); + test_navigation_observer_->StartWatchingNewWebContents(); + CHECK(controller->ShowMediaRouterDialog()); + test_navigation_observer_->Wait(); + test_navigation_observer_->StopWatchingNewWebContents(); + content::WebContents* dialog_contents = controller->GetMediaRouterDialog(); + CHECK(dialog_contents); + return dialog_contents; +} + base::FilePath MediaRouterIntegrationBrowserTest::GetResourceFile( base::FilePath::StringPieceType relative_path) const { base::FilePath base_dir; @@ -128,20 +183,49 @@ base::FilePath MediaRouterIntegrationBrowserTest::GetResourceFile( return full_path; } +int MediaRouterIntegrationBrowserTest::ExecuteScriptAndExtractInt( + const content::ToRenderFrameHost& adapter, const std::string& script) { + int result; + CHECK(content::ExecuteScriptAndExtractInt(adapter, script, &result)); + return result; +} + +std::string MediaRouterIntegrationBrowserTest::ExecuteScriptAndExtractString( + const content::ToRenderFrameHost& adapter, const std::string& script) { + std::string result; + CHECK(content::ExecuteScriptAndExtractString(adapter, script, &result)); + return result; +} + +bool MediaRouterIntegrationBrowserTest::IsRouteCreatedOnUI() { + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + content::WebContents* dialog_contents = GetMRDialog(web_contents); + std::string script; + script = base::StringPrintf( + "domAutomationController.send(window.document.getElementById(" + "'media-router-container').routeList.length)"); + return ExecuteScriptAndExtractInt(dialog_contents, script) == 1; +} + +void MediaRouterIntegrationBrowserTest::WaitUntilRouteCreated() { + ConditionalWait( + base::TimeDelta::FromSeconds(10), base::TimeDelta::FromSeconds(1), + base::Bind(&MediaRouterIntegrationBrowserTest::IsRouteCreatedOnUI, + base::Unretained(this))); +} + IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_Basic) { OpenTestPage(FILE_PATH_LITERAL("basic_test.html")); content::WebContents* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); ASSERT_TRUE(web_contents); - ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();"); - content::TestNavigationObserver test_navigation_observer(web_contents, 1); - test_navigation_observer.StartWatchingNewWebContents(); - ExecuteJavaScriptAPI(web_contents, "startSession();"); - test_navigation_observer.Wait(); - ChooseSink(web_contents, "id1"); - ExecuteJavaScriptAPI(web_contents, "checkSession();"); + ExecuteJavaScriptAPI(web_contents, kWaitDeviceScript); + StartSession(web_contents); + ChooseSink(web_contents, "id1", ""); + ExecuteJavaScriptAPI(web_contents, kCheckSessionScript); Wait(base::TimeDelta::FromSeconds(5)); - ExecuteJavaScriptAPI(web_contents, "stopSession();"); + ExecuteJavaScriptAPI(web_contents, kStopSessionScript); } IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, @@ -151,13 +235,10 @@ IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, content::WebContents* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); ASSERT_TRUE(web_contents); - ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();"); - content::TestNavigationObserver test_navigation_observer(web_contents, 1); - test_navigation_observer.StartWatchingNewWebContents(); - ExecuteJavaScriptAPI(web_contents, "startSession();"); - test_navigation_observer.Wait(); - ChooseSink(web_contents, "id1"); - ExecuteJavaScriptAPI(web_contents, "checkSessionFailedToStart();"); + ExecuteJavaScriptAPI(web_contents, kWaitDeviceScript); + StartSession(web_contents); + ChooseSink(web_contents, "id1", ""); + ExecuteJavaScriptAPI(web_contents, kCheckSessionFailedScript); } IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, @@ -167,13 +248,10 @@ IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, content::WebContents* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); ASSERT_TRUE(web_contents); - ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();"); - content::TestNavigationObserver test_navigation_observer(web_contents, 1); - test_navigation_observer.StartWatchingNewWebContents(); - ExecuteJavaScriptAPI(web_contents, "startSession();"); - test_navigation_observer.Wait(); - ChooseSink(web_contents, "id1"); - ExecuteJavaScriptAPI(web_contents, "checkSessionFailedToStart();"); + ExecuteJavaScriptAPI(web_contents, kWaitDeviceScript); + StartSession(web_contents); + ChooseSink(web_contents, "id1", ""); + ExecuteJavaScriptAPI(web_contents, kCheckSessionFailedScript); } IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_JoinSession) { @@ -181,13 +259,10 @@ IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_JoinSession) { content::WebContents* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); ASSERT_TRUE(web_contents); - ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();"); - content::TestNavigationObserver test_navigation_observer(web_contents, 1); - test_navigation_observer.StartWatchingNewWebContents(); - ExecuteJavaScriptAPI(web_contents, "startSession();"); - test_navigation_observer.Wait(); - ChooseSink(web_contents, "id1"); - ExecuteJavaScriptAPI(web_contents, "checkSession();"); + ExecuteJavaScriptAPI(web_contents, kWaitDeviceScript); + StartSession(web_contents); + ChooseSink(web_contents, "id1", ""); + ExecuteJavaScriptAPI(web_contents, kCheckSessionScript); std::string session_id; GetStartedSessionId(web_contents, &session_id); @@ -212,13 +287,11 @@ IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, content::WebContents* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); ASSERT_TRUE(web_contents); - ExecuteJavaScriptAPI(web_contents, "waitUntilDeviceAvailable();"); + ExecuteJavaScriptAPI(web_contents, kWaitDeviceScript); content::TestNavigationObserver test_navigation_observer(web_contents, 1); - test_navigation_observer.StartWatchingNewWebContents(); - ExecuteJavaScriptAPI(web_contents, "startSession();"); - test_navigation_observer.Wait(); - ChooseSink(web_contents, "id1"); - ExecuteJavaScriptAPI(web_contents, "checkSession();"); + StartSession(web_contents); + ChooseSink(web_contents, "id1", ""); + ExecuteJavaScriptAPI(web_contents, kCheckSessionScript); std::string session_id; GetStartedSessionId(web_contents, &session_id); diff --git a/chrome/test/media_router/media_router_integration_browsertest.h b/chrome/test/media_router/media_router_integration_browsertest.h index 30b426f..0968b12 100644 --- a/chrome/test/media_router/media_router_integration_browsertest.h +++ b/chrome/test/media_router/media_router_integration_browsertest.h @@ -9,7 +9,11 @@ #include "base/debug/stack_trace.h" #include "base/files/file_path.h" +#include "chrome/browser/ui/toolbar/media_router_action.h" #include "chrome/test/media_router/media_router_base_browsertest.h" +#include "content/public/test/browser_test_utils.h" +#include "content/public/test/test_navigation_observer.h" + namespace media_router { @@ -21,22 +25,52 @@ class MediaRouterIntegrationBrowserTest : public MediaRouterBaseBrowserTest { ~MediaRouterIntegrationBrowserTest() override; protected: + // InProcessBrowserTest Overrides + void TearDownOnMainThread() override; + // Simulate user action to choose one sink in the popup dialog. // |web_contents|: The web contents of the test page which invokes the popup // dialog. // |sink_id|: The sink id. + // |current_route|: The JSON string which represents current route. void ChooseSink(content::WebContents* web_contents, - const std::string& sink_id); + const std::string& sink_id, const std::string& current_route); // Execute javascript and check the return value. - void ExecuteJavaScriptAPI(content::WebContents* web_contents, + static void ExecuteJavaScriptAPI(content::WebContents* web_contents, const std::string& script); + static int ExecuteScriptAndExtractInt( + const content::ToRenderFrameHost& adapter, + const std::string& script); + + static std::string ExecuteScriptAndExtractString( + const content::ToRenderFrameHost& adapter, const std::string& script); + + // Get the chrome modal dialog. + // |web_contents|: The web contents of the test page which invokes the popup + // dialog. + content::WebContents* GetMRDialog(content::WebContents* web_contents); + void OpenTestPage(base::FilePath::StringPieceType file); void OpenTestPageInNewTab(base::FilePath::StringPieceType file); void SetTestData(base::FilePath::StringPieceType test_data_file); + // Start session and wait until the pop dialog shows up. + // |web_contents|: The web contents of the test page which invokes the popup + // dialog. + void StartSession(content::WebContents* web_contents); + + // Open the chrome modal dialog. + // |web_contents|: The web contents of the test page which invokes the popup + // dialog. + content::WebContents* OpenMRDialog(content::WebContents* web_contents); + + bool IsRouteCreatedOnUI(); + + void WaitUntilRouteCreated(); + private: // Get the full path of the resource file. // |relative_path|: The relative path to @@ -44,6 +78,8 @@ class MediaRouterIntegrationBrowserTest : public MediaRouterBaseBrowserTest { // browser_test_resources/ base::FilePath GetResourceFile( base::FilePath::StringPieceType relative_path) const; + + scoped_ptr<content::TestNavigationObserver> test_navigation_observer_; }; } // namespace media_router diff --git a/chrome/test/media_router/media_router_integration_ui_browsertest.cc b/chrome/test/media_router/media_router_integration_ui_browsertest.cc new file mode 100644 index 0000000..7a2481a --- /dev/null +++ b/chrome/test/media_router/media_router_integration_ui_browsertest.cc @@ -0,0 +1,53 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/test/media_router/media_router_integration_browsertest.h" + +#include "base/files/file_util.h" +#include "base/strings/stringprintf.h" +#include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "content/public/test/browser_test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + + +namespace media_router { + +IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_Dialog_Basic) { + OpenTestPage(FILE_PATH_LITERAL("basic_test.html")); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + content::WebContents* dialog_contents = OpenMRDialog(web_contents); + + // Verify the sink list. + std::string script; + script = base::StringPrintf( + "domAutomationController.send(window.document.getElementById(" + "'media-router-container').sinkList.length)"); + ASSERT_EQ(2, ExecuteScriptAndExtractInt(dialog_contents, script)); + + ChooseSink(web_contents, "id1", ""); + WaitUntilRouteCreated(); + + // Verify the new route. + script = base::StringPrintf( + "domAutomationController.send(window.document.getElementById(" + "'media-router-container').routeList[0].title)"); + std::string route_title = ExecuteScriptAndExtractString( + dialog_contents, script); + ASSERT_EQ("Test Route", route_title); + + script = base::StringPrintf( + "domAutomationController.send(window.document.getElementById(" + "'media-router-container').routeList[0].id)"); + std::string route_id = ExecuteScriptAndExtractString(dialog_contents, script); + std::string current_route = base::StringPrintf( + "{'id': '%s', 'sinkId': '%s', 'title': '%s', 'isLocal': '%s'}", + route_id.c_str(), "id1", route_title.c_str(), "false"); + + ChooseSink(web_contents, "id1", current_route); + // TODO(leilei): Verify the router details dialog, including the title and + // the text, also close the route. +} + +} // namespace media_router |