Skip to content

Commit 700543d

Browse files
Material Design Teamdrchen
Material Design Team
authored andcommitted
Avoid mocking java.lang.Runtime
PiperOrigin-RevId: 725357255
1 parent 2d3471f commit 700543d

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

catalog/java/io/material/catalog/feature/MemoryView.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.text.format.Formatter;
2929
import android.util.AttributeSet;
3030
import android.util.TypedValue;
31+
import androidx.annotation.NonNull;
3132
import com.google.common.collect.EvictingQueue;
3233
import java.util.Queue;
3334

@@ -129,7 +130,16 @@ protected void onAttachedToWindow() {
129130
paint.setColor(colorPrimary);
130131
}
131132

132-
public void refreshMemStats(Runtime runtime) {
133+
/** A wrapper around {@link Runtime} to allow mocking by tests. */
134+
interface RuntimeWrapper {
135+
long maxMemory();
136+
137+
long totalMemory();
138+
139+
long freeMemory();
140+
}
141+
142+
public void refreshMemStats(@NonNull RuntimeWrapper runtime) {
133143
maxMemoryInBytes = runtime.maxMemory();
134144
long availableMemInBytes = maxMemoryInBytes - (runtime.totalMemory() - runtime.freeMemory());
135145
long usedMemInBytes = maxMemoryInBytes - availableMemInBytes;

catalog/test/javatests/io/material/catalog/feature/MemoryViewTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class MemoryViewTest {
4545

4646
private static final int BYTES_IN_MB = 1024 * 1024;
4747
private MemoryView memoryView;
48-
private Runtime runtime;
48+
private MemoryView.RuntimeWrapper runtime;
4949

5050
@Before
5151
public void createAndMeasureMemoryView() {
@@ -60,7 +60,7 @@ public void createAndMeasureMemoryView() {
6060

6161
@Before
6262
public void setUpRuntime() {
63-
runtime = mock(Runtime.class);
63+
runtime = mock(MemoryView.RuntimeWrapper.class);
6464
when(runtime.maxMemory()).thenReturn(100L * BYTES_IN_MB);
6565
when(runtime.totalMemory()).thenReturn(100L * BYTES_IN_MB);
6666
when(runtime.freeMemory()).thenReturn(75L * BYTES_IN_MB);

0 commit comments

Comments
 (0)