Move SuggestionService AIDL related calls to a controller.

The controller makes it easier to manage IPCs and make it easier for
testing.

Bug: 65065268
Test: robotests
Change-Id: Ie5797655543cb22d8196267058598a1b4222a8d1
This commit is contained in:
Fan Zhang
2017-09-15 17:10:24 -07:00
parent 230b3767d9
commit ffd531f1d3
4 changed files with 243 additions and 82 deletions

View File

@@ -0,0 +1,61 @@
/*
* 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.dashboard.suggestions;
import android.service.settings.suggestions.Suggestion;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.List;
@Implements(SuggestionController.class)
public class ShadowSuggestionController {
public static boolean sStartCalled;
public static boolean sStopCalled;
public static boolean sGetSuggestionCalled;
public static List<Suggestion> sSuggestions;
public static void reset() {
sStartCalled = false;
sStopCalled = false;
sGetSuggestionCalled = false;
sSuggestions = null;
}
@Implementation
public void start() {
sStartCalled = true;
}
@Implementation
public void stop() {
sStopCalled = true;
}
public static void setSuggestion(List<Suggestion> suggestions) {
sSuggestions = suggestions;
}
@Implementation
public List<Suggestion> getSuggestions() {
sGetSuggestionCalled = true;
return sSuggestions;
}
}

View File

@@ -17,11 +17,9 @@
package com.android.settings.dashboard.suggestions;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.service.settings.suggestions.ISuggestionService;
import android.util.FeatureFlagUtils;
import com.android.settings.TestConfig;
@@ -36,32 +34,32 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = {
SettingsShadowSystemProperties.class
SettingsShadowSystemProperties.class,
ShadowSuggestionController.class
})
public class SuggestionControllerMixinTest {
@Mock
private Context mContext;
@Mock
private ISuggestionService mRemoteService;
private Lifecycle mLifecycle;
private SuggestionControllerMixin mMixin;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycle = new Lifecycle();
when(mContext.getApplicationContext()).thenReturn(mContext);
SettingsShadowSystemProperties.set(
FeatureFlagUtils.FFLAG_PREFIX + SuggestionControllerMixin.FEATURE_FLAG, "true");
}
@After
public void tearDown() {
ShadowSuggestionController.reset();
SettingsShadowSystemProperties.clear();
}
@@ -79,13 +77,23 @@ public class SuggestionControllerMixinTest {
assertThat(SuggestionControllerMixin.isEnabled()).isFalse();
}
@Test
public void goThroughLifecycle_onStartStop_shouldStartStopService() {
mMixin = new SuggestionControllerMixin(mContext, mLifecycle);
mLifecycle.onStart();
assertThat(ShadowSuggestionController.sStartCalled).isTrue();
mLifecycle.onStop();
assertThat(ShadowSuggestionController.sStopCalled).isTrue();
}
@Test
public void onServiceConnected_shouldGetSuggestion() {
mMixin = new SuggestionControllerMixin(mContext, mLifecycle);
ReflectionHelpers.setField(mMixin, "mRemoteService", mRemoteService);
mMixin.onServiceConnected();
verify(mRemoteService).getSuggestions();
assertThat(ShadowSuggestionController.sGetSuggestionCalled).isTrue();
}
}