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