summaryrefslogtreecommitdiff
path: root/sdksandbox/tests/hostsidetests/SdkSandboxStorageHostTest/codeprovider/src/com/android/tests/codeprovider/storagetest_1/StorageTestSandboxedSdkProvider.java
blob: c079cd33931a7378acc3f569abc34bbaa9621ed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2022 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.tests.codeprovider.storagetest_1;

import static com.google.common.truth.Truth.assertThat;

import android.app.sdksandbox.SandboxedSdk;
import android.app.sdksandbox.SandboxedSdkProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class StorageTestSandboxedSdkProvider extends SandboxedSdkProvider {
    private static final String TAG = "SdkSandboxStorageTestProvider";
    private static final String BUNDLE_KEY_PHASE_NAME = "phase-name";

    @Override
    public SandboxedSdk onLoadSdk(Bundle params) {
        return new SandboxedSdk(new Binder());
    }

    @Override
    public View getView(Context windowContext, Bundle params, int width, int height) {
        try {
            handlePhase(params);
        } catch (Throwable e) {
            Log.e(TAG, e.getMessage(), e);
            throw new RuntimeException();
        }
        return null;
    }

    private void handlePhase(Bundle params) throws Exception {
        String phaseName = params.getString(BUNDLE_KEY_PHASE_NAME, "");
        Log.i(TAG, "Handling phase: " + phaseName);
        switch (phaseName) {
            case "testSdkDataPackageDirectory_SharedStorageIsUsable":
                testSdkDataPackageDirectory_SharedStorageIsUsable();
                break;
            case "testSdkDataSubDirectory_PerSdkStorageIsUsable":
                testSdkDataSubDirectory_PerSdkStorageIsUsable();
                break;
            case "testSdkDataIsAttributedToApp":
                testSdkDataIsAttributedToApp();
                break;
            case "testSharedPreferences_BulkSyncReceived":
                testSharedPreferences_BulkSyncReceived();
                break;
            case "testSharedPreferences_UpdateReceived":
                testSharedPreferences_UpdateReceived();
                break;
            case "testSharedPreferences_UpdateNotReceived":
                testSharedPreferences_UpdateNotReceived();
                break;
            default:
        }
    }

    private void testSdkDataPackageDirectory_SharedStorageIsUsable() throws Exception {
        String sharedPath = getSharedStoragePath();
        // Read the file
        String input = Files.readAllLines(Paths.get(sharedPath, "readme.txt")).get(0);

        // Create a dir
        Files.createDirectory(Paths.get(sharedPath, "dir"));
        // Write to a file
        Path filepath = Paths.get(sharedPath, "dir", "file");
        Files.createFile(filepath);
        Files.write(filepath, input.getBytes());
    }

    private void testSdkDataSubDirectory_PerSdkStorageIsUsable() throws Exception {
        String sdkDataPath = getContext().getDataDir().toString();
        // Read the file
        String input = Files.readAllLines(Paths.get(sdkDataPath, "readme.txt")).get(0);

        // Create a dir
        Files.createDirectory(Paths.get(sdkDataPath, "dir"));
        // Write to a file
        Path filepath = Paths.get(sdkDataPath, "dir", "file");
        Files.createFile(filepath);
        Files.write(filepath, input.getBytes());
    }

    private void testSdkDataIsAttributedToApp() throws Exception {
        final byte[] buffer = new byte[1000000];
        String sharedPath = getSharedStoragePath();
        String sharedCachePath = getSharedStorageCachePath();

        Files.createDirectory(Paths.get(sharedPath, "attribution"));
        Path filepath = Paths.get(sharedPath, "attribution", "file");
        Files.createFile(filepath);
        Files.write(filepath, buffer);

        Files.createDirectory(Paths.get(sharedCachePath, "attribution"));
        Path cacheFilepath = Paths.get(sharedCachePath, "attribution", "file");
        Files.createFile(cacheFilepath);
        Files.write(cacheFilepath, buffer);
    }

    private void testSharedPreferences_BulkSyncReceived() throws Exception {
        SharedPreferences pref = getClientSharedPreferences();
        assertThat(pref.getString("hello", "")).isEqualTo("world");
    }

    private void testSharedPreferences_UpdateReceived() throws Exception {
        SharedPreferences pref = getClientSharedPreferences();
        assertThat(pref.getString("hello", "")).isEqualTo("update");
    }

    private void testSharedPreferences_UpdateNotReceived() throws Exception {
        SharedPreferences pref = getClientSharedPreferences();
        assertThat(pref.getString("hello", "")).isNotEqualTo("update");
    }

    // TODO(b/237410689): Replace with real getClientSharedPreferences
    private SharedPreferences getClientSharedPreferences() throws Exception {
        return PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext());
    }

    private String getSharedStoragePath() {
        return getContext().getApplicationContext().getDataDir().toString();
    }

    private String getSharedStorageCachePath() {
        return getContext().getApplicationContext().getCacheDir().toString();
    }
}