1 /*
2 * Copyright 2025 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.test.uiautomator
18
19 import android.graphics.Bitmap
20 import android.os.ParcelFileDescriptor
21 import java.io.File
22 import java.io.FileOutputStream
23
24 /**
25 * Utility function to save a bitmap onto the given [file].
26 *
27 * @param file the file to store the bitmap.
28 * @param compressFormat the [Bitmap.CompressFormat] to use when storing the bitmap. Default is jpg.
29 * @param quality the quality for the compression. This is always 100 for png.
30 * @return whether the bitmap was correctly saved onto a file.
31 */
saveToFilenull32 public fun Bitmap.saveToFile(
33 file: File,
34 compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
35 quality: Int = 100
36 ): Boolean = compress(compressFormat, quality, file.outputStream())
37
38 /**
39 * Utility function to save a bitmap onto the given [parcelFileDescriptor].
40 *
41 * @param parcelFileDescriptor the file descriptor pointing to a file to store the bitmap.
42 * @param compressFormat the [Bitmap.CompressFormat] to use when storing the bitmap. Default is jpg.
43 * @param quality the quality for the compression. This is always 100 for png.
44 * @return whether the bitmap was correctly saved onto a file.
45 */
46 public fun Bitmap.saveToFile(
47 parcelFileDescriptor: ParcelFileDescriptor,
48 compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
49 quality: Int = 100
50 ): Boolean =
51 compress(compressFormat, quality, FileOutputStream(parcelFileDescriptor.fileDescriptor))
52
53 /**
54 * Thrown when a view is not found after invoking [androidx.test.uiautomator.onView] or
55 * [androidx.test.uiautomator.onViews].
56 */
57 public class ViewNotFoundException(msg: String? = null) : Exception(msg)
58