-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathanalytics_test.cc
320 lines (269 loc) · 11.2 KB
/
analytics_test.cc
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if defined(FIREBASE_ANDROID_FOR_DESKTOP)
#define __ANDROID__
#include <jni.h>
#include "testing/run_all_tests.h"
#endif // defined(FIREBASE_ANDROID_FOR_DESKTOP)
#include "analytics/src/analytics_common.h"
#include "analytics/src/include/firebase/analytics.h"
#include "app/src/include/firebase/app.h"
#include "app/src/time.h"
#include "app/tests/include/firebase/app_for_testing.h"
#ifdef __ANDROID__
#include "app/src/semaphore.h"
#include "app/src/util_android.h"
#endif // __ANDROID__
#if defined(FIREBASE_ANDROID_FOR_DESKTOP)
#undef __ANDROID__
#endif // defined(FIREBASE_ANDROID_FOR_DESKTOP)
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "testing/config.h"
#include "testing/reporter.h"
#include "testing/ticker.h"
namespace firebase {
namespace analytics {
class AnalyticsTest : public ::testing::Test {
protected:
void SetUp() override {
firebase::testing::cppsdk::TickerReset();
firebase::testing::cppsdk::ConfigSet("{}");
reporter_.reset();
firebase_app_ = testing::CreateApp();
AddExpectationAndroid("FirebaseAnalytics.getInstance", {});
analytics::Initialize(*firebase_app_);
}
void TearDown() override {
firebase::testing::cppsdk::ConfigReset();
Terminate();
delete firebase_app_;
firebase_app_ = nullptr;
EXPECT_THAT(reporter_.getFakeReports(),
::testing::Eq(reporter_.getExpectations()));
}
void AddExpectationAndroid(const char* fake,
std::initializer_list<std::string> args) {
reporter_.addExpectation(fake, "", firebase::testing::cppsdk::kAndroid,
args);
}
void AddExpectationApple(const char* fake,
std::initializer_list<std::string> args) {
reporter_.addExpectation(fake, "", firebase::testing::cppsdk::kIos, args);
}
// Wait for a task executing on the main thread.
void WaitForMainThreadTask() {
#if defined(FIREBASE_ANDROID_FOR_DESKTOP)
Semaphore main_thread_signal(0);
util::RunOnMainThread(
firebase_app_->GetJNIEnv(), firebase_app_->activity(),
[](void* data) { reinterpret_cast<Semaphore*>(data)->Post(); },
&main_thread_signal);
main_thread_signal.Wait();
#endif // defined(FIREBASE_ANDROID_FOR_DESKTOP)
}
// Wait for a future up to the specified number of milliseconds.
template <typename T>
static void WaitForFutureWithTimeout(const Future<T>& future,
int timeout_milliseconds,
FutureStatus expected_status) {
while (future.status() != expected_status && timeout_milliseconds-- > 0) {
::firebase::internal::Sleep(1);
}
}
App* firebase_app_ = nullptr;
firebase::testing::cppsdk::Reporter reporter_;
};
TEST_F(AnalyticsTest, TestDestroyDefaultApp) {
EXPECT_TRUE(internal::IsInitialized());
delete firebase_app_;
firebase_app_ = nullptr;
EXPECT_FALSE(internal::IsInitialized());
}
TEST_F(AnalyticsTest, TestSetAnalyticsCollectionEnabled) {
AddExpectationAndroid("FirebaseAnalytics.setAnalyticsCollectionEnabled",
{"true"});
AddExpectationApple("+[FIRAnalytics setAnalyticsCollectionEnabled:]",
{"YES"});
SetAnalyticsCollectionEnabled(true);
}
TEST_F(AnalyticsTest, TestSetAnalyticsCollectionDisabled) {
AddExpectationAndroid("FirebaseAnalytics.setAnalyticsCollectionEnabled",
{"false"});
AddExpectationApple("+[FIRAnalytics setAnalyticsCollectionEnabled:]", {"NO"});
SetAnalyticsCollectionEnabled(false);
}
TEST_F(AnalyticsTest, TestLogEventString) {
AddExpectationAndroid("FirebaseAnalytics.logEvent",
{"my_event", "my_param=my_value"});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"my_event", "my_param=my_value"});
LogEvent("my_event", "my_param", "my_value");
}
TEST_F(AnalyticsTest, TestLogEventDouble) {
AddExpectationAndroid("FirebaseAnalytics.logEvent",
{"my_event", "my_param=1.01"});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"my_event", "my_param=1.01"});
LogEvent("my_event", "my_param", 1.01);
}
TEST_F(AnalyticsTest, TestLogEventInt64) {
int64_t value = 101;
AddExpectationAndroid("FirebaseAnalytics.logEvent",
{"my_event", "my_param=101"});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"my_event", "my_param=101"});
LogEvent("my_event", "my_param", value);
}
TEST_F(AnalyticsTest, TestLogEventInt) {
AddExpectationAndroid("FirebaseAnalytics.logEvent",
{"my_event", "my_param=101"});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"my_event", "my_param=101"});
LogEvent("my_event", "my_param", 101);
}
TEST_F(AnalyticsTest, TestLogEvent) {
AddExpectationAndroid("FirebaseAnalytics.logEvent", {"my_event", ""});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"my_event", ""});
LogEvent("my_event");
}
TEST_F(AnalyticsTest, TestLogEvent40CharName) {
AddExpectationAndroid("FirebaseAnalytics.logEvent",
{"0123456789012345678901234567890123456789", ""});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"0123456789012345678901234567890123456789", ""});
LogEvent("0123456789012345678901234567890123456789");
}
TEST_F(AnalyticsTest, TestLogEventString40CharName) {
AddExpectationAndroid(
"FirebaseAnalytics.logEvent",
{"my_event", "0123456789012345678901234567890123456789=my_value"});
AddExpectationApple(
"+[FIRAnalytics logEventWithName:parameters:]",
{"my_event", "0123456789012345678901234567890123456789=my_value"});
LogEvent("my_event", "0123456789012345678901234567890123456789", "my_value");
}
TEST_F(AnalyticsTest, TestLogEventString100CharValue) {
const std::string long_string =
"0123456789012345678901234567890123456789"
"012345678901234567890123456789012345678901234567890123456789";
const std::string result = "my_event=" + long_string;
AddExpectationAndroid("FirebaseAnalytics.logEvent",
{"my_event", result.c_str()});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"my_event", result.c_str()});
LogEvent("my_event", "my_event", long_string.c_str());
}
TEST_F(AnalyticsTest, TestLogEventParameters) {
// Params are sorted alphabetically by mock.
AddExpectationAndroid(
"FirebaseAnalytics.logEvent",
{"my_event",
"my_param_bool=1,my_param_double=1.01,my_param_int=101,"
"my_param_string=my_value"});
AddExpectationApple("+[FIRAnalytics logEventWithName:parameters:]",
{"my_event",
"my_param_bool=1,my_param_double=1.01,my_param_int=101,"
"my_param_string=my_value"});
Parameter parameters[] = {
Parameter("my_param_string", "my_value"),
Parameter("my_param_double", 1.01),
Parameter("my_param_int", 101),
Parameter("my_param_bool", true),
};
LogEvent("my_event", parameters, sizeof(parameters) / sizeof(parameters[0]));
}
TEST_F(AnalyticsTest,
TestInitiateOnDeviceConversionMeasurementWithEmailAddress) {
// InitiateOnDeviceConversionMeasurementWithEmailAddress is no-op on Android
AddExpectationApple(
"+[FIRAnalytics initiateOnDeviceConversionMeasurementWithEmailAddress:]",
{"my_email"});
InitiateOnDeviceConversionMeasurementWithEmailAddress("my_email");
}
TEST_F(AnalyticsTest,
TestInitiateOnDeviceConversionMeasurementWithPhoneNumber) {
// InitiateOnDeviceConversionMeasurementWithPhoneNumber is no-op on Android
AddExpectationApple(
"+[FIRAnalytics initiateOnDeviceConversionMeasurementWithPhoneNumber:]",
{"+15551234567"});
InitiateOnDeviceConversionMeasurementWithPhoneNumber("+15551234567");
}
TEST_F(AnalyticsTest, TestSetUserProperty) {
AddExpectationAndroid("FirebaseAnalytics.setUserProperty",
{"my_property", "my_value"});
AddExpectationApple("+[FIRAnalytics setUserPropertyString:forName:]",
{"my_property", "my_value"});
SetUserProperty("my_property", "my_value");
}
TEST_F(AnalyticsTest, TestSetUserPropertyNull) {
AddExpectationAndroid("FirebaseAnalytics.setUserProperty",
{"my_property", "null"});
AddExpectationApple("+[FIRAnalytics setUserPropertyString:forName:]",
{"my_property", "nil"});
SetUserProperty("my_property", nullptr);
}
TEST_F(AnalyticsTest, TestSetUserId) {
AddExpectationAndroid("FirebaseAnalytics.setUserId", {"my_user_id"});
AddExpectationApple("+[FIRAnalytics setUserID:]", {"my_user_id"});
SetUserId("my_user_id");
}
TEST_F(AnalyticsTest, TestSetUserIdNull) {
AddExpectationAndroid("FirebaseAnalytics.setUserId", {"null"});
AddExpectationApple("+[FIRAnalytics setUserID:]", {"nil"});
SetUserId(nullptr);
}
TEST_F(AnalyticsTest, TestSetSessionTimeoutDuration) {
AddExpectationAndroid("FirebaseAnalytics.setSessionTimeoutDuration",
{"1000"});
AddExpectationApple("+[FIRAnalytics setSessionTimeoutInterval:]", {"1.000"});
SetSessionTimeoutDuration(1000);
}
TEST_F(AnalyticsTest, TestResetAnalyticsData) {
AddExpectationAndroid("FirebaseAnalytics.resetAnalyticsData", {});
AddExpectationApple("+[FIRAnalytics resetAnalyticsData]", {});
AddExpectationApple("+[FIRAnalytics appInstanceID]", {});
ResetAnalyticsData();
}
TEST_F(AnalyticsTest, TestGetAnalyticsInstanceId) {
AddExpectationAndroid("FirebaseAnalytics.getAppInstanceId", {});
AddExpectationApple("+[FIRAnalytics appInstanceID]", {});
auto result = GetAnalyticsInstanceId();
// Wait for up to a second to fetch the ID.
WaitForFutureWithTimeout(result, 1000, firebase::kFutureStatusComplete);
EXPECT_EQ(firebase::kFutureStatusComplete, result.status());
EXPECT_EQ(std::string("FakeAnalyticsInstanceId0"), *result.result());
}
TEST_F(AnalyticsTest, TestSetDefaultEventParameters) {
std::map<std::string, firebase::Variant> parameters = {
{"key1", firebase::Variant("value1")},
{"key2", firebase::Variant(12345)},
{"key3", firebase::Variant(1.01)},
{"key4", firebase::Variant("my_value")},
{"key5", firebase::Variant(true)},
{"key6", firebase::Variant::EmptyMap()},
};
AddExpectationAndroid(
"FirebaseAnalytics.setDefaultEventParameters",
{"key1=value1,key2=12345,key3=1.01,key4=my_value,key5=1"});
AddExpectationApple(
"+[FIRAnalytics setDefaultEventParameters:]",
{"key1=value1,key2=12345,key3=1.01,key4=my_value,key5=1"});
SetDefaultEventParameters(parameters);
}
} // namespace analytics
} // namespace firebase