forked from LineageOS/android_packages_apps_StorageManager
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port the unit tests for Deletion Helper from Settings to new APK.
This adds the facilities to run the existing unit tests for the storage manager. Bug: 28965462 Change-Id: I1322c8c4c53fa2bd6c891170149ced6ad38e3a0a
- Loading branch information
Daniel Nishi
committed
Jun 9, 2016
1 parent
b26f457
commit 86af521
Showing
7 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
LOCAL_PATH := $(call my-dir) | ||
include $(CLEAR_VARS) | ||
|
||
# Include all makefiles in subdirectories | ||
include $(call all-makefiles-under,$(LOCAL_PATH)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
LOCAL_PATH:= $(call my-dir) | ||
include $(CLEAR_VARS) | ||
|
||
# We only want this apk build for tests. | ||
LOCAL_MODULE_TAGS := tests | ||
|
||
LOCAL_JAVA_LIBRARIES := android.test.runner | ||
|
||
LOCAL_STATIC_JAVA_LIBRARIES := android-support-test mockito-target | ||
|
||
# Include all test java files. | ||
LOCAL_SRC_FILES := $(call all-java-files-under, src) | ||
|
||
LOCAL_PACKAGE_NAME := StorageManagerUnitTests | ||
|
||
LOCAL_INSTRUMENTATION_FOR := StorageManager | ||
|
||
include $(BUILD_PACKAGE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Copyright (C) 2016 The Android Open Source Project | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.android.storagemanager.tests.unit"> | ||
|
||
<application> | ||
<uses-library android:name="android.test.runner" /> | ||
</application> | ||
|
||
<instrumentation android:name="android.support.test.runner.AndroidJUnitRunner" | ||
android:targetPackage="com.android.storagemanager" | ||
android:label="StorageManager Test Cases"> | ||
</instrumentation> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
To build the tests you can use the following command at the root of your android source tree | ||
$ make StorageManagerUnitTests | ||
|
||
The test apk then needs to be installed onto your test device via for example | ||
$ adb install -r out/target/product/angler/data/app/StorageManagerUnitTests/StorageManagerUnitTests.apk | ||
|
||
To run all tests: | ||
$ adb shell am instrument -w com.android.storagemanager.tests.unit/android.support.test.runner.AndroidJUnitRunner | ||
|
||
To run all tests in a specific class: | ||
$ adb shell am instrument -w -e class com.android.storagemanager.<class> com.android.storagemanager.tests.unit/android.support.test.runner.AndroidJUnitRunner | ||
|
||
To run a specific test: | ||
$ adb shell am instrument -w -e class com.android.storagemanager.<class>#<test> com.android.storagemanager.tests.unit/android.support.test.runner.AndroidJUnitRunner | ||
|
||
More general information can be found at | ||
http://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html |
90 changes: 90 additions & 0 deletions
90
tests/unit/src/com/android/storagemanager/deletionhelper/FetchDownloadsLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (C) 2016 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.storagemanager.deletionhelper; | ||
|
||
import com.android.storagemanager.deletionhelper.FetchDownloadsLoader.DownloadsResult; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(JUnit4.class) | ||
public class FetchDownloadsLoaderTest { | ||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void testEmptyDirectory() throws Exception { | ||
DownloadsResult result = | ||
FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot()); | ||
assertNotNull(result); | ||
assertEquals(0, result.totalSize); | ||
assertEquals(0, result.files.size()); | ||
} | ||
|
||
@Test | ||
public void testFilesInDirectory() throws Exception { | ||
temporaryFolder.newFile(); | ||
temporaryFolder.newFile(); | ||
|
||
DownloadsResult result = | ||
FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot()); | ||
assertNotNull(result); | ||
assertEquals(0, result.totalSize); | ||
assertEquals(2, result.files.size()); | ||
} | ||
|
||
@Test | ||
public void testNestedDirectories() throws Exception { | ||
File tempDir = temporaryFolder.newFolder(); | ||
|
||
File testFile = File.createTempFile("test", null, tempDir); | ||
testFile.deleteOnExit(); | ||
DownloadsResult result = | ||
FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot()); | ||
assertNotNull(result); | ||
assertEquals(0, result.totalSize); | ||
assertEquals(1, result.files.size()); | ||
} | ||
|
||
@Test | ||
public void testSumFileSizes() throws Exception { | ||
File first = temporaryFolder.newFile(); | ||
FileWriter fileWriter = new FileWriter(first); | ||
fileWriter.write("test"); | ||
fileWriter.close(); | ||
|
||
File second = temporaryFolder.newFile(); | ||
fileWriter = new FileWriter(second); | ||
fileWriter.write("test2"); | ||
fileWriter.close(); | ||
|
||
DownloadsResult result = | ||
FetchDownloadsLoader.collectFiles(temporaryFolder.getRoot()); | ||
assertNotNull(result); | ||
assertEquals(9, result.totalSize); | ||
assertEquals(2, result.files.size()); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
tests/unit/src/com/android/storagemanager/deletionhelper/PackageDeletionTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (C) 2016 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.storagemanager.deletionhelper; | ||
|
||
import android.test.AndroidTestCase; | ||
import android.content.pm.IPackageDeleteObserver; | ||
import android.content.pm.PackageManager; | ||
import android.os.RemoteException; | ||
import android.test.mock.MockPackageManager; | ||
import android.test.suitebuilder.annotation.SmallTest; | ||
|
||
import com.android.storagemanager.deletionhelper.PackageDeletionTask; | ||
import com.android.storagemanager.deletionhelper.PackageDeletionTask.Callback; | ||
|
||
import java.util.Set; | ||
import java.util.HashSet; | ||
|
||
public class PackageDeletionTaskTest extends AndroidTestCase { | ||
private FakePackageManager mPackageManager; | ||
private Set<String> mDeletedApps; | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
mPackageManager = new FakePackageManager(); | ||
mDeletedApps = new HashSet<String>(); | ||
} | ||
|
||
@SmallTest | ||
public void testDeleteNoApps() throws Exception { | ||
runTask(new HashSet<String>(), false); | ||
} | ||
|
||
@SmallTest | ||
public void testDeleteOneApp() throws Exception { | ||
HashSet<String> appsToDelete = new HashSet<String>(); | ||
appsToDelete.add("app.test1"); | ||
runTask(appsToDelete, false); | ||
} | ||
|
||
@SmallTest | ||
public void testDeleteManyApps() throws Exception { | ||
HashSet<String> appsToDelete = new HashSet<String>(); | ||
appsToDelete.add("app.test1"); | ||
appsToDelete.add("app.test2"); | ||
runTask(appsToDelete, false); | ||
} | ||
|
||
@SmallTest | ||
public void testDeleteFails() throws Exception { | ||
HashSet<String> appsToDelete = new HashSet<String>(); | ||
appsToDelete.add("app.test1"); | ||
mPackageManager.deletionSucceeds = false; | ||
runTask(appsToDelete, true); | ||
} | ||
|
||
private void runTask(HashSet<String> appsToDelete, boolean shouldFail) { | ||
PackageDeletionTask task = new PackageDeletionTask(mPackageManager, appsToDelete, | ||
new VerifierCallback(appsToDelete, shouldFail)); | ||
task.run(); | ||
} | ||
|
||
class FakePackageManager extends MockPackageManager { | ||
public boolean deletionSucceeds = true; | ||
|
||
@Override | ||
public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, | ||
int flags, int userId) { | ||
int resultCode; | ||
if (deletionSucceeds) { | ||
resultCode = PackageManager.DELETE_SUCCEEDED; | ||
mDeletedApps.add(packageName); | ||
} else { | ||
resultCode = PackageManager.DELETE_FAILED_INTERNAL_ERROR; | ||
} | ||
|
||
try { | ||
observer.packageDeleted(packageName, resultCode); | ||
} catch (RemoteException e) { | ||
fail(e.toString()); | ||
} | ||
} | ||
} | ||
|
||
class VerifierCallback extends Callback { | ||
private Set<String> mExpectedDeletedApps; | ||
private boolean mShouldFail; | ||
|
||
public VerifierCallback(HashSet<String> expectedDeletedApps, boolean shouldFail) { | ||
mExpectedDeletedApps = expectedDeletedApps; | ||
mShouldFail = shouldFail; | ||
} | ||
|
||
@Override | ||
public void onSuccess() { | ||
assertFalse(mShouldFail); | ||
assertEquals(mExpectedDeletedApps, mDeletedApps); | ||
} | ||
|
||
@Override | ||
public void onError() { | ||
assertTrue(mShouldFail); | ||
} | ||
} | ||
|
||
} |