diff options
author | Julien Desprez <jdesprez@google.com> | 2019-05-31 09:12:40 -0700 |
---|---|---|
committer | Jovana Knezevic <jovanak@google.com> | 2019-06-14 22:37:31 +0000 |
commit | d6b60a00230252438e9f2299e7424e323c6cebd5 (patch) | |
tree | 4e0548e1a3bcd6a740614649383f6d5906e2aa59 | |
parent | d71684b1270410d61a01a5b7a4ff7da9ced02a97 (diff) | |
download | tradefederation-d6b60a00230252438e9f2299e7424e323c6cebd5.tar.gz |
Make doesFileExists sdcard and user aware
In order to check files against the current user.
We might need to consider an interface that ignore this
since in some cases, we might want to make sure we don't
find the file from other users. (future improvements)
Test: unit tests
Bug: 129986353
Change-Id: I72202b5cbd2db0289dc1971e77eeac2e2bbe674f
Merged-In: I72202b5cbd2db0289dc1971e77eeac2e2bbe674f
(cherry picked from commit 878096f3590e6d4d226e917e35b230d54546722c)
-rw-r--r-- | src/com/android/tradefed/device/NativeDevice.java | 2 | ||||
-rw-r--r-- | src/com/android/tradefed/device/TestDevice.java | 11 | ||||
-rw-r--r-- | tests/src/com/android/tradefed/device/TestDeviceTest.java | 37 |
3 files changed, 49 insertions, 1 deletions
diff --git a/src/com/android/tradefed/device/NativeDevice.java b/src/com/android/tradefed/device/NativeDevice.java index d03ad9fda..d5bc048fc 100644 --- a/src/com/android/tradefed/device/NativeDevice.java +++ b/src/com/android/tradefed/device/NativeDevice.java @@ -98,7 +98,7 @@ import javax.annotation.concurrent.GuardedBy; */ public class NativeDevice implements IManagedTestDevice { - private static final String SD_CARD = "/sdcard/"; + protected static final String SD_CARD = "/sdcard/"; /** * Allow pauses of up to 2 minutes while receiving bugreport. * <p/> diff --git a/src/com/android/tradefed/device/TestDevice.java b/src/com/android/tradefed/device/TestDevice.java index fdea4a255..43f193c2e 100644 --- a/src/com/android/tradefed/device/TestDevice.java +++ b/src/com/android/tradefed/device/TestDevice.java @@ -763,6 +763,17 @@ public class TestDevice extends NativeDevice { return packages; } + /** {@inheritDoc} */ + @Override + public boolean doesFileExist(String deviceFilePath) throws DeviceNotAvailableException { + if (deviceFilePath.startsWith(SD_CARD)) { + deviceFilePath = + deviceFilePath.replaceFirst( + SD_CARD, String.format("/storage/emulated/%s/", getCurrentUser())); + } + return super.doesFileExist(deviceFilePath); + } + /** * {@inheritDoc} */ diff --git a/tests/src/com/android/tradefed/device/TestDeviceTest.java b/tests/src/com/android/tradefed/device/TestDeviceTest.java index 66a7bcd3d..13ef5028c 100644 --- a/tests/src/com/android/tradefed/device/TestDeviceTest.java +++ b/tests/src/com/android/tradefed/device/TestDeviceTest.java @@ -3807,4 +3807,41 @@ public class TestDeviceTest extends TestCase { // verifyMocks(); // } // >>>>>>> f39a78ced... Hook up pullFile to use content provider. + + /** Test {@link TestDevice#doesFileExist(String)}. */ + public void testDoesFileExists() throws Exception { + injectShellResponse("ls \"/data/local/tmp/file\"", "file"); + EasyMock.replay(mMockIDevice); + assertTrue(mTestDevice.doesFileExist("/data/local/tmp/file")); + EasyMock.verify(mMockIDevice); + } + + /** Test {@link TestDevice#doesFileExist(String)} when the file does not exists. */ + public void testDoesFileExists_notExists() throws Exception { + injectShellResponse( + "ls \"/data/local/tmp/file\"", + "ls: cannot access 'file': No such file or directory\n"); + EasyMock.replay(mMockIDevice); + assertFalse(mTestDevice.doesFileExist("/data/local/tmp/file")); + EasyMock.verify(mMockIDevice); + } + + /** + * Test {@link TestDevice#doesFileExist(String)} when the file exists on an sdcard from another + * user. + */ + public void testDoesFileExists_sdcard() throws Exception { + mTestDevice = + new TestableTestDevice() { + @Override + public int getCurrentUser() + throws DeviceNotAvailableException, DeviceRuntimeException { + return 10; + } + }; + injectShellResponse("ls \"/storage/emulated/10/file\"", "file"); + EasyMock.replay(mMockIDevice); + assertTrue(mTestDevice.doesFileExist("/sdcard/file")); + EasyMock.verify(mMockIDevice); + } } |