11a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu/*
21a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * Copyright (C) 2017 The Android Open Source Project
31a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu *
41a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * Licensed under the Apache License, Version 2.0 (the "License");
51a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * you may not use this file except in compliance with the License.
61a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * You may obtain a copy of the License at
71a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu *
81a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu *      http://www.apache.org/licenses/LICENSE-2.0
91a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu *
101a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * Unless required by applicable law or agreed to in writing, software
111a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * distributed under the License is distributed on an "AS IS" BASIS,
121a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * See the License for the specific language governing permissions and
141a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu * limitations under the License.
151a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu */
161a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
171a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liupackage com.android.settingslib.suggestions;
181a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
191a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport static android.arch.lifecycle.Lifecycle.Event.ON_START;
201a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport static android.arch.lifecycle.Lifecycle.Event.ON_STOP;
211a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport static com.google.common.truth.Truth.assertThat;
221a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport static org.mockito.Mockito.mock;
231a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport static org.mockito.Mockito.verify;
241a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport static org.mockito.Mockito.when;
251a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
261a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport android.app.LoaderManager;
271a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport android.arch.lifecycle.LifecycleOwner;
281a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport android.content.ComponentName;
291a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport android.content.Context;
301a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
311a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport com.android.settingslib.SettingsLibRobolectricTestRunner;
321a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport com.android.settingslib.core.lifecycle.Lifecycle;
331a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
341a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.junit.After;
351a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.junit.Before;
361a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.junit.Test;
371a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.junit.runner.RunWith;
381a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.mockito.Mock;
391a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.mockito.MockitoAnnotations;
401a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.robolectric.RuntimeEnvironment;
411a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liuimport org.robolectric.annotation.Config;
421a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
431a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu@RunWith(SettingsLibRobolectricTestRunner.class)
445c50dc11b857dfdc265a4e0aa80fd7d17a62e7f6James Lemieux@Config(shadows = ShadowSuggestionController.class)
451a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liupublic class SuggestionControllerMixinTest {
461a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
471a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @Mock
481a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    private SuggestionControllerMixin.SuggestionControllerHost mHost;
491a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
501a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    private Context mContext;
511a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    private LifecycleOwner mLifecycleOwner;
521a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    private Lifecycle mLifecycle;
531a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    private SuggestionControllerMixin mMixin;
541a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    private ComponentName mComponentName;
551a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @Before
561a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    public void setUp() {
571a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        MockitoAnnotations.initMocks(this);
581a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mContext = RuntimeEnvironment.application;
591a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mLifecycleOwner = () -> mLifecycle;
601a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mLifecycle = new Lifecycle(mLifecycleOwner);
611a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mComponentName = new ComponentName(
621a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu                "com.android.settings.intelligence",
631a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu                "com.android.settings.intelligence.suggestions.SuggestionService");
641a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    }
651a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
661a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @After
671a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    public void tearDown() {
681a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        ShadowSuggestionController.reset();
691a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    }
701a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
711a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @Test
721a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    public void goThroughLifecycle_onStartStop_shouldStartStopController() {
731a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
741a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
751a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mLifecycle.handleLifecycleEvent(ON_START);
761a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        assertThat(ShadowSuggestionController.sStartCalled).isTrue();
771a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
781a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mLifecycle.handleLifecycleEvent(ON_STOP);
791a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        assertThat(ShadowSuggestionController.sStopCalled).isTrue();
801a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    }
811a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
821a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @Test
831a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    public void onServiceConnected_shouldGetSuggestion() {
841a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        final LoaderManager loaderManager = mock(LoaderManager.class);
851a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        when(mHost.getLoaderManager()).thenReturn(loaderManager);
861a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
871a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
881a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin.onServiceConnected();
891a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
901a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        verify(loaderManager).restartLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS,
911a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu                null /* args */, mMixin /* callback */);
921a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    }
931a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
941a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @Test
951a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    public void onServiceConnected_hostNotAttached_shouldDoNothing() {
961a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        when(mHost.getLoaderManager()).thenReturn(null);
971a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
981a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
991a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin.onServiceConnected();
1001a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1011a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        verify(mHost).getLoaderManager();
1021a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    }
1031a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1041a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @Test
1051a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    public void onServiceDisconnected_hostNotAttached_shouldDoNothing() {
1061a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        when(mHost.getLoaderManager()).thenReturn(null);
1071a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1081a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
1091a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin.onServiceDisconnected();
1101a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1111a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        verify(mHost).getLoaderManager();
1121a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    }
1131a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1141a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    @Test
1151a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    public void doneLoadingg_shouldSetSuggestionLoaded() {
1161a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
1171a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1181a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin.onLoadFinished(mock(SuggestionLoader.class), null);
1191a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1201a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        assertThat(mMixin.isSuggestionLoaded()).isTrue();
1211a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1221a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        mMixin.onLoaderReset(mock(SuggestionLoader.class));
1231a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu
1241a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu        assertThat(mMixin.isSuggestionLoaded()).isFalse();
1251a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu    }
1261a35c133d23422d09d56f1edcd6b222d83b39170Zhizhi Liu}
127