shell_test_base.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "mojo/shell/shell_test_base.h"
6
7#include "base/command_line.h"
8#include "base/file_util.h"
9#include "base/files/file_path.h"
10#include "base/logging.h"
11#include "base/path_service.h"
12#include "build/build_config.h"
13#include "net/base/filename_util.h"
14#include "net/test/embedded_test_server/embedded_test_server.h"
15#include "url/gurl.h"
16
17namespace mojo {
18namespace shell {
19namespace test {
20
21ShellTestBase::ShellTestBase() {
22}
23
24ShellTestBase::~ShellTestBase() {
25}
26
27void ShellTestBase::SetUp() {
28  shell_context_.Init();
29  test_server_.reset(new net::test_server::EmbeddedTestServer());
30  ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
31  base::FilePath service_dir;
32  CHECK(PathService::Get(base::DIR_MODULE, &service_dir));
33  test_server_->ServeFilesFromDirectory(service_dir);
34}
35
36ScopedMessagePipeHandle ShellTestBase::ConnectToServiceViaNetwork(
37    const GURL& application_url,
38    const std::string& service_name) {
39  shell_context_.mojo_url_resolver()->SetBaseURL(
40      test_server_->base_url());
41
42  return shell_context_.service_manager()->ConnectToServiceByName(
43      application_url, service_name).Pass();
44}
45
46ScopedMessagePipeHandle ShellTestBase::ConnectToService(
47    const GURL& application_url,
48    const std::string& service_name) {
49  // Set the MojoURLResolver origin to be the same as the base file path for
50  // local files. This is primarily for test convenience, so that references
51  // to unknown mojo: urls that do not have specific local file or custom
52  // mappings registered on the URL resolver are treated as shared libraries.
53  base::FilePath service_dir;
54  CHECK(PathService::Get(base::DIR_MODULE, &service_dir));
55  shell_context_.mojo_url_resolver()->SetBaseURL(
56      net::FilePathToFileURL(service_dir));
57
58  return shell_context_.service_manager()->ConnectToServiceByName(
59      application_url, service_name).Pass();
60}
61
62}  // namespace test
63}  // namespace shell
64}  // namespace mojo
65