Skip to content

feat(storage-ktx): destructure ListResult and TaskSnapshots #1644

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

Merged
merged 21 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2259a35
feat(storage-ktx): destructure ListResult and TaskSnapshots
thatfiredev Jun 11, 2020
e39ca86
test(storage-ktx): add unit tests for destructured classes
thatfiredev Jun 15, 2020
e3db267
MutableList --> List
thatfiredev Jul 13, 2020
8e2e554
Merge branch 'master' of https://github.com/firebase/firebase-android…
thatfiredev Jul 13, 2020
6676660
Merge branch 'master' of https://github.com/firebase/firebase-android…
thatfiredev Jul 13, 2020
6a9ead6
Merge remote-tracking branch 'origin/rpf-storage-ktx-desctructure' in…
thatfiredev Jul 13, 2020
5dec4e4
remove duplicated imports
thatfiredev Jul 13, 2020
b08685b
style: re-organize imports
thatfiredev Jul 13, 2020
2ac8d82
Merge branch 'rpf-storage-ktx-destructure-tests' of https://github.co…
thatfiredev Jul 15, 2020
d031aae
test(storage-ktx): add tests for destructuring declarations
thatfiredev Jul 15, 2020
aae2a90
style: remove wildcard import
thatfiredev Jul 15, 2020
3cc4778
test: create TestUtl.listResult() for ktx tests
thatfiredev Aug 10, 2020
ff2b0b9
chore: add dots to kdocs comments
thatfiredev Aug 10, 2020
47f485e
style: explicitly set listOf() type
thatfiredev Aug 10, 2020
eaa905f
refactor: move TestUtil.listResult to separate package
thatfiredev Aug 10, 2020
e1d6aef
chore: add copyright to /src/test/kotlin/TestUtil.java
thatfiredev Aug 10, 2020
294de57
refactor: google java format
thatfiredev Aug 10, 2020
919bba5
refactor: move TestUtil from /test/kotlin to /testUtil/java
thatfiredev Aug 10, 2020
77b4809
style: google java format
thatfiredev Aug 10, 2020
ee5e5eb
TestUtil --> KtxTestUtil
thatfiredev Aug 10, 2020
6ead390
chore(firebase-storage-ktx): add java 8
thatfiredev Aug 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions firebase-storage/ktx/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ package com.google.firebase.storage.ktx {

public final class StorageKt {
ctor public StorageKt();
method public static operator long component1(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
method public static operator long component1(@NonNull com.google.firebase.storage.StreamDownloadTask.TaskSnapshot);
method public static operator long component1(@NonNull com.google.firebase.storage.FileDownloadTask.TaskSnapshot);
method @NonNull public static operator java.util.List<com.google.firebase.storage.StorageReference> component1(@NonNull com.google.firebase.storage.ListResult);
method public static operator long component2(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
method public static operator long component2(@NonNull com.google.firebase.storage.StreamDownloadTask.TaskSnapshot);
method public static operator long component2(@NonNull com.google.firebase.storage.FileDownloadTask.TaskSnapshot);
method @NonNull public static operator java.util.List<com.google.firebase.storage.StorageReference> component2(@NonNull com.google.firebase.storage.ListResult);
method @Nullable public static operator com.google.firebase.storage.StorageMetadata component3(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
method @NonNull public static operator java.io.InputStream component3(@NonNull com.google.firebase.storage.StreamDownloadTask.TaskSnapshot);
method @Nullable public static operator String component3(@NonNull com.google.firebase.storage.ListResult);
method @Nullable public static operator android.net.Uri component4(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
method @NonNull public static com.google.firebase.storage.FirebaseStorage getStorage(@NonNull com.google.firebase.ktx.Firebase);
method @NonNull public static com.google.firebase.storage.FirebaseStorage storage(@NonNull com.google.firebase.ktx.Firebase, @NonNull String url);
method @NonNull public static com.google.firebase.storage.FirebaseStorage storage(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
Expand Down
11 changes: 11 additions & 0 deletions firebase-storage/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ android {
main.java.srcDirs += 'src/main/kotlin'
test.java {
srcDir 'src/test/kotlin'
srcDir '../src/testUtil/java'
}
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
testOptions.unitTests.includeAndroidResources = true

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
Expand All @@ -59,4 +69,5 @@ dependencies {
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation 'junit:junit:4.12'
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'org.mockito:mockito-core:3.3.3'
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.ktx.Firebase
import com.google.firebase.platforminfo.LibraryVersionComponent
import com.google.firebase.storage.FileDownloadTask
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.ListResult
import com.google.firebase.storage.StorageMetadata
import com.google.firebase.storage.StorageReference
import com.google.firebase.storage.StreamDownloadTask
import com.google.firebase.storage.UploadTask

/** Returns the [FirebaseStorage] instance of the default [FirebaseApp]. */
val Firebase.storage: FirebaseStorage
Expand All @@ -44,6 +49,90 @@ fun storageMetadata(init: StorageMetadata.Builder.() -> Unit): StorageMetadata {
return builder.build()
}

/**
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide bytesTransferred.
*
* @return the bytesTransferred of the [UploadTask.TaskSnapshot]
*/
operator fun UploadTask.TaskSnapshot.component1() = bytesTransferred

/**
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide totalByteCount.
*
* @return the totalByteCount of the [UploadTask.TaskSnapshot]
*/
operator fun UploadTask.TaskSnapshot.component2() = totalByteCount

/**
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide its metadata.
*
* @return the metadata of the [UploadTask.TaskSnapshot]
*/
operator fun UploadTask.TaskSnapshot.component3() = metadata

/**
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide its uploadSessionUri.
*
* @return the uploadSessionUri of the [UploadTask.TaskSnapshot]
*/
operator fun UploadTask.TaskSnapshot.component4() = uploadSessionUri

/**
* Destructuring declaration for [StreamDownloadTask.TaskSnapshot] to provide bytesTransferred.
*
* @return the bytesTransferred of the [StreamDownloadTask.TaskSnapshot]
*/
operator fun StreamDownloadTask.TaskSnapshot.component1() = bytesTransferred

/**
* Destructuring declaration for [StreamDownloadTask.TaskSnapshot] to provide totalByteCount.
*
* @return the totalByteCount of the [StreamDownloadTask.TaskSnapshot]
*/
operator fun StreamDownloadTask.TaskSnapshot.component2() = totalByteCount

/**
* Destructuring declaration for [StreamDownloadTask.TaskSnapshot] to provide its stream.
*
* @return the stream of the [StreamDownloadTask.TaskSnapshot]
*/
operator fun StreamDownloadTask.TaskSnapshot.component3() = stream

/**
* Destructuring declaration for [FileDownloadTask.TaskSnapshot] to provide bytesTransferred.
*
* @return the bytesTransferred of the [FileDownloadTask.TaskSnapshot]
*/
operator fun FileDownloadTask.TaskSnapshot.component1() = bytesTransferred

/**
* Destructuring declaration for [FileDownloadTask.TaskSnapshot] to provide totalByteCount.
*
* @return the totalByteCount of the [FileDownloadTask.TaskSnapshot]
*/
operator fun FileDownloadTask.TaskSnapshot.component2() = totalByteCount

/**
* Destructuring declaration for [ListResult] to provide its items.
*
* @return the items of the [ListResult]
*/
operator fun ListResult.component1(): List<StorageReference> = items

/**
* Destructuring declaration for [ListResult] to provide its prefixes.
*
* @return the prefixes of the [ListResult]
*/
operator fun ListResult.component2(): List<StorageReference> = prefixes

/**
* Destructuring declaration for [ListResult] to provide its pageToken.
*
* @return the pageToken of the [ListResult]
*/
operator fun ListResult.component3(): String? = pageToken

internal const val LIBRARY_NAME: String = "fire-stg-ktx"

/** @suppress */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@

package com.google.firebase.storage.ktx

import android.net.Uri
import com.google.common.truth.Truth.assertThat
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.app
import com.google.firebase.ktx.initialize
import com.google.firebase.platforminfo.UserAgentPublisher
import com.google.firebase.storage.FileDownloadTask
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.KtxTestUtil
import com.google.firebase.storage.StorageMetadata
import com.google.firebase.storage.StorageReference
import com.google.firebase.storage.StreamDownloadTask
import com.google.firebase.storage.UploadTask
import java.io.ByteArrayInputStream
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

Expand Down Expand Up @@ -105,6 +114,61 @@ class StorageTests : BaseTestCase() {
assertThat(metadata.getContentType()).isEqualTo("text/html")
assertThat(metadata.getCacheControl()).isEqualTo("no-cache")
}

@Test
fun `ListResult destructuring declarations work`() {
val mockListResult = KtxTestUtil.listResult(listOf<StorageReference>(), listOf<StorageReference>(), null)

val (items, prefixes, pageToken) = mockListResult
assertThat(items).isSameInstanceAs(mockListResult.items)
assertThat(prefixes).isSameInstanceAs(mockListResult.prefixes)
assertThat(pageToken).isSameInstanceAs(mockListResult.pageToken)
}

@Test
fun `UploadTask#TaskSnapshot destructuring declarations work`() {
val mockTaskSnapshot = Mockito.mock(UploadTask.TaskSnapshot::class.java)
`when`(mockTaskSnapshot.bytesTransferred).thenReturn(50)
`when`(mockTaskSnapshot.totalByteCount).thenReturn(100)
`when`(mockTaskSnapshot.metadata).thenReturn(storageMetadata {
contentType = "image/png"
contentEncoding = "utf-8"
})
`when`(mockTaskSnapshot.uploadSessionUri).thenReturn(Uri.parse("https://test.com"))

val (bytesTransferred, totalByteCount, metadata, sessionUri) = mockTaskSnapshot

assertThat(bytesTransferred).isSameInstanceAs(mockTaskSnapshot.bytesTransferred)
assertThat(totalByteCount).isSameInstanceAs(mockTaskSnapshot.totalByteCount)
assertThat(metadata).isSameInstanceAs(mockTaskSnapshot.metadata)
assertThat(sessionUri).isSameInstanceAs(mockTaskSnapshot.uploadSessionUri)
}

@Test
fun `StreamDownloadTask#TaskSnapshot destructuring declarations work`() {
val mockTaskSnapshot = Mockito.mock(StreamDownloadTask.TaskSnapshot::class.java)
`when`(mockTaskSnapshot.bytesTransferred).thenReturn(50)
`when`(mockTaskSnapshot.totalByteCount).thenReturn(100)
`when`(mockTaskSnapshot.stream).thenReturn(ByteArrayInputStream("test".toByteArray()))

val (bytesTransferred, totalByteCount, stream) = mockTaskSnapshot

assertThat(bytesTransferred).isSameInstanceAs(mockTaskSnapshot.bytesTransferred)
assertThat(totalByteCount).isSameInstanceAs(mockTaskSnapshot.totalByteCount)
assertThat(stream).isSameInstanceAs(mockTaskSnapshot.stream)
}

@Test
fun `FileDownloadTask#TaskSnapshot destructuring declarations work`() {
val mockTaskSnapshot = Mockito.mock(FileDownloadTask.TaskSnapshot::class.java)
`when`(mockTaskSnapshot.bytesTransferred).thenReturn(50)
`when`(mockTaskSnapshot.totalByteCount).thenReturn(100)

val (bytesTransferred, totalByteCount) = mockTaskSnapshot

assertThat(bytesTransferred).isSameInstanceAs(mockTaskSnapshot.bytesTransferred)
assertThat(totalByteCount).isSameInstanceAs(mockTaskSnapshot.totalByteCount)
}
}

@RunWith(RobolectricTestRunner::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 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.storage;

import androidx.annotation.Nullable;
import java.util.List;

public class KtxTestUtil {
public static ListResult listResult(
List<StorageReference> prefixes, List<StorageReference> items, @Nullable String pageToken) {
return new ListResult(prefixes, items, pageToken);
}
}