blob: 521db7b18c1dec98a501b0b45562e9069e21d8ba [file] [log] [blame]
// Copyright 2018 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 <fuchsia/modular/cpp/fidl.h>
#include <lib/app_driver/cpp/module_driver.h>
#include <lib/async-loop/cpp/loop.h>
#include <lib/async/cpp/task.h>
#include <lib/async/default.h>
#include <lib/component/cpp/connect.h>
#include <lib/fsl/vmo/strings.h>
#include <lib/fxl/functional/make_copyable.h>
#include <test/peridot/tests/trigger/cpp/fidl.h>
#include "peridot/lib/rapidjson/rapidjson.h"
#include "peridot/public/lib/integration_testing/cpp/reporting.h"
#include "peridot/public/lib/integration_testing/cpp/testing.h"
#include "peridot/tests/module_context/defs.h"
using ::modular::testing::Await;
using ::modular::testing::Signal;
using ::modular::testing::TestPoint;
using ::test::peridot::tests::trigger::TriggerTestServicePtr;
namespace {
// Cf. README.md for what this test does and how.
class TestApp {
public:
TestPoint initialized_{"Entity module initialized"};
TestPoint created_entity_{"Created entity"};
TestPoint entity_data_correct_{"Entity data correct"};
TestPoint entity_data_correct_after_resolution_{
"Entity data correct after entity resolution"};
TestApp(modular::ModuleHost* const module_host,
fidl::InterfaceRequest<
fuchsia::ui::app::ViewProvider> /*view_provider_request*/)
: module_context_(module_host->module_context()) {
modular::testing::Init(module_host->startup_context(), __FILE__);
initialized_.Pass();
const char kTestString[] = "test";
const char kTestType[] = "com.fuchsia.test";
fsl::SizedVmo vmo;
fsl::VmoFromString(kTestString, &vmo);
module_context_->CreateEntity(
kTestType, std::move(vmo).ToTransport(), entity_.NewRequest(),
fxl::MakeCopyable([this](fidl::StringPtr entity_reference) mutable {
if (!entity_reference || entity_reference->empty()) {
modular::testing::Fail("Failed to create entity.");
return;
} else {
created_entity_.Pass();
}
}));
// Fetch the data and verify that it matches the data used to create the
// entity.
entity_->GetData(
kTestType,
fxl::MakeCopyable([this, kTestString](fuchsia::mem::BufferPtr data) {
if (data) {
std::string data_string;
fsl::StringFromVmo(*data, &data_string);
if (data_string == kTestString) {
entity_data_correct_.Pass();
}
}
Signal(kEntityModuleDoneFirstTask);
}));
// Fetch the reference from the entity to verify the round-trip
// resolution.
entity_->GetReference(fxl::MakeCopyable(
[this, kTestString, kTestType](fidl::StringPtr entity_reference) {
// Grab the entity resolver from the component context.
fuchsia::modular::EntityResolverPtr entity_resolver;
fuchsia::modular::ComponentContextPtr component_context;
module_context_->GetComponentContext(component_context.NewRequest());
component_context->GetEntityResolver(entity_resolver.NewRequest());
// Resolve the entity and verify the data is correct.
fuchsia::modular::EntityPtr resolved_entity;
entity_resolver->ResolveEntity(entity_reference,
resolved_entity.NewRequest());
resolved_entity->GetData(
kTestType,
fxl::MakeCopyable([this, kTestString,
resolved_entity = std::move(resolved_entity)](
fuchsia::mem::BufferPtr data) {
if (data) {
std::string data_string;
fsl::StringFromVmo(*data, &data_string);
if (data_string == kTestString) {
entity_data_correct_after_resolution_.Pass();
}
}
Signal(kEntityModuleDoneSecondTask);
}));
}));
}
TestApp(modular::ModuleHost* const module_host,
fidl::InterfaceRequest<
fuchsia::ui::viewsv1::ViewProvider> /*view_provider_request*/)
: TestApp(
module_host,
fidl::InterfaceRequest<fuchsia::ui::app::ViewProvider>(nullptr)) {}
TestPoint stopped_{"Entity module stopped"};
void Terminate(const std::function<void()>& done) {
stopped_.Pass();
modular::testing::Done(done);
}
private:
fuchsia::modular::EntityPtr entity_;
std::string module_name_ = "";
fuchsia::modular::OngoingActivityPtr ongoing_activity_;
fuchsia::modular::ModuleContext* module_context_;
FXL_DISALLOW_COPY_AND_ASSIGN(TestApp);
};
} // namespace
int main(int /*argc*/, const char** /*argv*/) {
async::Loop loop(&kAsyncLoopConfigAttachToThread);
auto context = component::StartupContext::CreateFromStartupInfo();
modular::ModuleDriver<TestApp> driver(context.get(),
[&loop] { loop.Quit(); });
loop.Run();
return 0;
}