146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// found in the LICENSE file.
446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "mojo/shell/shell_test_base.h"
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/command_line.h"
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/files/file_path.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/logging.h"
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/path_service.h"
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "build/build_config.h"
13f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "net/base/filename_util.h"
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "net/test/embedded_test_server/embedded_test_server.h"
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "url/gurl.h"
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace mojo {
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace shell {
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace test {
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)ShellTestBase::ShellTestBase() {
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)ShellTestBase::~ShellTestBase() {
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ShellTestBase::SetUp() {
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  shell_context_.Init();
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  test_server_.reset(new net::test_server::EmbeddedTestServer());
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  base::FilePath service_dir;
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  CHECK(PathService::Get(base::DIR_MODULE, &service_dir));
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  test_server_->ServeFilesFromDirectory(service_dir);
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
36116680a4aac90f2aa7413d9095a592090648e557Ben MurdochScopedMessagePipeHandle ShellTestBase::ConnectToService(
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const GURL& application_url,
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& service_name) {
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Set the MojoURLResolver origin to be the same as the base file path for
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // local files. This is primarily for test convenience, so that references
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // to unknown mojo: urls that do not have specific local file or custom
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // mappings registered on the URL resolver are treated as shared libraries.
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::FilePath service_dir;
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  CHECK(PathService::Get(base::DIR_MODULE, &service_dir));
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  shell_context_.mojo_url_resolver()->SetBaseURL(
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      net::FilePathToFileURL(service_dir));
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return shell_context_.ConnectToServiceByName(
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      application_url, service_name).Pass();
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciScopedMessagePipeHandle ShellTestBase::ConnectToServiceViaNetwork(
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const GURL& application_url,
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& service_name) {
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  shell_context_.mojo_url_resolver()->SetBaseURL(
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      test_server_->base_url());
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return shell_context_.ConnectToServiceByName(
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      application_url, service_name).Pass();
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
6246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}  // namespace test
6346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}  // namespace shell
6446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}  // namespace mojo
65