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

Commit 126e53dd authored by Tony Huang's avatar Tony Huang
Browse files

Fix NPE when record histoy

It seems occured while search view is still not injected, so we can
prevent this by return when no any search string.
Check search view null or not to ensure do not cause any NPE.

Fix: 140251324
Test: atest DocumentUIGoogleTests
Change-Id: Ib62e8a25e66302a785f638a463049d36f9e25a36
parent 7b216347
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -540,7 +540,7 @@ public class SearchViewManager implements
     * Record current search for history.
     */
    public void recordHistory() {
        if (!mRecordSearchSupplier.getAsBoolean()) {
        if (TextUtils.isEmpty(mCurrentSearch) || !mRecordSearchSupplier.getAsBoolean()) {
            return;
        }

@@ -548,6 +548,11 @@ public class SearchViewManager implements
    }

    protected void recordHistoryInternal() {
        if (mSearchView == null) {
            Log.w(TAG, "Search view is null, skip record history this time");
            return;
        }

        SearchHistoryManager.getInstance(
                mSearchView.getContext().getApplicationContext()).addHistory(mCurrentSearch);
    }
@@ -558,6 +563,11 @@ public class SearchViewManager implements
     * @param history target string for removed.
     */
    public void removeHistory(String history) {
        if (mSearchView == null) {
            Log.w(TAG, "Search view is null, skip remove history this time");
            return;
        }

        SearchHistoryManager.getInstance(
                mSearchView.getContext().getApplicationContext()).deleteHistory(history);
    }
+13 −0
Original line number Diff line number Diff line
@@ -137,6 +137,7 @@ public final class SearchViewManagerTest {
    private static class TestableSearchViewManager extends SearchViewManager {

        private String mHistoryRecorded;
        private boolean mIsHistoryRecorded;

        public TestableSearchViewManager(
                SearchManagerListener listener,
@@ -160,11 +161,16 @@ public final class SearchViewManagerTest {
        @Override
        protected void recordHistoryInternal() {
            mHistoryRecorded = getCurrentSearch();
            mIsHistoryRecorded = true;
        }

        public String getRecordedHistory() {
            return mHistoryRecorded;
        }

        public boolean isHistoryRecorded() {
            return mIsHistoryRecorded;
        }
    }

    private void fastForwardTo(long timeMs) {
@@ -334,6 +340,13 @@ public final class SearchViewManagerTest {
        assertNull(mSearchViewManager.getRecordedHistory());
    }

    @Test
    public void testHistoryRecorded_skipWhenNoSearchString() {
        mSearchViewManager.recordHistory();

        assertFalse(mSearchViewManager.isHistoryRecorded());
    }

    @Test
    public void testCheckedChipItems_IsEmptyIfSearchCanceled() throws Exception {
        mSearchViewManager.onClick(null);