Home | History | Annotate | Download | only in suggestions
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settingslib.suggestions;
     18 
     19 import static android.arch.lifecycle.Lifecycle.Event.ON_START;
     20 import static android.arch.lifecycle.Lifecycle.Event.ON_STOP;
     21 import static com.google.common.truth.Truth.assertThat;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 import android.app.LoaderManager;
     27 import android.arch.lifecycle.LifecycleOwner;
     28 import android.content.ComponentName;
     29 import android.content.Context;
     30 
     31 import com.android.settingslib.SettingsLibRobolectricTestRunner;
     32 import com.android.settingslib.core.lifecycle.Lifecycle;
     33 
     34 import org.junit.After;
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 import org.mockito.Mock;
     39 import org.mockito.MockitoAnnotations;
     40 import org.robolectric.RuntimeEnvironment;
     41 import org.robolectric.annotation.Config;
     42 
     43 @RunWith(SettingsLibRobolectricTestRunner.class)
     44 @Config(shadows = ShadowSuggestionController.class)
     45 public class SuggestionControllerMixinTest {
     46 
     47     @Mock
     48     private SuggestionControllerMixin.SuggestionControllerHost mHost;
     49 
     50     private Context mContext;
     51     private LifecycleOwner mLifecycleOwner;
     52     private Lifecycle mLifecycle;
     53     private SuggestionControllerMixin mMixin;
     54     private ComponentName mComponentName;
     55     @Before
     56     public void setUp() {
     57         MockitoAnnotations.initMocks(this);
     58         mContext = RuntimeEnvironment.application;
     59         mLifecycleOwner = () -> mLifecycle;
     60         mLifecycle = new Lifecycle(mLifecycleOwner);
     61         mComponentName = new ComponentName(
     62                 "com.android.settings.intelligence",
     63                 "com.android.settings.intelligence.suggestions.SuggestionService");
     64     }
     65 
     66     @After
     67     public void tearDown() {
     68         ShadowSuggestionController.reset();
     69     }
     70 
     71     @Test
     72     public void goThroughLifecycle_onStartStop_shouldStartStopController() {
     73         mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
     74 
     75         mLifecycle.handleLifecycleEvent(ON_START);
     76         assertThat(ShadowSuggestionController.sStartCalled).isTrue();
     77 
     78         mLifecycle.handleLifecycleEvent(ON_STOP);
     79         assertThat(ShadowSuggestionController.sStopCalled).isTrue();
     80     }
     81 
     82     @Test
     83     public void onServiceConnected_shouldGetSuggestion() {
     84         final LoaderManager loaderManager = mock(LoaderManager.class);
     85         when(mHost.getLoaderManager()).thenReturn(loaderManager);
     86 
     87         mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
     88         mMixin.onServiceConnected();
     89 
     90         verify(loaderManager).restartLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS,
     91                 null /* args */, mMixin /* callback */);
     92     }
     93 
     94     @Test
     95     public void onServiceConnected_hostNotAttached_shouldDoNothing() {
     96         when(mHost.getLoaderManager()).thenReturn(null);
     97 
     98         mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
     99         mMixin.onServiceConnected();
    100 
    101         verify(mHost).getLoaderManager();
    102     }
    103 
    104     @Test
    105     public void onServiceDisconnected_hostNotAttached_shouldDoNothing() {
    106         when(mHost.getLoaderManager()).thenReturn(null);
    107 
    108         mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
    109         mMixin.onServiceDisconnected();
    110 
    111         verify(mHost).getLoaderManager();
    112     }
    113 
    114     @Test
    115     public void doneLoadingg_shouldSetSuggestionLoaded() {
    116         mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle, mComponentName);
    117 
    118         mMixin.onLoadFinished(mock(SuggestionLoader.class), null);
    119 
    120         assertThat(mMixin.isSuggestionLoaded()).isTrue();
    121 
    122         mMixin.onLoaderReset(mock(SuggestionLoader.class));
    123 
    124         assertThat(mMixin.isSuggestionLoaded()).isFalse();
    125     }
    126 }
    127