diff options
author | leilei <leilei@chromium.org> | 2015-07-14 00:10:24 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-14 07:10:53 +0000 |
commit | 307e8e6d8baf09fe147f3f47696f50c1449bf4e5 (patch) | |
tree | 601386cca6afaf1c175521a8ea59ac367310c9dd /chrome/test/media_router | |
parent | eb673a3d35e2a00f7d69423e9dbfe5bbd554cda9 (diff) | |
download | chromium_src-307e8e6d8baf09fe147f3f47696f50c1449bf4e5.zip chromium_src-307e8e6d8baf09fe147f3f47696f50c1449bf4e5.tar.gz chromium_src-307e8e6d8baf09fe147f3f47696f50c1449bf4e5.tar.bz2 |
Add two test cases to cover failure scenarios for starting session.
It injects test data before running test, and the test provider in mr
extension consumes the test data set in localstorage.
BUG=
Review URL: https://codereview.chromium.org/1231683002
Cr-Commit-Position: refs/heads/master@{#338651}
Diffstat (limited to 'chrome/test/media_router')
8 files changed, 183 insertions, 7 deletions
diff --git a/chrome/test/media_router/media_router_integration_browsertest.cc b/chrome/test/media_router/media_router_integration_browsertest.cc index 63879f6..9b7d262 100644 --- a/chrome/test/media_router/media_router_integration_browsertest.cc +++ b/chrome/test/media_router/media_router_integration_browsertest.cc @@ -5,7 +5,9 @@ #include "chrome/test/media_router/media_router_integration_browsertest.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 "chrome/browser/profiles/profile.h" @@ -19,6 +21,14 @@ #include "net/base/filename_util.h" #include "testing/gtest/include/gtest/gtest.h" +namespace { +// The path relative to <chromium src>/out/<build config> for media router +// browser test resources. +const base::FilePath::StringPieceType kResourcePath = FILE_PATH_LITERAL( + "media_router/browser_test_resources/"); +} // namespace + + namespace media_router { MediaRouterIntegrationBrowserTest::MediaRouterIntegrationBrowserTest() { @@ -56,12 +66,7 @@ void MediaRouterIntegrationBrowserTest::ExecuteJavaScriptAPI( void MediaRouterIntegrationBrowserTest::OpenTestPage( base::FilePath::StringPieceType file_name) { - base::FilePath base_dir; - ASSERT_TRUE(PathService::Get(base::DIR_EXE, &base_dir)); - base::FilePath full_path = - base_dir.Append(FILE_PATH_LITERAL("media_router/browser_test_resources/")) - .Append(file_name); - ASSERT_TRUE(PathExists(full_path)); + base::FilePath full_path = GetResourceFile(file_name); ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(full_path)); } @@ -79,6 +84,35 @@ void MediaRouterIntegrationBrowserTest::ChooseSink( ASSERT_TRUE(content::ExecuteScript(dialog_contents, script)); } +void MediaRouterIntegrationBrowserTest::SetTestData( + base::FilePath::StringPieceType test_data_file) { + base::FilePath full_path = GetResourceFile(test_data_file); + JSONFileValueDeserializer deserializer(full_path); + int error_code = 0; + std::string error_message; + scoped_ptr<base::Value> value( + deserializer.Deserialize(&error_code, &error_message)); + CHECK(value.get()) << "Deserialize failed: " << error_message; + std::string test_data_str; + ASSERT_TRUE(base::JSONWriter::Write(*value, &test_data_str)); + ExecuteScriptInBackgroundPageNoWait( + extension_id_, + base::StringPrintf("localStorage['testdata'] = '%s'", + test_data_str.c_str())); +} + +base::FilePath MediaRouterIntegrationBrowserTest::GetResourceFile( + base::FilePath::StringPieceType relative_path) const { + base::FilePath base_dir; + // ASSERT_TRUE can only be used in void returning functions. + // Use CHECK instead in non-void returning functions. + CHECK(PathService::Get(base::DIR_EXE, &base_dir)); + base::FilePath full_path = + base_dir.Append(kResourcePath).Append(relative_path); + CHECK(PathExists(full_path)); + return full_path; +} + IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_Basic) { OpenTestPage(FILE_PATH_LITERAL("basic_test.html")); content::WebContents* web_contents = @@ -95,4 +129,36 @@ IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, MANUAL_Basic) { ExecuteJavaScriptAPI(web_contents, "stopSession();"); } +IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, + MANUAL_Fail_No_Provider) { + SetTestData(FILE_PATH_LITERAL("no_provider.json")); + OpenTestPage(FILE_PATH_LITERAL("no_provider.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, "checkSessionFailedToStart();"); +} + +IN_PROC_BROWSER_TEST_F(MediaRouterIntegrationBrowserTest, + MANUAL_Fail_Create_Route) { + SetTestData(FILE_PATH_LITERAL("fail_create_route.json")); + OpenTestPage(FILE_PATH_LITERAL("fail_create_route.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, "checkSessionFailedToStart();"); +} + } // namespace media_router diff --git a/chrome/test/media_router/media_router_integration_browsertest.h b/chrome/test/media_router/media_router_integration_browsertest.h index 23c2a5f..7e2be88 100644 --- a/chrome/test/media_router/media_router_integration_browsertest.h +++ b/chrome/test/media_router/media_router_integration_browsertest.h @@ -33,6 +33,16 @@ class MediaRouterIntegrationBrowserTest : public MediaRouterBaseBrowserTest { const std::string& script); void OpenTestPage(base::FilePath::StringPieceType file); + + void SetTestData(base::FilePath::StringPieceType test_data_file); + + private: + // Get the full path of the resource file. + // |relative_path|: The relative path to + // <chromium src>/out/<build config>/media_router/ + // browser_test_resources/ + base::FilePath GetResourceFile( + base::FilePath::StringPieceType relative_path) const; }; } // namespace media_router diff --git a/chrome/test/media_router/media_router_tests.gypi b/chrome/test/media_router/media_router_tests.gypi index c46a9e8..4589299 100644 --- a/chrome/test/media_router/media_router_tests.gypi +++ b/chrome/test/media_router/media_router_tests.gypi @@ -6,7 +6,11 @@ 'variables': { 'media_router_integration_test_resources': [ 'resources/basic_test.html', - 'resources/common.js' + 'resources/common.js', + 'resources/fail_create_route.html', + 'resources/fail_create_route.json', + 'resources/no_provider.html', + 'resources/no_provider.json', ], }, # end of variables 'targets': [ diff --git a/chrome/test/media_router/resources/README.md b/chrome/test/media_router/resources/README.md new file mode 100644 index 0000000..c03bfd2 --- /dev/null +++ b/chrome/test/media_router/resources/README.md @@ -0,0 +1,27 @@ +The document shows how to create a test data file which is used for the +integration test to inject test data before running test. +---------------------------------------------------------------------------- +The test data is a JSON string and here is full example with description: + +{ + // Define the return value for getAvailableSinks API defined in + // TestProvider.js. + // The return value is a list as following. Default value is: + // [{"id": "id1", "friendlyName": "test-sink-1"}, + // {"id": "id2", "friendlyName": "test-sink-2"}] + "getAvailableSinks": [{"id": "id1", "friendlyName": "test-device-1"}, + {"id": "id2", "friendlyName": "test-device-2"}], + + // Define the return value for canRoute API, the return value should be + // either 'true' or 'false'. The default value is 'true'. + "canRoute": "true", + + // Define the return value for createRoute API. Since the return type of + // createRoute is a Promise, here we just need to define + // if return successful result or error. + // The value for 'passed' should be either 'true' or 'false'. + // If it is 'false', you also need to give the corresponding + // error message. If it is 'true', the error message will be ignored. + // TODO(leilei): Change keyword 'passed' to 'success'. + "createRoute": {"passed": "false", "errorMessage": "Unknown sink"} +} diff --git a/chrome/test/media_router/resources/fail_create_route.html b/chrome/test/media_router/resources/fail_create_route.html new file mode 100644 index 0000000..eb36860 --- /dev/null +++ b/chrome/test/media_router/resources/fail_create_route.html @@ -0,0 +1,31 @@ +<!DOCTYPE HTML> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>MR Integration Basic Test</title> + <link href="https://www.google.com/cast?__testprovider__=true" rel="default-presentation"> + <script type="text/javascript" src="common.js"></script> + <script> + /** + * Checks if the session has NOT been started successfully. + */ + function checkSessionFailedToStart() { + if (!startSessionPromise) { + sendResult(false, 'Failed to start session'); + } else { + startSessionPromise.then( + function (currentSession) { + sendResult(false, 'Session should not be started successfully.'); + }).catch(function(e) { + if (e.message.indexOf('Unknown sink') > -1) { + sendResult(true, ''); + } + sendResult(false, + 'Error message is not correct, it should contain "Unknown sink"'); + }) + } + } + </script> + </head> + <body> + </body> +</html>
\ No newline at end of file diff --git a/chrome/test/media_router/resources/fail_create_route.json b/chrome/test/media_router/resources/fail_create_route.json new file mode 100644 index 0000000..b717be5 --- /dev/null +++ b/chrome/test/media_router/resources/fail_create_route.json @@ -0,0 +1,3 @@ +{ + "createRoute": {"passed": "false", "errorMessage": "Unknown sink"} +}
\ No newline at end of file diff --git a/chrome/test/media_router/resources/no_provider.html b/chrome/test/media_router/resources/no_provider.html new file mode 100644 index 0000000..95621d9 --- /dev/null +++ b/chrome/test/media_router/resources/no_provider.html @@ -0,0 +1,32 @@ +<!DOCTYPE HTML> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>MR Integration Basic Test</title> + <link href="https://www.google.com/cast?__testprovider__=true" rel="default-presentation"> + <script type="text/javascript" src="common.js"></script> + <script> + /** + * Checks if the session has NOT been started successfully. + */ + function checkSessionFailedToStart() { + if (!startSessionPromise) { + sendResult(false, 'Failed to start session'); + } else { + startSessionPromise.then( + function (currentSession) { + sendResult(false, 'Session should not be started successfully.'); + }).catch(function(e) { + if (e.message.indexOf('No provider supports it') > -1) { + sendResult(true, ''); + } + sendResult(false, + 'Error message is not correct, ' + + 'it should contain "No provider supports it"'); + }) + } + } + </script> + </head> + <body> + </body> +</html>
\ No newline at end of file diff --git a/chrome/test/media_router/resources/no_provider.json b/chrome/test/media_router/resources/no_provider.json new file mode 100644 index 0000000..3037b5e --- /dev/null +++ b/chrome/test/media_router/resources/no_provider.json @@ -0,0 +1,3 @@ +{ + "canRoute": "false" +}
\ No newline at end of file |