summaryrefslogtreecommitdiffstats
path: root/mojo/shell/tests/application_manager_apptest.cc
blob: 9af94a1c302472fff2ec903230a1ee954d604145 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2015 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 <stddef.h>
#include <stdint.h>

#include <utility>

#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/process/process_handle.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/shell/public/cpp/application_test_base.h"
#include "mojo/shell/public/cpp/interface_factory.h"
#include "mojo/shell/public/cpp/shell.h"
#include "mojo/shell/public/interfaces/application_manager.mojom.h"
#include "mojo/shell/tests/application_manager_apptests.mojom.h"

using mojo::shell::test::mojom::CreateInstanceForHandleTest;

namespace mojo {
namespace shell {
namespace {

class ApplicationManagerAppTestDelegate
    : public ShellClient,
      public InterfaceFactory<CreateInstanceForHandleTest>,
      public CreateInstanceForHandleTest {
 public:
  ApplicationManagerAppTestDelegate()
      : target_id_(mojom::Shell::kInvalidApplicationID),
        binding_(this) {}
  ~ApplicationManagerAppTestDelegate() override {}

  uint32_t target_id() const { return target_id_; }

 private:
  // mojo::ShellClient:
  void Initialize(Shell* shell, const std::string& url, uint32_t id,
                  uint32_t user_id) override {}
  bool AcceptConnection(Connection* connection) override {
    connection->AddInterface<CreateInstanceForHandleTest>(this);
    return true;
  }

  // InterfaceFactory<CreateInstanceForHandleTest>:
  void Create(Connection* connection,
              InterfaceRequest<CreateInstanceForHandleTest> request) override {
    binding_.Bind(std::move(request));
  }

  // CreateInstanceForHandleTest:
  void SetTargetID(uint32_t target_id) override {
    target_id_ = target_id;
    base::MessageLoop::current()->QuitWhenIdle();
  }

  uint32_t target_id_;

  Binding<CreateInstanceForHandleTest> binding_;

  DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTestDelegate);
};

}  // namespace

class ApplicationManagerAppTest : public mojo::test::ApplicationTestBase,
                                  public mojom::ApplicationManagerListener {
 public:
  ApplicationManagerAppTest() : delegate_(nullptr), binding_(this) {}
  ~ApplicationManagerAppTest() override {}

  void OnDriverQuit() {
    base::MessageLoop::current()->QuitNow();
  }

 protected:
  struct ApplicationInfo {
    ApplicationInfo(uint32_t id, const std::string& url)
        : id(id), url(url), pid(base::kNullProcessId) {}

    uint32_t id;
    std::string url;
    base::ProcessId pid;
  };

  void AddListenerAndWaitForApplications() {
    mojom::ApplicationManagerPtr application_manager;
    shell()->ConnectToInterface("mojo:shell", &application_manager);

    application_manager->AddListener(binding_.CreateInterfacePtrAndBind());
    binding_.WaitForIncomingMethodCall();
  }

  bool ContainsApplicationWithURL(const std::string& url) const {
    for (const auto& application : initial_applications_) {
      if (application.url == url)
        return true;
    }
    for (const auto& application : applications_) {
      if (application.url == url)
        return true;
    }
    return false;
  }

  uint32_t target_id() const {
    DCHECK(delegate_);
    return delegate_->target_id();
  }

  const std::vector<ApplicationInfo>& applications() const {
    return applications_;
  }

  ApplicationManagerAppTestDelegate* delegate() { return delegate_; }

 private:
  // test::ApplicationTestBase:
  ShellClient* GetShellClient() override {
    delegate_ = new ApplicationManagerAppTestDelegate;
    return delegate_;
  }

  // mojom::ApplicationManagerListener:
  void SetRunningApplications(
      Array<mojom::ApplicationInfoPtr> applications) override {
    for (size_t i = 0; i < applications.size(); ++i) {
      initial_applications_.push_back(ApplicationInfo(applications[i]->id,
                                                      applications[i]->url));
    }
  }
  void ApplicationInstanceCreated(
      mojom::ApplicationInfoPtr application) override {
    applications_.push_back(ApplicationInfo(application->id, application->url));
  }
  void ApplicationInstanceDestroyed(uint32_t id) override {
    for (auto it = applications_.begin(); it != applications_.end(); ++it) {
      auto& application = *it;
      if (application.id == id) {
        applications_.erase(it);
        break;
      }
    }
  }
  void ApplicationPIDAvailable(uint32_t id, uint32_t pid) override {
    for (auto& application : applications_) {
      if (application.id == id) {
        application.pid = pid;
        break;
      }
    }
  }

  ApplicationManagerAppTestDelegate* delegate_;
  Binding<mojom::ApplicationManagerListener> binding_;
  std::vector<ApplicationInfo> applications_;
  std::vector<ApplicationInfo> initial_applications_;

  DISALLOW_COPY_AND_ASSIGN(ApplicationManagerAppTest);
};

TEST_F(ApplicationManagerAppTest, CreateInstanceForHandle) {
  AddListenerAndWaitForApplications();

  // 1. Launch a process. (Actually, have the runner launch a process that
  //    launches a process. #becauselinkerrors).
  mojo::shell::test::mojom::DriverPtr driver;
  scoped_ptr<Connection> connection =
      shell()->Connect("exe:application_manager_apptest_driver");
  connection->GetInterface(&driver);

  // 2. Wait for the target to connect to us. (via
  //    mojo:application_manager_apptests)
  base::MessageLoop::current()->Run();

  uint32_t remote_id = mojom::Shell::kInvalidApplicationID;
  EXPECT_TRUE(connection->GetRemoteApplicationID(&remote_id));
  EXPECT_NE(mojom::Shell::kInvalidApplicationID, remote_id);

  // 3. Validate that this test suite's URL was received from the application
  //    manager.
  EXPECT_TRUE(ContainsApplicationWithURL("mojo://mojo_shell_apptests/"));

  // 4. Validate that the right applications/processes were created.
  //    Note that the target process will be created even if the tests are
  //    run with --single-process.
  EXPECT_EQ(2u, applications().size());
  {
    auto& application = applications().front();
    EXPECT_EQ(remote_id, application.id);
    EXPECT_EQ("exe://application_manager_apptest_driver/", application.url);
    EXPECT_NE(base::kNullProcessId, application.pid);
  }
  {
    auto& application = applications().back();
    // We learn about the target process id via a ping from it.
    EXPECT_EQ(target_id(), application.id);
    EXPECT_EQ("exe://application_manager_apptest_target/", application.url);
    EXPECT_NE(base::kNullProcessId, application.pid);
  }

  driver.set_connection_error_handler(
      base::Bind(&ApplicationManagerAppTest::OnDriverQuit,
                 base::Unretained(this)));
  driver->QuitDriver();
  base::MessageLoop::current()->Run();
}

}  // namespace shell
}  // namespace mojo