summaryrefslogtreecommitdiffstats
path: root/mojo/shell/shell_test_base.cc
blob: 7c8a9af213151c36e5e08b4dbb00f1182b435e91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2014 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 "mojo/shell/shell_test_base.h"

#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "net/base/filename_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h"

namespace mojo {
namespace shell {
namespace test {

ShellTestBase::ShellTestBase() {
}

ShellTestBase::~ShellTestBase() {
}

void ShellTestBase::SetUp() {
  shell_context_.Init();
  test_server_.reset(new net::test_server::EmbeddedTestServer());
  ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
  base::FilePath service_dir;
  CHECK(PathService::Get(base::DIR_MODULE, &service_dir));
  test_server_->ServeFilesFromDirectory(service_dir);
}

ScopedMessagePipeHandle ShellTestBase::ConnectToService(
    const GURL& application_url,
    const std::string& service_name) {
  // Set the MojoURLResolver origin to be the same as the base file path for
  // local files. This is primarily for test convenience, so that references
  // to unknown mojo: urls that do not have specific local file or custom
  // mappings registered on the URL resolver are treated as shared libraries.
  base::FilePath service_dir;
  CHECK(PathService::Get(base::DIR_MODULE, &service_dir));
  shell_context_.mojo_url_resolver()->SetBaseURL(
      net::FilePathToFileURL(service_dir));

  return shell_context_.ConnectToServiceByName(
      application_url, service_name).Pass();
}

ScopedMessagePipeHandle ShellTestBase::ConnectToServiceViaNetwork(
    const GURL& application_url,
    const std::string& service_name) {
  shell_context_.mojo_url_resolver()->SetBaseURL(
      test_server_->base_url());

  return shell_context_.ConnectToServiceByName(
      application_url, service_name).Pass();
}

}  // namespace test
}  // namespace shell
}  // namespace mojo