1 /*
2  * Copyright (C) 2023 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.localepicker;
18 
19 import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_APP_LOCALE;
20 import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_NOTIFICATION_ID;
21 
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 
28 import android.app.settings.SettingsEnums;
29 import android.content.Intent;
30 
31 import androidx.activity.result.ActivityResultLauncher;
32 
33 import com.android.settings.testutils.FakeFeatureFactory;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.Robolectric;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.android.controller.ActivityController;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class NotificationActionActivityTest {
46     private NotificationActionActivity mNotificationActivity;
47     private ActivityController<NotificationActionActivity> mActivityController;
48     private FakeFeatureFactory mFeatureFactory;
49     @Mock
50     private NotificationController mNotificationController;
51     @Mock
52     private ActivityResultLauncher<Intent> mLauncher;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         mFeatureFactory = FakeFeatureFactory.setupForTest();
58     }
59 
60     @Test
testOnCreate_launchSystemLanguageSettings()61     public void testOnCreate_launchSystemLanguageSettings() throws Exception {
62         String targetLocale = "ja-JP";
63         int notificationId = 123;
64         Intent intent = new Intent()
65                 .putExtra(EXTRA_APP_LOCALE, targetLocale)
66                 .putExtra(EXTRA_NOTIFICATION_ID, notificationId);
67 
68         mActivityController = Robolectric.buildActivity(NotificationActionActivity.class, intent);
69         mNotificationActivity = spy(mActivityController.get());
70         doReturn(mNotificationController).when(mNotificationActivity).getNotificationController(
71                 any());
72         doReturn(notificationId).when(mNotificationController).getNotificationId(eq(targetLocale));
73         doReturn(mLauncher).when(mNotificationActivity).getLauncher();
74 
75         mNotificationActivity.onCreate(null);
76 
77         verify(mLauncher).launch(any(Intent.class));
78         verify(mFeatureFactory.metricsFeatureProvider).action(
79                 any(), eq(SettingsEnums.ACTION_NOTIFICATION_CLICK_FOR_SYSTEM_LOCALE));
80         verify(mNotificationActivity).finish();
81     }
82 }
83