Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 17 | package com.android.mtp; |
| 18 | |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 19 | import android.database.Cursor; |
| 20 | import android.mtp.MtpConstants; |
| 21 | import android.mtp.MtpObjectInfo; |
| 22 | import android.provider.DocumentsContract; |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 23 | import android.provider.DocumentsContract.Document; |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 24 | import android.provider.DocumentsContract.Root; |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 25 | import android.test.AndroidTestCase; |
| 26 | import android.test.suitebuilder.annotation.SmallTest; |
| 27 | |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 28 | import java.io.FileNotFoundException; |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 29 | |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 30 | import static android.provider.DocumentsContract.Document.*; |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 31 | import static com.android.mtp.MtpDatabase.strings; |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 32 | import static com.android.mtp.MtpDatabaseConstants.*; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 33 | import static com.android.mtp.TestUtil.OPERATIONS_SUPPORTED; |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 34 | |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 35 | @SmallTest |
| 36 | public class MtpDatabaseTest extends AndroidTestCase { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 37 | private static final String[] COLUMN_NAMES = new String[] { |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 38 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 39 | MtpDatabaseConstants.COLUMN_DEVICE_ID, |
| 40 | MtpDatabaseConstants.COLUMN_STORAGE_ID, |
| 41 | MtpDatabaseConstants.COLUMN_OBJECT_HANDLE, |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 42 | DocumentsContract.Document.COLUMN_MIME_TYPE, |
| 43 | DocumentsContract.Document.COLUMN_DISPLAY_NAME, |
| 44 | DocumentsContract.Document.COLUMN_SUMMARY, |
| 45 | DocumentsContract.Document.COLUMN_LAST_MODIFIED, |
| 46 | DocumentsContract.Document.COLUMN_ICON, |
| 47 | DocumentsContract.Document.COLUMN_FLAGS, |
Daichi Hirono | 4b54e03 | 2015-12-11 15:24:53 +0900 | [diff] [blame] | 48 | DocumentsContract.Document.COLUMN_SIZE, |
| 49 | MtpDatabaseConstants.COLUMN_DOCUMENT_TYPE |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 50 | }; |
| 51 | |
Daichi Hirono | 85a808b | 2015-10-27 17:22:13 +0900 | [diff] [blame] | 52 | private final TestResources resources = new TestResources(); |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 53 | MtpDatabase mDatabase; |
| 54 | |
| 55 | @Override |
| 56 | public void setUp() { |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 57 | mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void tearDown() { |
| 62 | mDatabase.close(); |
| 63 | mDatabase = null; |
| 64 | } |
Daichi Hirono | 85a808b | 2015-10-27 17:22:13 +0900 | [diff] [blame] | 65 | |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 66 | private static int getInt(Cursor cursor, String columnName) { |
| 67 | return cursor.getInt(cursor.getColumnIndex(columnName)); |
| 68 | } |
| 69 | |
| 70 | private static boolean isNull(Cursor cursor, String columnName) { |
| 71 | return cursor.isNull(cursor.getColumnIndex(columnName)); |
| 72 | } |
| 73 | |
| 74 | private static String getString(Cursor cursor, String columnName) { |
| 75 | return cursor.getString(cursor.getColumnIndex(columnName)); |
| 76 | } |
| 77 | |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 78 | public void testPutSingleStorageDocuments() throws Exception { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 79 | addTestDevice(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 80 | |
| 81 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 82 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 83 | new MtpRoot(0, 1, "Storage", 1000, 2000, "") |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 84 | }); |
| 85 | mDatabase.getMapper().stopAddingDocuments("1"); |
| 86 | |
| 87 | { |
| 88 | final Cursor cursor = mDatabase.queryRootDocuments(COLUMN_NAMES); |
| 89 | assertEquals(1, cursor.getCount()); |
| 90 | |
| 91 | cursor.moveToNext(); |
| 92 | assertEquals(2, getInt(cursor, COLUMN_DOCUMENT_ID)); |
| 93 | assertEquals(0, getInt(cursor, COLUMN_DEVICE_ID)); |
| 94 | assertEquals(1, getInt(cursor, COLUMN_STORAGE_ID)); |
| 95 | assertTrue(isNull(cursor, COLUMN_OBJECT_HANDLE)); |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 96 | assertEquals( |
| 97 | DocumentsContract.Document.MIME_TYPE_DIR, getString(cursor, COLUMN_MIME_TYPE)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 98 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 99 | assertTrue(isNull(cursor, COLUMN_SUMMARY)); |
| 100 | assertTrue(isNull(cursor, COLUMN_LAST_MODIFIED)); |
| 101 | assertEquals(R.drawable.ic_root_mtp, getInt(cursor, COLUMN_ICON)); |
| 102 | assertEquals(0, getInt(cursor, COLUMN_FLAGS)); |
| 103 | assertEquals(1000, getInt(cursor, COLUMN_SIZE)); |
| 104 | assertEquals( |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 105 | MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE, |
| 106 | getInt(cursor, COLUMN_DOCUMENT_TYPE)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 107 | |
| 108 | cursor.close(); |
| 109 | } |
| 110 | |
| 111 | { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 112 | final Cursor cursor = mDatabase.queryRoots(resources, new String [] { |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 113 | Root.COLUMN_ROOT_ID, |
| 114 | Root.COLUMN_FLAGS, |
| 115 | Root.COLUMN_ICON, |
| 116 | Root.COLUMN_TITLE, |
| 117 | Root.COLUMN_SUMMARY, |
| 118 | Root.COLUMN_DOCUMENT_ID, |
| 119 | Root.COLUMN_AVAILABLE_BYTES, |
| 120 | Root.COLUMN_CAPACITY_BYTES |
| 121 | }); |
| 122 | assertEquals(1, cursor.getCount()); |
| 123 | |
| 124 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 125 | assertEquals(1, getInt(cursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 126 | assertEquals( |
| 127 | Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE, |
| 128 | getInt(cursor, Root.COLUMN_FLAGS)); |
| 129 | assertEquals(R.drawable.ic_root_mtp, getInt(cursor, Root.COLUMN_ICON)); |
| 130 | assertEquals("Device Storage", getString(cursor, Root.COLUMN_TITLE)); |
| 131 | assertTrue(isNull(cursor, Root.COLUMN_SUMMARY)); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 132 | assertEquals(1, getInt(cursor, Root.COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 133 | assertEquals(1000, getInt(cursor, Root.COLUMN_AVAILABLE_BYTES)); |
| 134 | assertEquals(2000, getInt(cursor, Root.COLUMN_CAPACITY_BYTES)); |
| 135 | |
| 136 | cursor.close(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public void testPutStorageDocuments() throws Exception { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 141 | addTestDevice(); |
| 142 | |
| 143 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 144 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 145 | new MtpRoot(0, 1, "Storage", 1000, 2000, ""), |
| 146 | new MtpRoot(0, 2, "Storage", 2000, 4000, ""), |
| 147 | new MtpRoot(0, 3, "/@#%&<>Storage", 3000, 6000,"") |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 148 | }); |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 149 | |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 150 | { |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 151 | final Cursor cursor = mDatabase.queryRootDocuments(COLUMN_NAMES); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 152 | assertEquals(3, cursor.getCount()); |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 153 | |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 154 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 155 | assertEquals(2, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 156 | assertEquals(0, getInt(cursor, COLUMN_DEVICE_ID)); |
| 157 | assertEquals(1, getInt(cursor, COLUMN_STORAGE_ID)); |
| 158 | assertTrue(isNull(cursor, COLUMN_OBJECT_HANDLE)); |
| 159 | assertEquals(DocumentsContract.Document.MIME_TYPE_DIR, getString(cursor, COLUMN_MIME_TYPE)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 160 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 161 | assertTrue(isNull(cursor, COLUMN_SUMMARY)); |
| 162 | assertTrue(isNull(cursor, COLUMN_LAST_MODIFIED)); |
| 163 | assertEquals(R.drawable.ic_root_mtp, getInt(cursor, COLUMN_ICON)); |
| 164 | assertEquals(0, getInt(cursor, COLUMN_FLAGS)); |
| 165 | assertEquals(1000, getInt(cursor, COLUMN_SIZE)); |
Daichi Hirono | 4b54e03 | 2015-12-11 15:24:53 +0900 | [diff] [blame] | 166 | assertEquals( |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 167 | MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE, getInt(cursor, COLUMN_DOCUMENT_TYPE)); |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 168 | |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 169 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 170 | assertEquals(3, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 171 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 172 | |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 173 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 174 | assertEquals(4, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 175 | assertEquals("/@#%&<>Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 88d2f78 | 2015-10-27 16:41:06 +0900 | [diff] [blame] | 176 | |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 177 | cursor.close(); |
| 178 | } |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 179 | } |
| 180 | |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 181 | private MtpObjectInfo createDocument(int objectHandle, String name, int format, int size) { |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 182 | final MtpObjectInfo.Builder builder = new MtpObjectInfo.Builder(); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 183 | builder.setObjectHandle(objectHandle); |
| 184 | builder.setName(name); |
| 185 | builder.setFormat(format); |
| 186 | builder.setCompressedSize(size); |
| 187 | return builder.build(); |
| 188 | } |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 189 | |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 190 | public void testPutChildDocuments() throws Exception { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 191 | addTestDevice(); |
| 192 | addTestStorage("1"); |
| 193 | |
| 194 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 195 | mDatabase.getMapper().putChildDocuments(0, "2", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 196 | createDocument(100, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 197 | createDocument(101, "image.jpg", MtpConstants.FORMAT_EXIF_JPEG, 2 * 1024 * 1024), |
| 198 | createDocument(102, "music.mp3", MtpConstants.FORMAT_MP3, 3 * 1024 * 1024) |
| 199 | }); |
| 200 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 201 | final Cursor cursor = mDatabase.queryChildDocuments(COLUMN_NAMES, "2"); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 202 | assertEquals(3, cursor.getCount()); |
| 203 | |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 204 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 205 | assertEquals(3, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 206 | assertEquals(0, getInt(cursor, COLUMN_DEVICE_ID)); |
| 207 | assertEquals(0, getInt(cursor, COLUMN_STORAGE_ID)); |
| 208 | assertEquals(100, getInt(cursor, COLUMN_OBJECT_HANDLE)); |
| 209 | assertEquals("text/plain", getString(cursor, COLUMN_MIME_TYPE)); |
| 210 | assertEquals("note.txt", getString(cursor, COLUMN_DISPLAY_NAME)); |
| 211 | assertTrue(isNull(cursor, COLUMN_SUMMARY)); |
| 212 | assertTrue(isNull(cursor, COLUMN_LAST_MODIFIED)); |
| 213 | assertTrue(isNull(cursor, COLUMN_ICON)); |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 214 | assertEquals( |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 215 | COLUMN_FLAGS, |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 216 | DocumentsContract.Document.FLAG_SUPPORTS_DELETE | |
| 217 | DocumentsContract.Document.FLAG_SUPPORTS_WRITE, |
Daichi Hirono | 88d2f78 | 2015-10-27 16:41:06 +0900 | [diff] [blame] | 218 | cursor.getInt(9)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 219 | assertEquals(1024, getInt(cursor, COLUMN_SIZE)); |
Daichi Hirono | 4b54e03 | 2015-12-11 15:24:53 +0900 | [diff] [blame] | 220 | assertEquals( |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 221 | MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT, getInt(cursor, COLUMN_DOCUMENT_TYPE)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 222 | |
| 223 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 224 | assertEquals(4, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 225 | assertEquals(0, getInt(cursor, COLUMN_DEVICE_ID)); |
| 226 | assertEquals(0, getInt(cursor, COLUMN_STORAGE_ID)); |
| 227 | assertEquals(101, getInt(cursor, COLUMN_OBJECT_HANDLE)); |
| 228 | assertEquals("image/jpeg", getString(cursor, COLUMN_MIME_TYPE)); |
| 229 | assertEquals("image.jpg", getString(cursor, COLUMN_DISPLAY_NAME)); |
| 230 | assertTrue(isNull(cursor, COLUMN_SUMMARY)); |
| 231 | assertTrue(isNull(cursor, COLUMN_LAST_MODIFIED)); |
| 232 | assertTrue(isNull(cursor, COLUMN_ICON)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 233 | assertEquals( |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 234 | COLUMN_FLAGS, |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 235 | DocumentsContract.Document.FLAG_SUPPORTS_DELETE | |
| 236 | DocumentsContract.Document.FLAG_SUPPORTS_WRITE, |
| 237 | cursor.getInt(9)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 238 | assertEquals(2 * 1024 * 1024, getInt(cursor, COLUMN_SIZE)); |
Daichi Hirono | 4b54e03 | 2015-12-11 15:24:53 +0900 | [diff] [blame] | 239 | assertEquals( |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 240 | MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT, getInt(cursor, COLUMN_DOCUMENT_TYPE)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 241 | |
| 242 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 243 | assertEquals(5, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 244 | assertEquals(0, getInt(cursor, COLUMN_DEVICE_ID)); |
| 245 | assertEquals(0, getInt(cursor, COLUMN_STORAGE_ID)); |
| 246 | assertEquals(102, getInt(cursor, COLUMN_OBJECT_HANDLE)); |
| 247 | assertEquals("audio/mpeg", getString(cursor, COLUMN_MIME_TYPE)); |
| 248 | assertEquals("music.mp3", getString(cursor, COLUMN_DISPLAY_NAME)); |
| 249 | assertTrue(isNull(cursor, COLUMN_SUMMARY)); |
| 250 | assertTrue(isNull(cursor, COLUMN_LAST_MODIFIED)); |
| 251 | assertTrue(isNull(cursor, COLUMN_ICON)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 252 | assertEquals( |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 253 | COLUMN_FLAGS, |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 254 | DocumentsContract.Document.FLAG_SUPPORTS_DELETE | |
| 255 | DocumentsContract.Document.FLAG_SUPPORTS_WRITE, |
| 256 | cursor.getInt(9)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 257 | assertEquals(3 * 1024 * 1024, getInt(cursor, COLUMN_SIZE)); |
Daichi Hirono | 4b54e03 | 2015-12-11 15:24:53 +0900 | [diff] [blame] | 258 | assertEquals( |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 259 | MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT, getInt(cursor, COLUMN_DOCUMENT_TYPE)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 260 | |
| 261 | cursor.close(); |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 262 | } |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 263 | |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 264 | public void testPutChildDocuments_operationsSupported() throws Exception { |
| 265 | addTestDevice(); |
| 266 | addTestStorage("1"); |
| 267 | |
| 268 | // Put a document with empty supported operations. |
| 269 | mDatabase.getMapper().startAddingDocuments("2"); |
| 270 | mDatabase.getMapper().putChildDocuments(0, "2", new int[0], new MtpObjectInfo[] { |
| 271 | createDocument(100, "note.txt", MtpConstants.FORMAT_TEXT, 1024) |
| 272 | }); |
| 273 | mDatabase.getMapper().stopAddingDocuments("2"); |
| 274 | |
| 275 | try (final Cursor cursor = |
| 276 | mDatabase.queryChildDocuments(strings(Document.COLUMN_FLAGS), "2")) { |
| 277 | assertEquals(1, cursor.getCount()); |
| 278 | cursor.moveToNext(); |
| 279 | assertEquals(0, cursor.getInt(0)); |
| 280 | } |
| 281 | |
| 282 | // Put a document with writable operations. |
| 283 | mDatabase.getMapper().startAddingDocuments("2"); |
| 284 | mDatabase.getMapper().putChildDocuments(0, "2", new int[] { |
| 285 | MtpConstants.OPERATION_SEND_OBJECT, |
| 286 | MtpConstants.OPERATION_SEND_OBJECT_INFO, |
| 287 | }, new MtpObjectInfo[] { |
| 288 | createDocument(100, "note.txt", MtpConstants.FORMAT_TEXT, 1024) |
| 289 | }); |
| 290 | mDatabase.getMapper().stopAddingDocuments("2"); |
| 291 | |
| 292 | try (final Cursor cursor = |
| 293 | mDatabase.queryChildDocuments(strings(Document.COLUMN_FLAGS), "2")) { |
| 294 | assertEquals(1, cursor.getCount()); |
| 295 | cursor.moveToNext(); |
| 296 | assertEquals(Document.FLAG_SUPPORTS_WRITE, cursor.getInt(0)); |
| 297 | } |
| 298 | |
| 299 | // Put a document with deletable operations. |
| 300 | mDatabase.getMapper().startAddingDocuments("2"); |
| 301 | mDatabase.getMapper().putChildDocuments(0, "2", new int[] { |
| 302 | MtpConstants.OPERATION_DELETE_OBJECT |
| 303 | }, new MtpObjectInfo[] { |
| 304 | createDocument(100, "note.txt", MtpConstants.FORMAT_TEXT, 1024) |
| 305 | }); |
| 306 | mDatabase.getMapper().stopAddingDocuments("2"); |
| 307 | |
| 308 | try (final Cursor cursor = |
| 309 | mDatabase.queryChildDocuments(strings(Document.COLUMN_FLAGS), "2")) { |
| 310 | assertEquals(1, cursor.getCount()); |
| 311 | cursor.moveToNext(); |
| 312 | assertEquals(Document.FLAG_SUPPORTS_DELETE, cursor.getInt(0)); |
| 313 | } |
| 314 | } |
| 315 | |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 316 | public void testRestoreIdForRootDocuments() throws Exception { |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 317 | final String[] columns = new String[] { |
| 318 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 319 | MtpDatabaseConstants.COLUMN_STORAGE_ID, |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 320 | DocumentsContract.Document.COLUMN_DISPLAY_NAME |
| 321 | }; |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 322 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 323 | // Add device and two storages. |
| 324 | addTestDevice(); |
| 325 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 326 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 327 | new MtpRoot(0, 100, "Storage A", 1000, 0, ""), |
| 328 | new MtpRoot(0, 101, "Storage B", 1001, 0, "") |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 329 | }); |
| 330 | |
| 331 | { |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 332 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 333 | assertEquals(2, cursor.getCount()); |
| 334 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 335 | assertEquals(2, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 336 | assertEquals(100, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 337 | assertEquals("Storage A", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 338 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 339 | assertEquals(3, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 340 | assertEquals(101, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 341 | assertEquals("Storage B", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 342 | cursor.close(); |
| 343 | } |
| 344 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 345 | // Clear mapping and add a device. |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 346 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 347 | addTestDevice(); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 348 | |
| 349 | { |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 350 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 351 | assertEquals(0, cursor.getCount()); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 352 | cursor.close(); |
| 353 | } |
| 354 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 355 | // Add two storages, but one's name is different from previous one. |
| 356 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 357 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 358 | new MtpRoot(0, 200, "Storage A", 2000, 0, ""), |
| 359 | new MtpRoot(0, 202, "Storage C", 2002, 0, "") |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 360 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 361 | mDatabase.getMapper().stopAddingDocuments("1"); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 362 | |
| 363 | { |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 364 | // After compeleting mapping, Storage A can be obtained with new storage ID. |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 365 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 366 | assertEquals(2, cursor.getCount()); |
| 367 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 368 | assertEquals(2, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 369 | assertEquals(200, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 370 | assertEquals("Storage A", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 371 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 372 | assertEquals(4, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 373 | assertEquals(202, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 374 | assertEquals("Storage C", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 375 | cursor.close(); |
| 376 | } |
| 377 | } |
| 378 | |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 379 | public void testRestoreIdForChildDocuments() throws Exception { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 380 | final String[] columns = new String[] { |
| 381 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 382 | MtpDatabaseConstants.COLUMN_OBJECT_HANDLE, |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 383 | DocumentsContract.Document.COLUMN_DISPLAY_NAME |
| 384 | }; |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 385 | |
| 386 | addTestDevice(); |
| 387 | addTestStorage("1"); |
| 388 | |
| 389 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 390 | mDatabase.getMapper().putChildDocuments(0, "2", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 391 | createDocument(100, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 392 | createDocument(101, "image.jpg", MtpConstants.FORMAT_EXIF_JPEG, 2 * 1024 * 1024), |
| 393 | createDocument(102, "music.mp3", MtpConstants.FORMAT_MP3, 3 * 1024 * 1024) |
| 394 | }); |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 395 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 396 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 397 | addTestDevice(); |
| 398 | addTestStorage("1"); |
| 399 | |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 400 | { |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 401 | // Don't return objects that lost MTP object handles. |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 402 | final Cursor cursor = mDatabase.queryChildDocuments(columns, "2"); |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 403 | assertEquals(0, cursor.getCount()); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 404 | cursor.close(); |
| 405 | } |
| 406 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 407 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 408 | mDatabase.getMapper().putChildDocuments(0, "2", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 409 | createDocument(200, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 410 | createDocument(203, "video.mp4", MtpConstants.FORMAT_MP4_CONTAINER, 1024), |
| 411 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 412 | mDatabase.getMapper().stopAddingDocuments("2"); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 413 | |
| 414 | { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 415 | final Cursor cursor = mDatabase.queryChildDocuments(columns, "2"); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 416 | assertEquals(2, cursor.getCount()); |
| 417 | |
| 418 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 419 | assertEquals(3, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 420 | assertEquals(200, getInt(cursor, COLUMN_OBJECT_HANDLE)); |
| 421 | assertEquals("note.txt", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 422 | |
| 423 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 424 | assertEquals(6, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 425 | assertEquals(203, getInt(cursor, COLUMN_OBJECT_HANDLE)); |
| 426 | assertEquals("video.mp4", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 427 | |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 428 | cursor.close(); |
| 429 | } |
| 430 | } |
| 431 | |
Daichi Hirono | b3fe72b | 2015-12-15 07:45:06 +0000 | [diff] [blame] | 432 | public void testRestoreIdForDifferentDevices() throws Exception { |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 433 | final String[] columns = new String[] { |
| 434 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 435 | MtpDatabaseConstants.COLUMN_STORAGE_ID, |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 436 | DocumentsContract.Document.COLUMN_DISPLAY_NAME |
| 437 | }; |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 438 | final String[] rootColumns = new String[] { |
| 439 | Root.COLUMN_ROOT_ID, |
| 440 | Root.COLUMN_AVAILABLE_BYTES |
| 441 | }; |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 442 | mDatabase.getMapper().startAddingDocuments(null); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 443 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 444 | 0, "Device A", "Device key A", true, new MtpRoot[0], null, null)); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 445 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 446 | 1, "Device B", "Device key B", true, new MtpRoot[0], null, null)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 447 | mDatabase.getMapper().stopAddingDocuments(null); |
| 448 | |
| 449 | mDatabase.getMapper().startAddingDocuments("1"); |
| 450 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 451 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 452 | new MtpRoot(0, 100, "Storage", 0, 0, "") |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 453 | }); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 454 | mDatabase.getMapper().putStorageDocuments("2", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 455 | new MtpRoot(1, 100, "Storage", 0, 0, "") |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 456 | }); |
| 457 | |
| 458 | { |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 459 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 460 | assertEquals(2, cursor.getCount()); |
| 461 | cursor.moveToNext(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 462 | assertEquals(3, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 463 | assertEquals(100, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 464 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 465 | cursor.moveToNext(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 466 | assertEquals(4, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 467 | assertEquals(100, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 468 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 469 | cursor.close(); |
| 470 | } |
| 471 | |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 472 | { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 473 | final Cursor cursor = mDatabase.queryRoots(resources, rootColumns); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 474 | assertEquals(2, cursor.getCount()); |
| 475 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 476 | assertEquals(1, getInt(cursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 477 | assertEquals(0, getInt(cursor, Root.COLUMN_AVAILABLE_BYTES)); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 478 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 479 | assertEquals(2, getInt(cursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 480 | assertEquals(0, getInt(cursor, Root.COLUMN_AVAILABLE_BYTES)); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 481 | cursor.close(); |
| 482 | } |
| 483 | |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 484 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 485 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 486 | mDatabase.getMapper().startAddingDocuments(null); |
| 487 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 488 | 0, "Device A", "Device key A", true, new MtpRoot[0], null, null)); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 489 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 490 | 1, "Device B", "Device key B", true, new MtpRoot[0], null, null)); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 491 | mDatabase.getMapper().stopAddingDocuments(null); |
| 492 | |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 493 | mDatabase.getMapper().startAddingDocuments("1"); |
| 494 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 495 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 496 | new MtpRoot(0, 200, "Storage", 2000, 0, "") |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 497 | }); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 498 | mDatabase.getMapper().putStorageDocuments("2", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 499 | new MtpRoot(1, 300, "Storage", 3000, 0, "") |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 500 | }); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 501 | mDatabase.getMapper().stopAddingDocuments("1"); |
| 502 | mDatabase.getMapper().stopAddingDocuments("2"); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 503 | |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 504 | { |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 505 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 506 | assertEquals(2, cursor.getCount()); |
| 507 | cursor.moveToNext(); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 508 | assertEquals(3, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 509 | assertEquals(200, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 510 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 511 | cursor.moveToNext(); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 512 | assertEquals(4, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 513 | assertEquals(300, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 514 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 515 | cursor.close(); |
| 516 | } |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 517 | |
| 518 | { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 519 | final Cursor cursor = mDatabase.queryRoots(resources, rootColumns); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 520 | assertEquals(2, cursor.getCount()); |
| 521 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 522 | assertEquals(1, getInt(cursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 523 | assertEquals(2000, getInt(cursor, Root.COLUMN_AVAILABLE_BYTES)); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 524 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 525 | assertEquals(2, getInt(cursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 526 | assertEquals(3000, getInt(cursor, Root.COLUMN_AVAILABLE_BYTES)); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 527 | cursor.close(); |
| 528 | } |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 529 | } |
| 530 | |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 531 | public void testRestoreIdForDifferentParents() throws Exception { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 532 | final String[] columns = new String[] { |
| 533 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 534 | MtpDatabaseConstants.COLUMN_OBJECT_HANDLE |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 535 | }; |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 536 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 537 | // Add device, storage, and two directories. |
| 538 | addTestDevice(); |
| 539 | addTestStorage("1"); |
| 540 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 541 | mDatabase.getMapper().putChildDocuments(0, "2", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 542 | createDocument(50, "A", MtpConstants.FORMAT_ASSOCIATION, 0), |
| 543 | createDocument(51, "B", MtpConstants.FORMAT_ASSOCIATION, 0), |
| 544 | }); |
| 545 | mDatabase.getMapper().stopAddingDocuments("2"); |
| 546 | |
| 547 | // Put note.txt in each directory. |
| 548 | mDatabase.getMapper().startAddingDocuments("3"); |
| 549 | mDatabase.getMapper().startAddingDocuments("4"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 550 | mDatabase.getMapper().putChildDocuments(0, "3", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 551 | createDocument(100, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 552 | }); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 553 | mDatabase.getMapper().putChildDocuments(0, "4", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 554 | createDocument(101, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 555 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 556 | |
| 557 | // Clear mapping. |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 558 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 559 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 560 | // Add device, storage, and two directories again. |
| 561 | addTestDevice(); |
| 562 | addTestStorage("1"); |
| 563 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 564 | mDatabase.getMapper().putChildDocuments(0, "2", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 565 | createDocument(50, "A", MtpConstants.FORMAT_ASSOCIATION, 0), |
| 566 | createDocument(51, "B", MtpConstants.FORMAT_ASSOCIATION, 0), |
| 567 | }); |
| 568 | mDatabase.getMapper().stopAddingDocuments("2"); |
| 569 | |
| 570 | // Add note.txt in each directory again. |
| 571 | mDatabase.getMapper().startAddingDocuments("3"); |
| 572 | mDatabase.getMapper().startAddingDocuments("4"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 573 | mDatabase.getMapper().putChildDocuments(0, "3", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 574 | createDocument(200, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 575 | }); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 576 | mDatabase.getMapper().putChildDocuments(0, "4", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 577 | createDocument(201, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 578 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 579 | mDatabase.getMapper().stopAddingDocuments("3"); |
| 580 | mDatabase.getMapper().stopAddingDocuments("4"); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 581 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 582 | // Check if the two note.txt are mapped correctly. |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 583 | { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 584 | final Cursor cursor = mDatabase.queryChildDocuments(columns, "3"); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 585 | assertEquals(1, cursor.getCount()); |
| 586 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 587 | assertEquals(5, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 588 | assertEquals(200, getInt(cursor, COLUMN_OBJECT_HANDLE)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 589 | cursor.close(); |
| 590 | } |
| 591 | { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 592 | final Cursor cursor = mDatabase.queryChildDocuments(columns, "4"); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 593 | assertEquals(1, cursor.getCount()); |
| 594 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 595 | assertEquals(6, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9fca541 | 2016-02-07 13:20:22 +0900 | [diff] [blame] | 596 | assertEquals(201, getInt(cursor, COLUMN_OBJECT_HANDLE)); |
Daichi Hirono | a8a3722e | 2015-10-26 16:39:54 +0900 | [diff] [blame] | 597 | cursor.close(); |
| 598 | } |
| 599 | } |
| 600 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 601 | public void testClearMtpIdentifierBeforeResolveRootDocuments() throws Exception { |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 602 | final String[] columns = new String[] { |
| 603 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 604 | MtpDatabaseConstants.COLUMN_STORAGE_ID, |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 605 | DocumentsContract.Document.COLUMN_DISPLAY_NAME |
| 606 | }; |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 607 | final String[] rootColumns = new String[] { |
| 608 | Root.COLUMN_ROOT_ID, |
| 609 | Root.COLUMN_AVAILABLE_BYTES |
| 610 | }; |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 611 | |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 612 | addTestDevice(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 613 | |
| 614 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 615 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 616 | new MtpRoot(0, 100, "Storage", 0, 0, ""), |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 617 | }); |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 618 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 619 | |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 620 | addTestDevice(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 621 | |
| 622 | try (final Cursor cursor = mDatabase.queryRoots(resources, rootColumns)) { |
| 623 | assertEquals(1, cursor.getCount()); |
| 624 | cursor.moveToNext(); |
| 625 | assertEquals("1", getString(cursor, Root.COLUMN_ROOT_ID)); |
| 626 | } |
| 627 | |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 628 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 629 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 630 | new MtpRoot(0, 200, "Storage", 2000, 0, ""), |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 631 | }); |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 632 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 633 | |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 634 | addTestDevice(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 635 | |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 636 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 637 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 638 | new MtpRoot(0, 300, "Storage", 3000, 0, ""), |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 639 | }); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 640 | mDatabase.getMapper().stopAddingDocuments("1"); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 641 | |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 642 | { |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 643 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 644 | assertEquals(1, cursor.getCount()); |
| 645 | cursor.moveToNext(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 646 | assertEquals(2, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 647 | assertEquals(300, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 648 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 649 | cursor.close(); |
| 650 | } |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 651 | { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 652 | final Cursor cursor = mDatabase.queryRoots(resources, rootColumns); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 653 | assertEquals(1, cursor.getCount()); |
| 654 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 655 | assertEquals(1, getInt(cursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 656 | assertEquals(3000, getInt(cursor, Root.COLUMN_AVAILABLE_BYTES)); |
Daichi Hirono | 0378da4 | 2015-11-05 11:56:12 +0900 | [diff] [blame] | 657 | cursor.close(); |
| 658 | } |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | public void testPutSameNameRootsAfterClearing() throws Exception { |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 662 | final String[] columns = new String[] { |
| 663 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 664 | MtpDatabaseConstants.COLUMN_STORAGE_ID, |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 665 | DocumentsContract.Document.COLUMN_DISPLAY_NAME |
| 666 | }; |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 667 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 668 | // Add a device and a storage. |
| 669 | addTestDevice(); |
| 670 | addTestStorage("1"); |
| 671 | |
| 672 | // Disconnect devices. |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 673 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 674 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 675 | // Add a device and two storages that has same name. |
| 676 | addTestDevice(); |
| 677 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 678 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 679 | new MtpRoot(0, 200, "Storage", 2000, 0, ""), |
| 680 | new MtpRoot(0, 201, "Storage", 2001, 0, ""), |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 681 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 682 | mDatabase.getMapper().stopAddingDocuments("1"); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 683 | |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 684 | { |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 685 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 686 | assertEquals(2, cursor.getCount()); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 687 | |
| 688 | // First storage reuse document ID of previous storage. |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 689 | cursor.moveToNext(); |
Daichi Hirono | 9fca541 | 2016-02-07 13:20:22 +0900 | [diff] [blame] | 690 | // One reuses exisitng document ID 1. |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 691 | assertEquals(2, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 692 | assertEquals(200, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 693 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 694 | |
| 695 | // Second one has new document ID. |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 696 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 697 | assertEquals(3, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 698 | assertEquals(201, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 699 | assertEquals("Storage", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 700 | |
Daichi Hirono | 6de8a0e | 2015-10-27 16:18:17 +0900 | [diff] [blame] | 701 | cursor.close(); |
| 702 | } |
| 703 | } |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 704 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 705 | public void testReplaceExistingRoots() throws Exception { |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 706 | addTestDevice(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 707 | |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 708 | // The client code should be able to replace existing rows with new information. |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 709 | // Add one. |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 710 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 711 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 712 | new MtpRoot(0, 100, "Storage A", 0, 0, ""), |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 713 | }); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 714 | mDatabase.getMapper().stopAddingDocuments("1"); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 715 | // Replace it. |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 716 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 717 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 718 | new MtpRoot(0, 100, "Storage B", 1000, 1000, ""), |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 719 | }); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 720 | mDatabase.getMapper().stopAddingDocuments("1"); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 721 | { |
| 722 | final String[] columns = new String[] { |
| 723 | DocumentsContract.Document.COLUMN_DOCUMENT_ID, |
Daichi Hirono | 18d70d5 | 2015-11-13 15:19:47 +0900 | [diff] [blame] | 724 | MtpDatabaseConstants.COLUMN_STORAGE_ID, |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 725 | DocumentsContract.Document.COLUMN_DISPLAY_NAME |
| 726 | }; |
Daichi Hirono | 4604b74 | 2015-11-12 12:12:48 +0900 | [diff] [blame] | 727 | final Cursor cursor = mDatabase.queryRootDocuments(columns); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 728 | assertEquals(1, cursor.getCount()); |
| 729 | cursor.moveToNext(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 730 | assertEquals(2, getInt(cursor, COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 731 | assertEquals(100, getInt(cursor, COLUMN_STORAGE_ID)); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 732 | assertEquals("Storage B", getString(cursor, COLUMN_DISPLAY_NAME)); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 733 | cursor.close(); |
| 734 | } |
| 735 | { |
| 736 | final String[] columns = new String[] { |
| 737 | Root.COLUMN_ROOT_ID, |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 738 | Root.COLUMN_TITLE, |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 739 | Root.COLUMN_AVAILABLE_BYTES |
| 740 | }; |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 741 | final Cursor cursor = mDatabase.queryRoots(resources, columns); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 742 | assertEquals(1, cursor.getCount()); |
| 743 | cursor.moveToNext(); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 744 | assertEquals(1, getInt(cursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 745 | assertEquals("Device Storage B", getString(cursor, Root.COLUMN_TITLE)); |
Daichi Hirono | 9984ebd | 2015-12-15 16:02:27 +0900 | [diff] [blame] | 746 | assertEquals(1000, getInt(cursor, Root.COLUMN_AVAILABLE_BYTES)); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 747 | cursor.close(); |
| 748 | } |
| 749 | } |
| 750 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 751 | public void testFailToReplaceExisitingUnmappedRoots() throws Exception { |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 752 | // The client code should not be able to replace rows before resolving 'unmapped' rows. |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 753 | // Add one. |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 754 | addTestDevice(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 755 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 756 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 757 | new MtpRoot(0, 100, "Storage A", 0, 0, ""), |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 758 | }); |
Daichi Hirono | 259ce80 | 2015-11-20 17:51:53 +0900 | [diff] [blame] | 759 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 760 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 761 | addTestDevice(); |
| 762 | try (final Cursor oldCursor = |
| 763 | mDatabase.queryRoots(resources, strings(Root.COLUMN_ROOT_ID))) { |
| 764 | assertEquals(1, oldCursor.getCount()); |
| 765 | oldCursor.moveToNext(); |
| 766 | assertEquals("1", getString(oldCursor, Root.COLUMN_ROOT_ID)); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 767 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 768 | // Add one. |
| 769 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 770 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 771 | new MtpRoot(0, 101, "Storage B", 1000, 1000, ""), |
| 772 | }); |
| 773 | // Add one more before resolving unmapped documents. |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 774 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 775 | new MtpRoot(0, 102, "Storage B", 1000, 1000, ""), |
| 776 | }); |
| 777 | mDatabase.getMapper().stopAddingDocuments("1"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 778 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 779 | // Because the roots shares the same name, the roots should have new IDs. |
| 780 | try (final Cursor newCursor = mDatabase.queryChildDocuments( |
| 781 | strings(Document.COLUMN_DOCUMENT_ID), "1")) { |
| 782 | assertEquals(2, newCursor.getCount()); |
| 783 | newCursor.moveToNext(); |
| 784 | assertFalse(oldCursor.getString(0).equals(newCursor.getString(0))); |
| 785 | newCursor.moveToNext(); |
| 786 | assertFalse(oldCursor.getString(0).equals(newCursor.getString(0))); |
| 787 | } |
| 788 | } |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 789 | } |
| 790 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 791 | public void testQueryDocuments() throws Exception { |
| 792 | addTestDevice(); |
| 793 | addTestStorage("1"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 794 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 795 | final Cursor cursor = mDatabase.queryDocument("2", strings(Document.COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 796 | assertEquals(1, cursor.getCount()); |
| 797 | cursor.moveToNext(); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 798 | assertEquals("Storage", getString(cursor, Document.COLUMN_DISPLAY_NAME)); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 799 | cursor.close(); |
| 800 | } |
| 801 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 802 | public void testQueryRoots() throws Exception { |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 803 | // Add device document. |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 804 | addTestDevice(); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 805 | |
| 806 | // It the device does not have storages, it shows a device root. |
| 807 | { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 808 | final Cursor cursor = mDatabase.queryRoots(resources, strings(Root.COLUMN_TITLE)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 809 | assertEquals(1, cursor.getCount()); |
| 810 | cursor.moveToNext(); |
| 811 | assertEquals("Device", cursor.getString(0)); |
| 812 | cursor.close(); |
| 813 | } |
| 814 | |
| 815 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 816 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 817 | new MtpRoot(0, 100, "Storage A", 0, 0, "") |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 818 | }); |
| 819 | mDatabase.getMapper().stopAddingDocuments("1"); |
| 820 | |
| 821 | // It the device has single storage, it shows a storage root. |
| 822 | { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 823 | final Cursor cursor = mDatabase.queryRoots(resources, strings(Root.COLUMN_TITLE)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 824 | assertEquals(1, cursor.getCount()); |
| 825 | cursor.moveToNext(); |
| 826 | assertEquals("Device Storage A", cursor.getString(0)); |
| 827 | cursor.close(); |
| 828 | } |
| 829 | |
| 830 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 831 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 832 | new MtpRoot(0, 100, "Storage A", 0, 0, ""), |
| 833 | new MtpRoot(0, 101, "Storage B", 0, 0, "") |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 834 | }); |
| 835 | mDatabase.getMapper().stopAddingDocuments("1"); |
| 836 | |
| 837 | // It the device has multiple storages, it shows a device root. |
| 838 | { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 839 | final Cursor cursor = mDatabase.queryRoots(resources, strings(Root.COLUMN_TITLE)); |
Daichi Hirono | 81d4853 | 2015-12-16 15:03:19 +0900 | [diff] [blame] | 840 | assertEquals(1, cursor.getCount()); |
| 841 | cursor.moveToNext(); |
| 842 | assertEquals("Device", cursor.getString(0)); |
| 843 | cursor.close(); |
| 844 | } |
| 845 | } |
| 846 | |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 847 | public void testGetParentId() throws FileNotFoundException { |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 848 | addTestDevice(); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 849 | |
Daichi Hirono | 7a375c4 | 2015-12-14 17:14:29 +0900 | [diff] [blame] | 850 | mDatabase.getMapper().startAddingDocuments("1"); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 851 | mDatabase.getMapper().putStorageDocuments("1", OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 852 | new MtpRoot(0, 100, "Storage A", 0, 0, ""), |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 853 | }); |
Daichi Hirono | 7a375c4 | 2015-12-14 17:14:29 +0900 | [diff] [blame] | 854 | mDatabase.getMapper().stopAddingDocuments("1"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 855 | |
Daichi Hirono | 7a375c4 | 2015-12-14 17:14:29 +0900 | [diff] [blame] | 856 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 857 | mDatabase.getMapper().putChildDocuments(0, "2", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
| 858 | createDocument(200, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 859 | }); |
Daichi Hirono | 7a375c4 | 2015-12-14 17:14:29 +0900 | [diff] [blame] | 860 | mDatabase.getMapper().stopAddingDocuments("2"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 861 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 862 | assertEquals("2", mDatabase.getParentIdentifier("3").mDocumentId); |
| 863 | } |
| 864 | |
| 865 | public void testDeleteDocument() throws Exception { |
| 866 | addTestDevice(); |
| 867 | addTestStorage("1"); |
| 868 | |
| 869 | mDatabase.getMapper().startAddingDocuments("2"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 870 | mDatabase.getMapper().putChildDocuments(0, "2", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
| 871 | createDocument(200, "dir", MtpConstants.FORMAT_ASSOCIATION, 1024), |
| 872 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 873 | mDatabase.getMapper().stopAddingDocuments("2"); |
| 874 | |
| 875 | mDatabase.getMapper().startAddingDocuments("3"); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 876 | mDatabase.getMapper().putChildDocuments(0, "3", OPERATIONS_SUPPORTED, new MtpObjectInfo[] { |
| 877 | createDocument(200, "note.txt", MtpConstants.FORMAT_TEXT, 1024), |
| 878 | }); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 879 | mDatabase.getMapper().stopAddingDocuments("3"); |
| 880 | |
| 881 | mDatabase.deleteDocument("3"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 882 | |
| 883 | { |
| 884 | // Do not query deleted documents. |
| 885 | final Cursor cursor = |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 886 | mDatabase.queryChildDocuments(strings(Document.COLUMN_DOCUMENT_ID), "2"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 887 | assertEquals(0, cursor.getCount()); |
| 888 | cursor.close(); |
| 889 | } |
| 890 | |
| 891 | { |
| 892 | // Child document should be deleted also. |
| 893 | final Cursor cursor = |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 894 | mDatabase.queryDocument("4", strings(Document.COLUMN_DOCUMENT_ID)); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 895 | assertEquals(0, cursor.getCount()); |
| 896 | cursor.close(); |
| 897 | } |
| 898 | } |
| 899 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 900 | public void testPutNewDocument() throws Exception { |
| 901 | addTestDevice(); |
| 902 | addTestStorage("1"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 903 | |
| 904 | assertEquals( |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 905 | "3", |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 906 | mDatabase.putNewDocument( |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 907 | 0, "2", OPERATIONS_SUPPORTED, |
| 908 | createDocument(200, "note.txt", MtpConstants.FORMAT_TEXT, 1024))); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 909 | |
| 910 | { |
| 911 | final Cursor cursor = |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 912 | mDatabase.queryChildDocuments(strings(Document.COLUMN_DOCUMENT_ID), "2"); |
Daichi Hirono | 49f920f | 2015-11-19 10:19:52 +0900 | [diff] [blame] | 913 | assertEquals(1, cursor.getCount()); |
| 914 | cursor.moveToNext(); |
| 915 | assertEquals("3", cursor.getString(0)); |
| 916 | cursor.close(); |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 917 | } |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 918 | |
| 919 | // The new document should not be mapped with existing invalidated document. |
| 920 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | 8e87364 | 2016-02-07 15:17:16 +0900 | [diff] [blame] | 921 | addTestDevice(); |
| 922 | addTestStorage("1"); |
| 923 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 924 | mDatabase.getMapper().startAddingDocuments("2"); |
| 925 | mDatabase.putNewDocument( |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 926 | 0, "2", OPERATIONS_SUPPORTED, |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 927 | createDocument(201, "note.txt", MtpConstants.FORMAT_TEXT, 1024)); |
| 928 | mDatabase.getMapper().stopAddingDocuments("2"); |
| 929 | |
| 930 | { |
| 931 | final Cursor cursor = |
| 932 | mDatabase.queryChildDocuments(strings(Document.COLUMN_DOCUMENT_ID), "2"); |
| 933 | assertEquals(1, cursor.getCount()); |
| 934 | cursor.moveToNext(); |
| 935 | assertEquals("4", cursor.getString(0)); |
| 936 | cursor.close(); |
| 937 | } |
Daichi Hirono | cfcb0c0 | 2015-11-10 12:52:59 +0900 | [diff] [blame] | 938 | } |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 939 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 940 | public void testGetDocumentIdForDevice() throws Exception { |
| 941 | addTestDevice(); |
| 942 | assertEquals("1", mDatabase.getDocumentIdForDevice(0)); |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 943 | } |
Daichi Hirono | 83983d7 | 2016-01-29 17:37:36 +0900 | [diff] [blame] | 944 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 945 | public void testGetClosedDevice() throws Exception { |
Daichi Hirono | 83983d7 | 2016-01-29 17:37:36 +0900 | [diff] [blame] | 946 | mDatabase.getMapper().startAddingDocuments(null); |
| 947 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 948 | 0, "Device", null /* deviceKey */, /* opened is */ false, new MtpRoot[0], null, |
| 949 | null)); |
Daichi Hirono | 83983d7 | 2016-01-29 17:37:36 +0900 | [diff] [blame] | 950 | mDatabase.getMapper().stopAddingDocuments(null); |
| 951 | |
| 952 | final String[] columns = new String [] { |
| 953 | DocumentsContract.Root.COLUMN_ROOT_ID, |
| 954 | DocumentsContract.Root.COLUMN_TITLE, |
| 955 | DocumentsContract.Root.COLUMN_AVAILABLE_BYTES |
| 956 | }; |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 957 | try (final Cursor cursor = mDatabase.queryRoots(resources, columns)) { |
Daichi Hirono | 83983d7 | 2016-01-29 17:37:36 +0900 | [diff] [blame] | 958 | assertEquals(1, cursor.getCount()); |
| 959 | assertTrue(cursor.moveToNext()); |
| 960 | assertEquals(1, cursor.getLong(0)); |
| 961 | assertEquals("Device", cursor.getString(1)); |
| 962 | assertTrue(cursor.isNull(2)); |
| 963 | } |
| 964 | } |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 965 | |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 966 | public void testMappingWithoutKey() throws FileNotFoundException { |
| 967 | mDatabase.getMapper().startAddingDocuments(null); |
| 968 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
| 969 | 0, "Device", null /* device key */, /* opened is */ true, new MtpRoot[0], null, |
| 970 | null)); |
| 971 | mDatabase.getMapper().stopAddingDocuments(null); |
| 972 | |
| 973 | mDatabase.getMapper().startAddingDocuments(null); |
| 974 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
| 975 | 0, "Device", null /* device key */, /* opened is */ true, new MtpRoot[0], null, |
| 976 | null)); |
| 977 | mDatabase.getMapper().stopAddingDocuments(null); |
| 978 | |
| 979 | try (final Cursor cursor = |
| 980 | mDatabase.queryRoots(resources, strings(DocumentsContract.Root.COLUMN_ROOT_ID))) { |
| 981 | assertEquals(1, cursor.getCount()); |
| 982 | assertTrue(cursor.moveToNext()); |
| 983 | assertEquals(1, cursor.getLong(0)); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | public void testMappingFailsWithoutKey() throws FileNotFoundException { |
| 988 | mDatabase.getMapper().startAddingDocuments(null); |
| 989 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
| 990 | 0, "Device", null /* device key */, /* opened is */ true, new MtpRoot[0], null, |
| 991 | null)); |
| 992 | mDatabase.getMapper().stopAddingDocuments(null); |
| 993 | |
| 994 | // MTP identifier is cleared here. Mapping no longer works without device key. |
| 995 | mDatabase.getMapper().startAddingDocuments(null); |
| 996 | mDatabase.getMapper().stopAddingDocuments(null); |
| 997 | |
| 998 | mDatabase.getMapper().startAddingDocuments(null); |
| 999 | mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
| 1000 | 0, "Device", null /* device key */, /* opened is */ true, new MtpRoot[0], null, |
| 1001 | null)); |
| 1002 | mDatabase.getMapper().stopAddingDocuments(null); |
| 1003 | |
| 1004 | try (final Cursor cursor = |
| 1005 | mDatabase.queryRoots(resources, strings(DocumentsContract.Root.COLUMN_ROOT_ID))) { |
| 1006 | assertEquals(1, cursor.getCount()); |
| 1007 | assertTrue(cursor.moveToNext()); |
| 1008 | assertEquals(2, cursor.getLong(0)); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | public void testUpdateDocumentWithoutChange() throws FileNotFoundException { |
| 1013 | mDatabase.getMapper().startAddingDocuments(null); |
| 1014 | assertTrue(mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
| 1015 | 0, "Device", "device_key", /* opened is */ true, new MtpRoot[0], null, |
| 1016 | null))); |
| 1017 | assertFalse(mDatabase.getMapper().stopAddingDocuments(null)); |
| 1018 | |
| 1019 | mDatabase.getMapper().startAddingDocuments(null); |
| 1020 | assertFalse(mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord( |
| 1021 | 0, "Device", "device_key", /* opened is */ true, new MtpRoot[0], null, |
| 1022 | null))); |
| 1023 | assertFalse(mDatabase.getMapper().stopAddingDocuments(null)); |
| 1024 | } |
| 1025 | |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 1026 | private void addTestDevice() throws FileNotFoundException { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 1027 | TestUtil.addTestDevice(mDatabase); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | private void addTestStorage(String parentId) throws FileNotFoundException { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 1031 | TestUtil.addTestStorage(mDatabase, parentId); |
Daichi Hirono | 619afda | 2016-02-07 14:23:43 +0900 | [diff] [blame] | 1032 | } |
Daichi Hirono | 9678f60 | 2015-10-22 12:35:05 +0900 | [diff] [blame] | 1033 | } |