Skip to content

Commit 8c187fd

Browse files
authored
feat(storage-ktx): destructure ListResult and TaskSnapshots (#1644)
* feat(storage-ktx): destructure ListResult and TaskSnapshots
1 parent a69ede1 commit 8c187fd

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

firebase-storage/ktx/api.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ package com.google.firebase.storage.ktx {
33

44
public final class StorageKt {
55
ctor public StorageKt();
6+
method public static operator long component1(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
7+
method public static operator long component1(@NonNull com.google.firebase.storage.StreamDownloadTask.TaskSnapshot);
8+
method public static operator long component1(@NonNull com.google.firebase.storage.FileDownloadTask.TaskSnapshot);
9+
method @NonNull public static operator java.util.List<com.google.firebase.storage.StorageReference> component1(@NonNull com.google.firebase.storage.ListResult);
10+
method public static operator long component2(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
11+
method public static operator long component2(@NonNull com.google.firebase.storage.StreamDownloadTask.TaskSnapshot);
12+
method public static operator long component2(@NonNull com.google.firebase.storage.FileDownloadTask.TaskSnapshot);
13+
method @NonNull public static operator java.util.List<com.google.firebase.storage.StorageReference> component2(@NonNull com.google.firebase.storage.ListResult);
14+
method @Nullable public static operator com.google.firebase.storage.StorageMetadata component3(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
15+
method @NonNull public static operator java.io.InputStream component3(@NonNull com.google.firebase.storage.StreamDownloadTask.TaskSnapshot);
16+
method @Nullable public static operator String component3(@NonNull com.google.firebase.storage.ListResult);
17+
method @Nullable public static operator android.net.Uri component4(@NonNull com.google.firebase.storage.UploadTask.TaskSnapshot);
618
method @NonNull public static com.google.firebase.storage.FirebaseStorage getStorage(@NonNull com.google.firebase.ktx.Firebase);
719
method @NonNull public static com.google.firebase.storage.FirebaseStorage storage(@NonNull com.google.firebase.ktx.Firebase, @NonNull String url);
820
method @NonNull public static com.google.firebase.storage.FirebaseStorage storage(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);

firebase-storage/ktx/ktx.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,20 @@ android {
3636
main.java.srcDirs += 'src/main/kotlin'
3737
test.java {
3838
srcDir 'src/test/kotlin'
39+
srcDir '../src/testUtil/java'
3940
}
4041
androidTest.java.srcDirs += 'src/androidTest/kotlin'
4142
}
4243
testOptions.unitTests.includeAndroidResources = true
44+
45+
compileOptions {
46+
sourceCompatibility JavaVersion.VERSION_1_8
47+
targetCompatibility JavaVersion.VERSION_1_8
48+
}
49+
50+
kotlinOptions {
51+
jvmTarget = "1.8"
52+
}
4353
}
4454

4555
dependencies {
@@ -59,4 +69,5 @@ dependencies {
5969
testImplementation "org.robolectric:robolectric:$robolectricVersion"
6070
testImplementation 'junit:junit:4.12'
6171
testImplementation "com.google.truth:truth:$googleTruthVersion"
72+
testImplementation 'org.mockito:mockito-core:3.3.3'
6273
}

firebase-storage/ktx/src/main/kotlin/com/google/firebase/storage/ktx/Storage.kt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ import com.google.firebase.components.Component
2020
import com.google.firebase.components.ComponentRegistrar
2121
import com.google.firebase.ktx.Firebase
2222
import com.google.firebase.platforminfo.LibraryVersionComponent
23+
import com.google.firebase.storage.FileDownloadTask
2324
import com.google.firebase.storage.FirebaseStorage
25+
import com.google.firebase.storage.ListResult
2426
import com.google.firebase.storage.StorageMetadata
27+
import com.google.firebase.storage.StorageReference
28+
import com.google.firebase.storage.StreamDownloadTask
29+
import com.google.firebase.storage.UploadTask
2530

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

52+
/**
53+
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide bytesTransferred.
54+
*
55+
* @return the bytesTransferred of the [UploadTask.TaskSnapshot]
56+
*/
57+
operator fun UploadTask.TaskSnapshot.component1() = bytesTransferred
58+
59+
/**
60+
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide totalByteCount.
61+
*
62+
* @return the totalByteCount of the [UploadTask.TaskSnapshot]
63+
*/
64+
operator fun UploadTask.TaskSnapshot.component2() = totalByteCount
65+
66+
/**
67+
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide its metadata.
68+
*
69+
* @return the metadata of the [UploadTask.TaskSnapshot]
70+
*/
71+
operator fun UploadTask.TaskSnapshot.component3() = metadata
72+
73+
/**
74+
* Destructuring declaration for [UploadTask.TaskSnapshot] to provide its uploadSessionUri.
75+
*
76+
* @return the uploadSessionUri of the [UploadTask.TaskSnapshot]
77+
*/
78+
operator fun UploadTask.TaskSnapshot.component4() = uploadSessionUri
79+
80+
/**
81+
* Destructuring declaration for [StreamDownloadTask.TaskSnapshot] to provide bytesTransferred.
82+
*
83+
* @return the bytesTransferred of the [StreamDownloadTask.TaskSnapshot]
84+
*/
85+
operator fun StreamDownloadTask.TaskSnapshot.component1() = bytesTransferred
86+
87+
/**
88+
* Destructuring declaration for [StreamDownloadTask.TaskSnapshot] to provide totalByteCount.
89+
*
90+
* @return the totalByteCount of the [StreamDownloadTask.TaskSnapshot]
91+
*/
92+
operator fun StreamDownloadTask.TaskSnapshot.component2() = totalByteCount
93+
94+
/**
95+
* Destructuring declaration for [StreamDownloadTask.TaskSnapshot] to provide its stream.
96+
*
97+
* @return the stream of the [StreamDownloadTask.TaskSnapshot]
98+
*/
99+
operator fun StreamDownloadTask.TaskSnapshot.component3() = stream
100+
101+
/**
102+
* Destructuring declaration for [FileDownloadTask.TaskSnapshot] to provide bytesTransferred.
103+
*
104+
* @return the bytesTransferred of the [FileDownloadTask.TaskSnapshot]
105+
*/
106+
operator fun FileDownloadTask.TaskSnapshot.component1() = bytesTransferred
107+
108+
/**
109+
* Destructuring declaration for [FileDownloadTask.TaskSnapshot] to provide totalByteCount.
110+
*
111+
* @return the totalByteCount of the [FileDownloadTask.TaskSnapshot]
112+
*/
113+
operator fun FileDownloadTask.TaskSnapshot.component2() = totalByteCount
114+
115+
/**
116+
* Destructuring declaration for [ListResult] to provide its items.
117+
*
118+
* @return the items of the [ListResult]
119+
*/
120+
operator fun ListResult.component1(): List<StorageReference> = items
121+
122+
/**
123+
* Destructuring declaration for [ListResult] to provide its prefixes.
124+
*
125+
* @return the prefixes of the [ListResult]
126+
*/
127+
operator fun ListResult.component2(): List<StorageReference> = prefixes
128+
129+
/**
130+
* Destructuring declaration for [ListResult] to provide its pageToken.
131+
*
132+
* @return the pageToken of the [ListResult]
133+
*/
134+
operator fun ListResult.component3(): String? = pageToken
135+
47136
internal const val LIBRARY_NAME: String = "fire-stg-ktx"
48137

49138
/** @suppress */

firebase-storage/ktx/src/test/kotlin/com/google/firebase/storage/ktx/StorageTest.kt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,28 @@
1414

1515
package com.google.firebase.storage.ktx
1616

17+
import android.net.Uri
1718
import com.google.common.truth.Truth.assertThat
1819
import com.google.firebase.FirebaseApp
1920
import com.google.firebase.FirebaseOptions
2021
import com.google.firebase.ktx.Firebase
2122
import com.google.firebase.ktx.app
2223
import com.google.firebase.ktx.initialize
2324
import com.google.firebase.platforminfo.UserAgentPublisher
25+
import com.google.firebase.storage.FileDownloadTask
2426
import com.google.firebase.storage.FirebaseStorage
27+
import com.google.firebase.storage.KtxTestUtil
2528
import com.google.firebase.storage.StorageMetadata
29+
import com.google.firebase.storage.StorageReference
30+
import com.google.firebase.storage.StreamDownloadTask
31+
import com.google.firebase.storage.UploadTask
32+
import java.io.ByteArrayInputStream
2633
import org.junit.After
2734
import org.junit.Before
2835
import org.junit.Test
2936
import org.junit.runner.RunWith
37+
import org.mockito.Mockito
38+
import org.mockito.Mockito.`when`
3039
import org.robolectric.RobolectricTestRunner
3140
import org.robolectric.RuntimeEnvironment
3241

@@ -105,6 +114,61 @@ class StorageTests : BaseTestCase() {
105114
assertThat(metadata.getContentType()).isEqualTo("text/html")
106115
assertThat(metadata.getCacheControl()).isEqualTo("no-cache")
107116
}
117+
118+
@Test
119+
fun `ListResult destructuring declarations work`() {
120+
val mockListResult = KtxTestUtil.listResult(listOf<StorageReference>(), listOf<StorageReference>(), null)
121+
122+
val (items, prefixes, pageToken) = mockListResult
123+
assertThat(items).isSameInstanceAs(mockListResult.items)
124+
assertThat(prefixes).isSameInstanceAs(mockListResult.prefixes)
125+
assertThat(pageToken).isSameInstanceAs(mockListResult.pageToken)
126+
}
127+
128+
@Test
129+
fun `UploadTask#TaskSnapshot destructuring declarations work`() {
130+
val mockTaskSnapshot = Mockito.mock(UploadTask.TaskSnapshot::class.java)
131+
`when`(mockTaskSnapshot.bytesTransferred).thenReturn(50)
132+
`when`(mockTaskSnapshot.totalByteCount).thenReturn(100)
133+
`when`(mockTaskSnapshot.metadata).thenReturn(storageMetadata {
134+
contentType = "image/png"
135+
contentEncoding = "utf-8"
136+
})
137+
`when`(mockTaskSnapshot.uploadSessionUri).thenReturn(Uri.parse("https://test.com"))
138+
139+
val (bytesTransferred, totalByteCount, metadata, sessionUri) = mockTaskSnapshot
140+
141+
assertThat(bytesTransferred).isSameInstanceAs(mockTaskSnapshot.bytesTransferred)
142+
assertThat(totalByteCount).isSameInstanceAs(mockTaskSnapshot.totalByteCount)
143+
assertThat(metadata).isSameInstanceAs(mockTaskSnapshot.metadata)
144+
assertThat(sessionUri).isSameInstanceAs(mockTaskSnapshot.uploadSessionUri)
145+
}
146+
147+
@Test
148+
fun `StreamDownloadTask#TaskSnapshot destructuring declarations work`() {
149+
val mockTaskSnapshot = Mockito.mock(StreamDownloadTask.TaskSnapshot::class.java)
150+
`when`(mockTaskSnapshot.bytesTransferred).thenReturn(50)
151+
`when`(mockTaskSnapshot.totalByteCount).thenReturn(100)
152+
`when`(mockTaskSnapshot.stream).thenReturn(ByteArrayInputStream("test".toByteArray()))
153+
154+
val (bytesTransferred, totalByteCount, stream) = mockTaskSnapshot
155+
156+
assertThat(bytesTransferred).isSameInstanceAs(mockTaskSnapshot.bytesTransferred)
157+
assertThat(totalByteCount).isSameInstanceAs(mockTaskSnapshot.totalByteCount)
158+
assertThat(stream).isSameInstanceAs(mockTaskSnapshot.stream)
159+
}
160+
161+
@Test
162+
fun `FileDownloadTask#TaskSnapshot destructuring declarations work`() {
163+
val mockTaskSnapshot = Mockito.mock(FileDownloadTask.TaskSnapshot::class.java)
164+
`when`(mockTaskSnapshot.bytesTransferred).thenReturn(50)
165+
`when`(mockTaskSnapshot.totalByteCount).thenReturn(100)
166+
167+
val (bytesTransferred, totalByteCount) = mockTaskSnapshot
168+
169+
assertThat(bytesTransferred).isSameInstanceAs(mockTaskSnapshot.bytesTransferred)
170+
assertThat(totalByteCount).isSameInstanceAs(mockTaskSnapshot.totalByteCount)
171+
}
108172
}
109173

110174
@RunWith(RobolectricTestRunner::class)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.storage;
16+
17+
import androidx.annotation.Nullable;
18+
import java.util.List;
19+
20+
public class KtxTestUtil {
21+
public static ListResult listResult(
22+
List<StorageReference> prefixes, List<StorageReference> items, @Nullable String pageToken) {
23+
return new ListResult(prefixes, items, pageToken);
24+
}
25+
}

0 commit comments

Comments
 (0)