|
| 1 | +// Copyright 2020 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 | +// You may obtain a copy of the License at |
| 6 | +// |
| 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.firestore; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.assertFalse; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.junit.Assert.fail; |
| 21 | + |
| 22 | +import androidx.annotation.NonNull; |
| 23 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 24 | +import com.google.firebase.FirebaseApp; |
| 25 | +import com.google.firebase.FirebaseOptions; |
| 26 | +import com.google.firebase.emulators.EmulatedServiceSettings; |
| 27 | +import com.google.firebase.emulators.EmulatorSettings; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.robolectric.RobolectricTestRunner; |
| 31 | +import org.robolectric.annotation.Config; |
| 32 | + |
| 33 | +@RunWith(RobolectricTestRunner.class) |
| 34 | +@Config(manifest = Config.NONE) |
| 35 | +public class FirebaseFirestoreTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void getInstance_withEmulator() { |
| 39 | + FirebaseApp app = getApp("getInstance_withEmulator"); |
| 40 | + |
| 41 | + app.enableEmulators( |
| 42 | + new EmulatorSettings.Builder() |
| 43 | + .addEmulatedService( |
| 44 | + FirebaseFirestore.EMULATOR, new EmulatedServiceSettings("10.0.2.2", 8080)) |
| 45 | + .build()); |
| 46 | + |
| 47 | + FirebaseFirestore firestore = FirebaseFirestore.getInstance(app); |
| 48 | + FirebaseFirestoreSettings settings = firestore.getFirestoreSettings(); |
| 49 | + |
| 50 | + assertEquals(settings.getHost(), "10.0.2.2:8080"); |
| 51 | + assertFalse(settings.isSslEnabled()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void getInstance_withEmulator_mergeSettingsSuccess() { |
| 56 | + FirebaseApp app = getApp("getInstance_withEmulator_mergeSettingsSuccess"); |
| 57 | + app.enableEmulators( |
| 58 | + new EmulatorSettings.Builder() |
| 59 | + .addEmulatedService( |
| 60 | + FirebaseFirestore.EMULATOR, new EmulatedServiceSettings("10.0.2.2", 8080)) |
| 61 | + .build()); |
| 62 | + |
| 63 | + FirebaseFirestore firestore = FirebaseFirestore.getInstance(app); |
| 64 | + firestore.setFirestoreSettings( |
| 65 | + new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(false).build()); |
| 66 | + |
| 67 | + FirebaseFirestoreSettings settings = firestore.getFirestoreSettings(); |
| 68 | + |
| 69 | + assertEquals(settings.getHost(), "10.0.2.2:8080"); |
| 70 | + assertFalse(settings.isSslEnabled()); |
| 71 | + assertFalse(settings.isPersistenceEnabled()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void getInstance_withEmulator_mergeSettingsFailure() { |
| 76 | + FirebaseApp app = getApp("getInstance_withEmulator_mergeSettingsFailure"); |
| 77 | + app.enableEmulators( |
| 78 | + new EmulatorSettings.Builder() |
| 79 | + .addEmulatedService( |
| 80 | + FirebaseFirestore.EMULATOR, new EmulatedServiceSettings("10.0.2.2", 8080)) |
| 81 | + .build()); |
| 82 | + |
| 83 | + try { |
| 84 | + FirebaseFirestore firestore = FirebaseFirestore.getInstance(app); |
| 85 | + firestore.setFirestoreSettings( |
| 86 | + new FirebaseFirestoreSettings.Builder().setHost("myhost.com").build()); |
| 87 | + fail("Exception should be thrown"); |
| 88 | + } catch (Exception e) { |
| 89 | + assertTrue(e instanceof IllegalStateException); |
| 90 | + assertEquals( |
| 91 | + e.getMessage(), |
| 92 | + "Cannot specify the host in FirebaseFirestoreSettings when EmulatedServiceSettings is provided."); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void setSettings_repeatedSuccess() { |
| 98 | + FirebaseApp app = getApp("setSettings_repeatedSuccess"); |
| 99 | + FirebaseFirestore firestore = FirebaseFirestore.getInstance(app); |
| 100 | + |
| 101 | + FirebaseFirestoreSettings settings = |
| 102 | + new FirebaseFirestoreSettings.Builder().setHost("myhost.com").setSslEnabled(false).build(); |
| 103 | + firestore.setFirestoreSettings(settings); |
| 104 | + |
| 105 | + // This should 'start' Firestore |
| 106 | + DocumentReference reference = firestore.document("foo/bar"); |
| 107 | + |
| 108 | + // Second settings set should pass because the settings are equal |
| 109 | + firestore.setFirestoreSettings(settings); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void setSettings_repeatedSuccess_withEmulator() { |
| 114 | + FirebaseApp app = getApp("setSettings_repeatedSuccess_withEmulator"); |
| 115 | + app.enableEmulators( |
| 116 | + new EmulatorSettings.Builder() |
| 117 | + .addEmulatedService( |
| 118 | + FirebaseFirestore.EMULATOR, new EmulatedServiceSettings("10.0.2.2", 8080)) |
| 119 | + .build()); |
| 120 | + |
| 121 | + FirebaseFirestore firestore = FirebaseFirestore.getInstance(app); |
| 122 | + |
| 123 | + FirebaseFirestoreSettings settings = |
| 124 | + new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(false).build(); |
| 125 | + firestore.setFirestoreSettings(settings); |
| 126 | + |
| 127 | + // This should 'start' Firestore |
| 128 | + DocumentReference reference = firestore.document("foo/bar"); |
| 129 | + |
| 130 | + // Second settings set should pass because the settings are equal |
| 131 | + firestore.setFirestoreSettings(settings); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void setSettings_repeatedFailure() { |
| 136 | + FirebaseApp app = getApp("setSettings_repeatedFailure"); |
| 137 | + FirebaseFirestore firestore = FirebaseFirestore.getInstance(app); |
| 138 | + |
| 139 | + FirebaseFirestoreSettings settings = |
| 140 | + new FirebaseFirestoreSettings.Builder().setHost("myhost.com").setSslEnabled(false).build(); |
| 141 | + |
| 142 | + FirebaseFirestoreSettings otherSettings = |
| 143 | + new FirebaseFirestoreSettings.Builder() |
| 144 | + .setHost("otherhost.com") |
| 145 | + .setSslEnabled(false) |
| 146 | + .build(); |
| 147 | + |
| 148 | + firestore.setFirestoreSettings(settings); |
| 149 | + |
| 150 | + // This should 'start' Firestore |
| 151 | + DocumentReference reference = firestore.document("foo/bar"); |
| 152 | + |
| 153 | + try { |
| 154 | + firestore.setFirestoreSettings(otherSettings); |
| 155 | + fail("Exception should be thrown"); |
| 156 | + } catch (Exception e) { |
| 157 | + assertTrue(e instanceof IllegalStateException); |
| 158 | + assertTrue( |
| 159 | + e.getMessage() |
| 160 | + .startsWith( |
| 161 | + "FirebaseFirestore has already been started and its settings can no longer be changed.")); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @NonNull |
| 166 | + private FirebaseApp getApp(@NonNull String name) { |
| 167 | + return FirebaseApp.initializeApp( |
| 168 | + InstrumentationRegistry.getInstrumentation().getContext(), |
| 169 | + new FirebaseOptions.Builder() |
| 170 | + .setApplicationId("appid") |
| 171 | + .setApiKey("apikey") |
| 172 | + .setProjectId("projectid") |
| 173 | + .build(), |
| 174 | + name); |
| 175 | + } |
| 176 | +} |
0 commit comments