• 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.FragmentParamMatchers.eqParam;
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.net.Uri;
27 import android.util.Pair;
28 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
29 import com.google.android.libraries.mobiledatadownload.file.backends.FileUri;
30 import com.google.android.libraries.mobiledatadownload.file.spi.Backend;
31 import com.google.android.libraries.mobiledatadownload.file.spi.Monitor;
32 import com.google.android.libraries.mobiledatadownload.file.spi.Transform;
33 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos;
34 import com.google.common.collect.ImmutableList;
35 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner;
36 import java.io.Closeable;
37 import java.io.InputStream;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnit;
44 import org.mockito.junit.MockitoRule;
45 
46 @RunWith(GoogleRobolectricTestRunner.class)
47 public final class NativeReadOpenerTest {
48 
49   private SynchronousFileStorage storage;
50 
51   @Rule public final MockitoRule mocks = MockitoJUnit.rule();
52 
53   @Mock protected Backend fileBackend;
54   @Mock protected Transform compressTransform;
55   @Mock protected Monitor countingMonitor;
56   @Mock protected Closeable closeable;
57 
58   @Before
initStorage()59   public void initStorage() throws Exception {
60     when(fileBackend.name()).thenReturn("file");
61     when(compressTransform.name()).thenReturn("compress");
62     when(fileBackend.openForNativeRead(any()))
63         .thenReturn(Pair.create(Uri.parse("fd:123"), closeable));
64 
65     storage =
66         new SynchronousFileStorage(
67             ImmutableList.of(fileBackend),
68             ImmutableList.of(compressTransform),
69             ImmutableList.of(countingMonitor));
70   }
71 
72   @Test
nativeRead_shouldEncodeButNotInvokeTransforms()73   public void nativeRead_shouldEncodeButNotInvokeTransforms() throws Exception {
74     String file1Filename = "file1.txt";
75     Uri file1CompressUri =
76         FileUri.builder()
77             .setPath(file1Filename)
78             .withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC)
79             .build();
80     Uri compressParam = Uri.parse("#transform=compress");
81     assertThat(storage.open(file1CompressUri, NativeReadOpener.create())).isNotNull();
82     verify(compressTransform, never()).wrapForRead(eqParam(compressParam), any(InputStream.class));
83     verify(compressTransform).encode(eqParam(compressParam), eq(file1Filename));
84   }
85 }
86