27
27
import androidx .test .core .app .ApplicationProvider ;
28
28
import androidx .test .runner .AndroidJUnit4 ;
29
29
import com .google .firebase .platforminfo .UserAgentPublisher ;
30
+ import java .io .ByteArrayOutputStream ;
31
+ import java .io .IOException ;
30
32
import java .util .ArrayList ;
31
33
import java .util .Base64 ;
32
34
import java .util .Collections ;
37
39
import java .util .concurrent .LinkedBlockingQueue ;
38
40
import java .util .concurrent .ThreadPoolExecutor ;
39
41
import java .util .concurrent .TimeUnit ;
42
+ import java .util .zip .GZIPOutputStream ;
40
43
import org .json .JSONArray ;
41
44
import org .json .JSONException ;
42
45
import org .json .JSONObject ;
@@ -96,7 +99,7 @@ public void getHeartBeatCode_noHeartBeat() {
96
99
97
100
@ Test
98
101
public void generateHeartBeat_oneHeartBeat ()
99
- throws ExecutionException , InterruptedException , JSONException {
102
+ throws ExecutionException , InterruptedException , JSONException , IOException {
100
103
ArrayList <HeartBeatResult > returnResults = new ArrayList <>();
101
104
returnResults .add (
102
105
HeartBeatResult .create (
@@ -111,9 +114,10 @@ public void generateHeartBeat_oneHeartBeat()
111
114
.getHeartBeatsHeader ()
112
115
.addOnCompleteListener (executor , getOnCompleteListener );
113
116
String expected =
114
- Base64 .getEncoder ()
117
+ Base64 .getUrlEncoder ()
115
118
.encodeToString (
116
- "{\" heartbeats\" :[{\" agent\" :\" test-agent\" ,\" date\" :\" [2015-02-03]\" }],\" version\" :\" 2\" }"
119
+ compress (
120
+ "{\" heartbeats\" :[{\" agent\" :\" test-agent\" ,\" date\" :\" [2015-02-03]\" }],\" version\" :\" 2\" }" )
117
121
.getBytes ());
118
122
assertThat (getOnCompleteListener .await ().replace ("\n " , "" )).isEqualTo (expected );
119
123
}
@@ -142,7 +146,7 @@ public void firstNewThenOld_synchronizedCorrectly()
142
146
143
147
@ Test
144
148
public void firstOldThenNew_synchronizedCorrectly ()
145
- throws ExecutionException , InterruptedException {
149
+ throws ExecutionException , InterruptedException , IOException {
146
150
Context context = ApplicationProvider .getApplicationContext ();
147
151
SharedPreferences heartBeatSharedPreferences =
148
152
context .getSharedPreferences ("testHeartBeat" , Context .MODE_PRIVATE );
@@ -152,7 +156,8 @@ public void firstOldThenNew_synchronizedCorrectly()
152
156
new DefaultHeartBeatController (
153
157
() -> heartBeatInfoStorage , logSources , executor , () -> publisher , context );
154
158
String emptyString =
155
- Base64 .getEncoder ().encodeToString ("{\" heartbeats\" :[],\" version\" :\" 2\" }" .getBytes ());
159
+ Base64 .getUrlEncoder ()
160
+ .encodeToString (compress ("{\" heartbeats\" :[],\" version\" :\" 2\" }" ).getBytes ());
156
161
controller .registerHeartBeat ().addOnCompleteListener (executor , storeOnCompleteListener );
157
162
storeOnCompleteListener .await ();
158
163
int heartBeatCode = controller .getHeartBeatCode ("test" ).getCode ();
@@ -169,7 +174,7 @@ public void firstOldThenNew_synchronizedCorrectly()
169
174
170
175
@ Test
171
176
public void generateHeartBeat_twoHeartBeatsSameUserAgent ()
172
- throws ExecutionException , InterruptedException , JSONException {
177
+ throws ExecutionException , InterruptedException , JSONException , IOException {
173
178
ArrayList <HeartBeatResult > returnResults = new ArrayList <>();
174
179
ArrayList <String > dateList = new ArrayList <>();
175
180
dateList .add ("2015-03-02" );
@@ -191,19 +196,26 @@ public void generateHeartBeat_twoHeartBeatsSameUserAgent()
191
196
JSONObject output = new JSONObject ();
192
197
JSONArray array = new JSONArray ();
193
198
JSONObject obj = new JSONObject ();
194
- ;
195
199
obj .put ("agent" , "test-agent" );
196
200
obj .put ("date" , dateList );
197
201
array .put (obj );
198
202
output .put ("heartbeats" , array );
199
203
output .put ("version" , "2" );
200
- String expected = Base64 .getEncoder ().encodeToString (output .toString ().getBytes ());
204
+ String expected = Base64 .getUrlEncoder ().encodeToString (compress ( output .toString () ).getBytes ());
201
205
assertThat (getOnCompleteListener .await ().replace ("\n " , "" )).isEqualTo (expected );
202
206
}
203
207
208
+ private String compress (String str ) throws IOException {
209
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
210
+ GZIPOutputStream gzip = new GZIPOutputStream (out );
211
+ gzip .write (str .toString ().getBytes ());
212
+ gzip .close ();
213
+ return out .toString ("UTF-8" );
214
+ }
215
+
204
216
@ Test
205
217
public void generateHeartBeat_twoHeartBeatstwoUserAgents ()
206
- throws ExecutionException , InterruptedException , JSONException {
218
+ throws ExecutionException , InterruptedException , JSONException , IOException {
207
219
ArrayList <HeartBeatResult > returnResults = new ArrayList <>();
208
220
returnResults .add (
209
221
HeartBeatResult .create (
@@ -225,9 +237,10 @@ public void generateHeartBeat_twoHeartBeatstwoUserAgents()
225
237
.getHeartBeatsHeader ()
226
238
.addOnCompleteListener (executor , getOnCompleteListener );
227
239
String expected =
228
- Base64 .getEncoder ()
240
+ Base64 .getUrlEncoder ()
229
241
.encodeToString (
230
- "{\" heartbeats\" :[{\" agent\" :\" test-agent\" ,\" date\" :\" [2015-03-02]\" },{\" agent\" :\" test-agent-1\" ,\" date\" :\" [2015-03-03]\" }],\" version\" :\" 2\" }"
242
+ compress (
243
+ "{\" heartbeats\" :[{\" agent\" :\" test-agent\" ,\" date\" :\" [2015-03-02]\" },{\" agent\" :\" test-agent-1\" ,\" date\" :\" [2015-03-03]\" }],\" version\" :\" 2\" }" )
231
244
.getBytes ());
232
245
assertThat (getOnCompleteListener .await ().replace ("\n " , "" )).isEqualTo (expected );
233
246
}
0 commit comments