aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGoogler <noreply@google.com>2024-04-17 16:22:47 -0700
committerCopybara-Service <copybara-worker@google.com>2024-04-17 16:23:26 -0700
commit1920a4cbbf574f775bd360b901b3e4102b7ad8b4 (patch)
treefa64b24d3546f18dac1cf2c4767331f50911dba7
parent71dc7ee3b39a334e2b1eecdffcd24e95b99a130f (diff)
downloadrobolectric-1920a4cbbf574f775bd360b901b3e4102b7ad8b4.tar.gz
Small fixes on potential null pointers since ARSC loading support.
With the support of ARSC loading, some pointers potentially become null. This CL fixes this. PiperOrigin-RevId: 625839793
-rw-r--r--resources/src/main/java/org/robolectric/res/android/AssetDir.java3
-rw-r--r--resources/src/main/java/org/robolectric/res/android/CppApkAssets.java8
-rw-r--r--resources/src/test/java/org/robolectric/res/android/AssetDirTest.java29
-rw-r--r--resources/src/test/java/org/robolectric/res/android/CppApkAssetsTest.java17
4 files changed, 54 insertions, 3 deletions
diff --git a/resources/src/main/java/org/robolectric/res/android/AssetDir.java b/resources/src/main/java/org/robolectric/res/android/AssetDir.java
index b5a690172..fcf97cb82 100644
--- a/resources/src/main/java/org/robolectric/res/android/AssetDir.java
+++ b/resources/src/main/java/org/robolectric/res/android/AssetDir.java
@@ -20,6 +20,9 @@ public class AssetDir {
* Vector-style access.
*/
public int getFileCount() {
+ if (mFileInfo == null) {
+ return 0;
+ }
return mFileInfo.size();
}
diff --git a/resources/src/main/java/org/robolectric/res/android/CppApkAssets.java b/resources/src/main/java/org/robolectric/res/android/CppApkAssets.java
index 58d436b69..58370c892 100644
--- a/resources/src/main/java/org/robolectric/res/android/CppApkAssets.java
+++ b/resources/src/main/java/org/robolectric/res/android/CppApkAssets.java
@@ -5,7 +5,6 @@ package org.robolectric.res.android;
import static org.robolectric.res.android.CppAssetManager.FileType.kFileTypeDirectory;
import static org.robolectric.res.android.CppAssetManager.FileType.kFileTypeRegular;
-import static org.robolectric.res.android.Util.CHECK;
import static org.robolectric.res.android.ZipFileRO.OpenArchive;
import static org.robolectric.res.android.ZipFileRO.kCompressDeflated;
@@ -57,7 +56,7 @@ public class CppApkAssets {
// bool ForEachFile(const String& path,
// const std::function<void(const StringPiece&, FileType)>& f) const;
- private CppApkAssets() {
+ CppApkAssets() {
this.zipFileRO = null;
}
@@ -357,7 +356,10 @@ public class CppApkAssets {
boolean ForEachFile(String root_path,
ForEachFileCallback f) {
- CHECK(zip_handle_ != null);
+ if (zip_handle_ == null || zipFileRO == null) {
+ // In this case, the ApkAssets was loaded from a pure ARSC, and does not have assets.
+ return false;
+ }
String root_path_full = root_path;
// if (root_path_full.back() != '/') {
diff --git a/resources/src/test/java/org/robolectric/res/android/AssetDirTest.java b/resources/src/test/java/org/robolectric/res/android/AssetDirTest.java
new file mode 100644
index 000000000..4337fde56
--- /dev/null
+++ b/resources/src/test/java/org/robolectric/res/android/AssetDirTest.java
@@ -0,0 +1,29 @@
+package org.robolectric.res.android;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class AssetDirTest {
+
+ @Test
+ public void getFileCount_returnsZeroIfInitializedTrivially() {
+ assertThat(new AssetDir().getFileCount()).isEqualTo(0);
+ }
+
+ @Test
+ public void getFileCount_returnsCorrectFileCount() {
+ AssetDir.FileInfo fileInfo1 = new AssetDir.FileInfo(new String8("a/a.txt"));
+ AssetDir.FileInfo fileInfo2 = new AssetDir.FileInfo(new String8("b/b.txt"));
+ SortedVector<AssetDir.FileInfo> fileInfos = new SortedVector<>();
+ fileInfos.add(fileInfo1);
+ fileInfos.add(fileInfo2);
+ AssetDir assetDir = new AssetDir();
+ assetDir.setFileList(fileInfos);
+
+ assertThat(assetDir.getFileCount()).isEqualTo(2);
+ }
+}
diff --git a/resources/src/test/java/org/robolectric/res/android/CppApkAssetsTest.java b/resources/src/test/java/org/robolectric/res/android/CppApkAssetsTest.java
new file mode 100644
index 000000000..e4545af7c
--- /dev/null
+++ b/resources/src/test/java/org/robolectric/res/android/CppApkAssetsTest.java
@@ -0,0 +1,17 @@
+package org.robolectric.res.android;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class CppApkAssetsTest {
+
+ @Test
+ public void forEachFile_returnsFalseIfInitializedTrivially() {
+ boolean runningResult = new CppApkAssets().ForEachFile("a/robo", (string, type) -> {});
+ assertThat(runningResult).isFalse();
+ }
+}