forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] Provide a path instead of throwing NPE when getDir() suite…
…s returns null (flutter#30367)
- Loading branch information
Showing
3 changed files
with
128 additions
and
4 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
89 changes: 89 additions & 0 deletions
89
shell/platform/android/test/io/flutter/util/PathUtilsTest.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,89 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.content.Context; | ||
import android.os.Build; | ||
import java.io.File; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
@Config(manifest = Config.NONE) | ||
@RunWith(RobolectricTestRunner.class) | ||
public class PathUtilsTest { | ||
|
||
private static final String APP_DATA_PATH = "/data/data/package_name"; | ||
|
||
@Test | ||
public void canGetFilesDir() { | ||
Context context = mock(Context.class); | ||
when(context.getFilesDir()).thenReturn(new File(APP_DATA_PATH + "/files")); | ||
assertEquals(PathUtils.getFilesDir(context), APP_DATA_PATH + "/files"); | ||
} | ||
|
||
@Test | ||
public void canOnlyGetFilesPathWhenDiskFullAndFilesDirNotCreated() { | ||
Context context = mock(Context.class); | ||
when(context.getFilesDir()).thenReturn(null); | ||
if (Build.VERSION.SDK_INT >= 24) { | ||
when(context.getDataDir()).thenReturn(new File(APP_DATA_PATH)); | ||
} else { | ||
when(context.getApplicationInfo().dataDir).thenReturn(APP_DATA_PATH); | ||
} | ||
assertEquals(PathUtils.getFilesDir(context), APP_DATA_PATH + "/files"); | ||
} | ||
|
||
@Test | ||
public void canGetFlutterDataDir() { | ||
Context context = mock(Context.class); | ||
when(context.getDir("flutter", Context.MODE_PRIVATE)) | ||
.thenReturn(new File(APP_DATA_PATH + "/app_flutter")); | ||
assertEquals(PathUtils.getDataDirectory(context), APP_DATA_PATH + "/app_flutter"); | ||
} | ||
|
||
@Test | ||
public void canOnlyGetFlutterDataPathWhenDiskFullAndFlutterDataDirNotCreated() { | ||
Context context = mock(Context.class); | ||
when(context.getDir("flutter", Context.MODE_PRIVATE)).thenReturn(null); | ||
if (Build.VERSION.SDK_INT >= 24) { | ||
when(context.getDataDir()).thenReturn(new File(APP_DATA_PATH)); | ||
} else { | ||
when(context.getApplicationInfo().dataDir).thenReturn(APP_DATA_PATH); | ||
} | ||
assertEquals(PathUtils.getDataDirectory(context), APP_DATA_PATH + "/app_flutter"); | ||
} | ||
|
||
@Test | ||
public void canGetCacheDir() { | ||
Context context = mock(Context.class); | ||
when(context.getCacheDir()).thenReturn(new File(APP_DATA_PATH + "/cache")); | ||
if (Build.VERSION.SDK_INT >= 21) { | ||
when(context.getCodeCacheDir()).thenReturn(new File(APP_DATA_PATH + "/code_cache")); | ||
} | ||
assertTrue(PathUtils.getCacheDirectory(context).startsWith(APP_DATA_PATH)); | ||
} | ||
|
||
@Test | ||
public void canOnlyGetCachePathWhenDiskFullAndCacheDirNotCreated() { | ||
Context context = mock(Context.class); | ||
when(context.getCacheDir()).thenReturn(null); | ||
if (Build.VERSION.SDK_INT >= 21) { | ||
when(context.getCodeCacheDir()).thenReturn(null); | ||
} | ||
if (Build.VERSION.SDK_INT >= 24) { | ||
when(context.getDataDir()).thenReturn(new File(APP_DATA_PATH)); | ||
} else { | ||
when(context.getApplicationInfo().dataDir).thenReturn(APP_DATA_PATH); | ||
} | ||
assertEquals(PathUtils.getCacheDirectory(context), APP_DATA_PATH + "/cache"); | ||
} | ||
} |
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