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 20 import android.net.Uri; 21 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; 22 import java.io.File; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 26 /** Test {@link com.google.android.libraries.mobiledatadownload.file.backends.FileUri}. */ 27 @RunWith(GoogleRobolectricTestRunner.class) 28 public class FileUriTest { 29 30 @Test shouldGenerateUriFromFile()31 public void shouldGenerateUriFromFile() { 32 Uri uri = FileUri.builder().fromFile(new File("/tmp/foo.txt")).build(); 33 assertThat(uri.getScheme()).isEqualTo("file"); 34 assertThat(uri.getPath()).isEqualTo("/tmp/foo.txt"); 35 assertThat(uri.toString()).isEqualTo("file:///tmp/foo.txt"); 36 } 37 38 @Test shouldGenerateUriFromStringPath()39 public void shouldGenerateUriFromStringPath() { 40 Uri uri = FileUri.builder().setPath("/tmp/foo.txt").build(); 41 assertThat(uri.toString()).isEqualTo("file:///tmp/foo.txt"); 42 } 43 44 @Test builder_appendPath_addsPathSegment()45 public void builder_appendPath_addsPathSegment() { 46 Uri uri = FileUri.builder().setPath("tmp").appendPath("foo.txt").build(); 47 assertThat(uri.toString()).isEqualTo("file:///tmp/foo.txt"); 48 } 49 50 @Test hasSafeDefault()51 public void hasSafeDefault() { 52 Uri uri = FileUri.builder().build(); 53 assertThat(uri.toString()).isEqualTo("file:///"); 54 } 55 56 @Test repeatedCalls_shouldRetainInfo()57 public void repeatedCalls_shouldRetainInfo() { 58 FileUri.Builder uriBuilder = FileUri.builder().setPath("/fake"); 59 Uri fileUri = uriBuilder.build(); 60 Uri fileUriWithTransform = fileUri.buildUpon().encodedFragment("transform=foo").build(); 61 assertThat(fileUri.toString()).isEqualTo("file:///fake"); 62 assertThat(fileUriWithTransform.toString()).isEqualTo("file:///fake#transform=foo"); 63 } 64 } 65