Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a04d81ef authored by Anton Hansson's avatar Anton Hansson
Browse files

Make TranslatingCursor deal with dupe column names

TranslatingCursor previously assumed each column name appeared
only once in the base cursor and used getColumIndex(String) to
find the index of it. It would then translate only that single
index.

Instead, maintain a set of column indices that need translating.

Also add a unit test.

Bug: 125467331
Test: atest FrameworksCoreTests:TranslatingCursorTest
Test: verified broken app in bug report is working correctly
Change-Id: I33a24bf6474338210ec7b9c6b54912ed49f23cb3
parent fdafee50
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.CancellationSignal;
import android.util.ArraySet;

import com.android.internal.util.ArrayUtils;

@@ -59,7 +60,7 @@ public class TranslatingCursor extends CrossProcessCursorWrapper {
    private final boolean mDropLast;

    private final int mAuxiliaryColumnIndex;
    private final int[] mTranslateColumnIndices;
    private final ArraySet<Integer> mTranslateColumnIndices;

    public TranslatingCursor(@NonNull Cursor cursor, @NonNull Config config,
            @NonNull Translator translator, boolean dropLast) {
@@ -70,9 +71,12 @@ public class TranslatingCursor extends CrossProcessCursorWrapper {
        mDropLast = dropLast;

        mAuxiliaryColumnIndex = cursor.getColumnIndexOrThrow(config.auxiliaryColumn);
        mTranslateColumnIndices = new int[config.translateColumns.length];
        for (int i = 0; i < mTranslateColumnIndices.length; ++i) {
            mTranslateColumnIndices[i] = cursor.getColumnIndex(config.translateColumns[i]);
        mTranslateColumnIndices = new ArraySet<>();
        for (int i = 0; i < cursor.getColumnCount(); ++i) {
            String columnName = cursor.getColumnName(i);
            if (ArrayUtils.contains(config.translateColumns, columnName)) {
                mTranslateColumnIndices.add(i);
            }
        }
    }

+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.database;

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

import android.database.TranslatingCursor.Translator;
import android.net.Uri;

import junit.framework.TestCase;

public class TranslatingCursorTest extends TestCase {

    public void testDuplicateColumnName() {
        MatrixCursor base = new MatrixCursor(new String[] {"_id", "colA", "colB", "colA"});
        base.addRow(new Object[] { 0, "r1_a", "r1_b", "r1_a"});
        base.addRow(new Object[] { 1, "r2_a", "r2_b", "r2_a"});
        Translator translator = (data, idIndex, matchingColumn, cursor) -> data.toUpperCase();
        TranslatingCursor.Config config = new TranslatingCursor.Config(Uri.EMPTY, "_id", "colA");
        TranslatingCursor translating = new TranslatingCursor(base, config, translator, false);

        translating.moveToNext();
        String[] expected = new String[] { "ignored", "R1_A", "r1_b", "R1_A" };
        for (int i = 1; i < translating.getColumnCount(); i++) {
            assertThat(translating.getString(i)).isEqualTo(expected[i]);
        }
        translating.moveToNext();
        expected = new String[] { "ignored", "R2_A", "r2_b", "R2_A" };
        for (int i = 1; i < translating.getColumnCount(); i++) {
            assertThat(translating.getString(i)).isEqualTo(expected[i]);
        }
    }

}