Adding setting suggestion ranker.

Test: RunSettingsGoogleRoboTests
This is currently behind a flag and it is no-op CL.

Change-Id: Ieed3e5a9c3c2fbb0ce1bfea77c588b04778540eb
This commit is contained in:
Soroosh Mariooryad
2017-01-17 18:19:41 -08:00
parent 88c0364ace
commit b1922dbc39
9 changed files with 663 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
/*
* 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.suggestions;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.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);
}
}

View File

@@ -0,0 +1,109 @@
/*
* 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.suggestions;
import java.util.Arrays;
import java.util.Map;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.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);
Map<String, Double> features = mSuggestionFeaturizer.featurize(Arrays.asList("pkg"))
.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);
Map<String, Map<String, Double>> features = mSuggestionFeaturizer
.featurize(Arrays.asList("pkg1", "pkg2"));
Map<String, Double> features1 = features.get("pkg1");
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);
}
}

View File

@@ -0,0 +1,94 @@
/*
* 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.suggestions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settingslib.drawer.Tile;
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 static com.google.common.truth.Truth.assertThat;
import org.robolectric.RuntimeEnvironment;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.same;
import static org.mockito.Mockito.spy;
@RunWith(SettingsRobolectricTestRunner.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<String> mPkgNames;
private List<Tile> mSuggestions;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mPkgNames = Arrays.asList("pkg1", "pkg2", "pkg3");
mFeatures = new HashMap<String, Map<String, Double>>();
mFeatures.put("pkg1", new HashMap<String, Double>());
mFeatures.put("pkg2", new HashMap<String, Double>());
mFeatures.put("pkg3", new HashMap<String, Double>());
mSuggestions = new ArrayList<Tile>() {
{
add(new Tile());
add(new Tile());
add(new Tile());
}
};
mSuggestionFeaturizer = mock(SuggestionFeaturizer.class);
mSuggestionRanker = new SuggestionRanker(mSuggestionFeaturizer);
when(mSuggestionFeaturizer.featurize(mPkgNames)).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<Tile> expectedOrderdList = new ArrayList<Tile>() {
{
add(mSuggestions.get(0)); // relevance = 0.9
add(mSuggestions.get(2)); // relevance = 0.5
add(mSuggestions.get(1)); // relevance = 0.1
}
};
mSuggestionRanker.rank(mSuggestions, mPkgNames);
assertThat(mSuggestions).isEqualTo(expectedOrderdList);
}
}