From bab932f258ebbb0fd41960603e79cab4e8bed8d6 Mon Sep 17 00:00:00 2001 From: Matías Hernández Date: Thu, 30 Jan 2025 16:19:11 +0100 Subject: Generate sequential instead of random rule ids in TestModeBuilder Because we were only generating numbers between 0 and 1000 for whatever reason, there was a relatively high chance that two Modes could end up with the same id. This is generally not a problem because ZenMode has a well-defined equals method and we rarely use the ids except for API calls (which unit tests don't exercise). However some parts of the code (notably ModesDialogViewModel) do match modes by id (for visual stability), so collisions would lead to wrong results and test flakiness. With this change we will only get collisions if a test uses a list containing 4294967296 modes. Good luck with that! Fixes: 391727306 Test: atest ModesDialogViewModelTest Flag: TEST_ONLY Change-Id: I1b7dfdd18f41ffe04bba7b2d6a54f53f259a0a05 --- .../com/android/settingslib/notification/modes/TestModeBuilder.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'packages/SettingsLib/src') diff --git a/packages/SettingsLib/src/com/android/settingslib/notification/modes/TestModeBuilder.java b/packages/SettingsLib/src/com/android/settingslib/notification/modes/TestModeBuilder.java index 64a2de5025de..ecea5fd35150 100644 --- a/packages/SettingsLib/src/com/android/settingslib/notification/modes/TestModeBuilder.java +++ b/packages/SettingsLib/src/com/android/settingslib/notification/modes/TestModeBuilder.java @@ -33,10 +33,12 @@ import android.service.notification.ZenPolicy; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; -import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; public class TestModeBuilder { + private static final AtomicInteger sNextId = new AtomicInteger(0); + private String mId; private AutomaticZenRule mRule; private ZenModeConfig.ZenRule mConfigZenRule; @@ -47,7 +49,7 @@ public class TestModeBuilder { public TestModeBuilder() { // Reasonable defaults - int id = new Random().nextInt(1000); + int id = sNextId.incrementAndGet(); mId = "rule_" + id; mRule = new AutomaticZenRule.Builder("Test Rule #" + id, Uri.parse("rule://" + id)) .setPackage("some_package") -- cgit v1.2.3-59-g8ed1b