Skip to content

Commit 57ffa1f

Browse files
authored
Re-implement Unified Emulator Settings (#1802)
1 parent e585469 commit 57ffa1f

File tree

27 files changed

+181
-384
lines changed

27 files changed

+181
-384
lines changed

docs/ktx/firestore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ firestore.setFirestoreSettings(settings)
8585
**Kotlin + KTX**
8686
```kotlin
8787
firestore.firestoreSettings = firestoreSettings {
88-
host = "http://10.0.0.2:8080"
88+
host = "http://10.0.2.2:8080"
8989
isSslEnabled = false
9090
isPersistenceEnabled = false
9191
}

firebase-common/src/androidTest/java/com/google/firebase/FirebaseAppTest.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
import com.google.firebase.components.TestComponentOne;
4646
import com.google.firebase.components.TestComponentTwo;
4747
import com.google.firebase.components.TestUserAgentDependentComponent;
48-
import com.google.firebase.emulators.EmulatedServiceSettings;
49-
import com.google.firebase.emulators.EmulatorSettings;
50-
import com.google.firebase.emulators.FirebaseEmulator;
5148
import com.google.firebase.platforminfo.UserAgentPublisher;
5249
import com.google.firebase.testing.FirebaseAppRule;
5350
import java.lang.reflect.InvocationTargetException;
@@ -420,43 +417,6 @@ public void testDirectBoot_shouldPreserveDataCollectionAfterUnlock() {
420417
assertTrue(firebaseApp.isDataCollectionDefaultEnabled());
421418
}
422419

423-
@Test
424-
public void testEnableEmulators_shouldAllowDoubleSetBeforeAccess() {
425-
Context mockContext = createForwardingMockContext();
426-
FirebaseApp firebaseApp = FirebaseApp.initializeApp(mockContext);
427-
428-
// A developer would call FirebaseDatabase.EMULATOR but we can't introduce that
429-
// dependency for this test.
430-
FirebaseEmulator emulator = FirebaseEmulator.forName("database");
431-
432-
EmulatedServiceSettings databaseSettings = new EmulatedServiceSettings("10.0.2.2", 9000);
433-
EmulatorSettings emulatorSettings =
434-
new EmulatorSettings.Builder().addEmulatedService(emulator, databaseSettings).build();
435-
436-
// Set twice
437-
firebaseApp.enableEmulators(emulatorSettings);
438-
firebaseApp.enableEmulators(emulatorSettings);
439-
}
440-
441-
@Test
442-
public void testEnableEmulators_shouldThrowIfSetAfterAccess() {
443-
Context mockContext = createForwardingMockContext();
444-
FirebaseApp firebaseApp = FirebaseApp.initializeApp(mockContext);
445-
446-
FirebaseEmulator emulator = FirebaseEmulator.forName("database");
447-
448-
EmulatedServiceSettings databaseSettings = new EmulatedServiceSettings("10.0.2.2", 9000);
449-
EmulatorSettings emulatorSettings =
450-
new EmulatorSettings.Builder().addEmulatedService(emulator, databaseSettings).build();
451-
firebaseApp.enableEmulators(emulatorSettings);
452-
453-
// Access (as if from the Database SDK)
454-
firebaseApp.getEmulatorSettings().getServiceSettings(emulator);
455-
456-
// Try to set again
457-
assertThrows(IllegalStateException.class, () -> firebaseApp.enableEmulators(emulatorSettings));
458-
}
459-
460420
/** Returns mock context that forwards calls to targetContext and localBroadcastManager. */
461421
private Context createForwardingMockContext() {
462422
final UserManager spyUserManager = spy(targetContext.getSystemService(UserManager.class));

firebase-common/src/main/java/com/google/firebase/FirebaseApp.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import com.google.firebase.components.ComponentRegistrar;
4545
import com.google.firebase.components.ComponentRuntime;
4646
import com.google.firebase.components.Lazy;
47-
import com.google.firebase.emulators.EmulatorSettings;
4847
import com.google.firebase.events.Publisher;
4948
import com.google.firebase.heartbeatinfo.DefaultHeartBeatInfo;
5049
import com.google.firebase.internal.DataCollectionConfigStorage;
@@ -111,9 +110,6 @@ public class FirebaseApp {
111110
private final FirebaseOptions options;
112111
private final ComponentRuntime componentRuntime;
113112

114-
private final AtomicBoolean emulatorSettingsFrozen = new AtomicBoolean(false);
115-
private EmulatorSettings emulatorSettings = EmulatorSettings.DEFAULT;
116-
117113
// Default disabled. We released Firebase publicly without this feature, so making it default
118114
// enabled is a backwards incompatible change.
119115
private final AtomicBoolean automaticResourceManagementEnabled = new AtomicBoolean(false);
@@ -146,20 +142,6 @@ public FirebaseOptions getOptions() {
146142
return options;
147143
}
148144

149-
/**
150-
* Returns the specified {@link EmulatorSettings} or a default.
151-
*
152-
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
153-
*
154-
* @hide
155-
*/
156-
@NonNull
157-
public EmulatorSettings getEmulatorSettings() {
158-
checkNotDeleted();
159-
emulatorSettingsFrozen.set(true);
160-
return emulatorSettings;
161-
}
162-
163145
@Override
164146
public boolean equals(Object o) {
165147
if (!(o instanceof FirebaseApp)) {
@@ -323,26 +305,6 @@ public static FirebaseApp initializeApp(
323305
return firebaseApp;
324306
}
325307

326-
/**
327-
* Specify which services should access local emulators for this FirebaseApp instance.
328-
*
329-
* <p>For example, if the {@link EmulatorSettings} contain {@link
330-
* com.google.firebase.emulators.EmulatedServiceSettings} for {@link FirebaseDatabase#EMULATOR},
331-
* then calls to Cloud Firestore will communicate with the emulator rather than production.
332-
*
333-
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
334-
*
335-
* @param emulatorSettings the emulator settings for all services.
336-
* @hide
337-
*/
338-
public void enableEmulators(@NonNull EmulatorSettings emulatorSettings) {
339-
checkNotDeleted();
340-
Preconditions.checkState(
341-
!this.emulatorSettingsFrozen.get(),
342-
"Cannot enable emulators after Firebase SDKs have already been used.");
343-
this.emulatorSettings = emulatorSettings;
344-
}
345-
346308
/**
347309
* Deletes the {@link FirebaseApp} and all its data. All calls to this {@link FirebaseApp}
348310
* instance will throw once it has been called.

firebase-common/src/main/java/com/google/firebase/emulators/EmulatedServiceSettings.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
/**
2020
* Settings to connect a single Firebase service to a local emulator.
2121
*
22-
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
23-
*
24-
* @see EmulatorSettings
2522
* @hide
2623
*/
2724
public final class EmulatedServiceSettings {

firebase-common/src/main/java/com/google/firebase/emulators/EmulatorSettings.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

firebase-common/src/main/java/com/google/firebase/emulators/FirebaseEmulator.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

firebase-database/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Unreleased
22

33
- [changed] Added internal HTTP header to the WebChannel connection.
4+
- [feature] Realtime Database now supports connecting to a local emulator via
5+
`FirebaseDatabase#useEmulator()`
46

57
# 19.3.0
68
- [feature] Added ServerValue.increment() to support atomic field value increments

firebase-database/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ package com.google.firebase.database {
9696
method public void setLogLevel(@NonNull com.google.firebase.database.Logger.Level);
9797
method public void setPersistenceCacheSizeBytes(long);
9898
method public void setPersistenceEnabled(boolean);
99+
method public void useEmulator(@NonNull String, int);
99100
}
100101

101102
public abstract class GenericTypeIndicator<T> {

firebase-database/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
version=19.3.1
15+
version=19.4.0
1616
latestReleasedVersion=19.3.0
1717
android.enableUnitTestBinaryResources=true

firebase-database/src/androidTest/java/com/google/firebase/database/FirebaseDatabaseTest.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import com.google.firebase.database.core.persistence.MockPersistenceStorageEngine;
3131
import com.google.firebase.database.core.persistence.PersistenceManager;
3232
import com.google.firebase.database.future.WriteFuture;
33-
import com.google.firebase.emulators.EmulatedServiceSettings;
34-
import com.google.firebase.emulators.EmulatorSettings;
3533
import java.util.ArrayList;
3634
import java.util.Arrays;
3735
import java.util.List;
@@ -71,19 +69,36 @@ public void getInstanceForAppWithEmulator() {
7169
FirebaseApp app =
7270
appForDatabaseUrl(IntegrationTestValues.getAltNamespace(), "getInstanceForAppWithEmulator");
7371

74-
EmulatedServiceSettings serviceSettings = new EmulatedServiceSettings("10.0.2.2", 9000);
75-
EmulatorSettings emulatorSettings =
76-
new EmulatorSettings.Builder()
77-
.addEmulatedService(FirebaseDatabase.EMULATOR, serviceSettings)
78-
.build();
79-
app.enableEmulators(emulatorSettings);
80-
8172
FirebaseDatabase db = FirebaseDatabase.getInstance(app);
73+
db.useEmulator("10.0.2.2", 9000);
74+
8275
DatabaseReference rootRef = db.getReference();
8376
assertEquals(rootRef.toString(), "http://10.0.2.2:9000");
8477

8578
DatabaseReference urlReference = db.getReferenceFromUrl("https://otherns.firebaseio.com");
8679
assertEquals(urlReference.toString(), "http://10.0.2.2:9000");
80+
81+
DatabaseReference urlReferenceWithPath =
82+
db.getReferenceFromUrl("https://otherns.firebaseio.com/foo");
83+
assertEquals(urlReferenceWithPath.toString(), "http://10.0.2.2:9000/foo");
84+
}
85+
86+
@Test
87+
public void getInstanceForAppWithEmulator_throwsIfSetLate() {
88+
FirebaseApp app =
89+
appForDatabaseUrl(
90+
IntegrationTestValues.getAltNamespace(),
91+
"getInstanceForAppWithEmulator_throwsIfSetLate");
92+
93+
FirebaseDatabase db = FirebaseDatabase.getInstance(app);
94+
DatabaseReference rootRef = db.getReference();
95+
96+
try {
97+
db.useEmulator("10.0.2.2", 9000);
98+
fail("Expected to throw");
99+
} catch (IllegalStateException e) {
100+
// Expected to throw
101+
}
87102
}
88103

89104
@Test

0 commit comments

Comments
 (0)