• 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.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.makeArrayOfBytesContent;
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.net.Uri;
23 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
24 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend;
25 import com.google.android.libraries.mobiledatadownload.file.common.testing.FakeFileBackend;
26 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri;
27 import com.google.android.libraries.mobiledatadownload.file.samples.ByteCountingMonitor;
28 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform;
29 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos;
30 import java.util.Arrays;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RobolectricTestRunner;
35 
36 @RunWith(RobolectricTestRunner.class)
37 public final class ReadByteArrayOpenerTest {
38 
39   @Rule public TemporaryUri tmpUri = new TemporaryUri();
40 
41   private final FakeFileBackend fakeBackend = new FakeFileBackend();
42 
storageWithTransform()43   public SynchronousFileStorage storageWithTransform() throws Exception {
44     return new SynchronousFileStorage(
45         Arrays.asList(new JavaFileBackend()), Arrays.asList(new CompressTransform()));
46   }
47 
storageWithMonitor()48   public SynchronousFileStorage storageWithMonitor() throws Exception {
49     return new SynchronousFileStorage(
50         Arrays.asList(new JavaFileBackend()),
51         Arrays.asList(),
52         Arrays.asList(new ByteCountingMonitor()));
53   }
54 
storageWithFakeBackend()55   public SynchronousFileStorage storageWithFakeBackend() throws Exception {
56     return new SynchronousFileStorage(Arrays.asList(fakeBackend));
57   }
58 
59   @Test
directFile_producesArray()60   public void directFile_producesArray() throws Exception {
61     SynchronousFileStorage storage = storageWithTransform();
62     Uri uri = tmpUri.newUri();
63     byte[] expected = makeArrayOfBytesContent();
64     createFile(storage, uri, expected);
65 
66     ReadByteArrayOpener opener = ReadByteArrayOpener.create();
67     byte[] bytes = storage.open(uri, opener);
68 
69     assertThat(bytes).isEqualTo(expected);
70   }
71 
72   @Test
withMonitor_producesArray()73   public void withMonitor_producesArray() throws Exception {
74     SynchronousFileStorage storage = storageWithMonitor();
75     Uri uri = tmpUri.newUri();
76     byte[] expected = makeArrayOfBytesContent();
77     createFile(storage, uri, expected);
78 
79     ReadByteArrayOpener opener = ReadByteArrayOpener.create();
80     byte[] bytes = storage.open(uri, opener);
81 
82     assertThat(bytes).isEqualTo(expected);
83   }
84 
85   @Test
withTransform_producesArray()86   public void withTransform_producesArray() throws Exception {
87     SynchronousFileStorage storage = storageWithTransform();
88     Uri uri = tmpUri.newUriBuilder().withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC).build();
89     byte[] expected = makeArrayOfBytesContent();
90     createFile(storage, uri, expected);
91 
92     ReadByteArrayOpener opener = ReadByteArrayOpener.create();
93     byte[] bytes = storage.open(uri, opener);
94 
95     assertThat(bytes).isEqualTo(expected);
96   }
97 
98   @Test
withFakeBackend_producesArray()99   public void withFakeBackend_producesArray() throws Exception {
100     SynchronousFileStorage storage = storageWithFakeBackend();
101     Uri uri = tmpUri.newUri();
102     byte[] expected = makeArrayOfBytesContent();
103 
104     storage.open(uri, WriteByteArrayOpener.create(expected));
105 
106     byte[] bytes = storage.open(uri, ReadByteArrayOpener.create());
107 
108     assertThat(bytes).isEqualTo(expected);
109   }
110 }
111