OSDN Git Service

Fix null pointer on dismissing suggestion.
authorDoris Ling <dling@google.com>
Mon, 27 Mar 2017 21:34:30 +0000 (14:34 -0700)
committerDoris Ling <dling@google.com>
Tue, 28 Mar 2017 17:53:52 +0000 (17:53 +0000)
Add null check for context when getting the suggestion identifier, since
this is called from async task and context might becomes null.

Change-Id: If17036803de1634891b659a4fc6bd4b8a76ef5fb
Fix: 36605386
Test: make RunSettingsRoboTests

src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java
tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java

index 779a8aa..f3f8af9 100644 (file)
@@ -83,7 +83,8 @@ public class SuggestionFeatureProviderImpl implements SuggestionFeatureProvider
 
     @Override
     public String getSuggestionIdentifier(Context context, Tile suggestion) {
-        if (suggestion.intent == null || suggestion.intent.getComponent() == null) {
+        if (suggestion.intent == null || suggestion.intent.getComponent() == null
+                || context == null) {
             return "unknown_suggestion";
         }
         String packageName = suggestion.intent.getComponent().getPackageName();
index 3efe15a..6183acc 100644 (file)
@@ -108,6 +108,15 @@ public class SuggestionFeatureProviderImplTest {
     }
 
     @Test
+    public void getSuggestionIdentifier_nullContext_shouldNotCrash() {
+        final Tile suggestion = new Tile();
+        suggestion.intent = new Intent()
+            .setClassName(RuntimeEnvironment.application.getPackageName(), "123");
+        assertThat(mProvider.getSuggestionIdentifier(null, suggestion))
+            .isNotEmpty();
+    }
+
+    @Test
     public void dismissSuggestion_hasMoreDismissCount_shouldNotDisableComponent() {
         when(mSuggestionParser.dismissSuggestion(any(Tile.class), anyBoolean()))
                 .thenReturn(false);
@@ -120,7 +129,6 @@ public class SuggestionFeatureProviderImplTest {
         verify(mContext, never()).getPackageManager();
     }
 
-
     @Test
     public void dismissSuggestion_noContext_shouldDoNothing() {
         mProvider.dismissSuggestion(null, mSuggestionParser, mSuggestion);