summaryrefslogtreecommitdiff
path: root/tests/robolectric/src/com/android/systemui/plugin/globalactions/wallet/WalletPluginServiceTest.java
blob: aaf8a80c706e5b3d64be2316ab1dd0dde8d20e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.android.systemui.plugin.globalactions.wallet;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.service.quickaccesswallet.QuickAccessWalletClient;

import androidx.test.core.app.ApplicationProvider;

import com.android.systemui.plugins.GlobalActionsPanelPlugin;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = Build.VERSION_CODES.R)
public class WalletPluginServiceTest {

    private final Context mContext = ApplicationProvider.getApplicationContext();
    @Mock QuickAccessWalletClient mWalletClient;
    @Mock GlobalActionsPanelPlugin.Callbacks mPluginCallbacks;
    private WalletPluginService mPluginService;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(true);
        mPluginService = new WalletPluginService();
        ShadowLog.stream = System.out;
    }

    @Test
    public void onCreate_serviceAvailable_enablesFeatureInSettings() {
        mPluginService.onCreate(mContext, mContext, mWalletClient);

        ContentResolver cr = mContext.getContentResolver();
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE, -1)).isEqualTo(1);
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, -1)).isEqualTo(1);
    }

    @Test
    public void onCreate_serviceUnavailable_disablesFeatureInSettings() {
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(false);

        mPluginService.onCreate(mContext, mContext, mWalletClient);

        ContentResolver cr = mContext.getContentResolver();
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE, -1)).isEqualTo(0);
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, -1)).isEqualTo(1);
    }

    @Test
    public void onCreate_doesNotOverridePanelEnabledSettingIfOff() {
        ContentResolver cr = mContext.getContentResolver();
        Settings.Secure.putInt(cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, 0);

        mPluginService.onCreate(mContext, mContext, mWalletClient);

        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, -1)).isEqualTo(0);
    }

    @Test
    public void onPanelShown_wasUnavailable_nowAvailable_updatesFeatureAvailabilityInSettings() {
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(false);
        mPluginService.onCreate(mContext, mContext, mWalletClient);
        ContentResolver cr = mContext.getContentResolver();
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE, -1)).isEqualTo(0);
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, -1)).isEqualTo(1);
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(true);

        mPluginService.onPanelShown(mPluginCallbacks, false, mWalletClient);

        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE, -1)).isEqualTo(1);
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, -1)).isEqualTo(1);
    }

    @Test
    public void onPanelShown_wasAvailable_nowUnavailable_updatesFeatureAvailabilityInSettings() {
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(true);
        mPluginService.onCreate(mContext, mContext, mWalletClient);
        ContentResolver cr = mContext.getContentResolver();
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE, -1)).isEqualTo(1);
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, -1)).isEqualTo(1);
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(false);

        mPluginService.onPanelShown(mPluginCallbacks, false, mWalletClient);

        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE, -1)).isEqualTo(0);
        assertThat(Settings.Secure.getInt(
                cr, Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED, -1)).isEqualTo(1);
    }

    @Test
    public void onPanelShown_returnsControllerIfFeatureAvailable() {
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(true);
        when(mWalletClient.isWalletFeatureAvailable()).thenReturn(true);
        mPluginService.onCreate(mContext, mContext, mWalletClient);

        GlobalActionsPanelPlugin.PanelViewController viewController =
                mPluginService.onPanelShown(mPluginCallbacks, false, mWalletClient);

        assertThat(viewController).isNotNull();
    }

    @Test
    public void onPanelShown_performsGetWalletCardsRequest() {
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(true);
        when(mWalletClient.isWalletFeatureAvailable()).thenReturn(true);
        mPluginService.onCreate(mContext, mContext, mWalletClient);

        mPluginService.onPanelShown(mPluginCallbacks, false, mWalletClient);

        verify(mWalletClient).getWalletCards(any(), any(), any());
    }

    @Test
    public void onPanelShown_returnsNullIfFeatureUnavailable() {
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(true);
        when(mWalletClient.isWalletFeatureAvailable()).thenReturn(false);
        mPluginService.onCreate(mContext, mContext, mWalletClient);

        GlobalActionsPanelPlugin.PanelViewController viewController =
                mPluginService.onPanelShown(mPluginCallbacks, false, mWalletClient);

        assertThat(viewController).isNull();
    }

    @Test
    public void onPanelShown_returnsNullIfServiceUnavailable() {
        when(mWalletClient.isWalletServiceAvailable()).thenReturn(false);
        when(mWalletClient.isWalletFeatureAvailable()).thenReturn(true);
        mPluginService.onCreate(mContext, mContext, mWalletClient);

        GlobalActionsPanelPlugin.PanelViewController viewController =
                mPluginService.onPanelShown(mPluginCallbacks, false, mWalletClient);

        assertThat(viewController).isNull();
    }
}