Robert Berry | 6d2fbdf | 2017-07-17 09:56:44 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 | |
| 17 | package com.android.server.backup; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.platform.test.annotations.Presubmit; |
| 22 | import android.support.test.filters.SmallTest; |
| 23 | import android.support.test.runner.AndroidJUnit4; |
| 24 | |
| 25 | import com.google.android.collect.Sets; |
| 26 | |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Rule; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.rules.TemporaryFolder; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.mockito.MockitoAnnotations; |
| 33 | |
| 34 | import java.io.DataInputStream; |
| 35 | import java.io.DataOutputStream; |
| 36 | import java.io.File; |
| 37 | import java.io.FileInputStream; |
| 38 | import java.io.FileNotFoundException; |
| 39 | import java.io.FileOutputStream; |
| 40 | import java.util.HashSet; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | @SmallTest |
| 44 | @Presubmit |
| 45 | @RunWith(AndroidJUnit4.class) |
| 46 | public class AppsBackedUpOnThisDeviceJournalTest { |
| 47 | private static final String JOURNAL_FILE_NAME = "processed"; |
| 48 | |
| 49 | private static final String GOOGLE_PHOTOS = "com.google.photos"; |
| 50 | private static final String GMAIL = "com.google.gmail"; |
| 51 | private static final String GOOGLE_PLUS = "com.google.plus"; |
| 52 | |
| 53 | @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); |
| 54 | |
| 55 | private File mStateDirectory; |
| 56 | private AppsBackedUpOnThisDeviceJournal mAppsBackedUpOnThisDeviceJournal; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
| 60 | MockitoAnnotations.initMocks(this); |
| 61 | mStateDirectory = mTemporaryFolder.newFolder(); |
| 62 | mAppsBackedUpOnThisDeviceJournal = new AppsBackedUpOnThisDeviceJournal(mStateDirectory); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void constructor_loadsAnyPreviousJournalFromDisk() throws Exception { |
| 67 | writePermanentJournalPackages(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL)); |
| 68 | |
| 69 | AppsBackedUpOnThisDeviceJournal journalFromDisk = |
| 70 | new AppsBackedUpOnThisDeviceJournal(mStateDirectory); |
| 71 | |
| 72 | assertThat(journalFromDisk.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue(); |
| 73 | assertThat(journalFromDisk.hasBeenProcessed(GMAIL)).isTrue(); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void hasBeenProcessed_isFalseForAnyPackageFromBlankInit() { |
| 78 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse(); |
| 79 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GMAIL)).isFalse(); |
| 80 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse(); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void addPackage_addsPackageToObjectState() { |
| 85 | mAppsBackedUpOnThisDeviceJournal.addPackage(GOOGLE_PHOTOS); |
| 86 | |
| 87 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isTrue(); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void addPackage_addsPackageToFileSystem() throws Exception { |
| 92 | mAppsBackedUpOnThisDeviceJournal.addPackage(GOOGLE_PHOTOS); |
| 93 | |
| 94 | assertThat(readJournalPackages()).contains(GOOGLE_PHOTOS); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void getPackagesCopy_returnsTheCurrentState() throws Exception { |
| 99 | mAppsBackedUpOnThisDeviceJournal.addPackage(GOOGLE_PHOTOS); |
| 100 | mAppsBackedUpOnThisDeviceJournal.addPackage(GMAIL); |
| 101 | |
| 102 | assertThat(mAppsBackedUpOnThisDeviceJournal.getPackagesCopy()) |
| 103 | .isEqualTo(Sets.newHashSet(GOOGLE_PHOTOS, GMAIL)); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void getPackagesCopy_returnsACopy() throws Exception { |
| 108 | mAppsBackedUpOnThisDeviceJournal.getPackagesCopy().add(GMAIL); |
| 109 | |
| 110 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GMAIL)).isFalse(); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void reset_removesAllPackagesFromObjectState() { |
| 115 | mAppsBackedUpOnThisDeviceJournal.addPackage(GOOGLE_PHOTOS); |
| 116 | mAppsBackedUpOnThisDeviceJournal.addPackage(GOOGLE_PLUS); |
| 117 | mAppsBackedUpOnThisDeviceJournal.addPackage(GMAIL); |
| 118 | |
| 119 | mAppsBackedUpOnThisDeviceJournal.reset(); |
| 120 | |
| 121 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GOOGLE_PHOTOS)).isFalse(); |
| 122 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GMAIL)).isFalse(); |
| 123 | assertThat(mAppsBackedUpOnThisDeviceJournal.hasBeenProcessed(GOOGLE_PLUS)).isFalse(); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void reset_removesAllPackagesFromFileSystem() throws Exception { |
| 128 | mAppsBackedUpOnThisDeviceJournal.addPackage(GOOGLE_PHOTOS); |
| 129 | mAppsBackedUpOnThisDeviceJournal.addPackage(GOOGLE_PLUS); |
| 130 | mAppsBackedUpOnThisDeviceJournal.addPackage(GMAIL); |
| 131 | |
| 132 | mAppsBackedUpOnThisDeviceJournal.reset(); |
| 133 | |
| 134 | assertThat(readJournalPackages()).isEmpty(); |
| 135 | } |
| 136 | |
| 137 | private HashSet<String> readJournalPackages() throws Exception { |
| 138 | File journal = new File(mStateDirectory, JOURNAL_FILE_NAME); |
| 139 | HashSet<String> packages = new HashSet<>(); |
| 140 | |
| 141 | try (FileInputStream fis = new FileInputStream(journal); |
| 142 | DataInputStream dis = new DataInputStream(fis)) { |
| 143 | while (dis.available() > 0) { |
| 144 | packages.add(dis.readUTF()); |
| 145 | } |
| 146 | } catch (FileNotFoundException e) { |
| 147 | return new HashSet<>(); |
| 148 | } |
| 149 | |
| 150 | return packages; |
| 151 | } |
| 152 | |
| 153 | private void writePermanentJournalPackages(Set<String> packages) throws Exception { |
| 154 | File journal = new File(mStateDirectory, JOURNAL_FILE_NAME); |
| 155 | |
| 156 | try (FileOutputStream fos = new FileOutputStream(journal); |
| 157 | DataOutputStream dos = new DataOutputStream(fos)) { |
| 158 | for (String packageName : packages) { |
| 159 | dos.writeUTF(packageName); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |