• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SynchronousFileStorage;
25 import com.google.android.libraries.mobiledatadownload.file.common.UnsupportedFileStorageOperation;
26 import com.google.android.libraries.mobiledatadownload.file.openers.NativeReadOpener;
27 import com.google.android.libraries.mobiledatadownload.file.openers.ReadStringOpener;
28 import com.google.common.collect.ImmutableList;
29 import java.io.FileNotFoundException;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 @RunWith(JUnit4.class)
36 public final class AssetFileBackendTest {
37   private final Context context = ApplicationProvider.getApplicationContext();
38 
39   private SynchronousFileStorage storage;
40 
41   @Before
setUp()42   public void setUp() {
43     AssetFileBackend backend = AssetFileBackend.builder(context).build();
44     storage = new SynchronousFileStorage(ImmutableList.of(backend));
45   }
46 
47   @Test
openForRead_opensEmbeddedFiles()48   public void openForRead_opensEmbeddedFiles() throws Exception {
49     Uri path = Uri.parse("asset:/AssetFileBackendTest.java");
50     String contents = storage.open(path, ReadStringOpener.create());
51     assertThat(contents).contains("AssetFileBackendTest");
52   }
53 
54   @Test
fileSize_opensEmbeddedFiles()55   public void fileSize_opensEmbeddedFiles() throws Exception {
56     Uri path = Uri.parse("asset:/AssetFileBackendTest.java");
57     Long size = storage.fileSize(path);
58     assertThat(size).isGreaterThan(0);
59   }
60 
61   @Test
openForRead_handlesMissingAssets()62   public void openForRead_handlesMissingAssets() throws Exception {
63     Uri path = Uri.parse("asset:/DOES_NOT_EXIST");
64     FileNotFoundException ex =
65         assertThrows(
66             FileNotFoundException.class, () -> storage.open(path, ReadStringOpener.create()));
67     assertThat(ex).hasMessageThat().isEqualTo("DOES_NOT_EXIST");
68   }
69 
70   @Test
openForNativeRead_isUnsupported()71   public void openForNativeRead_isUnsupported() throws Exception {
72     Uri path = Uri.parse("asset:/AssetFileBackendTest.java");
73     UnsupportedFileStorageOperation ex =
74         assertThrows(
75             UnsupportedFileStorageOperation.class,
76             () -> storage.open(path, NativeReadOpener.create()));
77     assertThat(ex).hasMessageThat().isEqualTo("Native read not supported (b/210546473)");
78   }
79 }
80