Add CompilationCachingTest/TokenNotProvided.

Add the test case that the client does not provide caching information
at NDK level. Test if the runtime will invoke the correct compilation
method.

Bug: 123602616
Test: NeuralNetworksTest_static
Change-Id: Ib5ebfc9f1d2f9f55ddaf43a6bfe5eef01981112b
Merged-In: Ib5ebfc9f1d2f9f55ddaf43a6bfe5eef01981112b
(cherry picked from commit 55b826cfd2dac7342b173752d3627cdba4df42f6)
diff --git a/runtime/test/TestCompilationCaching.cpp b/runtime/test/TestCompilationCaching.cpp
index de687df..5705417 100644
--- a/runtime/test/TestCompilationCaching.cpp
+++ b/runtime/test/TestCompilationCaching.cpp
@@ -260,7 +260,7 @@
         }
     }
 
-    void compileModel(const sp<CachingDriver>& driver) {
+    void compileModel(const sp<CachingDriver>& driver, bool withToken) {
         DeviceManager::get()->forTest_registerDevice(kDeviceName, driver);
 
         // Make device list including only a single driver device.
@@ -285,9 +285,11 @@
         ASSERT_EQ(ANeuralNetworksCompilation_createForDevices(mModel.getHandle(), devices.data(),
                                                               devices.size(), &compilation),
                   ANEURALNETWORKS_NO_ERROR);
-        ASSERT_EQ(ANeuralNetworksCompilation_setCaching(compilation, mCacheDir.c_str(),
-                                                        mToken.data()),
-                  ANEURALNETWORKS_NO_ERROR);
+        if (withToken) {
+            ASSERT_EQ(ANeuralNetworksCompilation_setCaching(compilation, mCacheDir.c_str(),
+                                                            mToken.data()),
+                      ANEURALNETWORKS_NO_ERROR);
+        }
         ASSERT_EQ(ANeuralNetworksCompilation_finish(compilation), ANEURALNETWORKS_NO_ERROR);
 
         DeviceManager::get()->forTest_reInitializeDeviceList();
@@ -296,7 +298,7 @@
     void createCache() {
         sp<CachingDriver> driver = new CachingDriver(kDeviceName, ErrorStatus::NONE, kNumModelCache,
                                                      kNumDataCache, ErrorStatus::NONE);
-        compileModel(driver);
+        compileModel(driver, /*withToken=*/true);
     }
 
     static constexpr char kDeviceName[] = "deviceTestCompilationCaching";
@@ -310,14 +312,14 @@
     std::vector<uint8_t> mToken;
 };
 
-TEST_P(CompilationCachingTest, CacheNotExist) {
+TEST_P(CompilationCachingTest, TokenProvidedAndCacheNotExist) {
     if (DeviceManager::get()->getUseCpuOnly()) {
         return;
     }
     sp<CachingDriver> driver =
             new CachingDriver(kDeviceName, kErrorStatusGetNumCacheFiles, kNumModelCache,
                               kNumDataCache, kErrorStatusPrepareFromCache);
-    compileModel(driver);
+    compileModel(driver, /*withToken=*/true);
 
     // When cache file does not exist, the runtime should never call prepareModelFromCache.
     EXPECT_EQ(driver->hasCalledPrepareModelFromCache(), false);
@@ -328,7 +330,7 @@
                                                        : HasCalledPrepareModel::WITHOUT_CACHING);
 }
 
-TEST_P(CompilationCachingTest, CacheExist) {
+TEST_P(CompilationCachingTest, TokenProvidedAndCacheExist) {
     if (DeviceManager::get()->getUseCpuOnly()) {
         return;
     }
@@ -336,7 +338,7 @@
     sp<CachingDriver> driver =
             new CachingDriver(kDeviceName, kErrorStatusGetNumCacheFiles, kNumModelCache,
                               kNumDataCache, kErrorStatusPrepareFromCache);
-    compileModel(driver);
+    compileModel(driver, /*withToken=*/true);
 
     // When cache files exist, the runtime should call prepareModelFromCache iff caching supported.
     EXPECT_EQ(driver->hasCalledPrepareModelFromCache(), mIsCachingSupportedAndNoError);
@@ -359,6 +361,21 @@
     EXPECT_EQ(driver->hasCalledPrepareModel(), expectHasCalledPrepareModel);
 }
 
+TEST_P(CompilationCachingTest, TokenNotProvided) {
+    if (DeviceManager::get()->getUseCpuOnly()) {
+        return;
+    }
+    sp<CachingDriver> driver =
+            new CachingDriver(kDeviceName, kErrorStatusGetNumCacheFiles, kNumModelCache,
+                              kNumDataCache, kErrorStatusPrepareFromCache);
+    compileModel(driver, /*withToken=*/false);
+
+    // When no NDK token is provided by the client, the runtime should never call
+    // prepareModelFromCache or request caching with prepareModel_1_2.
+    EXPECT_EQ(driver->hasCalledPrepareModelFromCache(), false);
+    EXPECT_EQ(driver->hasCalledPrepareModel(), HasCalledPrepareModel::WITHOUT_CACHING);
+}
+
 static const auto kErrorStatusGetNumCacheFilesChoices =
         testing::Values(ErrorStatus::NONE, ErrorStatus::DEVICE_UNAVAILABLE);
 static const auto kNumCacheChoices =