[modular][tests] Migrate component_context tests to //src/modular/tests

The first of the three test cases covered by the current component_context
tests is covered by the queue_persistence test.
component_context_test.cc covers the other two test cases

TEST=fx run-test modular_integration_tests -t trigger_test --
--gtest_repeat=200 --gtest_break_on_failure=1
MF-386 #done

Change-Id: I8279cc25b74a3bc51dab77b61affed1a4cbe7a29
diff --git a/src/modular/tests/BUILD.gn b/src/modular/tests/BUILD.gn
index 7f3fc7f..d487e2e 100644
--- a/src/modular/tests/BUILD.gn
+++ b/src/modular/tests/BUILD.gn
@@ -321,6 +321,27 @@
   ]
 }
 
+executable("component_context_test") {
+  testonly = true
+
+  sources = [
+    "component_context_test.cc",
+  ]
+
+  deps = [
+    "//peridot/lib/testing:session_shell_impl",
+    "//peridot/public/lib/modular_test_harness/cpp:test_harness_fixture",
+    "//sdk/fidl/fuchsia.modular.testing",
+    "//sdk/fidl/fuchsia.sys",
+    "//sdk/fidl/fuchsia.ui.views",
+    "//sdk/lib/sys/cpp",
+    "//sdk/lib/sys/cpp/testing:integration",
+    "//sdk/lib/ui/scenic/cpp",
+    "//third_party/googletest:gtest_main",
+    "//zircon/public/lib/fs",
+  ]
+}
+
 test_package("modular_integration_tests") {
   tests = [
     {
@@ -379,11 +400,16 @@
       name = "login_override_test"
       environments = basic_envs
     },
+    {
+      name = "component_context_test"
+      environments = basic_envs
+    },
   ]
 
   deps = [
     ":agent_services_test",
     ":clipboard_test",
+    ":component_context_test",
     ":intents_test",
     ":last_focus_time_test",
     ":login_override_test",
diff --git a/src/modular/tests/component_context_test.cc b/src/modular/tests/component_context_test.cc
new file mode 100644
index 0000000..034825f
--- /dev/null
+++ b/src/modular/tests/component_context_test.cc
@@ -0,0 +1,77 @@
+// Copyright 2019 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 <fuchsia/modular/testing/cpp/fidl.h>
+#include <lib/message_queue/cpp/message_sender_client.h>
+#include <lib/modular_test_harness/cpp/fake_agent.h>
+#include <lib/modular_test_harness/cpp/fake_module.h>
+#include <lib/modular_test_harness/cpp/test_harness_fixture.h>
+#include <sdk/lib/sys/cpp/component_context.h>
+#include <sdk/lib/sys/cpp/service_directory.h>
+#include <src/lib/fxl/logging.h>
+
+namespace {
+
+class ComponentContextTest : public modular::testing::TestHarnessFixture {};
+
+// Tests that an agent is able to start another agent through
+// fuchsia::modular::ComponentContext.ConnectToAgent(). Asserts that closing
+// fuchsia::modular::AgentController triggers the agent to stop.
+TEST_F(ComponentContextTest, AgentStartsSecondAgent) {
+  modular::testing::TestHarnessBuilder builder;
+
+  modular::testing::FakeModule fake_module;
+  const auto fake_module_url = modular::testing::GenerateFakeUrl();
+  builder.InterceptComponent(
+      fake_module.GetOnCreateHandler(),
+      {.url = fake_module_url,
+       .sandbox_services = modular::testing::FakeModule::GetSandboxServices()});
+
+  modular::testing::FakeAgent first_fake_agent;
+  const auto first_fake_agent_url = modular::testing::GenerateFakeUrl();
+  builder.InterceptComponent(
+      first_fake_agent.GetOnCreateHandler(),
+      {.url = first_fake_agent_url,
+       .sandbox_services = modular::testing::FakeAgent::GetSandboxServices()});
+
+  modular::testing::FakeAgent second_fake_agent;
+  const auto second_fake_agent_url = modular::testing::GenerateFakeUrl();
+  builder.InterceptComponent(
+      second_fake_agent.GetOnCreateHandler(),
+      {.url = second_fake_agent_url,
+       .sandbox_services = modular::testing::FakeAgent::GetSandboxServices()});
+
+  builder.BuildAndRun(test_harness());
+
+  // Add the test mod.
+  fuchsia::modular::Intent intent;
+  intent.handler = fake_module_url;
+  modular::testing::AddModToStory(test_harness(), "story_name", "mod_name", std::move(intent));
+  RunLoopUntil([&] { return fake_module.is_running(); });
+
+  // Connect to the first fake agent from the fake mod.
+  fuchsia::sys::ServiceProviderPtr first_agent_services;
+  fuchsia::modular::AgentControllerPtr first_agent_controller;
+  fake_module.modular_component_context()->ConnectToAgent(
+      first_fake_agent_url, first_agent_services.NewRequest(), first_agent_controller.NewRequest());
+  RunLoopUntil([&] { return first_fake_agent.is_running(); });
+
+  // Connect to the second fake agent from the first fake agent.
+  fuchsia::sys::ServiceProviderPtr second_agent_services;
+  fuchsia::modular::AgentControllerPtr second_agent_controller;
+  first_fake_agent.modular_component_context()->ConnectToAgent(
+      second_fake_agent_url, second_agent_services.NewRequest(),
+      second_agent_controller.NewRequest());
+  RunLoopUntil([&] { return second_fake_agent.is_running(); });
+
+  // Killing the agent controller should stop the agent.
+  second_agent_controller.Unbind();
+  RunLoopUntil([&] { return !second_fake_agent.is_running(); });
+
+  first_agent_controller.Unbind();
+  RunLoopUntil([&] { return !first_fake_agent.is_running(); });
+}
+
+}  // namespace
diff --git a/src/modular/tests/meta/component_context_test.cmx b/src/modular/tests/meta/component_context_test.cmx
new file mode 100644
index 0000000..2abd647
--- /dev/null
+++ b/src/modular/tests/meta/component_context_test.cmx
@@ -0,0 +1,11 @@
+{
+    "program": {
+        "binary": "test/component_context_test"
+    },
+    "sandbox": {
+        "services": [
+            "fuchsia.sys.Launcher",
+            "fuchsia.sys.Environment"
+        ]
+    }
+}