blob: 29e1f9b64da6e498998fa92c3cc7ed5a3ad8888c [file] [log] [blame]
// Copyright 2017 The Fuchsia 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 "lib/app/cpp/connect.h"
#include "lib/app_driver/cpp/module_driver.h"
#include "lib/component/fidl/component_context.fidl.h"
#include "lib/fsl/tasks/message_loop.h"
#include "lib/module/fidl/module.fidl.h"
#include "peridot/lib/testing/reporting.h"
#include "peridot/lib/testing/testing.h"
#include "peridot/lib/util/weak_callback.h"
#include "peridot/tests/triggers/trigger_test_agent_interface.fidl.h"
using modular::testing::TestPoint;
namespace {
// This is how long we wait for the test to finish before we timeout and tear
// down our test.
constexpr int kTimeoutMilliseconds = 10000;
constexpr char kTestAgent[] =
"file:///system/apps/modular_tests/trigger_test_agent";
class ParentApp {
public:
ParentApp(
modular::ModuleHost* module_host,
fidl::InterfaceRequest<mozart::ViewProvider> /*view_provider_request*/,
fidl::InterfaceRequest<app::ServiceProvider> /*outgoing_services*/)
: module_host_(module_host), weak_ptr_factory_(this) {
modular::testing::Init(module_host->application_context(), __FILE__);
initialized_.Pass();
// Exercise ComponentContext.ConnectToAgent()
module_host_->module_context()->GetComponentContext(
component_context_.NewRequest());
app::ServiceProviderPtr agent_services;
component_context_->ConnectToAgent(kTestAgent, agent_services.NewRequest(),
agent_controller_.NewRequest());
ConnectToService(agent_services.get(),
trigger_agent_interface_.NewRequest());
modular::testing::GetStore()->Get(
"trigger_test_agent_connected", [this](const fidl::String&) {
agent_connected_.Pass();
trigger_agent_interface_->GetMessageQueueToken(
[this](const fidl::String& token) {
received_trigger_token_.Pass();
// Stop the agent.
agent_controller_.reset();
modular::testing::GetStore()->Get(
"trigger_test_agent_stopped",
[this, token](const fidl::String&) {
agent_stopped_.Pass();
// Send a message to the stopped agent which should
// trigger it.
modular::MessageSenderPtr message_sender;
component_context_->GetMessageSender(
token, message_sender.NewRequest());
message_sender->Send("Time to wake up...");
modular::testing::GetStore()->Get(
"trigger_test_agent_run_task",
[this](const fidl::String&) {
task_triggered_.Pass();
modular::testing::GetStore()->Get(
"trigger_test_agent_stopped",
[this](const fidl::String&) {
module_host_->module_context()->Done();
});
});
});
});
});
// Start a timer to quit in case another test component misbehaves and we
// time out.
fsl::MessageLoop::GetCurrent()->task_runner()->PostDelayedTask(
modular::WeakCallback(
weak_ptr_factory_.GetWeakPtr(),
[this] { module_host_->module_context()->Done(); }),
fxl::TimeDelta::FromMilliseconds(kTimeoutMilliseconds));
}
// Called by ModuleDriver.
void Terminate(const std::function<void()>& done) {
stopped_.Pass();
modular::testing::Done(done);
}
private:
modular::ModuleHost* module_host_;
modular::AgentControllerPtr agent_controller_;
modular::testing::TriggerAgentInterfacePtr trigger_agent_interface_;
modular::ComponentContextPtr component_context_;
modular::MessageQueuePtr msg_queue_;
TestPoint initialized_{"Root module initialized"};
TestPoint received_trigger_token_{"Received trigger token"};
TestPoint stopped_{"Root module stopped"};
TestPoint agent_connected_{"Agent accepted connection"};
TestPoint agent_stopped_{"Agent1 stopped"};
TestPoint task_triggered_{"Agent task triggered"};
fxl::WeakPtrFactory<ParentApp> weak_ptr_factory_;
};
} // namespace
int main(int /*argc*/, const char** /*argv*/) {
fsl::MessageLoop loop;
auto app_context = app::ApplicationContext::CreateFromStartupInfo();
modular::ModuleDriver<ParentApp> driver(app_context.get(),
[&loop] { loop.QuitNow(); });
loop.Run();
return 0;
}