summaryrefslogtreecommitdiffstats
path: root/mojo/shell/shell_test_helper.h
blob: 53d84a30b76faed2256397fe69ca647c4ed25e22 (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
65
66
// 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.

#ifndef MOJO_SHELL_SHELL_TEST_HELPER_
#define MOJO_SHELL_SHELL_TEST_HELPER_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/threading/thread.h"
#include "mojo/public/cpp/bindings/remote_ptr.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/interfaces/shell/shell.mojom.h"

namespace base {
class MessageLoopProxy;
class RunLoop;
}

namespace mojo {
namespace shell {

// ShellTestHelper is useful for tests to establish a connection to the Shell.
// ShellTestHelper does this by spawning a thread and connecting. Invoke Init()
// to do this. Once done, shell() returns the handle to the Shell.
class ShellTestHelper {
 public:
  struct State;

  ShellTestHelper();
  ~ShellTestHelper();

  void Init();

  // Returns a handle to the Shell. ShellTestHelper owns the shell.
  Shell* shell() { return shell_.get(); }

 private:
  class TestShellClient;

  // Invoked once connection has been established.
  void OnShellStarted();

  Environment environment_;

  base::Thread shell_thread_;

  // If non-null we're in Init() and waiting for connection.
  scoped_ptr<base::RunLoop> run_loop_;

  // See comment in declaration for details.
  State* state_;

  // Client interface for the shell.
  scoped_ptr<TestShellClient> shell_client_;

  RemotePtr<Shell> shell_;

  DISALLOW_COPY_AND_ASSIGN(ShellTestHelper);
};

}  // namespace shell
}  // namespace mojo

#endif  // MOJO_SHELL_SHELL_TEST_HELPER_