Merge "Migrate car service to userlib."
diff --git a/service/src/com/android/car/CarLocationService.java b/service/src/com/android/car/CarLocationService.java
index 3aa54ee..e0a55c9 100644
--- a/service/src/com/android/car/CarLocationService.java
+++ b/service/src/com/android/car/CarLocationService.java
@@ -168,14 +168,18 @@
                 asyncOperation(() -> {
                     storeLocation();
                     // Notify the CarPowerManager that it may proceed to shutdown or suspend.
-                    future.complete(null);
+                    if (future != null) {
+                        future.complete(null);
+                    }
                 });
                 break;
             case CarPowerStateListener.SHUTDOWN_CANCELLED:
             case CarPowerStateListener.SUSPEND_EXIT:
                 // This service does not need to do any work for these events but should still
                 // notify the CarPowerManager that it may proceed.
-                future.complete(null);
+                if (future != null) {
+                    future.complete(null);
+                }
                 break;
         }
     }
diff --git a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
index 4f7454a..bee8282 100644
--- a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
@@ -18,6 +18,7 @@
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
@@ -367,10 +368,10 @@
 
     /**
      * Test that the {@link CarLocationService} stores the {@link LocationManager}'s last known
-     * location upon prepare-shutdown events.
+     * location upon power state-changed SUSPEND events.
      */
     @Test
-    public void testStoresLocationUponPrepareShutdown()
+    public void testStoresLocationUponStateChanged()
             throws IOException, RemoteException, InterruptedException {
         long currentTime = System.currentTimeMillis();
         long elapsedTime = SystemClock.elapsedRealtimeNanos();
@@ -400,6 +401,18 @@
     }
 
     /**
+     * Test that the {@link CarLocationService} does not throw an exception on SUSPEND_EXIT events.
+     */
+    @Test
+    public void testDoesNotThrowExceptionUponStateChanged() {
+        try {
+            mCarLocationService.onStateChanged(CarPowerStateListener.SUSPEND_EXIT, null);
+        } catch (Exception e) {
+            fail("onStateChanged should not throw an exception: " + e);
+        }
+    }
+
+    /**
      * Test that the {@link CarLocationService} does not store null locations.
      */
     @Test