From ba38872349d70f41e55d028f7641115b13a4fda6 Mon Sep 17 00:00:00 2001 From: Long Xiang Date: Mon, 16 Sep 2013 22:41:57 +0800 Subject: [PATCH] Add main document browser test --- application/common/manifest_unittest.cc | 4 +- .../application_main_document_browsertest.cc | 89 +++++++++++++++++++ application/test/data/main_document/main.html | 5 ++ application/test/data/main_document/main.js | 4 + .../test/data/main_document/manifest.json | 10 +++ xwalk_tests.gypi | 1 + 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 application/test/application_main_document_browsertest.cc create mode 100644 application/test/data/main_document/main.html create mode 100644 application/test/data/main_document/main.js create mode 100644 application/test/data/main_document/manifest.json diff --git a/application/common/manifest_unittest.cc b/application/common/manifest_unittest.cc index 04637498f9..f4f833507b 100644 --- a/application/common/manifest_unittest.cc +++ b/application/common/manifest_unittest.cc @@ -100,10 +100,10 @@ TEST_F(ManifestTest, ApplicationTypes) { // Platform app. MutateManifest( - &manifest, keys::kPlatformAppBackgroundKey, new base::DictionaryValue()); + &manifest, keys::kAppMainKey, new base::DictionaryValue()); AssertType(manifest.get(), Manifest::TYPE_PACKAGED_APP); MutateManifest( - &manifest, keys::kPlatformAppBackgroundKey, NULL); + &manifest, keys::kAppMainKey, NULL); // Hosted app. MutateManifest( diff --git a/application/test/application_main_document_browsertest.cc b/application/test/application_main_document_browsertest.cc new file mode 100644 index 0000000000..1fd5f4f9de --- /dev/null +++ b/application/test/application_main_document_browsertest.cc @@ -0,0 +1,89 @@ +// Copyright (c) 2013 Intel Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/command_line.h" +#include "base/files/file_path.h" +#include "base/path_service.h" +#include "base/strings/utf_string_conversions.h" +#include "content/public/browser/web_contents.h" +#include "content/public/test/browser_test_utils.h" +#include "content/public/test/test_utils.h" +#include "net/base/net_util.h" +#include "xwalk/application/browser/application_system.h" +#include "xwalk/application/common/application.h" +#include "xwalk/application/common/constants.h" +#include "xwalk/runtime/browser/runtime.h" +#include "xwalk/runtime/browser/runtime_registry.h" +#include "xwalk/runtime/common/xwalk_notification_types.h" +#include "xwalk/test/base/in_process_browser_test.h" + +namespace { + +bool WaitForRuntimeCountCallback(int* count) { + --(*count); + return *count == 0; +} + +} // namespace + +using xwalk::application::Application; +using xwalk::Runtime; +using xwalk::RuntimeRegistry; + +class ApplicationMainDocumentBrowserTest: public InProcessBrowserTest { + public: + virtual void SetUp() OVERRIDE; + virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; + + base::FilePath test_data_dir_; +}; + +void ApplicationMainDocumentBrowserTest::SetUp() { + PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_); + test_data_dir_ = test_data_dir_ + .Append(FILE_PATH_LITERAL("xwalk")) + .Append(FILE_PATH_LITERAL("application")) + .Append(FILE_PATH_LITERAL("test")) + .Append(FILE_PATH_LITERAL("data")); + InProcessBrowserTest::SetUp(); +} + +void ApplicationMainDocumentBrowserTest::SetUpCommandLine( + CommandLine* command_line) { + GURL url = net::FilePathToFileURL(test_data_dir_.Append( + FILE_PATH_LITERAL("main_document"))); + command_line->AppendArg(url.spec()); +} + +// Verifies the runtime creation when main document is used. +IN_PROC_BROWSER_TEST_F(ApplicationMainDocumentBrowserTest, MainDocument) { + content::RunAllPendingInMessageLoop(); + const xwalk::RuntimeList& runtimes = RuntimeRegistry::Get()->runtimes(); + // At least the main document's runtime exist after launch. + ASSERT_GE(runtimes.size(), 1); + + Runtime* main_runtime = runtimes[0]; + xwalk::RuntimeContext* runtime_context = main_runtime->runtime_context(); + xwalk::application::ApplicationService* service = + runtime_context->GetApplicationSystem()->application_service(); + const Application* app = service->GetRunningApplication(); + GURL generated_url = + app->GetResourceURL(xwalk::application::kGeneratedMainDocumentFilename); + // Check main document URL. + ASSERT_EQ(main_runtime->web_contents()->GetURL(), generated_url); + ASSERT_TRUE(!main_runtime->window()); + + // There should exist 2 runtimes(one for generated main document, one for the + // window created by main document). As WindowedNotificationObserver::Wait() + // will call the callback function once when inovke, so add one more for the + // Wait() to consume. + int count = 2 - runtimes.size() + 1; + if (count > 1) { + content::WindowedNotificationObserver( + xwalk::NOTIFICATION_RUNTIME_OPENED, + base::Bind(&WaitForRuntimeCountCallback, &count)).Wait(); + } + + ASSERT_EQ(2, RuntimeRegistry::Get()->runtimes().size()); +} diff --git a/application/test/data/main_document/main.html b/application/test/data/main_document/main.html new file mode 100644 index 0000000000..9b4ef8ad5f --- /dev/null +++ b/application/test/data/main_document/main.html @@ -0,0 +1,5 @@ + + +

hello

+ + diff --git a/application/test/data/main_document/main.js b/application/test/data/main_document/main.js new file mode 100644 index 0000000000..1c81d3ba5d --- /dev/null +++ b/application/test/data/main_document/main.js @@ -0,0 +1,4 @@ +setTimeout( + function(){ + window.open("main.html"); + }, 100); diff --git a/application/test/data/main_document/manifest.json b/application/test/data/main_document/manifest.json new file mode 100644 index 0000000000..12d55c995c --- /dev/null +++ b/application/test/data/main_document/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "main_document_test", + "manifest_version": 1, + "version": "1.0", + "app": { + "main": { + "scripts": ["main.js"] + } + } +} diff --git a/xwalk_tests.gypi b/xwalk_tests.gypi index e04b1ca1ba..92fe2943a8 100644 --- a/xwalk_tests.gypi +++ b/xwalk_tests.gypi @@ -107,6 +107,7 @@ 'HAS_OUT_OF_PROC_TEST_RUNNER', ], 'sources': [ + 'application/test/application_main_document_browsertest.cc', 'runtime/browser/xwalk_download_browsertest.cc', 'runtime/browser/xwalk_form_input_browsertest.cc', 'runtime/browser/xwalk_runtime_browsertest.cc',