Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for ShadowStatsManager PiperOrigin-RevId: 555660568
- Loading branch information
Googler
authored and
Copybara-Service
committed
Aug 10, 2023
1 parent
b5f191d
commit 2ab5634
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
robolectric/src/test/java/org/robolectric/shadows/ShadowStatsManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.robolectric.shadows; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import android.app.StatsManager; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import androidx.test.core.app.ApplicationProvider; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
/** Tests for {@link ShadowStatsManager} */ | ||
@RunWith(RobolectricTestRunner.class) | ||
@Config(minSdk = Build.VERSION_CODES.P) | ||
public final class ShadowStatsManagerTest { | ||
|
||
@Test | ||
public void testGetMetadata() throws Exception { | ||
Context context = ApplicationProvider.getApplicationContext(); | ||
StatsManager statsManager = context.getSystemService(StatsManager.class); | ||
byte[] metadataBytes = new byte[] {1, 2, 3, 4, 5}; | ||
ShadowStatsManager.setStatsMetadata(metadataBytes); | ||
|
||
assertThat(statsManager.getMetadata()).isEqualTo(metadataBytes); | ||
} | ||
|
||
@Test | ||
public void testGetReports_multipleReports() throws Exception { | ||
Context context = ApplicationProvider.getApplicationContext(); | ||
StatsManager statsManager = context.getSystemService(StatsManager.class); | ||
long reportId1 = 1L; | ||
long reportId2 = 2L; | ||
byte[] report1Bytes = new byte[] {1, 2, 3, 4, 5}; | ||
byte[] report2Bytes = new byte[] {1, 2, 3}; | ||
ShadowStatsManager.addReportData(reportId1, report1Bytes); | ||
ShadowStatsManager.addReportData(reportId2, report2Bytes); | ||
|
||
assertThat(statsManager.getReports(reportId1)).isEqualTo(report1Bytes); | ||
assertThat(statsManager.getReports(reportId2)).isEqualTo(report2Bytes); | ||
} | ||
|
||
@Test | ||
public void testGetReports_clearsExistingReport() throws Exception { | ||
Context context = ApplicationProvider.getApplicationContext(); | ||
StatsManager statsManager = context.getSystemService(StatsManager.class); | ||
long reportId1 = 1L; | ||
byte[] report1Bytes = new byte[] {1, 2, 3, 4, 5}; | ||
ShadowStatsManager.addReportData(reportId1, report1Bytes); | ||
|
||
assertThat(statsManager.getReports(reportId1)).isEqualTo(report1Bytes); | ||
assertThat(statsManager.getReports(reportId1)).isEqualTo(new byte[] {}); | ||
} | ||
|
||
@Test | ||
public void testReset_clearsReports() throws Exception { | ||
Context context = ApplicationProvider.getApplicationContext(); | ||
StatsManager statsManager = context.getSystemService(StatsManager.class); | ||
long reportId1 = 1L; | ||
byte[] report1Bytes = new byte[] {1, 2, 3, 4, 5}; | ||
ShadowStatsManager.addReportData(reportId1, report1Bytes); | ||
|
||
ShadowStatsManager.reset(); | ||
|
||
assertThat(statsManager.getReports(reportId1)).isEqualTo(new byte[] {}); | ||
} | ||
|
||
@Test | ||
public void testReset_clearsMetadata() throws Exception { | ||
Context context = ApplicationProvider.getApplicationContext(); | ||
StatsManager statsManager = context.getSystemService(StatsManager.class); | ||
byte[] metadataBytes = new byte[] {1, 2, 3, 4, 5}; | ||
ShadowStatsManager.setStatsMetadata(metadataBytes); | ||
|
||
ShadowStatsManager.reset(); | ||
|
||
assertThat(statsManager.getMetadata()).isEqualTo(new byte[] {}); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
shadows/framework/src/main/java/org/robolectric/shadows/ShadowStatsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.robolectric.shadows; | ||
|
||
import static android.os.Build.VERSION_CODES.P; | ||
|
||
import android.app.StatsManager; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
import org.robolectric.annotation.Resetter; | ||
|
||
/** Shadow for {@link ShadowStatsManager} */ | ||
@Implements(value = StatsManager.class, isInAndroidSdk = false, minSdk = P) | ||
public class ShadowStatsManager { | ||
|
||
private static final Map<Long, byte[]> dataMap = new HashMap<>(); | ||
private static byte[] statsMetadata = new byte[] {}; | ||
|
||
@Resetter | ||
public static void reset() { | ||
dataMap.clear(); | ||
statsMetadata = new byte[] {}; | ||
} | ||
|
||
public static void addReportData(long configKey, byte[] data) { | ||
dataMap.put(configKey, data); | ||
} | ||
|
||
public static void setStatsMetadata(byte[] metadata) { | ||
statsMetadata = metadata; | ||
} | ||
|
||
@Implementation | ||
protected byte[] getReports(long configKey) { | ||
byte[] data = dataMap.getOrDefault(configKey, new byte[] {}); | ||
dataMap.remove(configKey); | ||
return data; | ||
} | ||
|
||
@Implementation | ||
protected byte[] getStatsMetadata() { | ||
return statsMetadata; | ||
} | ||
} |