• 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.common.truth.Truth.assertThat;
19 
20 import android.net.Uri;
21 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
22 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend;
23 import com.google.android.libraries.mobiledatadownload.file.behaviors.SyncingBehavior;
24 import com.google.android.libraries.mobiledatadownload.file.common.internal.Charsets;
25 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri;
26 import java.util.Arrays;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.robolectric.RobolectricTestRunner;
33 
34 @RunWith(RobolectricTestRunner.class)
35 public final class StringOpenerTest {
36 
37   SynchronousFileStorage storage;
38   @Rule public TemporaryUri tmpUri = new TemporaryUri();
39 
40   @Before
setUpStorage()41   public void setUpStorage() throws Exception {
42     storage = new SynchronousFileStorage(Arrays.asList(new JavaFileBackend()));
43   }
44 
45   @Test
withMonitor_writesString()46   public void withMonitor_writesString() throws Exception {
47 
48     Uri uri = tmpUri.newUri();
49     String expected = "The five boxing wizards jump quickly";
50     storage.open(uri, WriteStringOpener.create(expected));
51     assertThat(storage.open(uri, ReadStringOpener.create())).isEqualTo(expected);
52   }
53 
54   @Test
writesString_withDifferentCharsets()55   public void writesString_withDifferentCharsets() throws Exception {
56     Uri uri = tmpUri.newUri();
57     String expected = "The five boxing wizards jump quickly";
58 
59     storage.open(uri, WriteStringOpener.create(expected).withCharset(Charsets.US_ASCII));
60     assertThat(storage.open(uri, ReadStringOpener.create().withCharset(Charsets.US_ASCII)))
61         .isEqualTo(expected);
62 
63     storage.open(uri, WriteStringOpener.create(expected).withCharset(Charsets.ISO_8859_1));
64     assertThat(storage.open(uri, ReadStringOpener.create().withCharset(Charsets.ISO_8859_1)))
65         .isEqualTo(expected);
66   }
67 
68   @Test
invokes_autoSync()69   public void invokes_autoSync() throws Exception {
70     Uri uri1 = tmpUri.newUri();
71     SyncingBehavior syncing = Mockito.spy(new SyncingBehavior());
72     storage.open(uri1, WriteStringOpener.create("some string").withBehaviors(syncing));
73     Mockito.verify(syncing).sync();
74   }
75 }
76