Move SuggestionRanker to SettingsIntelligence.

Bug: 65065268
Test: robotests
Change-Id: I68ed88a7c88af8bea67e3d3f6b142313bccc6f39
diff --git a/tests/robotests/Android.mk b/tests/robotests/Android.mk
index 37a52a7..cb106ca 100644
--- a/tests/robotests/Android.mk
+++ b/tests/robotests/Android.mk
@@ -8,6 +8,7 @@
 
 # Include the testing libraries (JUnit4 + Robolectric libs).
 LOCAL_STATIC_JAVA_LIBRARIES := \
+    mockito-robolectric-prebuilt \
     truth-prebuilt
 
 LOCAL_JAVA_LIBRARIES := \
diff --git a/tests/robotests/src/android/service/settings/suggestions/Suggestion.java b/tests/robotests/src/android/service/settings/suggestions/Suggestion.java
new file mode 100644
index 0000000..2bb6192
--- /dev/null
+++ b/tests/robotests/src/android/service/settings/suggestions/Suggestion.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2017 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.service.settings.suggestions;
+
+import android.app.PendingIntent;
+import android.os.Parcel;
+import android.text.TextUtils;
+
+public class Suggestion {
+    private final String mId;
+    private final CharSequence mTitle;
+    private final CharSequence mSummary;
+    private final PendingIntent mPendingIntent;
+
+    /**
+     * Gets the id for the suggestion object.
+     */
+    public String getId() {
+        return mId;
+    }
+
+    /**
+     * Title of the suggestion that is shown to the user.
+     */
+    public CharSequence getTitle() {
+        return mTitle;
+    }
+
+    /**
+     * Optional summary describing what this suggestion controls.
+     */
+    public CharSequence getSummary() {
+        return mSummary;
+    }
+
+    /**
+     * The Intent to launch when the suggestion is activated.
+     */
+    public PendingIntent getPendingIntent() {
+        return mPendingIntent;
+    }
+
+    private Suggestion(Builder builder) {
+        mTitle = builder.mTitle;
+        mSummary = builder.mSummary;
+        mPendingIntent = builder.mPendingIntent;
+        mId = builder.mId;
+    }
+
+    private Suggestion(Parcel in) {
+        mId = in.readString();
+        mTitle = in.readCharSequence();
+        mSummary = in.readCharSequence();
+        mPendingIntent = in.readParcelable(PendingIntent.class.getClassLoader());
+    }
+
+    /**
+     * Builder class for {@link Suggestion}.
+     */
+    public static class Builder {
+        private final String mId;
+        private CharSequence mTitle;
+        private CharSequence mSummary;
+        private PendingIntent mPendingIntent;
+
+        public Builder(String id) {
+            if (TextUtils.isEmpty(id)) {
+                throw new IllegalArgumentException("Suggestion id cannot be empty");
+            }
+            mId = id;
+        }
+
+        /**
+         * Sets suggestion title
+         */
+
+        public Builder setTitle(CharSequence title) {
+            mTitle = title;
+            return this;
+        }
+
+        /**
+         * Sets suggestion summary
+         */
+        public Builder setSummary(CharSequence summary) {
+            mSummary = summary;
+            return this;
+        }
+
+        /**
+         * Sets suggestion intent
+         */
+        public Builder setPendingIntent(PendingIntent pendingIntent) {
+            mPendingIntent = pendingIntent;
+            return this;
+        }
+
+        /**
+         * Builds an immutable {@link Suggestion} object.
+         */
+        public Suggestion build() {
+            return new Suggestion(this /* builder */);
+        }
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/EventStoreTest.java b/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/EventStoreTest.java
new file mode 100644
index 0000000..c99b972
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/EventStoreTest.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2017 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 com.android.settings.intelligence.suggestions.ranking;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.android.settings.intelligence.SettingsIntelligenceRobolectricTestRunner;
+import com.android.settings.intelligence.TestConfig;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsIntelligenceRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class EventStoreTest {
+    private EventStore mEventStore;
+
+    @Before
+    public void setUp() {
+        mEventStore = new EventStore(RuntimeEnvironment.application);
+    }
+
+    @Test
+    public void testWriteRead() {
+        mEventStore.writeEvent("pkg", EventStore.EVENT_SHOWN);
+        long timeMs = System.currentTimeMillis();
+        assertThat(mEventStore.readMetric("pkg", EventStore.EVENT_SHOWN, EventStore.METRIC_COUNT))
+                .isEqualTo(1);
+        assertThat(Math.abs(timeMs - mEventStore.readMetric("pkg", EventStore.EVENT_SHOWN,
+                EventStore.METRIC_LAST_EVENT_TIME)) < 10000)
+                .isTrue();
+    }
+
+    @Test
+    public void testWriteRead_shouldHaveLatestValues() {
+        mEventStore.writeEvent("pkg", EventStore.EVENT_DISMISSED);
+        mEventStore.writeEvent("pkg", EventStore.EVENT_DISMISSED);
+        assertThat(
+                mEventStore.readMetric("pkg", EventStore.EVENT_DISMISSED, EventStore.METRIC_COUNT))
+                .isEqualTo(2);
+    }
+
+    @Test
+    public void testWriteRead_shouldReturnDefaultIfNotAvailable() {
+        assertThat(mEventStore.readMetric("pkg", EventStore.EVENT_SHOWN, EventStore.METRIC_COUNT))
+                .isEqualTo(0);
+        assertThat(mEventStore.readMetric("pkg", EventStore.EVENT_SHOWN,
+                EventStore.METRIC_LAST_EVENT_TIME))
+                .isEqualTo(0);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/SuggestionFeaturizerTest.java b/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/SuggestionFeaturizerTest.java
new file mode 100644
index 0000000..c82428c
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/SuggestionFeaturizerTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2017 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 com.android.settings.intelligence.suggestions.ranking;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.service.settings.suggestions.Suggestion;
+
+import com.android.settings.intelligence.SettingsIntelligenceRobolectricTestRunner;
+import com.android.settings.intelligence.TestConfig;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+@RunWith(SettingsIntelligenceRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class SuggestionFeaturizerTest {
+
+    private EventStore mEventStore;
+    private SuggestionFeaturizer mSuggestionFeaturizer;
+
+    @Before
+    public void setUp() {
+        mEventStore = new EventStore(RuntimeEnvironment.application);
+        mSuggestionFeaturizer = new SuggestionFeaturizer(mEventStore);
+    }
+
+    @Test
+    public void testFeaturize_singlePackage() {
+        mEventStore.writeEvent("pkg", EventStore.EVENT_DISMISSED);
+        mEventStore.writeEvent("pkg", EventStore.EVENT_SHOWN);
+        mEventStore.writeEvent("pkg", EventStore.EVENT_SHOWN);
+        final Map<String, Double> features = mSuggestionFeaturizer
+                .featurize(Arrays.asList(new Suggestion.Builder("pkg").build()))
+                .get("pkg");
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_IS_SHOWN)).isEqualTo(1.0);
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_IS_DISMISSED)).isEqualTo(1.0);
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_IS_CLICKED)).isEqualTo(0.0);
+
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_SHOWN)).isLessThan
+                (1.0);
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_DISMISSED))
+                .isLessThan(1.0);
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_CLICKED))
+                .isEqualTo(1.0);
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_SHOWN_COUNT))
+                .isEqualTo(2.0 / SuggestionFeaturizer.COUNT_NORMALIZATION_FACTOR);
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_DISMISSED_COUNT))
+                .isEqualTo(1.0 / SuggestionFeaturizer.COUNT_NORMALIZATION_FACTOR);
+        assertThat(features.get(SuggestionFeaturizer.FEATURE_CLICKED_COUNT)).isEqualTo(0.0);
+    }
+
+    @Test
+    public void testFeaturize_multiplePackages() {
+        mEventStore.writeEvent("pkg1", EventStore.EVENT_DISMISSED);
+        mEventStore.writeEvent("pkg2", EventStore.EVENT_SHOWN);
+        mEventStore.writeEvent("pkg1", EventStore.EVENT_SHOWN);
+        final List<Suggestion> suggestions = new ArrayList<Suggestion>() {
+            {
+                add(new Suggestion.Builder("pkg1").build());
+                add(new Suggestion.Builder("pkg2").build());
+            }
+        };
+        final Map<String, Map<String, Double>> features = mSuggestionFeaturizer
+                .featurize(suggestions);
+        final Map<String, Double> features1 = features.get("pkg1");
+        final Map<String, Double> features2 = features.get("pkg2");
+
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_IS_SHOWN)).isEqualTo(1.0);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_IS_DISMISSED)).isEqualTo(1.0);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_IS_CLICKED)).isEqualTo(0.0);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_SHOWN))
+                .isLessThan(1.0);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_DISMISSED))
+                .isLessThan(1.0);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_CLICKED))
+                .isEqualTo(1.0);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_SHOWN_COUNT))
+                .isEqualTo(1.0 / SuggestionFeaturizer.COUNT_NORMALIZATION_FACTOR);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_DISMISSED_COUNT))
+                .isEqualTo(1.0 / SuggestionFeaturizer.COUNT_NORMALIZATION_FACTOR);
+        assertThat(features1.get(SuggestionFeaturizer.FEATURE_CLICKED_COUNT)).isEqualTo(0.0);
+
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_IS_SHOWN)).isEqualTo(1.0);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_IS_DISMISSED)).isEqualTo(0.0);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_IS_CLICKED)).isEqualTo(0.0);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_SHOWN))
+                .isLessThan(1.0);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_DISMISSED))
+                .isEqualTo(1.0);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_CLICKED))
+                .isEqualTo(1.0);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_SHOWN_COUNT))
+                .isEqualTo(1.0 / SuggestionFeaturizer.COUNT_NORMALIZATION_FACTOR);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_DISMISSED_COUNT)).isEqualTo(0.0);
+        assertThat(features2.get(SuggestionFeaturizer.FEATURE_CLICKED_COUNT)).isEqualTo(0.0);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/SuggestionRankerTest.java b/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/SuggestionRankerTest.java
new file mode 100644
index 0000000..7866ea3
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/intelligence/suggestions/ranking/SuggestionRankerTest.java
@@ -0,0 +1,73 @@
+package com.android.settings.intelligence.suggestions.ranking;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.same;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.service.settings.suggestions.Suggestion;
+
+import com.android.settings.intelligence.SettingsIntelligenceRobolectricTestRunner;
+import com.android.settings.intelligence.TestConfig;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RunWith(SettingsIntelligenceRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class SuggestionRankerTest {
+
+    @Mock
+    private SuggestionRanker mSuggestionRanker;
+    @Mock
+    private SuggestionFeaturizer mSuggestionFeaturizer;
+
+    private Map<String, Map<String, Double>> mFeatures;
+    private List<Suggestion> mSuggestions;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mFeatures = new HashMap<>();
+        mFeatures.put("pkg1", new HashMap<>());
+        mFeatures.put("pkg2", new HashMap<>());
+        mFeatures.put("pkg3", new HashMap<>());
+        mSuggestions = new ArrayList<Suggestion>() {
+            {
+                add(new Suggestion.Builder("pkg1").build());
+                add(new Suggestion.Builder("pkg2").build());
+                add(new Suggestion.Builder("pkg3").build());
+            }
+        };
+        mSuggestionFeaturizer = mock(SuggestionFeaturizer.class);
+        mSuggestionRanker = new SuggestionRanker(mSuggestionFeaturizer);
+        when(mSuggestionFeaturizer.featurize(mSuggestions)).thenReturn(mFeatures);
+        mSuggestionRanker = spy(mSuggestionRanker);
+        when(mSuggestionRanker.getRelevanceMetric(same(mFeatures.get("pkg1")))).thenReturn(0.9);
+        when(mSuggestionRanker.getRelevanceMetric(same(mFeatures.get("pkg2")))).thenReturn(0.1);
+        when(mSuggestionRanker.getRelevanceMetric(same(mFeatures.get("pkg3")))).thenReturn(0.5);
+    }
+
+    @Test
+    public void testRank() {
+        List<Suggestion> expectedOrderdList = new ArrayList<Suggestion>() {
+            {
+                add(mSuggestions.get(0)); // relevance = 0.9
+                add(mSuggestions.get(2)); // relevance = 0.5
+                add(mSuggestions.get(1)); // relevance = 0.1
+            }
+        };
+        mSuggestionRanker.rankSuggestions(mSuggestions);
+        assertThat(mSuggestions).isEqualTo(expectedOrderdList);
+    }
+}
\ No newline at end of file