[modular] Allow optional componentContextProxy as an argument to modular.connectToAgentService
so that tests may use the SDK.
Additionally, fixed punctuation in agent_service_connection.dart to be consistent, and added
a small integration tests for the agent_service_connection.

Change-Id: I30afacd6401f7376d56f4ed6d2eeaa7b337370f9
diff --git a/public/dart/fuchsia_modular/BUILD.gn b/public/dart/fuchsia_modular/BUILD.gn
index b4c20d6..b7a4f3a 100644
--- a/public/dart/fuchsia_modular/BUILD.gn
+++ b/public/dart/fuchsia_modular/BUILD.gn
@@ -101,6 +101,8 @@
 #  fx run -kN  (tip: to terminate the emulator press "ctrl+a x")
 # Run integration tests:
 #  fx shell runtests pkgfs/packages/fuchsia_modular_package_integration_tests/0/test
+# Alternatively, run:
+#  fx run-test fuchsia_modular_package_integration_tests
 dart_fuchsia_test("fuchsia_modular_package_integration_tests") {
   meta = [
     {
@@ -115,6 +117,7 @@
     "lifecycle/internal/lifecycle_impl_test.dart",
     "module/internal/module_impl_integ_test.dart",
     "proposal/proposal_integ_test.dart",
+    "service_connection/agent_service_connection_integ_test.dart",
   ]
 
   deps = [
diff --git a/public/dart/fuchsia_modular/lib/src/service_connection/agent_service_connection.dart b/public/dart/fuchsia_modular/lib/src/service_connection/agent_service_connection.dart
index 7b0cd1a..fb25696 100644
--- a/public/dart/fuchsia_modular/lib/src/service_connection/agent_service_connection.dart
+++ b/public/dart/fuchsia_modular/lib/src/service_connection/agent_service_connection.dart
@@ -10,13 +10,12 @@
 import '../internal/_component_context.dart';
 
 /// Connect to the service specified by [serviceProxy] and implemented by the
-/// agent with [agentUrl].
+/// agent with [agentUrl]. Optionally, provide a [componentContextProxy] which
+/// will be used to connect to the agent.
 ///
 /// The agent will be launched if it's not already running.
-void connectToAgentService<T>(
-  String agentUrl,
-  AsyncProxy<T> serviceProxy,
-) {
+void connectToAgentService<T>(String agentUrl, AsyncProxy<T> serviceProxy,
+    {fidl_modular.ComponentContextProxy componentContextProxy}) {
   if (agentUrl == null || agentUrl.isEmpty) {
     throw Exception(
         'agentUrl must not be null or empty in call to connectToAgentService');
@@ -34,8 +33,9 @@
   // proxy calls without awaiting for the connection to actually establish.
   final serviceProxyRequest = serviceProxy.ctrl.request();
 
-  // Connect to the agent with agentUrl
-  getComponentContext()
+  componentContextProxy ??= getComponentContext();
+  // Connect to the agent with componentContextProxy.
+  componentContextProxy
       .connectToAgent(
     agentUrl,
     serviceProviderProxy.ctrl.request(),
@@ -43,7 +43,7 @@
   )
       .then((_) {
     final serviceName = serviceProxy.ctrl.$serviceName;
-    // Connect to the service
+    // Connect to the service.
     if (serviceName == null) {
       throw Exception("${serviceProxy.ctrl.$interfaceName}'s "
           'proxyServiceController.\$serviceName must not be null. Check the FIDL '
@@ -52,14 +52,14 @@
     serviceProviderProxy
         .connectToService(serviceName, serviceProxyRequest.passChannel())
         .then((_) {
-      // Close agent controller when the service proxy is closed
+      // Close agent controller when the service proxy is closed.
       serviceProxy.ctrl.whenClosed.then((_) {
         log.info('Service proxy [${serviceProxy.ctrl.$serviceName}] is closed. '
             'Closing the associated AgentControllerProxy.');
         agentControllerProxy.ctrl.close();
       });
 
-      // Close all unnecessary bindings
+      // Close all unnecessary bindings.
       serviceProviderProxy.ctrl.close();
     });
   }).catchError((e) {
diff --git a/public/dart/fuchsia_modular/test/service_connection/agent_service_connection_integ_test.dart b/public/dart/fuchsia_modular/test/service_connection/agent_service_connection_integ_test.dart
new file mode 100644
index 0000000..2831a39
--- /dev/null
+++ b/public/dart/fuchsia_modular/test/service_connection/agent_service_connection_integ_test.dart
@@ -0,0 +1,45 @@
+// 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.
+
+// ignore_for_file: implementation_imports
+import 'package:fidl/fidl.dart';
+import 'package:fidl_fuchsia_modular/fidl_async.dart' as fidl_modular;
+import 'package:fidl_fuchsia_sys/fidl_async.dart' as fuchsia_sys;
+import 'package:fuchsia_modular/src/service_connection/agent_service_connection.dart';
+import 'package:test/test.dart';
+
+void main() {
+  group('connectToAgentService:=', () {
+    test('verify should call custom componentContext.connectToAgent', () {
+      final mockComponentContext = FakeComponentContextProxy();
+      connectToAgentService('agentUrl',
+          FakeAsyncProxy('fuchsia.modular.FakeService', r'FakeService'),
+          componentContextProxy: mockComponentContext);
+      expect(mockComponentContext.calls, 1);
+    });
+  });
+}
+
+class FakeAsyncProxy<T> extends AsyncProxy<T> {
+  String serviceName;
+  String interfaceName;
+  FakeAsyncProxy(this.serviceName, this.interfaceName)
+      : super(AsyncProxyController(
+          $serviceName: serviceName,
+          $interfaceName: interfaceName,
+        ));
+}
+
+class FakeComponentContextProxy extends fidl_modular.ComponentContextProxy {
+  int calls = 0;
+
+  @override
+  Future<void> connectToAgent(
+      String url,
+      InterfaceRequest<fuchsia_sys.ServiceProvider> incomingServices,
+      InterfaceRequest<fidl_modular.AgentController> controller) async {
+    calls += 1;
+    return;
+  }
+}