blob: 4a2cc7872c249f7054d0b10db49c3e4e4cacdc10 [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 <fuchsia/modular/cpp/fidl.h>
#include <lib/component/cpp/connect.h>
#include <lib/component/cpp/startup_context.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/binding_set.h>
#include <lib/fxl/logging.h>
#include <lib/fxl/macros.h>
#include "peridot/lib/testing/component_base.h"
#include "peridot/lib/testing/reporting.h"
#include "peridot/lib/testing/testing.h"
#include "peridot/tests/common/defs.h"
#include "peridot/tests/suggestion/defs.h"
using modular::testing::Await;
using modular::testing::Signal;
using modular::testing::TestPoint;
namespace {
// Cf. README.md for what this test does and how.
class TestApp
: fuchsia::modular::NextListener,
public modular::testing::ComponentBase<void> {
public:
TestApp(component::StartupContext* const startup_context)
: ComponentBase(startup_context) {
TestInit(__FILE__);
user_shell_context_ =
startup_context
->ConnectToEnvironmentService<fuchsia::modular::UserShellContext>();
user_shell_context_->GetStoryProvider(story_provider_.NewRequest());
user_shell_context_->GetSuggestionProvider(
suggestion_provider_.NewRequest());
suggestion_provider_->SubscribeToNext(
suggestion_listener_bindings_.AddBinding(this),
20 /* arbitrarily chosen */);
CreateStory();
}
~TestApp() override = default;
private:
void CreateStory() {
story_provider_->CreateStory(
kSuggestionTestModule,
[this](const fidl::StringPtr& story_id) { StartStoryById(story_id); });
Await(kSuggestionTestModuleDone, [this] {
story_controller_->Stop([this] {
story_controller_.Unbind();
Signal(modular::testing::kTestShutdown);
});
});
}
void StartStoryById(const fidl::StringPtr& story_id) {
story_provider_->GetController(story_id, story_controller_.NewRequest());
story_controller_.set_error_handler([this, story_id] {
FXL_LOG(ERROR) << "Story controller for story " << story_id
<< " died. Does this story exist?";
});
story_controller_->Start(view_owner_.NewRequest());
}
TestPoint received_suggestion_{"SuggestionTestUserShell received suggestion"};
// |fuchsia::modular::NextListener|
void OnNextResults(
fidl::VectorPtr<fuchsia::modular::Suggestion> suggestions) override {
for (auto& suggestion : *suggestions) {
auto& display = suggestion.display;
if (display.headline == "foo" && display.subheadline == "bar" &&
display.details == "baz") {
modular::testing::GetStore()->Put("suggestion_proposal_received", "",
[] {});
received_suggestion_.Pass();
fuchsia::modular::Interaction interaction;
interaction.type = fuchsia::modular::InteractionType::SELECTED;
suggestion_provider_->NotifyInteraction(suggestion.uuid, interaction);
break;
}
}
}
// |fuchsia::modular::NextListener|
void OnProcessingChange(bool processing) override {}
fuchsia::ui::viewsv1token::ViewOwnerPtr view_owner_;
fuchsia::modular::UserShellContextPtr user_shell_context_;
fuchsia::modular::StoryProviderPtr story_provider_;
fuchsia::modular::StoryControllerPtr story_controller_;
fuchsia::modular::SuggestionProviderPtr suggestion_provider_;
fidl::BindingSet<fuchsia::modular::NextListener>
suggestion_listener_bindings_;
FXL_DISALLOW_COPY_AND_ASSIGN(TestApp);
};
} // namespace
int main(int /*argc*/, const char** /*argv*/) {
modular::testing::ComponentMain<TestApp>();
return 0;
}