• 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 org.junit.Assert.assertThrows;
21 
22 import android.net.Uri;
23 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
24 import com.google.android.libraries.mobiledatadownload.file.backends.FileUri;
25 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend;
26 import com.google.android.libraries.mobiledatadownload.file.common.testing.DummyTransforms;
27 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri;
28 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform;
29 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.util.Arrays;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.RobolectricTestRunner;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public final class RecursiveSizeOpenerTest {
40 
41   @Rule public TemporaryUri tmpUri = new TemporaryUri();
42 
43   private final SynchronousFileStorage storage =
44       new SynchronousFileStorage(
45           Arrays.asList(new JavaFileBackend()),
46           Arrays.asList(new CompressTransform(), DummyTransforms.CAP_FILENAME_TRANSFORM));
47 
48   @Test
open_nonExistentUri_throwsFileNotFoundException()49   public void open_nonExistentUri_throwsFileNotFoundException() throws IOException {
50     Uri missing = tmpUri.newUri();
51     assertThrows(
52         FileNotFoundException.class, () -> storage.open(missing, RecursiveSizeOpener.create()));
53   }
54 
55   @Test
open_file_throwsFileNotFoundException()56   public void open_file_throwsFileNotFoundException() throws IOException {
57     Uri file = tmpUri.newUri();
58     createFile(storage, file, "12345");
59     assertThrows(
60         FileNotFoundException.class, () -> storage.open(file, RecursiveSizeOpener.create()));
61   }
62 
63   @Test
open_emptyDirectory_returns0()64   public void open_emptyDirectory_returns0() throws IOException {
65     Uri dir = tmpUri.newDirectoryUri();
66     assertThat(storage.open(dir, RecursiveSizeOpener.create())).isEqualTo(0);
67   }
68 
69   @Test
open_nonEmptyDirectory_returnsSizeOfChildrenFiles()70   public void open_nonEmptyDirectory_returnsSizeOfChildrenFiles() throws IOException {
71     Uri dir = tmpUri.newDirectoryUri();
72 
73     // TODO: consider adding FileUri.fromFileUri to make this cleaner
74     createFile(
75         storage, FileUri.builder().setPath(dir.getPath()).appendPath("file0").build(), "12345");
76     createFile(
77         storage, FileUri.builder().setPath(dir.getPath()).appendPath("file1").build(), "678");
78 
79     assertThat(storage.open(dir, RecursiveSizeOpener.create())).isEqualTo(8);
80   }
81 
82   @Test
open_directoryTree_recurses()83   public void open_directoryTree_recurses() throws IOException {
84     Uri dir = tmpUri.newDirectoryUri();
85     Uri subDir = FileUri.builder().setPath(dir.getPath()).appendPath("subDir").build();
86     Uri subSubDir = FileUri.builder().setPath(subDir.getPath()).appendPath("subSubDir").build();
87     Uri emptySubDir = FileUri.builder().setPath(dir.getPath()).appendPath("emptySubDir").build();
88 
89     // TODO: consider adding FileUri.fromFileUri to make this cleaner
90     storage.createDirectory(subDir);
91     storage.createDirectory(subSubDir);
92     storage.createDirectory(emptySubDir);
93     createFile(storage, FileUri.builder().setPath(dir.getPath()).appendPath("a").build(), "1");
94     createFile(storage, FileUri.builder().setPath(dir.getPath()).appendPath("b").build(), "2");
95     createFile(storage, FileUri.builder().setPath(subDir.getPath()).appendPath("c").build(), "3");
96     createFile(
97         storage, FileUri.builder().setPath(subSubDir.getPath()).appendPath("d").build(), "45");
98     createFile(
99         storage, FileUri.builder().setPath(subSubDir.getPath()).appendPath("e").build(), "67");
100 
101     assertThat(storage.open(dir, RecursiveSizeOpener.create())).isEqualTo(7);
102   }
103 
104   @Test
open_canFindChildRegardlessOfFilenameEncoding()105   public void open_canFindChildRegardlessOfFilenameEncoding() throws IOException {
106     Uri dir = tmpUri.newDirectoryUri();
107     Uri dirWithTransform =
108         FileUri.builder()
109             .setPath(dir.getPath())
110             .build()
111             .buildUpon()
112             .encodedFragment("transform=cap")
113             .build();
114     Uri childWithTransform =
115         FileUri.builder()
116             .setPath(dir.getPath())
117             .appendPath("this-will-be-all-caps-on-disk")
118             .build()
119             .buildUpon()
120             .encodedFragment("transform=cap")
121             .build();
122     createFile(storage, childWithTransform, "12345");
123 
124     assertThat(storage.open(dir, RecursiveSizeOpener.create())).isEqualTo(5);
125     assertThat(storage.open(dirWithTransform, RecursiveSizeOpener.create())).isEqualTo(5);
126   }
127 
128   @Test
open_returnsOnDiskSizeNotLogicalTransformedSize()129   public void open_returnsOnDiskSizeNotLogicalTransformedSize() throws IOException {
130     Uri dir = tmpUri.newDirectoryUri();
131     Uri child =
132         FileUri.builder()
133             .setPath(dir.getPath())
134             .appendPath("filename")
135             .withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC)
136             .build();
137     String content = "this content should not be decompressed when calculating on-disk size";
138     createFile(storage, child, content);
139 
140     assertThat(storage.open(dir, RecursiveSizeOpener.create())).isLessThan((long) content.length());
141   }
142 }
143