• 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.makeArrayOfBytesContent;
19 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.makeByteContentThatExceedsOsBufferSize;
20 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.readFileInBytesFromSource;
21 import static com.google.common.truth.Truth.assertThat;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.when;
24 
25 import android.net.Uri;
26 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
27 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend;
28 import com.google.android.libraries.mobiledatadownload.file.behaviors.SyncingBehavior;
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.android.libraries.mobiledatadownload.file.spi.Backend;
32 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform;
33 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos;
34 import java.io.ByteArrayInputStream;
35 import java.io.ByteArrayOutputStream;
36 import java.util.Arrays;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.junit.MockitoJUnit;
43 import org.mockito.junit.MockitoRule;
44 import org.robolectric.RobolectricTestRunner;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public final class WriteByteArrayOpenerTest {
48 
49   @Rule public TemporaryUri tmpUri = new TemporaryUri();
50 
51   @Rule public final MockitoRule mocks = MockitoJUnit.rule();
52 
53   @Mock private Backend mockBackend;
54 
storageWithTransform()55   public SynchronousFileStorage storageWithTransform() throws Exception {
56     return new SynchronousFileStorage(
57         Arrays.asList(new JavaFileBackend()), Arrays.asList(new CompressTransform()));
58   }
59 
storageWithMonitor()60   public SynchronousFileStorage storageWithMonitor() throws Exception {
61     return new SynchronousFileStorage(
62         Arrays.asList(new JavaFileBackend()),
63         Arrays.asList(),
64         Arrays.asList(new ByteCountingMonitor()));
65   }
66 
storageWithMockBackend()67   public SynchronousFileStorage storageWithMockBackend() throws Exception {
68     when(mockBackend.name()).thenReturn("mock");
69     return new SynchronousFileStorage(Arrays.asList(mockBackend));
70   }
71 
72   @Test
directFile_writesArray()73   public void directFile_writesArray() throws Exception {
74     SynchronousFileStorage storage = storageWithTransform();
75     Uri uri = tmpUri.newUri();
76     byte[] expected = makeArrayOfBytesContent();
77     storage.open(uri, WriteByteArrayOpener.create(expected));
78     assertThat(readFileInBytesFromSource(storage.open(uri, ReadStreamOpener.create())))
79         .isEqualTo(expected);
80   }
81 
82   @Test
directFile_writesArrayThatExceedsOsBufferSize()83   public void directFile_writesArrayThatExceedsOsBufferSize() throws Exception {
84     SynchronousFileStorage storage = storageWithTransform();
85     Uri uri = tmpUri.newUri();
86     byte[] expected = makeByteContentThatExceedsOsBufferSize();
87     storage.open(uri, WriteByteArrayOpener.create(expected));
88     assertThat(readFileInBytesFromSource(storage.open(uri, ReadStreamOpener.create())))
89         .isEqualTo(expected);
90   }
91 
92   @Test
withMonitor_writesArray()93   public void withMonitor_writesArray() throws Exception {
94     SynchronousFileStorage storage = storageWithMonitor();
95     Uri uri = tmpUri.newUri();
96     byte[] expected = makeArrayOfBytesContent();
97     storage.open(uri, WriteByteArrayOpener.create(expected));
98     assertThat(readFileInBytesFromSource(storage.open(uri, ReadStreamOpener.create())))
99         .isEqualTo(expected);
100   }
101 
102   @Test
withTransform_producesArray()103   public void withTransform_producesArray() throws Exception {
104     SynchronousFileStorage storage = storageWithTransform();
105     Uri uri = tmpUri.newUriBuilder().withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC).build();
106     byte[] expected = makeArrayOfBytesContent();
107     storage.open(uri, WriteByteArrayOpener.create(expected));
108     assertThat(readFileInBytesFromSource(storage.open(uri, ReadStreamOpener.create())))
109         .isEqualTo(expected);
110   }
111 
112   @Test
withMockBackend_producesArray()113   public void withMockBackend_producesArray() throws Exception {
114     SynchronousFileStorage storage = storageWithMockBackend();
115     Uri uri = Uri.parse("mock:/");
116     byte[] expected = makeArrayOfBytesContent();
117     when(mockBackend.openForWrite(any())).thenReturn(new ByteArrayOutputStream());
118     when(mockBackend.openForRead(any())).thenReturn(new ByteArrayInputStream(expected));
119 
120     storage.open(uri, WriteByteArrayOpener.create(expected));
121     assertThat(readFileInBytesFromSource(storage.open(uri, ReadStreamOpener.create())))
122         .isEqualTo(expected);
123   }
124 
125   @Test
invokes_autoSync()126   public void invokes_autoSync() throws Exception {
127     SynchronousFileStorage storage = storageWithTransform();
128     Uri uri1 = tmpUri.newUri();
129     SyncingBehavior syncing = Mockito.spy(new SyncingBehavior());
130     storage.open(
131         uri1, WriteByteArrayOpener.create(makeArrayOfBytesContent()).withBehaviors(syncing));
132     Mockito.verify(syncing).sync();
133   }
134 }
135