1 /*
2  * Copyright (C) 2024 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.datetime;
18 
19 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 
28 import android.app.Activity;
29 import android.content.Context;
30 import android.content.Intent;
31 
32 import androidx.preference.Preference;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.robolectric.Robolectric;
39 import org.robolectric.RobolectricTestRunner;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class TimeFeedbackPreferenceControllerTest {
43 
44     private Context mContext;
45 
46     @Before
setUp()47     public void setUp() {
48         mContext = spy(Robolectric.setupActivity(Activity.class));
49     }
50 
51     @Test
emptyIntentUri_controllerNotAvailable()52     public void emptyIntentUri_controllerNotAvailable() {
53         String emptyIntentUri = "";
54         TimeFeedbackPreferenceController controller =
55                 new TimeFeedbackPreferenceController(mContext, "test_key", emptyIntentUri);
56         assertThat(controller.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
57     }
58 
59     @Test
clickPreference()60     public void clickPreference() {
61         Preference preference = new Preference(mContext);
62 
63         String intentUri =
64                 "intent:#Intent;"
65                         + "action=com.android.settings.test.LAUNCH_USER_FEEDBACK;"
66                         + "package=com.android.settings.test.target;"
67                         + "end";
68         TimeFeedbackPreferenceController controller =
69                 new TimeFeedbackPreferenceController(mContext, "test_key", intentUri);
70 
71         // Click a preference that's not controlled by this controller.
72         preference.setKey("fake_key");
73         assertThat(controller.handlePreferenceTreeClick(preference)).isFalse();
74 
75         // Check for startActivity() call.
76         verify(mContext, never()).startActivity(any());
77 
78         // Click a preference controlled by this controller.
79         preference.setKey(controller.getPreferenceKey());
80         assertThat(controller.handlePreferenceTreeClick(preference)).isTrue();
81 
82         // Check for startActivity() call.
83         ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
84         verify(mContext).startActivity(intentCaptor.capture());
85         Intent actualIntent = intentCaptor.getValue();
86         assertThat(actualIntent.getAction()).isEqualTo(
87                 "com.android.settings.test.LAUNCH_USER_FEEDBACK");
88         assertThat(actualIntent.getPackage()).isEqualTo("com.android.settings.test.target");
89     }
90 }
91