-
Notifications
You must be signed in to change notification settings - Fork 617
Packaging prototype #5154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Packaging prototype #5154
Changes from all commits
31d3d05
319cfe1
8985e56
3afcb15
c23c0f9
034fbe6
b7c1be5
9cc3ce5
806db8f
b9aca18
1723142
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2019 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. | ||
package com.google.firebase.ktx | ||
|
||
import android.content.Context | ||
import androidx.annotation.Keep | ||
import com.google.firebase.FirebaseApp | ||
import com.google.firebase.FirebaseOptions | ||
import com.google.firebase.components.Component | ||
import com.google.firebase.components.ComponentRegistrar | ||
|
||
/** | ||
* Single access point to all firebase SDKs from Kotlin. | ||
* | ||
* <p>Acts as a target for extension methods provided by sdks. | ||
*/ | ||
object Firebase | ||
|
||
/** Returns the default firebase app instance. */ | ||
val Firebase.app: FirebaseApp | ||
get() = FirebaseApp.getInstance() | ||
|
||
/** Returns a named firebase app instance. */ | ||
fun Firebase.app(name: String): FirebaseApp = FirebaseApp.getInstance(name) | ||
|
||
/** Initializes and returns a FirebaseApp. */ | ||
fun Firebase.initialize(context: Context): FirebaseApp? = FirebaseApp.initializeApp(context) | ||
|
||
/** Initializes and returns a FirebaseApp. */ | ||
fun Firebase.initialize(context: Context, options: FirebaseOptions): FirebaseApp = | ||
FirebaseApp.initializeApp(context, options) | ||
|
||
/** Initializes and returns a FirebaseApp. */ | ||
fun Firebase.initialize(context: Context, options: FirebaseOptions, name: String): FirebaseApp = | ||
FirebaseApp.initializeApp(context, options, name) | ||
|
||
/** Returns options of default FirebaseApp */ | ||
val Firebase.options: FirebaseOptions | ||
get() = Firebase.app.options | ||
|
||
internal const val LIBRARY_NAME: String = "fire-core-ktx" | ||
|
||
/** @suppress */ | ||
@Keep | ||
class FirebaseCommonKtxRegistrar : ComponentRegistrar { | ||
override fun getComponents(): List<Component<*>> { | ||
return listOf() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2019 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. | ||
|
||
package com.google.firebase | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import com.google.android.gms.tasks.Tasks | ||
import com.google.common.truth.Truth.assertThat | ||
import com.google.firebase.platforminfo.UserAgentPublisher | ||
import kotlinx.coroutines.tasks.await | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.fail | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
fun withApp(name: String, block: FirebaseApp.() -> Unit) { | ||
val app = | ||
Firebase.initialize( | ||
ApplicationProvider.getApplicationContext(), | ||
FirebaseOptions.Builder().setApplicationId("appId").build(), | ||
name | ||
) | ||
try { | ||
block(app) | ||
} finally { | ||
app.delete() | ||
} | ||
} | ||
|
||
class TestException(message: String) : Exception(message) | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class VersionTests { | ||
@Test | ||
fun libraryVersions_shouldBeRegisteredWithRuntime() { | ||
withApp("ktxTestApp") { | ||
val uaPublisher = get(UserAgentPublisher::class.java) | ||
assertThat(uaPublisher.userAgent).contains("kotlin") | ||
assertThat(uaPublisher.userAgent).contains(LIBRARY_NAME) | ||
} | ||
} | ||
} | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class KtxTests { | ||
@Test | ||
fun `Firebase#app should delegate to FirebaseApp#getInstance()`() { | ||
withApp(FirebaseApp.DEFAULT_APP_NAME) { | ||
assertThat(Firebase.app).isSameInstanceAs(FirebaseApp.getInstance()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Firebase#app(String) should delegate to FirebaseApp#getInstance(String)`() { | ||
val appName = "testApp" | ||
withApp(appName) { | ||
assertThat(Firebase.app(appName)).isSameInstanceAs(FirebaseApp.getInstance(appName)) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Firebase#options should delegate to FirebaseApp#getInstance()#options`() { | ||
withApp(FirebaseApp.DEFAULT_APP_NAME) { | ||
assertThat(Firebase.options).isSameInstanceAs(FirebaseApp.getInstance().options) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Firebase#initialize(Context, FirebaseOptions) should initialize the app correctly`() { | ||
val options = FirebaseOptions.Builder().setApplicationId("appId").build() | ||
val app = Firebase.initialize(ApplicationProvider.getApplicationContext(), options) | ||
try { | ||
assertThat(app).isNotNull() | ||
assertThat(app.name).isEqualTo(FirebaseApp.DEFAULT_APP_NAME) | ||
assertThat(app.options).isSameInstanceAs(options) | ||
assertThat(app.applicationContext) | ||
.isSameInstanceAs(ApplicationProvider.getApplicationContext()) | ||
} finally { | ||
app.delete() | ||
} | ||
} | ||
|
||
@Test | ||
fun `Firebase#initialize(Context, FirebaseOptions, String) should initialize the app correctly`() { | ||
val options = FirebaseOptions.Builder().setApplicationId("appId").build() | ||
val name = "appName" | ||
val app = Firebase.initialize(ApplicationProvider.getApplicationContext(), options, name) | ||
try { | ||
assertThat(app).isNotNull() | ||
assertThat(app.name).isEqualTo(name) | ||
assertThat(app.options).isSameInstanceAs(options) | ||
assertThat(app.applicationContext) | ||
.isSameInstanceAs(ApplicationProvider.getApplicationContext()) | ||
} finally { | ||
app.delete() | ||
} | ||
} | ||
} | ||
|
||
class CoroutinesPlayServicesTests { | ||
// We are only interested in the await() function offered by kotlinx-coroutines-play-services | ||
// So we're not testing the other functions provided by that library. | ||
|
||
@Test | ||
fun `Task#await() resolves to the same result as Task#getResult()`() = runTest { | ||
val task = Tasks.forResult(21) | ||
|
||
val expected = task.result | ||
val actual = task.await() | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
assertThat(task.isSuccessful).isTrue() | ||
assertThat(task.exception).isNull() | ||
} | ||
|
||
@Test | ||
fun `Task#await() throws an Exception for failing Tasks`() = runTest { | ||
val task = Tasks.forException<TestException>(TestException("some error happened")) | ||
|
||
try { | ||
task.await() | ||
fail("Task#await should throw an Exception") | ||
} catch (e: Exception) { | ||
assertThat(e).isInstanceOf(TestException::class.java) | ||
assertThat(task.isSuccessful).isFalse() | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,7 @@ class LibraryVersionTest : BaseTestCase() { | |
@Test | ||
fun `library version should be registered with runtime`() { | ||
val publisher = Firebase.app.get(UserAgentPublisher::class.java) | ||
assertThat(publisher.userAgent).contains(LIBRARY_NAME) | ||
assertThat(publisher.userAgent).contains("fire-fun-ktx") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only happens if the ktx sdk is present, right? or should it work even if there's no common-ktx artifact? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. currently i think with the new approach irrespective of anything this will be logged.. I think we should stop supporting this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to register what was discussed offline: there's value keep reporting the ktx usage in the user agent string. |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we mark everything in this file as deprecated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes