Camera: Fix handling of dead processes for arbitration

A dead (or dying) process will have state NONEXISTENT, OOM score
INVALID when queried from ProcessInfoService.  Ensure that this maps
to a very low total score for prioritization.

Also add unit testing for this, and improve the unit tests and service
code to use named constants instead of magic numbers for state/score.

Test: New unit tests pass; CTS not regressed on coral
Bug: 181777896
Change-Id: I19f1a7e0b75f35e64ff790ef7098b3d40b7e55be
diff --git a/services/camera/libcameraservice/tests/ClientManagerTest.cpp b/services/camera/libcameraservice/tests/ClientManagerTest.cpp
index 6a38427..037c5c2 100644
--- a/services/camera/libcameraservice/tests/ClientManagerTest.cpp
+++ b/services/camera/libcameraservice/tests/ClientManagerTest.cpp
@@ -17,10 +17,13 @@
 #define LOG_NDEBUG 0
 #define LOG_TAG "ClientManagerTest"
 
+#include <binder/ActivityManager.h>
+
 #include "../utils/ClientManager.h"
 #include <gtest/gtest.h>
 
 using namespace android::resource_policy;
+using namespace android;
 
 struct TestClient {
     TestClient(int id, int32_t cost, const std::set<int>& conflictingKeys, int32_t ownerId,
@@ -59,13 +62,15 @@
 
     TestClientManager cm;
     TestClient cam0Client(/*ID*/0, /*cost*/100, /*conflicts*/{1},
-            /*ownerId*/ 1000, /*score*/50, /*state*/ 1, /*isVendorClient*/ false);
+            /*ownerId*/ 1000, PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ,
+            ActivityManager::PROCESS_STATE_PERSISTENT_UI, /*isVendorClient*/ false);
     auto cam0Desc = makeDescFromTestClient(cam0Client);
     auto evicted = cm.addAndEvict(cam0Desc);
     ASSERT_EQ(evicted.size(), 0u) << "Evicted list must be empty";
 
     TestClient cam1Client(/*ID*/1, /*cost*/100, /*conflicts*/{0},
-            /*ownerId*/ 1000, /*score*/50, /*state*/ 1, /*isVendorClient*/ false);
+            /*ownerId*/ 1000, PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ,
+            ActivityManager::PROCESS_STATE_PERSISTENT_UI, /*isVendorClient*/ false);
     auto cam1Desc = makeDescFromTestClient(cam1Client);
 
     // 1. Check with conflicting devices, new client would be evicted
@@ -76,13 +81,15 @@
     cm.removeAll();
 
     TestClient cam2Client(/*ID*/2, /*cost*/100, /*conflicts*/{},
-            /*ownerId*/ 1000, /*score*/50, /*state*/ 1, /*isVendorClient*/ false);
+            /*ownerId*/ 1000, PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ,
+            ActivityManager::PROCESS_STATE_PERSISTENT_UI, /*isVendorClient*/ false);
     auto cam2Desc = makeDescFromTestClient(cam2Client);
     evicted = cm.addAndEvict(cam2Desc);
     ASSERT_EQ(evicted.size(), 0u) << "Evicted list must be empty";
 
     TestClient cam3Client(/*ID*/3, /*cost*/100, /*conflicts*/{},
-            /*ownerId*/ 1000, /*score*/50, /*state*/ 1, /*isVendorClient*/ false);
+            /*ownerId*/ 1000, PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ,
+            ActivityManager::PROCESS_STATE_PERSISTENT_UI, /*isVendorClient*/ false);
     auto cam3Desc = makeDescFromTestClient(cam3Client);
 
     // 2. Check without conflicting devices, the pre-existing client won't be evicted
@@ -97,12 +104,42 @@
     ASSERT_EQ(evicted.size(), 0u) << "Evicted list must be empty";
 
     TestClient cam0ClientNew(/*ID*/0, /*cost*/100, /*conflicts*/{1},
-            /*ownerId*/ 1000, /*score*/50, /*state*/ 1, /*isVendorClient*/ false);
+            /*ownerId*/ 1000, PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ,
+            ActivityManager::PROCESS_STATE_PERSISTENT_UI, /*isVendorClient*/ false);
     auto cam0DescNew = makeDescFromTestClient(cam0ClientNew);
     wouldBeEvicted = cm.wouldEvict(cam0DescNew);
 
     // 3. Check opening the same camera twice will evict the older client
     ASSERT_EQ(wouldBeEvicted.size(), 1u) << "Evicted list length must be 1";
     ASSERT_EQ(wouldBeEvicted[0], cam0Desc) << "cam0 (old) must be evicted";
-}
 
+    // 4. Check that an invalid client (dead process) will be evicted
+
+    cm.removeAll();
+
+    TestClient camDeadClient(/*ID*/ 0, /*cost*/100, /*conflicts*/{},
+            /*ownerId*/ 1000, INVALID_ADJ,
+            ActivityManager::PROCESS_STATE_NONEXISTENT, /*isVendorClient*/ false);
+    auto camDeadDesc = makeDescFromTestClient(camDeadClient);
+    evicted = cm.addAndEvict(camDeadDesc);
+    wouldBeEvicted = cm.wouldEvict(cam0Desc);
+
+    ASSERT_EQ(evicted.size(), 0u) << "Evicted list must be empty";
+    ASSERT_EQ(wouldBeEvicted.size(), 1u) << "Evicted list length must be 1";
+    ASSERT_EQ(wouldBeEvicted[0], camDeadDesc) << "dead cam must be evicted";
+
+    // 5. Check that a more important client will win
+
+    TestClient cam0ForegroundClient(/*ID*/0, /*cost*/100, /*conflicts*/{1},
+            /*ownerId*/ 1000, FOREGROUND_APP_ADJ,
+            ActivityManager::PROCESS_STATE_PERSISTENT_UI, /*isVendorClient*/ false);
+    auto cam0FgDesc = makeDescFromTestClient(cam0ForegroundClient);
+
+    cm.removeAll();
+    evicted = cm.addAndEvict(cam0Desc);
+    wouldBeEvicted = cm.wouldEvict(cam0FgDesc);
+
+    ASSERT_EQ(evicted.size(), 0u);
+    ASSERT_EQ(wouldBeEvicted.size(), 1u);
+    ASSERT_EQ(wouldBeEvicted[0],cam0Desc) << "less important cam0 must be evicted";
+}