Skip to content

Commit 3e94fe3

Browse files
Improve Kotlin API, add logs and tests
1 parent 21a39c5 commit 3e94fe3

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

firebase-config/src/main/java/com/google/firebase/remoteconfig/RemoteConfig.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ fun remoteConfigSettings(
4848
return builder.build()
4949
}
5050

51+
fun customSignals(builder: CustomSignals.Builder.() -> Unit) =
52+
CustomSignals.Builder().apply(builder).build()
53+
5154
/**
5255
* Starts listening for config updates from the Remote Config backend and emits [ConfigUpdate]s via
5356
* a [Flow]. See [FirebaseRemoteConfig.addOnConfigUpdateListener] for more information.

firebase-config/src/main/java/com/google/firebase/remoteconfig/internal/ConfigFetchHttpClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ private JSONObject createFetchRequestBody(
353353

354354
if (!customSignalsMap.isEmpty()) {
355355
requestBodyMap.put(CUSTOM_SIGNALS, new JSONObject(customSignalsMap));
356+
357+
// Log the custom signals during fetch.
358+
Log.d(TAG, "Fetching with custom signals: " + customSignalsMap);
356359
}
357360

358361
if (firstOpenTime != null) {

firebase-config/src/main/java/com/google/firebase/remoteconfig/internal/ConfigSharedPrefsClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ public void setCustomSignals(Map<String, String> newCustomSignals) {
314314
.edit()
315315
.putString(CUSTOM_SIGNALS, new JSONObject(existingCustomSignals).toString())
316316
.commit();
317+
318+
// Log the final updated custom signals.
319+
Log.d(TAG, "Updated custom signals: " + getCustomSignals());
317320
}
318321
}
319322

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
//
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.remoteconfig;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertTrue;
19+
20+
import com.google.common.collect.ImmutableMap;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.robolectric.RobolectricTestRunner;
26+
import org.robolectric.annotation.Config;
27+
28+
/** Unit tests for the {@link CustomSignals}.*/
29+
@RunWith(RobolectricTestRunner.class)
30+
@Config(manifest = Config.NONE)
31+
public class CustomSignalsTest {
32+
@Test
33+
public void testCustomSignals_builderPutString() {
34+
CustomSignals customSignals =
35+
new CustomSignals.Builder().put("key1", "value1").put("key2", "value2").build();
36+
Map<String, String> expectedSignals = ImmutableMap.of("key1", "value1", "key2", "value2");
37+
assertEquals(expectedSignals, customSignals.customSignals);
38+
}
39+
40+
@Test
41+
public void testCustomSignals_builderPutLong() {
42+
CustomSignals customSignals =
43+
new CustomSignals.Builder().put("key1", 123L).put("key2", 456L).build();
44+
Map<String, String> expectedSignals = ImmutableMap.of("key1", "123", "key2", "456");
45+
assertEquals(expectedSignals, customSignals.customSignals);
46+
}
47+
48+
@Test
49+
public void testCustomSignals_builderPutDouble() {
50+
CustomSignals customSignals =
51+
new CustomSignals.Builder().put("key1", 12.34).put("key2", 56.78).build();
52+
Map<String, String> expectedSignals = ImmutableMap.of("key1", "12.34", "key2", "56.78");
53+
assertEquals(expectedSignals, customSignals.customSignals);
54+
}
55+
56+
@Test
57+
public void testCustomSignals_builderPutMixedTypes() {
58+
CustomSignals customSignals =
59+
new CustomSignals.Builder()
60+
.put("key1", "value1")
61+
.put("key2", 123L)
62+
.put("key3", 45.67)
63+
.build();
64+
Map<String, String> expectedSignals =
65+
ImmutableMap.of("key1", "value1", "key2", "123", "key3", "45.67");
66+
assertEquals(expectedSignals, customSignals.customSignals);
67+
}
68+
69+
@Test
70+
public void testCustomSignals_builderPutNullValue() {
71+
CustomSignals customSignals = new CustomSignals.Builder().put("key1", null).build();
72+
Map<String, String> expectedSignals = new HashMap<>();
73+
expectedSignals.put("key1", null);
74+
assertEquals(expectedSignals, customSignals.customSignals);
75+
}
76+
77+
@Test
78+
public void testCustomSignals_builderEmpty() {
79+
CustomSignals customSignals = new CustomSignals.Builder().build();
80+
assertTrue(customSignals.customSignals.isEmpty());
81+
}
82+
}

firebase-config/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,21 @@ public void realtimeRequest_setRequestParams_succeedsWithCorrectParams() throws
16601660
assertThat(fakeConnection.getRequestMethod()).isEqualTo("POST");
16611661
}
16621662

1663+
@Test
1664+
public void setCustomSignals_succeeds_and_calls_sharedPrefsClient() {
1665+
CustomSignals customSignals =
1666+
new CustomSignals.Builder()
1667+
.put("key1", "value1")
1668+
.put("key2", 123L)
1669+
.put("key3", 12.34)
1670+
.build();
1671+
1672+
Task<Void> setterTask = frc.setCustomSignals(customSignals);
1673+
1674+
assertThat(setterTask.isSuccessful()).isTrue();
1675+
verify(sharedPrefsClient).setCustomSignals(customSignals.customSignals);
1676+
}
1677+
16631678
private static void loadCacheWithConfig(
16641679
ConfigCacheClient cacheClient, ConfigContainer container) {
16651680
when(cacheClient.getBlocking()).thenReturn(container);

firebase-config/src/test/java/com/google/firebase/remoteconfig/RemoteConfigTests.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.firebase.remoteconfig
1818

1919
import androidx.test.core.app.ApplicationProvider
20+
import com.google.common.collect.ImmutableMap
2021
import com.google.common.truth.Truth.assertThat
2122
import com.google.common.util.concurrent.MoreExecutors
2223
import com.google.firebase.Firebase
@@ -147,6 +148,60 @@ class ConfigTests : BaseTestCase() {
147148
`when`(mockGetHandler.getValue("KEY")).thenReturn(StringRemoteConfigValue("non default value"))
148149
assertThat(remoteConfig["KEY"].asString()).isEqualTo("non default value")
149150
}
151+
152+
@Test
153+
fun `Custom Signals builder put string`() {
154+
val customSignals = customSignals {
155+
put("key1", "value1")
156+
put("key2", "value2")
157+
}
158+
val expectedSignals = ImmutableMap.of("key1", "value1", "key2", "value2")
159+
assertThat(customSignals.customSignals).isEqualTo(expectedSignals)
160+
}
161+
162+
@Test
163+
fun `Custom Signals builder put long`() {
164+
val customSignals = customSignals {
165+
put("key1", 123L)
166+
put("key2", 456L)
167+
}
168+
val expectedSignals = ImmutableMap.of("key1", "123", "key2", "456")
169+
assertThat(customSignals.customSignals).isEqualTo(expectedSignals)
170+
}
171+
172+
@Test
173+
fun `Custom Signals builder put double`() {
174+
val customSignals = customSignals {
175+
put("key1", 12.34)
176+
put("key2", 56.78)
177+
}
178+
val expectedSignals = ImmutableMap.of("key1", "12.34", "key2", "56.78")
179+
assertThat(customSignals.customSignals).isEqualTo(expectedSignals)
180+
}
181+
182+
@Test
183+
fun `Custom Signals builder put mixed types`() {
184+
val customSignals = customSignals {
185+
put("key1", "value1")
186+
put("key2", 123L)
187+
put("key3", 45.67)
188+
}
189+
val expectedSignals = ImmutableMap.of("key1", "value1", "key2", "123", "key3", "45.67")
190+
assertThat(customSignals.customSignals).isEqualTo(expectedSignals)
191+
}
192+
193+
@Test
194+
fun `Custom Signals builder put null value`() {
195+
val customSignals = customSignals { put("key1", null) }
196+
val expectedSignals = mapOf("key1" to null)
197+
assertThat(customSignals.customSignals).isEqualTo(expectedSignals)
198+
}
199+
200+
@Test
201+
fun `Custom Signals empty builder`() {
202+
val customSignals = customSignals {}
203+
assertThat(customSignals.customSignals).isEmpty()
204+
}
150205
}
151206

152207
@RunWith(RobolectricTestRunner::class)

0 commit comments

Comments
 (0)