1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.file.backends; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.junit.Assert.assertThrows; 20 21 import android.content.Context; 22 import android.net.Uri; 23 import androidx.test.core.app.ApplicationProvider; 24 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException; 25 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; 26 import java.io.File; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 /** Test {@link com.google.android.libraries.mobiledatadownload.file.backends.GenericUriAdapter}. */ 31 @RunWith(GoogleRobolectricTestRunner.class) 32 public class GenericUriAdapterTest { 33 34 private final Context context = ApplicationProvider.getApplicationContext(); 35 36 @Test shouldGenerateFileFromFileUri()37 public void shouldGenerateFileFromFileUri() throws Exception { 38 File file = GenericUriAdapter.forContext(context).toFile(Uri.parse("file:///tmp/foo.txt")); 39 assertThat(file.getPath()).isEqualTo("/tmp/foo.txt"); 40 } 41 42 @Test shouldGenerateFileFromAndroidUri()43 public void shouldGenerateFileFromAndroidUri() throws Exception { 44 File file = 45 GenericUriAdapter.forContext(context) 46 .toFile(Uri.parse("android://package/files/common/shared/path")); 47 assertThat(file.getPath()).endsWith("/files/common/shared/path"); 48 } 49 50 @Test shouldGenerateFileFromExternalLocationUri()51 public void shouldGenerateFileFromExternalLocationUri() throws Exception { 52 File file = 53 GenericUriAdapter.forContext(context) 54 .toFile(Uri.parse("android://package/external/common/shared/path")); 55 assertThat(file.getPath()).endsWith("/external-files/common/shared/path"); 56 } 57 58 @Test shouldCheckScheme()59 public void shouldCheckScheme() throws Exception { 60 assertThrows( 61 MalformedUriException.class, 62 () -> FileUriAdapter.instance().toFile(Uri.parse("xxx:///tmp/foo.txt"))); 63 } 64 } 65