diff options
author | 2025-02-21 11:47:36 -0800 | |
---|---|---|
committer | 2025-02-21 11:47:36 -0800 | |
commit | e80937fbb66fecde22ea73b02676fd39aa9be536 (patch) | |
tree | 051c158d3655011059366ad19aab308afab9eae4 | |
parent | fb7a2f4e2042598778c94de3aa21044a0e839bdb (diff) | |
parent | a46e0d4111ff3057370e06628fa3c48e8b5ea035 (diff) |
Merge "Add I/O tests for UserWakeupStore" into main
3 files changed, 93 insertions, 2 deletions
diff --git a/services/tests/mockingservicestests/res/xml/expectedUserWakeupList_1.xml b/services/tests/mockingservicestests/res/xml/expectedUserWakeupList_1.xml new file mode 100644 index 000000000000..21e6dab518fc --- /dev/null +++ b/services/tests/mockingservicestests/res/xml/expectedUserWakeupList_1.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2025 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> +<users version="1"> + <user user_id="10" /> + <user user_id="11" /> +</users>
\ No newline at end of file diff --git a/services/tests/mockingservicestests/res/xml/expectedUserWakeupList_2.xml b/services/tests/mockingservicestests/res/xml/expectedUserWakeupList_2.xml new file mode 100644 index 000000000000..d0b371f060da --- /dev/null +++ b/services/tests/mockingservicestests/res/xml/expectedUserWakeupList_2.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2025 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> +<users version="1"> + <user user_id="10" /> +</users>
\ No newline at end of file diff --git a/services/tests/mockingservicestests/src/com/android/server/alarm/UserWakeupStoreTest.java b/services/tests/mockingservicestests/src/com/android/server/alarm/UserWakeupStoreTest.java index 72883e269a65..240284a42406 100644 --- a/services/tests/mockingservicestests/src/com/android/server/alarm/UserWakeupStoreTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/alarm/UserWakeupStoreTest.java @@ -23,15 +23,21 @@ import static com.android.server.alarm.UserWakeupStore.USER_START_TIME_DEVIATION import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import android.content.res.AssetManager; +import android.content.res.XmlResourceParser; import android.os.Environment; import android.os.FileUtils; import android.os.SystemClock; +import android.util.Xml; import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.runner.AndroidJUnit4; import com.android.internal.os.BackgroundThread; +import com.android.internal.util.XmlUtils; +import com.android.modules.utils.TypedXmlPullParser; import com.android.modules.utils.testing.ExtendedMockitoRule; import org.junit.After; @@ -40,8 +46,11 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.xmlpull.v1.XmlPullParserException; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.concurrent.ExecutorService; @@ -55,6 +64,7 @@ public class UserWakeupStoreTest { private static final File TEST_SYSTEM_DIR = new File(InstrumentationRegistry .getInstrumentation().getContext().getDataDir(), "alarmsTestDir"); private static final File ROOT_DIR = new File(TEST_SYSTEM_DIR, UserWakeupStore.ROOT_DIR_NAME); + private static final String USERS_FILE_NAME = "usersWithAlarmClocks.xml"; private ExecutorService mMockExecutorService = null; UserWakeupStore mUserWakeupStore; @@ -105,7 +115,7 @@ public class UserWakeupStoreTest { Collections.sort(userWakeups); assertEquals(userIds, userWakeups); - final File file = new File(ROOT_DIR , "usersWithAlarmClocks.xml"); + final File file = new File(ROOT_DIR, USERS_FILE_NAME); assertTrue(file.exists()); } @@ -178,5 +188,47 @@ public class UserWakeupStoreTest { assertTrue(mUserWakeupStore.getWakeupTimeForUser(USER_ID_2) - realtime < 3 * BUFFER_TIME_MS + USER_START_TIME_DEVIATION_LIMIT_MS); } - //TODO: b/330264023 - Add tests for I/O in usersWithAlarmClocks.xml. + + @Test + public void testWriteWakeups_xmlIsOrdered() { + mUserWakeupStore.addUserWakeup(USER_ID_1, TEST_TIMESTAMP - 19_000); + mUserWakeupStore.addUserWakeup(USER_ID_2, TEST_TIMESTAMP - 7_000); + assertFileContentsMatchExpectedXml("res/xml/expectedUserWakeupList_1.xml"); + } + + @Test + public void testWriteWakeups_containsOneEntryPerUser() { + mUserWakeupStore.addUserWakeup(USER_ID_1, TEST_TIMESTAMP - 19_000); + mUserWakeupStore.addUserWakeup(USER_ID_1, TEST_TIMESTAMP - 7_000); + assertFileContentsMatchExpectedXml("res/xml/expectedUserWakeupList_2.xml"); + } + + private static void assertFileContentsMatchExpectedXml(String expectedContentsFile) { + final File actual = new File(ROOT_DIR, USERS_FILE_NAME); + AssetManager assetManager = + InstrumentationRegistry.getInstrumentation().getContext().getAssets(); + try (FileInputStream actualFis = new FileInputStream(actual)) { + final TypedXmlPullParser actualParser = Xml.resolvePullParser(actualFis); + final XmlResourceParser expectedParser = assetManager.openXmlResourceParser( + expectedContentsFile); + for (XmlUtils.nextElement(expectedParser), XmlUtils.nextElement(actualParser); + actualParser.getEventType() != XmlResourceParser.END_DOCUMENT + && expectedParser.getEventType() != XmlResourceParser.END_DOCUMENT; + XmlUtils.nextElement(actualParser), XmlUtils.nextElement(expectedParser)) { + assertEquals("Event types differ ", expectedParser.getEventType(), + actualParser.getEventType()); + for (int i = 0; i < expectedParser.getAttributeCount(); i++) { + assertEquals("Attribute names differ at index " + i, + expectedParser.getAttributeName(i), actualParser.getAttributeName(i)); + assertEquals("Attribute values differ at index " + i, + expectedParser.getAttributeValue(i), actualParser.getAttributeValue(i)); + } + } + // Ensure they are both at the end of document + assertEquals("One of the parsers has not reached the EOF", + expectedParser.getEventType(), actualParser.getEventType()); + } catch (IOException | XmlPullParserException e) { + fail(e.getLocalizedMessage()); + } + } } |