1515package com .google .firebase .perf .session .gauges ;
1616
1717import static com .google .common .truth .Truth .assertThat ;
18- import static java .nio .charset .StandardCharsets .UTF_8 ;
1918import static org .mockito .MockitoAnnotations .initMocks ;
2019import static org .robolectric .Shadows .shadowOf ;
2120
2221import android .app .ActivityManager ;
2322import android .content .Context ;
24- import android .os .Environment ;
2523import androidx .test .core .app .ApplicationProvider ;
2624import com .google .firebase .perf .FirebasePerformanceTestBase ;
2725import com .google .firebase .perf .util .StorageUnit ;
28- import java .io .File ;
2926import java .io .IOException ;
30- import java .io .Writer ;
31- import java .nio .file .Files ;
3227import org .junit .Before ;
3328import org .junit .Test ;
3429import org .junit .runner .RunWith ;
3530import org .mockito .Mock ;
3631import org .robolectric .RobolectricTestRunner ;
37- import org .robolectric .shadows .ShadowEnvironment ;
3832
3933/** Unit tests for {@link com.google.firebase.perf.session.gauges.GaugeMetadataManager} */
4034@ RunWith (RobolectricTestRunner .class )
@@ -49,12 +43,11 @@ public class GaugeMetadataManagerTest extends FirebasePerformanceTestBase {
4943
5044 @ Mock private Runtime runtime ;
5145 private ActivityManager activityManager ;
52- private Context appContext ;
5346
5447 @ Before
5548 public void setUp () {
5649 initMocks (this );
57- appContext = ApplicationProvider .getApplicationContext ();
50+ Context appContext = ApplicationProvider .getApplicationContext ();
5851 activityManager = (ActivityManager ) appContext .getSystemService (Context .ACTIVITY_SERVICE );
5952
6053 mockMemory ();
@@ -90,62 +83,5 @@ public void testGetDeviceRamSize_returnsExpectedValue() throws IOException {
9083 int ramSize = testGaugeMetadataManager .getDeviceRamSizeKb ();
9184
9285 assertThat (ramSize ).isEqualTo (StorageUnit .BYTES .toKilobytes (DEVICE_RAM_SIZE_BYTES ));
93- assertThat (ramSize ).isEqualTo (testGaugeMetadataManager .readTotalRAM (createFakeMemInfoFile ()));
9486 }
95-
96- /** @return The file path of this fake file which can be used to read the file. */
97- private String createFakeMemInfoFile () throws IOException {
98- // Due to file permission issues on forge, it's easiest to just write this file to the emulated
99- // robolectric external storage.
100- ShadowEnvironment .setExternalStorageState (Environment .MEDIA_MOUNTED );
101-
102- File file = new File (Environment .getExternalStorageDirectory (), "FakeProcMemInfoFile" );
103- Writer fileWriter ;
104-
105- fileWriter = Files .newBufferedWriter (file .toPath (), UTF_8 );
106- fileWriter .write (MEM_INFO_CONTENTS );
107- fileWriter .close ();
108-
109- return file .getAbsolutePath ();
110- }
111-
112- private static final String MEM_INFO_CONTENTS =
113- "MemTotal: "
114- + DEVICE_RAM_SIZE_KB
115- + " kB\n "
116- + "MemFree: 542404 kB\n "
117- + "MemAvailable: 1392324 kB\n "
118- + "Buffers: 64292 kB\n "
119- + "Cached: 826180 kB\n "
120- + "SwapCached: 4196 kB\n "
121- + "Active: 934768 kB\n "
122- + "Inactive: 743812 kB\n "
123- + "Active(anon): 582132 kB\n "
124- + "Inactive(anon): 241500 kB\n "
125- + "Active(file): 352636 kB\n "
126- + "Inactive(file): 502312 kB\n "
127- + "Unevictable: 5148 kB\n "
128- + "Mlocked: 256 kB\n "
129- + "SwapTotal: 524284 kB\n "
130- + "SwapFree: 484800 kB\n "
131- + "Dirty: 4 kB\n "
132- + "Writeback: 0 kB\n "
133- + "AnonPages: 789404 kB\n "
134- + "Mapped: 241928 kB\n "
135- + "Shmem: 30632 kB\n "
136- + "Slab: 122320 kB\n "
137- + "SReclaimable: 42552 kB\n "
138- + "SUnreclaim: 79768 kB\n "
139- + "KernelStack: 22816 kB\n "
140- + "PageTables: 35344 kB\n "
141- + "NFS_Unstable: 0 kB\n "
142- + "Bounce: 0 kB\n "
143- + "WritebackTmp: 0 kB\n "
144- + "CommitLimit: 2042280 kB\n "
145- + "Committed_AS: 76623352 kB\n "
146- + "VmallocTotal: 251658176 kB\n "
147- + "VmallocUsed: 232060 kB\n "
148- + "VmallocChunk: 251347444 kB\n "
149- + "NvMapMemFree: 48640 kB\n "
150- + "NvMapMemUsed: 471460 kB\n " ;
15187}
0 commit comments