From 23c9b688e23239642668ec637c3c5092c312cb68 Mon Sep 17 00:00:00 2001 From: Pinyao Ting Date: Fri, 5 Jun 2020 14:08:37 -0700 Subject: [PATCH] fix FileNotFound exception when attempting to read bubble xml BubbleDataRepository tried to read the xml file before a write, and the xml file is only created on write if it didn't exist, which caused the FileNotFound exception. Bug: 158231089 Test: BubblePersistentRepositoryTest Change-Id: I7cd75b1d74be277df49f433338ad814ce50c87c1 --- .../systemui/bubbles/storage/BubblePersistentRepository.kt | 1 + .../bubbles/storage/BubblePersistentRepositoryTest.kt | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubblePersistentRepository.kt b/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubblePersistentRepository.kt index 7c3271e4b76c5..5b4d8c71e5c03 100644 --- a/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubblePersistentRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubblePersistentRepository.kt @@ -54,6 +54,7 @@ class BubblePersistentRepository @Inject constructor( fun readFromDisk(): List { synchronized(bubbleFile) { + if (!bubbleFile.exists()) return emptyList() try { return bubbleFile.openRead().use(::readXml) } catch (e: Throwable) { Log.e(TAG, "Failed to open bubble file", e) } diff --git a/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubblePersistentRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubblePersistentRepositoryTest.kt index 2aed75e6519f0..1d02b8dba910b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubblePersistentRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubblePersistentRepositoryTest.kt @@ -20,6 +20,8 @@ import android.testing.AndroidTestingRunner import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import junit.framework.Assert.assertEquals +import junit.framework.Assert.assertNotNull +import junit.framework.Assert.assertTrue import org.junit.Before import org.junit.Test import org.junit.runner.RunWith @@ -42,6 +44,11 @@ class BubblePersistentRepositoryTest : SysuiTestCase() { @Test fun testReadWriteOperation() { + // Verify read before write doesn't cause FileNotFoundException + val actual = repository.readFromDisk() + assertNotNull(actual) + assertTrue(actual.isEmpty()) + repository.persistsToDisk(bubbles) assertEquals(bubbles, repository.readFromDisk()) }