diff options
author | Enrico Granata <egranata@google.com> | 2017-09-21 17:19:04 -0700 |
---|---|---|
committer | Enrico Granata <egranata@google.com> | 2017-09-22 16:29:38 -0700 |
commit | 9c1f27227f843f10a2d286ded886464b60854e08 (patch) | |
tree | 7b037678cd67afb7f7bea78d35180dd4faf3fe13 /tests/common_utils | |
parent | 414c5a39003f3b6fd64b5470e03605976a73b2c6 (diff) | |
download | Car-9c1f27227f843f10a2d286ded886464b60854e08.tar.gz |
Tweaks to enhance testability
1) Add two entry points to SystemInterface:
- getFilesDir(): returns a directory where permanent data can be stored (by default Context.getFilesDir())
This allows components under test to store data without affecting the real version of the files
- getCurrentUptime(): returns the length of time that the system has been running (by default via SystemClock)
This is useful to enable testing of time-sensitive components
2) Add a TemporaryDirectory helper with the same semantics as TemporaryFile.
Move both TemporaryFile and TemporaryDirectory to their own package in order to share between carservice_test and carservice_unit_test
Bug: 32512551
Test: runtest -x packages/services/Car/tests/carservice_unit_test
runtest -x packages/services/Car/tests/carservice_unit_test/src/com/android/car/CarPowerManagementServiceTest.java
runtest -x packages/services/Car/tests/carservice_test/src/com/android/car/CarStorageMonitoringTest.java
Change-Id: I31480cbd6b66df83804cc331d8c25a1403f10fe7
Diffstat (limited to 'tests/common_utils')
3 files changed, 168 insertions, 0 deletions
diff --git a/tests/common_utils/Android.mk b/tests/common_utils/Android.mk new file mode 100644 index 0000000000..19302c6033 --- /dev/null +++ b/tests/common_utils/Android.mk @@ -0,0 +1,26 @@ +# Copyright (C) 2017 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. +# + +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := com.android.car.test.utils + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +include $(BUILD_STATIC_JAVA_LIBRARY) diff --git a/tests/common_utils/src/com/android/car/test/utils/TemporaryDirectory.java b/tests/common_utils/src/com/android/car/test/utils/TemporaryDirectory.java new file mode 100644 index 0000000000..fa5727d903 --- /dev/null +++ b/tests/common_utils/src/com/android/car/test/utils/TemporaryDirectory.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2017 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.car.test.utils; + +import android.annotation.Nullable; +import android.os.SystemClock; +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; + +/** + * A utility class that creates a temporary directory. + * The directory, and any files contained within, are automatically deleted when calling close(). + * + * Example usage: + * + * try (TemporaryDirectory tf = new TemporaryDirectory("myTest")) { + * ... + * } // directory gets deleted here + */ +public class TemporaryDirectory implements AutoCloseable { + private Path mDirectory; + + private static final class DeletingVisitor extends SimpleFileVisitor<Path> { + FileVisitResult consume(Path path) throws IOException { + Files.delete(path); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) + throws IOException { + return consume(path); + } + + @Override + public FileVisitResult postVisitDirectory(Path path, IOException e) + throws IOException { + return consume(path); + } + } + + private static final SimpleFileVisitor<Path> DELETE = new DeletingVisitor(); + + public TemporaryDirectory(@Nullable String prefix) throws IOException { + if (prefix == null) { + prefix = TemporaryDirectory.class.getSimpleName(); + } + prefix += String.valueOf(SystemClock.elapsedRealtimeNanos()); + + mDirectory = Files.createTempDirectory(prefix); + } + + @Override + public void close() throws Exception { + Files.walkFileTree(mDirectory, DELETE); + } + + public File getDirectory() { + return mDirectory.toFile(); + } +} diff --git a/tests/common_utils/src/com/android/car/test/utils/TemporaryFile.java b/tests/common_utils/src/com/android/car/test/utils/TemporaryFile.java new file mode 100644 index 0000000000..a26c1ab98c --- /dev/null +++ b/tests/common_utils/src/com/android/car/test/utils/TemporaryFile.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2017 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.car.test.utils; + +import android.annotation.Nullable; +import android.os.SystemClock; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; + +/** + * A utility class that creates a temporary file. + * The file is automatically deleted when calling close(). + * + * Example usage: + * + * try (TemporaryFile tf = new TemporaryFile("myTest")) { + * ... + * } // file gets deleted here + */ +public final class TemporaryFile implements AutoCloseable { + private File mFile; + + public TemporaryFile(@Nullable String prefix) throws IOException { + if (prefix == null) { + prefix = TemporaryFile.class.getSimpleName(); + } + mFile = File.createTempFile(prefix, String.valueOf(SystemClock.elapsedRealtimeNanos())); + } + + @Override + public void close() throws Exception { + Files.delete(mFile.toPath()); + } + + public void write(String s) throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter(mFile)); + writer.write(s); + writer.close(); + } + + public File getFile() { + return mFile; + } +} |