1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
package org.robolectric.shadows;
import static com.google.common.truth.Truth.assertThat;
import android.os.SystemProperties;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
@RunWith(AndroidJUnit4.class)
public class ShadowSystemPropertiesTest {
@Test
public void get() {
assertThat(SystemProperties.get("ro.product.device")).isEqualTo("robolectric");
}
@Test
public void getWithDefault() {
assertThat(SystemProperties.get("foo", "bar")).isEqualTo("bar");
}
@Test
public void getBoolean() {
ShadowSystemProperties.override("false_1", "0");
ShadowSystemProperties.override("false_2", "n");
ShadowSystemProperties.override("false_3", "no");
ShadowSystemProperties.override("false_4", "off");
ShadowSystemProperties.override("false_5", "false");
ShadowSystemProperties.override("true_1", "1");
ShadowSystemProperties.override("true_2", "y");
ShadowSystemProperties.override("true_3", "yes");
ShadowSystemProperties.override("true_4", "on");
ShadowSystemProperties.override("true_5", "true");
ShadowSystemProperties.override("error_value", "error");
assertThat(SystemProperties.getBoolean("false_1", true)).isFalse();
assertThat(SystemProperties.getBoolean("false_2", true)).isFalse();
assertThat(SystemProperties.getBoolean("false_3", true)).isFalse();
assertThat(SystemProperties.getBoolean("false_4", true)).isFalse();
assertThat(SystemProperties.getBoolean("false_5", true)).isFalse();
assertThat(SystemProperties.getBoolean("true_1", false)).isTrue();
assertThat(SystemProperties.getBoolean("true_2", false)).isTrue();
assertThat(SystemProperties.getBoolean("true_3", false)).isTrue();
assertThat(SystemProperties.getBoolean("true_4", false)).isTrue();
assertThat(SystemProperties.getBoolean("true_5", false)).isTrue();
assertThat(SystemProperties.getBoolean("error_value", false)).isFalse();
assertThat(SystemProperties.getBoolean("error_value", true)).isTrue();
}
// The following readPropFromJarNotClassPathXX tests check build.prop is loaded from appropriate
// android-all jar instead of loading build.prop from classpath aka LATEST_SDK.
@Test
@Config(sdk = 19)
public void readPropFromJarNotClassPath19() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(19);
}
@Test
@Config(sdk = 21)
public void readPropFromJarNotClassPath21() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(21);
}
@Test
@Config(sdk = 22)
public void readPropFromJarNotClassPath22() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(22);
}
@Test
@Config(sdk = 23)
public void readPropFromJarNotClassPath23() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(23);
}
@Test
@Config(sdk = 24)
public void readPropFromJarNotClassPath24() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(24);
}
@Test
@Config(sdk = 25)
public void readPropFromJarNotClassPath25() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(25);
}
@Test
@Config(sdk = 26)
public void readPropFromJarNotClassPath26() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(26);
}
@Test
@Config(sdk = 27)
public void readPropFromJarNotClassPath27() {
assertThat(SystemProperties.getInt("ro.build.version.sdk", 0)).isEqualTo(27);
}
@Test
public void set() {
assertThat(SystemProperties.get("newkey")).isEqualTo("");
SystemProperties.set("newkey", "val");
assertThat(SystemProperties.get("newkey")).isEqualTo("val");
SystemProperties.set("newkey", null);
assertThat(SystemProperties.get("newkey")).isEqualTo("");
}
@Test
public void override() {
assertThat(SystemProperties.get("newkey")).isEqualTo("");
ShadowSystemProperties.override("newkey", "val");
assertThat(SystemProperties.get("newkey")).isEqualTo("val");
ShadowSystemProperties.override("newkey", null);
assertThat(SystemProperties.get("newkey")).isEqualTo("");
}
}
|