Skip to content

Commit 90b3847

Browse files
committed
fix heartbeat tests for copybara
1 parent 352a26e commit 90b3847

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

firebase-common/src/test/java/com/google/firebase/heartbeatinfo/DefaultHeartBeatControllerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.junit.Before;
4747
import org.junit.Test;
4848
import org.junit.runner.RunWith;
49+
import org.robolectric.annotation.Config;
4950

5051
@RunWith(AndroidJUnit4.class)
5152
public class DefaultHeartBeatControllerTest {
@@ -97,6 +98,7 @@ public void getHeartBeatCode_noHeartBeat() {
9798
assertThat(heartBeatController.getHeartBeatCode("fire-iid").getCode()).isEqualTo(0);
9899
}
99100

101+
@Config(sdk = 29)
100102
@Test
101103
public void generateHeartBeat_oneHeartBeat()
102104
throws ExecutionException, InterruptedException, JSONException, IOException {
@@ -123,6 +125,7 @@ public void generateHeartBeat_oneHeartBeat()
123125
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
124126
}
125127

128+
@Config(sdk = 29)
126129
@Test
127130
public void firstNewThenOld_synchronizedCorrectly()
128131
throws ExecutionException, InterruptedException {
@@ -147,6 +150,7 @@ public void firstNewThenOld_synchronizedCorrectly()
147150
assertThat(heartBeatCode).isEqualTo(0);
148151
}
149152

153+
@Config(sdk = 29)
150154
@Test
151155
public void firstOldThenNew_synchronizedCorrectly()
152156
throws ExecutionException, InterruptedException, IOException {
@@ -176,6 +180,7 @@ public void firstOldThenNew_synchronizedCorrectly()
176180
assertThat(output.replace("\n", "")).isEqualTo(emptyString);
177181
}
178182

183+
@Config(sdk = 29)
179184
@Test
180185
public void generateHeartBeat_twoHeartBeatsSameUserAgent()
181186
throws ExecutionException, InterruptedException, JSONException, IOException {
@@ -220,6 +225,7 @@ private String compress(String str) throws IOException {
220225
return out.toString("UTF-8");
221226
}
222227

228+
@Config(sdk = 29)
223229
@Test
224230
public void generateHeartBeat_twoHeartBeatstwoUserAgents()
225231
throws ExecutionException, InterruptedException, JSONException, IOException {
@@ -239,6 +245,7 @@ public void generateHeartBeat_twoHeartBeatstwoUserAgents()
239245
.registerHeartBeat()
240246
.addOnCompleteListener(executor, storeOnCompleteListener);
241247
storeOnCompleteListener.await();
248+
Thread.sleep(1000);
242249
verify(storage, times(2)).storeHeartBeat(anyLong(), anyString());
243250
heartBeatController
244251
.getHeartBeatsHeader()

firebase-common/src/test/java/com/google/firebase/heartbeatinfo/HeartBeatInfoStorageTest.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.util.ArrayList;
2424
import java.util.Collections;
2525
import org.junit.After;
26+
import org.junit.Before;
2627
import org.junit.Test;
2728
import org.junit.runner.RunWith;
29+
import org.robolectric.annotation.Config;
2830

2931
@RunWith(AndroidJUnit4.class)
3032
public class HeartBeatInfoStorageTest {
@@ -37,17 +39,17 @@ public class HeartBeatInfoStorageTest {
3739
private HeartBeatInfoStorage heartBeatInfoStorage =
3840
new HeartBeatInfoStorage(heartBeatSharedPreferences);
3941

40-
@After
41-
public void tearDown() {
42+
@Before
43+
public void setUp() {
4244
heartBeatSharedPreferences.edit().clear().apply();
4345
}
4446

45-
@Test
46-
public void shouldSendSdkHeartBeat_answerIsNo() {
47-
heartBeatSharedPreferences.edit().putLong(testSdk, 1).apply();
48-
assertThat(heartBeatInfoStorage.shouldSendSdkHeartBeat(testSdk, 1)).isFalse();
47+
@After
48+
public void tearDown() {
49+
heartBeatSharedPreferences.edit().clear().apply();
4950
}
5051

52+
@Config(sdk = 29)
5153
@Test
5254
public void storeOneHeartbeat_storesProperly() {
5355
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
@@ -73,6 +75,7 @@ public void storeOneHeartbeat_storesProperly() {
7375
assertThat(results.size()).isEqualTo(0);
7476
}
7577

78+
@Config(sdk = 29)
7679
@Test
7780
public void storeTwoHeartbeat_storesProperly() {
7881
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
@@ -82,18 +85,24 @@ public void storeTwoHeartbeat_storesProperly() {
8285
ArrayList<HeartBeatResult> results =
8386
(ArrayList<HeartBeatResult>) heartBeatInfoStorage.getAllHeartBeats();
8487
assertThat(results.size()).isEqualTo(2);
85-
assertThat(results.get(0).getUserAgent()).isEqualTo("test-agent");
86-
assertThat(results.get(0).getUsedDates())
88+
int userAgentTest = 0, userAgentTest1 = 1;
89+
if (results.get(0).getUserAgent().contains("-1")) {
90+
userAgentTest = 1;
91+
userAgentTest1 = 0;
92+
}
93+
assertThat(results.get(userAgentTest).getUserAgent()).isEqualTo("test-agent");
94+
assertThat(results.get(userAgentTest).getUsedDates())
8795
.isEqualTo(new ArrayList<String>(Collections.singleton("1970-01-01")));
88-
assertThat(results.get(1).getUserAgent()).isEqualTo("test-agent-1");
89-
assertThat(results.get(1).getUsedDates())
96+
assertThat(results.get(userAgentTest1).getUserAgent()).isEqualTo("test-agent-1");
97+
assertThat(results.get(userAgentTest1).getUsedDates())
9098
.isEqualTo(new ArrayList<String>(Collections.singleton("1970-01-02")));
9199
heartBeatInfoStorage.deleteAllHeartBeats();
92100
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
93101
results = (ArrayList<HeartBeatResult>) heartBeatInfoStorage.getAllHeartBeats();
94102
assertThat(results.size()).isEqualTo(0);
95103
}
96104

105+
@Config(sdk = 29)
97106
@Test
98107
public void storeExcessHeartBeats_cleanUpProperly() {
99108
for (int i = 0; i < HEART_BEAT_COUNT_LIMIT - 1; i++) {
@@ -118,9 +127,14 @@ public void storeExcessHeartBeats_cleanUpProperly() {
118127
heartBeatInfoStorage.storeHeartBeat((HEART_BEAT_COUNT_LIMIT) * (86400001L), "test-agent-1");
119128
results = (ArrayList<HeartBeatResult>) heartBeatInfoStorage.getAllHeartBeats();
120129
assertThat(results.size()).isEqualTo(2);
121-
assertThat(results.get(0).getUsedDates().size()).isEqualTo(HEART_BEAT_COUNT_LIMIT - 2);
122-
assertThat(results.get(0).getUsedDates()).doesNotContain("1970-01-01");
123-
assertThat(results.get(0).getUsedDates()).doesNotContain("1970-01-02");
130+
int testAgentIndex = 0;
131+
if (results.get(1).getUserAgent().equals("test-agent")) {
132+
testAgentIndex = 1;
133+
}
134+
assertThat(results.get(testAgentIndex).getUsedDates().size())
135+
.isEqualTo(HEART_BEAT_COUNT_LIMIT - 2);
136+
assertThat(results.get(testAgentIndex).getUsedDates()).doesNotContain("1970-01-01");
137+
assertThat(results.get(testAgentIndex).getUsedDates()).doesNotContain("1970-01-02");
124138

125139
heartBeatInfoStorage.deleteAllHeartBeats();
126140
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);

0 commit comments

Comments
 (0)