• 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.base.Charsets.UTF_8;
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.net.Uri;
25 import androidx.test.core.app.ApplicationProvider;
26 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
27 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException;
28 import com.google.android.libraries.mobiledatadownload.file.common.UnsupportedFileStorageOperation;
29 import com.google.android.libraries.mobiledatadownload.file.openers.AppendStreamOpener;
30 import com.google.android.libraries.mobiledatadownload.file.openers.ReadStreamOpener;
31 import com.google.android.libraries.mobiledatadownload.file.openers.WriteStreamOpener;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.io.ByteStreams;
34 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner;
35 import java.io.ByteArrayInputStream;
36 import java.io.InputStream;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.robolectric.Shadows;
41 
42 @RunWith(GoogleRobolectricTestRunner.class)
43 public class ContentResolverBackendTest {
44 
45   private final Context context = ApplicationProvider.getApplicationContext();
46   private SynchronousFileStorage storage;
47   private ContentResolver contentResolver;
48 
49   @Before
setUpStorage()50   public final void setUpStorage() {
51     ContentResolverBackend backend = ContentResolverBackend.builder(context).build();
52     storage = new SynchronousFileStorage(ImmutableList.of(backend));
53     contentResolver = context.getContentResolver();
54   }
55 
56   @Test
openForRead_reads()57   public void openForRead_reads() throws Exception {
58     Uri uri = Uri.parse("content://test/openForRead_reads");
59     String expected = "expected content";
60     Shadows.shadowOf(contentResolver)
61         .registerInputStream(uri, new ByteArrayInputStream(expected.getBytes(UTF_8)));
62 
63     try (InputStream in = storage.open(uri, ReadStreamOpener.create())) {
64       String actual = new String(ByteStreams.toByteArray(in), UTF_8);
65       assertThat(actual).isEqualTo(expected);
66     }
67   }
68 
69   @Test
openForRead_missingFile()70   public void openForRead_missingFile() throws Exception {
71     Uri uri = Uri.parse("content://test/openForRead_missingFile");
72 
73     try (InputStream in = storage.open(uri, ReadStreamOpener.create())) {
74       // The shadow is weird: it returns a stream even if not registered, but that stream throws
75       // when you try to use it.
76       assertThrows(UnsupportedOperationException.class, () -> in.read());
77     }
78   }
79 
80   // TODO(b/110493197): Add test for native read. SynchronousFileStorage lacks
81   // registerFileDescriptor.
82 
83   @Test
openForWrite_notImplemented()84   public void openForWrite_notImplemented() throws Exception {
85     Uri uri = Uri.parse("content://test/openForWrite_notImplemented");
86 
87     assertThrows(
88         UnsupportedFileStorageOperation.class, () -> storage.open(uri, WriteStreamOpener.create()));
89   }
90 
91   @Test
openForAppend_notImplemented()92   public void openForAppend_notImplemented() throws Exception {
93     Uri uri = Uri.parse("content://test/ok");
94 
95     assertThrows(
96         UnsupportedFileStorageOperation.class,
97         () -> storage.open(uri, AppendStreamOpener.create()));
98   }
99 
100   @Test
nonEmbedded_checksScheme()101   public void nonEmbedded_checksScheme() throws Exception {
102     ContentResolverBackend nonEmbedded = ContentResolverBackend.builder(context).build();
103     Uri uri = Uri.parse("WRONG://test/nonEmbedded_checksScheme");
104 
105     assertThrows(MalformedUriException.class, () -> nonEmbedded.openForRead(uri));
106     assertThat(nonEmbedded.name()).isEqualTo("content");
107   }
108 
109   @Test
embedded_rewritesScheme()110   public void embedded_rewritesScheme() throws Exception {
111     ContentResolverBackend embedded =
112         ContentResolverBackend.builder(context).setEmbedded(true).build();
113     Uri uri = Uri.parse("OTHERSCHEME://test/embedded_rewritesScheme");
114     Uri contentUri = uri.buildUpon().scheme("content").build();
115     String expected = "expected content";
116     Shadows.shadowOf(contentResolver)
117         .registerInputStream(contentUri, new ByteArrayInputStream(expected.getBytes(UTF_8)));
118 
119     try (InputStream in = embedded.openForRead(uri)) {
120       String actual = new String(ByteStreams.toByteArray(in), UTF_8);
121       assertThat(actual).isEqualTo(expected);
122     }
123     assertThrows(IllegalStateException.class, () -> embedded.name());
124   }
125 }
126