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
17package com.android.settings.users;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Matchers.anyInt;
21import static org.mockito.Mockito.spy;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.app.Activity;
26import android.content.pm.UserInfo;
27import android.os.UserManager;
28
29import com.android.settings.R;
30import com.android.settings.dashboard.SummaryLoader;
31import com.android.settings.testutils.SettingsRobolectricTestRunner;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38import org.robolectric.Robolectric;
39
40@RunWith(SettingsRobolectricTestRunner.class)
41public class UserSettingsTest {
42
43    @Mock
44    private UserManager mUserManager;
45    @Mock
46    private SummaryLoader mSummaryLoader;
47    private Activity mActivity;
48    private SummaryLoader.SummaryProvider mSummaryProvider;
49
50    @Before
51    public void setUp() {
52        MockitoAnnotations.initMocks(this);
53        mActivity = spy(Robolectric.buildActivity(Activity.class).get());
54        when((Object) mActivity.getSystemService(UserManager.class)).thenReturn(mUserManager);
55
56        mSummaryProvider =
57            UserSettings.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(mActivity, mSummaryLoader);
58    }
59
60    @Test
61    public void setListening_shouldSetSummaryWithUserName() {
62        final String name = "John";
63        final UserInfo userInfo = new UserInfo();
64        userInfo.name = name;
65        when(mUserManager.getUserInfo(anyInt())).thenReturn(userInfo);
66
67        mSummaryProvider.setListening(true);
68
69        verify(mSummaryLoader)
70            .setSummary(mSummaryProvider, mActivity.getString(R.string.users_summary, name));
71    }
72
73    @Test
74    public void testAssignDefaultPhoto_ContextNull_ReturnFalseAndNotCrash() {
75        // Should not crash here
76        assertThat(UserSettings.assignDefaultPhoto(null, 0)).isFalse();
77    }
78}
79