1 package com.android.settings.survey;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Matchers.eq;
6 import static org.mockito.Mockito.never;
7 import static org.mockito.Mockito.spy;
8 import static org.mockito.Mockito.times;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.when;
11 
12 import android.app.Activity;
13 import android.content.BroadcastReceiver;
14 import android.content.Context;
15 import android.content.IntentFilter;
16 import android.support.v4.content.LocalBroadcastManager;
17 import com.android.settings.SettingsRobolectricTestRunner;
18 import com.android.settings.TestConfig;
19 import com.android.settings.core.InstrumentedPreferenceFragment;
20 import com.android.settings.overlay.SurveyFeatureProvider;
21 import com.android.settings.testutils.FakeFeatureFactory;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.robolectric.Robolectric;
30 import org.robolectric.RuntimeEnvironment;
31 import org.robolectric.annotation.Config;
32 import org.robolectric.util.ReflectionHelpers;
33 
34 @RunWith(SettingsRobolectricTestRunner.class)
35 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
36 public class SurveyMixinTest {
37 
38     private static final String FAKE_KEY = "fake_key";
39     private static final String FAKE_SURVEY_ID = "fake_id";
40 
41     private FakeFeatureFactory mFactory;
42     private Context mContext;
43     private SurveyFeatureProvider mProvider;
44     @Mock
45     private BroadcastReceiver mReceiver;
46     @Mock
47     private InstrumentedPreferenceFragment mFragment;
48 
49     @Before
setUp()50     public void setUp() {
51         // set up the fakefeature factory to mock out the survey provider
52         MockitoAnnotations.initMocks(this);
53         mContext = spy(RuntimeEnvironment.application.getApplicationContext());
54         FakeFeatureFactory.setupForTest(mContext);
55         mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
56         mProvider = mFactory.getSurveyFeatureProvider(mContext);
57         when(mProvider.getSurveyId(any(), eq(FAKE_KEY))).thenReturn(FAKE_SURVEY_ID);
58     }
59 
60     @Test
onResume_triesRegisteringReceiverAndDownloadingWhenNoSurveyDetected()61     public void onResume_triesRegisteringReceiverAndDownloadingWhenNoSurveyDetected() {
62         // Pretend there is no survey in memory
63         when(mProvider.getSurveyExpirationDate(any(), any())).thenReturn(-1L);
64 
65         // Pretend we are an activity that is starting up
66         Activity temp = Robolectric.setupActivity(Activity.class);
67         when(mFragment.getActivity()).thenReturn(temp);
68         SurveyMixin mixin = new SurveyMixin(mFragment, FAKE_KEY);
69         mixin.onResume();
70 
71         // Verify that a download was attempted
72         verify(mProvider, times(1)).downloadSurvey(any(), any(), any());
73         // Verify that we registered a receiver for download completion broadcasts
74         verify(mProvider, times(1)).createAndRegisterReceiver(any());
75         // Verify we did not try to show a survey
76         verify(mProvider, never()).showSurveyIfAvailable(any(), any());
77     }
78 
79     @Test
onResume_triesShowingSurveyWhenOneIsPresent()80     public void onResume_triesShowingSurveyWhenOneIsPresent() {
81         // Pretend there is a survey in memory
82         when(mProvider.getSurveyExpirationDate(any(), any())).thenReturn(0L);
83 
84         // Pretend we are an activity that is starting up
85         Activity temp = Robolectric.setupActivity(Activity.class);
86         when(mFragment.getActivity()).thenReturn(temp);
87         SurveyMixin mixin = new SurveyMixin(mFragment, FAKE_KEY);
88         mixin.onResume();
89 
90         // Verify that a download was not attempted
91         verify(mProvider, never()).downloadSurvey(any(), any(), any());
92         // Verify that we did not register a receiver
93         verify(mProvider, never()).createAndRegisterReceiver(any());
94         // Verify we tried to show a survey
95         verify(mProvider, times(1)).showSurveyIfAvailable(any(), any());
96     }
97 
98     @Test
onResume_doesNothingWhenActivityIsNull()99     public void onResume_doesNothingWhenActivityIsNull() {
100         // Pretend the activity died somewhere in the process
101         when(mFragment.getActivity()).thenReturn(null);
102         SurveyMixin mixin = new SurveyMixin(mFragment, FAKE_KEY);
103         mixin.onResume();
104 
105         // Verify we don't try showing or downloading a survey
106         verify(mProvider, never()).showSurveyIfAvailable(any(), any());
107         verify(mProvider, never()).downloadSurvey(any(), any(), any());
108     }
109 
110     @Test
onPause_removesReceiverIfPreviouslySet()111     public void onPause_removesReceiverIfPreviouslySet() {
112         // Pretend there is a survey in memory
113         when(mProvider.getSurveyExpirationDate(any(), any())).thenReturn(-1L);
114 
115         // Pretend we are an activity that starts and stops
116         Activity temp = Robolectric.setupActivity(Activity.class);
117         when(mFragment.getActivity()).thenReturn(temp);
118         when(mProvider.createAndRegisterReceiver(any())).thenReturn(mReceiver);
119         LocalBroadcastManager manager = LocalBroadcastManager.getInstance(temp);
120         SurveyMixin mixin = new SurveyMixin(mFragment, FAKE_KEY);
121         mixin.onResume();
122         manager.registerReceiver(mReceiver, new IntentFilter());
123         mixin.onPause();
124 
125         // Verify we remove the receiver
126         HashMap<BroadcastReceiver, ArrayList<IntentFilter>> map =
127                 ReflectionHelpers.getField(manager, "mReceivers");
128         assertThat(map.containsKey(mReceiver)).isFalse();
129     }
130 
131     @Test
onPause_doesNothingWhenActivityOrReceiverNull()132     public void onPause_doesNothingWhenActivityOrReceiverNull() {
133         // Pretend there is a survey in memory
134         when(mProvider.getSurveyExpirationDate(any(), any())).thenReturn(-1L);
135 
136         // Pretend we are an activity that fails to create a receiver properly
137         Activity temp = Robolectric.setupActivity(Activity.class);
138         when(mFragment.getActivity()).thenReturn(temp);
139         SurveyMixin mixin = new SurveyMixin(mFragment, FAKE_KEY);
140         mixin.onPause();
141 
142         // Verify we do nothing;
143         verify(mProvider, never()).showSurveyIfAvailable(any(), any());
144 
145         // pretend the activity died before onPause
146         when(mFragment.getActivity()).thenReturn(null);
147         mixin.onPause();
148 
149         // Verify we do nothing
150         verify(mProvider, never()).showSurveyIfAvailable(any(), any());
151     }
152 
153 }
154