blob: c79671fa9bfe5f9636d14b79702c9a8eef413db2 [file] [log] [blame]
Yang Yuc047f7b2021-06-25 12:22:42 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.appsearch;
18
19import static com.android.server.appsearch.UserStorageInfo.STORAGE_INFO_FILE;
20
21import static com.google.common.truth.Truth.assertThat;
22
23import com.android.server.appsearch.icing.proto.DocumentStorageInfoProto;
24import com.android.server.appsearch.icing.proto.NamespaceStorageInfoProto;
25import com.android.server.appsearch.icing.proto.StorageInfoProto;
26
27import org.junit.Before;
28import org.junit.Rule;
29import org.junit.Test;
30import org.junit.rules.TemporaryFolder;
31
32import java.io.File;
33import java.io.FileOutputStream;
34import java.io.IOException;
35
36public class UserStorageInfoTest {
37 @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
38 private File mFileParentPath;
39 private UserStorageInfo mUserStorageInfo;
40
41 @Before
42 public void setUp() throws Exception {
43 mFileParentPath = mTemporaryFolder.newFolder();
44 mUserStorageInfo = new UserStorageInfo(mFileParentPath);
45 }
46
47 @Test
48 public void testReadStorageInfoFromFile() throws IOException {
49 NamespaceStorageInfoProto namespaceStorageInfo1 =
50 NamespaceStorageInfoProto.newBuilder()
51 .setNamespace("pkg1$db/namespace")
52 .setNumAliveDocuments(2)
53 .setNumExpiredDocuments(1)
54 .build();
55 NamespaceStorageInfoProto namespaceStorageInfo2 =
56 NamespaceStorageInfoProto.newBuilder()
57 .setNamespace("pkg2$db/namespace")
58 .setNumAliveDocuments(3)
59 .setNumExpiredDocuments(3)
60 .build();
61 DocumentStorageInfoProto documentStorageInfo =
62 DocumentStorageInfoProto.newBuilder()
63 .setNumAliveDocuments(5)
64 .setNumExpiredDocuments(4)
65 .addNamespaceStorageInfo(namespaceStorageInfo1)
66 .addNamespaceStorageInfo(namespaceStorageInfo2)
67 .build();
68 StorageInfoProto storageInfo =
69 StorageInfoProto.newBuilder()
70 .setDocumentStorageInfo(documentStorageInfo)
71 .setTotalStorageSize(9)
72 .build();
73 File storageInfoFile = new File(mFileParentPath, STORAGE_INFO_FILE);
74 try (FileOutputStream out = new FileOutputStream(storageInfoFile)) {
75 storageInfo.writeTo(out);
76 }
77
78 mUserStorageInfo.readStorageInfoFromFile();
79
80 assertThat(mUserStorageInfo.getTotalSizeBytes()).isEqualTo(
81 storageInfo.getTotalStorageSize());
82 // We calculate the package storage size based on number of documents a package has.
83 // Here, total storage size is 9. pkg1 has 3 docs and pkg2 has 6 docs. So storage size of
84 // pkg1 is 3. pkg2's storage size is 6.
85 assertThat(mUserStorageInfo.getSizeBytesForPackage("pkg1")).isEqualTo(3);
86 assertThat(mUserStorageInfo.getSizeBytesForPackage("pkg2")).isEqualTo(6);
87 assertThat(mUserStorageInfo.getSizeBytesForPackage("invalid_pkg")).isEqualTo(0);
88 }
89
90 @Test
91 public void testCalculatePackageStorageInfoMap() {
92 NamespaceStorageInfoProto namespaceStorageInfo1 =
93 NamespaceStorageInfoProto.newBuilder()
94 .setNamespace("pkg1$db/namespace")
95 .setNumAliveDocuments(2)
96 .setNumExpiredDocuments(1)
97 .build();
98 NamespaceStorageInfoProto namespaceStorageInfo2 =
99 NamespaceStorageInfoProto.newBuilder()
100 .setNamespace("pkg2$db/namespace")
101 .setNumAliveDocuments(3)
102 .setNumExpiredDocuments(3)
103 .build();
104 DocumentStorageInfoProto documentStorageInfo =
105 DocumentStorageInfoProto.newBuilder()
106 .setNumAliveDocuments(5)
107 .setNumExpiredDocuments(4)
108 .addNamespaceStorageInfo(namespaceStorageInfo1)
109 .addNamespaceStorageInfo(namespaceStorageInfo2)
110 .build();
111 StorageInfoProto storageInfo =
112 StorageInfoProto.newBuilder()
113 .setDocumentStorageInfo(documentStorageInfo)
114 .setTotalStorageSize(9)
115 .build();
116
117 // We calculate the package storage size based on number of documents a package has.
118 // Here, total storage size is 9. pkg1 has 3 docs and pkg2 has 6 docs. So storage size of
119 // pkg1 is 3. pkg2's storage size is 6.
120 assertThat(mUserStorageInfo.calculatePackageStorageInfoMap(storageInfo))
121 .containsExactly("pkg1", 3L, "pkg2", 6L);
122 }
123}