• 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.openers;
17 
18 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.createFile;
19 import static com.google.common.truth.Truth.assertThat;
20 import static java.nio.charset.StandardCharsets.UTF_8;
21 import static org.junit.Assert.assertThrows;
22 
23 import android.content.res.AssetFileDescriptor;
24 import android.net.Uri;
25 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
26 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend;
27 import com.google.android.libraries.mobiledatadownload.file.common.testing.FileDescriptorLeakChecker;
28 import com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils;
29 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri;
30 import com.google.android.libraries.mobiledatadownload.file.samples.ByteCountingMonitor;
31 import com.google.common.collect.ImmutableList;
32 import com.google.common.io.CharStreams;
33 import java.io.FileNotFoundException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.Reader;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.JUnit4;
42 
43 @RunWith(JUnit4.class)
44 public final class AssetFileDescriptorOpenerAndroidTest {
45 
46   private SynchronousFileStorage storage;
47 
48   @Rule public FileDescriptorLeakChecker leakChecker = new FileDescriptorLeakChecker();
49   @Rule public TemporaryUri tmpUri = new TemporaryUri();
50 
51   @Before
initStorage()52   public void initStorage() throws Exception {
53     storage = new SynchronousFileStorage(ImmutableList.of(new JavaFileBackend()));
54   }
55 
56   @Test
openAssetFileDescriptor_shouldReadFile()57   public void openAssetFileDescriptor_shouldReadFile() throws Exception {
58     Uri uri = tmpUri.newUri();
59     createFile(storage, uri, "content");
60     try (AssetFileDescriptor result = storage.open(uri, AssetFileDescriptorOpener.create())) {
61       assertThat(result.getLength()).isEqualTo(7);
62       InputStream in = result.createInputStream();
63       Reader reader = new InputStreamReader(in, UTF_8);
64       assertThat(CharStreams.toString(reader)).isEqualTo("content");
65     }
66   }
67 
68   @Test
openAssetFileDescriptor_withMissingFile_throwsFileNotFound()69   public void openAssetFileDescriptor_withMissingFile_throwsFileNotFound() throws Exception {
70     Uri uri = Uri.parse("file:/does-not-exist");
71     assertThrows(
72         FileNotFoundException.class, () -> storage.open(uri, AssetFileDescriptorOpener.create()));
73   }
74 
75   @Test
openAssetFileDescriptor_withMonitor_shouldReadFile()76   public void openAssetFileDescriptor_withMonitor_shouldReadFile() throws Exception {
77     SynchronousFileStorage storageWithMonitor =
78         new SynchronousFileStorage(
79             ImmutableList.of(new JavaFileBackend()),
80             ImmutableList.of(),
81             ImmutableList.of(new ByteCountingMonitor()));
82 
83     Uri uri = tmpUri.newUri();
84     byte[] content = StreamUtils.makeArrayOfBytesContent();
85     StreamUtils.createFile(storageWithMonitor, uri, content);
86 
87     try (InputStream in =
88         storage.open(uri, AssetFileDescriptorOpener.create()).createInputStream()) {
89       assertThat(StreamUtils.readFileInBytesFromSource(in)).isEqualTo(content);
90     }
91   }
92 }
93