Flutter Engine
The Flutter Engine
PathUtilsTest.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.util;
6
7import static io.flutter.Build.API_LEVELS;
8import static org.junit.Assert.assertEquals;
9import static org.junit.Assert.assertTrue;
10import static org.mockito.Mockito.mock;
11import static org.mockito.Mockito.when;
12
13import android.content.Context;
14import android.os.Build;
15import java.io.File;
16import org.junit.Test;
17import org.junit.runner.RunWith;
18import org.robolectric.RobolectricTestRunner;
19import org.robolectric.annotation.Config;
20
21@Config(manifest = Config.NONE)
22@RunWith(RobolectricTestRunner.class)
23public class PathUtilsTest {
24
25 private static final String APP_DATA_PATH = "/data/data/package_name";
26
27 @Test
28 public void canGetFilesDir() {
29 Context context = mock(Context.class);
30 when(context.getFilesDir()).thenReturn(new File(APP_DATA_PATH + "/files"));
31 assertEquals(PathUtils.getFilesDir(context), APP_DATA_PATH + "/files");
32 }
33
34 @Test
36 Context context = mock(Context.class);
37 when(context.getFilesDir()).thenReturn(null);
38 if (Build.VERSION.SDK_INT >= API_LEVELS.API_24) {
39 when(context.getDataDir()).thenReturn(new File(APP_DATA_PATH));
40 } else {
41 when(context.getApplicationInfo().dataDir).thenReturn(APP_DATA_PATH);
42 }
43 assertEquals(PathUtils.getFilesDir(context), APP_DATA_PATH + "/files");
44 }
45
46 @Test
47 public void canGetFlutterDataDir() {
48 Context context = mock(Context.class);
49 when(context.getDir("flutter", Context.MODE_PRIVATE))
50 .thenReturn(new File(APP_DATA_PATH + "/app_flutter"));
51 assertEquals(PathUtils.getDataDirectory(context), APP_DATA_PATH + "/app_flutter");
52 }
53
54 @Test
56 Context context = mock(Context.class);
57 when(context.getDir("flutter", Context.MODE_PRIVATE)).thenReturn(null);
58 if (Build.VERSION.SDK_INT >= API_LEVELS.API_24) {
59 when(context.getDataDir()).thenReturn(new File(APP_DATA_PATH));
60 } else {
61 when(context.getApplicationInfo().dataDir).thenReturn(APP_DATA_PATH);
62 }
63 assertEquals(PathUtils.getDataDirectory(context), APP_DATA_PATH + "/app_flutter");
64 }
65
66 @Test
67 public void canGetCacheDir() {
68 Context context = mock(Context.class);
69 when(context.getCacheDir()).thenReturn(new File(APP_DATA_PATH + "/cache"));
70 if (Build.VERSION.SDK_INT >= API_LEVELS.API_21) {
71 when(context.getCodeCacheDir()).thenReturn(new File(APP_DATA_PATH + "/code_cache"));
72 }
73 assertTrue(PathUtils.getCacheDirectory(context).startsWith(APP_DATA_PATH));
74 }
75
76 @Test
78 Context context = mock(Context.class);
79 when(context.getCacheDir()).thenReturn(null);
80 if (Build.VERSION.SDK_INT >= API_LEVELS.API_21) {
81 when(context.getCodeCacheDir()).thenReturn(null);
82 }
83 if (Build.VERSION.SDK_INT >= API_LEVELS.API_24) {
84 when(context.getDataDir()).thenReturn(new File(APP_DATA_PATH));
85 } else {
86 when(context.getApplicationInfo().dataDir).thenReturn(APP_DATA_PATH);
87 }
88 assertEquals(PathUtils.getCacheDirectory(context), APP_DATA_PATH + "/cache");
89 }
90}
static final int API_21
Definition: Build.java:11
static final int API_24
Definition: Build.java:14
void canOnlyGetFilesPathWhenDiskFullAndFilesDirNotCreated()
void canOnlyGetCachePathWhenDiskFullAndCacheDirNotCreated()
void canOnlyGetFlutterDataPathWhenDiskFullAndFlutterDataDirNotCreated()
static String getDataDirectory(@NonNull Context applicationContext)
Definition: PathUtils.java:25
static String getCacheDirectory(@NonNull Context applicationContext)
Definition: PathUtils.java:35
static String getFilesDir(@NonNull Context applicationContext)
Definition: PathUtils.java:16
def Build(configs, env, options)
Definition: build.py:232