• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.accounts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 
29 import android.accounts.Account;
30 import android.content.ContentProvider;
31 import android.content.ContentResolver;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.content.pm.ApplicationInfo;
35 import android.content.pm.PackageInfo;
36 import android.content.pm.ProviderInfo;
37 import android.content.pm.ResolveInfo;
38 import android.graphics.Bitmap;
39 import android.net.Uri;
40 import android.os.Bundle;
41 import android.widget.ImageView;
42 
43 import com.android.settings.homepage.SettingsHomepageActivity;
44 import com.android.settings.homepage.contextualcards.slices.BatteryFixSliceTest;
45 import com.android.settings.testutils.shadow.ShadowUserManager;
46 
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.MockitoAnnotations;
51 import org.robolectric.Robolectric;
52 import org.robolectric.RobolectricTestRunner;
53 import org.robolectric.RuntimeEnvironment;
54 import org.robolectric.android.controller.ActivityController;
55 import org.robolectric.annotation.Config;
56 import org.robolectric.annotation.Implementation;
57 import org.robolectric.annotation.Implements;
58 import org.robolectric.shadow.api.Shadow;
59 import org.robolectric.shadows.ShadowContentResolver;
60 import org.robolectric.shadows.ShadowPackageManager;
61 
62 @RunWith(RobolectricTestRunner.class)
63 @Config(shadows = ShadowUserManager.class)
64 public class AvatarViewMixinTest {
65     private static final String FAKE_ACCOUNT = "test@domain.com";
66     private static final String FAKE_DOMAIN = "domain.com";
67     private static final String FAKE_AUTHORITY = "authority.domain.com";
68     private static final String METHOD_GET_ACCOUNT_AVATAR = "getAccountAvatar";
69 
70     private Context mContext;
71     private ImageView mImageView;
72     private ActivityController mController;
73     private SettingsHomepageActivity mActivity;
74 
75     @Before
setUp()76     public void setUp() {
77         MockitoAnnotations.initMocks(this);
78         mContext = RuntimeEnvironment.application;
79         mImageView = new ImageView(mContext);
80         mController = Robolectric.buildActivity(SettingsHomepageActivity.class).create();
81         mActivity = (SettingsHomepageActivity) mController.get();
82     }
83 
84     @Test
hasAccount_useDefaultAccountData_returnFalse()85     public void hasAccount_useDefaultAccountData_returnFalse() {
86         final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(mActivity, mImageView);
87         assertThat(avatarViewMixin.hasAccount()).isFalse();
88     }
89 
90     @Test
91     @Config(shadows = ShadowAccountFeatureProviderImpl.class)
hasAccount_useShadowAccountData_returnTrue()92     public void hasAccount_useShadowAccountData_returnTrue() {
93         final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(mActivity, mImageView);
94         assertThat(avatarViewMixin.hasAccount()).isTrue();
95     }
96 
97     @Test
98     @Config(shadows = BatteryFixSliceTest.ShadowBatteryTipLoader.class)
onStart_useMockAvatarViewMixin_shouldBeExecuted()99     public void onStart_useMockAvatarViewMixin_shouldBeExecuted() {
100         final AvatarViewMixin mockAvatar = spy(new AvatarViewMixin(mActivity, mImageView));
101 
102         mActivity.getLifecycle().addObserver(mockAvatar);
103         mController.start();
104 
105         verify(mockAvatar).hasAccount();
106     }
107 
108     @Test
onStart_noAccount_mAccountNameShouldBeNull()109     public void onStart_noAccount_mAccountNameShouldBeNull() {
110         final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(mActivity, mImageView);
111         avatarViewMixin.mAccountName = FAKE_ACCOUNT;
112 
113         avatarViewMixin.onStart();
114 
115         assertThat(avatarViewMixin.mAccountName).isNull();
116     }
117 
118     @Test
queryProviderAuthority_useShadowPackagteManager_returnNull()119     public void queryProviderAuthority_useShadowPackagteManager_returnNull() {
120         final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(mActivity, mImageView);
121 
122         assertThat(avatarViewMixin.queryProviderAuthority()).isNull();
123     }
124 
125     @Test
queryProviderAuthority_useNewShadowPackagteManager_returnAuthority()126     public void queryProviderAuthority_useNewShadowPackagteManager_returnAuthority() {
127         final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(mActivity, mImageView);
128         ShadowPackageManager shadowPackageManager = Shadow.extract(mContext.getPackageManager());
129         final PackageInfo accountProvider = new PackageInfo();
130         accountProvider.packageName = "test.pkg";
131         accountProvider.applicationInfo = new ApplicationInfo();
132         accountProvider.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
133         accountProvider.applicationInfo.packageName = accountProvider.packageName;
134         accountProvider.providers = new ProviderInfo[1];
135         accountProvider.providers[0] = new ProviderInfo();
136         accountProvider.providers[0].authority = FAKE_AUTHORITY;
137         accountProvider.providers[0].packageName = accountProvider.packageName;
138         accountProvider.providers[0].name = "test.class";
139         accountProvider.providers[0].applicationInfo = accountProvider.applicationInfo;
140 
141         final ResolveInfo resolveInfo = new ResolveInfo();
142         resolveInfo.providerInfo = accountProvider.providers[0];
143         shadowPackageManager.addResolveInfoForIntent(AvatarViewMixin.INTENT_GET_ACCOUNT_DATA,
144                 resolveInfo);
145         assertThat(avatarViewMixin.queryProviderAuthority()).isEqualTo(FAKE_AUTHORITY);
146     }
147 
148     @Test
callWithGetAccountAvatarMethod_useFakeData_shouldReturnAccountNameAndAvatar()149     public void callWithGetAccountAvatarMethod_useFakeData_shouldReturnAccountNameAndAvatar() {
150         final ContentResolver contentResolver = mContext.getContentResolver();
151         final Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(
152                 FAKE_AUTHORITY).build();
153         final ContentProvider mockContentProvider = mock(ContentProvider.class);
154 
155         ShadowContentResolver.registerProviderInternal(FAKE_AUTHORITY, mockContentProvider);
156 
157         final Bundle bundle = new Bundle();
158         final Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
159         bundle.putParcelable("account_avatar", bitmap);
160         bundle.putString("account_name", FAKE_ACCOUNT);
161         doReturn(bundle).when(mockContentProvider).call(anyString(), anyString(),
162                 any(Bundle.class));
163 
164         contentResolver.call(uri, METHOD_GET_ACCOUNT_AVATAR, null /* arg */, null /* extras */);
165 
166         final Object object = bundle.getParcelable("account_avatar");
167         assertThat(object instanceof Bitmap).isTrue();
168         assertThat(bundle.getString("account_name")).isEqualTo(FAKE_ACCOUNT);
169     }
170 
171     @Test
onClickAvatar_withEmptyUri_startActivityShouldNotBeExecuted()172     public void onClickAvatar_withEmptyUri_startActivityShouldNotBeExecuted() {
173         final SettingsHomepageActivity activity = spy((SettingsHomepageActivity) mController.get());
174         final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(activity, mImageView);
175 
176         mImageView.performClick();
177 
178         verify(activity, never()).startActivity(any(Intent.class));
179     }
180 
181     @Implements(value = AccountFeatureProviderImpl.class)
182     public static class ShadowAccountFeatureProviderImpl {
183 
184         @Implementation
getAccounts(Context context)185         protected Account[] getAccounts(Context context) {
186             return new Account[]{new Account(FAKE_ACCOUNT, FAKE_DOMAIN)};
187         }
188     }
189 }
190