1 /*
2  * Copyright (C) 2022 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.systemui.screenshot
18 
19 import android.content.ComponentName
20 import android.content.Context
21 import android.content.Intent
22 import android.net.Uri
23 import androidx.test.ext.truth.content.IntentSubject.assertThat as assertThatIntent
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.res.R
26 import com.android.systemui.SysuiTestCase
27 import com.android.systemui.util.mockito.eq
28 import com.android.systemui.util.mockito.mock
29 import com.google.common.truth.Truth.assertThat
30 import com.google.common.truth.Truth.assertWithMessage
31 import org.junit.Test
32 import org.mockito.Mockito.`when` as whenever
33 
34 @SmallTest
35 class ActionIntentCreatorTest : SysuiTestCase() {
36 
37     @Test
testCreateSharenull38     fun testCreateShare() {
39         val uri = Uri.parse("content://fake")
40 
41         val output = ActionIntentCreator.createShare(uri)
42 
43         assertThatIntent(output).hasAction(Intent.ACTION_CHOOSER)
44         assertThatIntent(output)
45             .hasFlags(
46                 Intent.FLAG_ACTIVITY_NEW_TASK or
47                     Intent.FLAG_ACTIVITY_CLEAR_TASK or
48                     Intent.FLAG_GRANT_READ_URI_PERMISSION
49             )
50 
51         assertThatIntent(output).extras().parcelable<Intent>(Intent.EXTRA_INTENT).isNotNull()
52         val wrappedIntent = output.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java)
53 
54         assertThatIntent(wrappedIntent).hasAction(Intent.ACTION_SEND)
55         assertThatIntent(wrappedIntent).hasData(uri)
56         assertThatIntent(wrappedIntent).hasType("image/png")
57         assertThatIntent(wrappedIntent).extras().doesNotContainKey(Intent.EXTRA_SUBJECT)
58         assertThatIntent(wrappedIntent).extras().doesNotContainKey(Intent.EXTRA_TEXT)
59         assertThatIntent(wrappedIntent).extras().parcelable<Uri>(Intent.EXTRA_STREAM).isEqualTo(uri)
60     }
61 
62     @Test
testCreateShare_embeddedUserIdRemovednull63     fun testCreateShare_embeddedUserIdRemoved() {
64         val uri = Uri.parse("content://555@fake")
65 
66         val output = ActionIntentCreator.createShare(uri)
67 
68         assertThatIntent(output.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java))
69             .hasData(Uri.parse("content://fake"))
70     }
71 
72     @Test
testCreateShareWithSubjectnull73     fun testCreateShareWithSubject() {
74         val uri = Uri.parse("content://fake")
75         val subject = "Example subject"
76 
77         val output = ActionIntentCreator.createShareWithSubject(uri, subject)
78 
79         assertThatIntent(output).hasAction(Intent.ACTION_CHOOSER)
80         assertThatIntent(output)
81             .hasFlags(
82                 Intent.FLAG_ACTIVITY_NEW_TASK or
83                     Intent.FLAG_ACTIVITY_CLEAR_TASK or
84                     Intent.FLAG_GRANT_READ_URI_PERMISSION
85             )
86 
87         val wrappedIntent = output.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java)
88         assertThatIntent(wrappedIntent).hasAction(Intent.ACTION_SEND)
89         assertThatIntent(wrappedIntent).hasData(uri)
90         assertThatIntent(wrappedIntent).hasType("image/png")
91         assertThatIntent(wrappedIntent).extras().string(Intent.EXTRA_SUBJECT).isEqualTo(subject)
92         assertThatIntent(wrappedIntent).extras().doesNotContainKey(Intent.EXTRA_TEXT)
93         assertThatIntent(wrappedIntent).extras().parcelable<Uri>(Intent.EXTRA_STREAM).isEqualTo(uri)
94     }
95 
96     @Test
testCreateShareWithTextnull97     fun testCreateShareWithText() {
98         val uri = Uri.parse("content://fake")
99         val extraText = "Extra text"
100 
101         val output = ActionIntentCreator.createShareWithText(uri, extraText)
102 
103         assertThatIntent(output).hasAction(Intent.ACTION_CHOOSER)
104         assertThatIntent(output)
105             .hasFlags(
106                 Intent.FLAG_ACTIVITY_NEW_TASK or
107                     Intent.FLAG_ACTIVITY_CLEAR_TASK or
108                     Intent.FLAG_GRANT_READ_URI_PERMISSION
109             )
110 
111         val wrappedIntent = output.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java)
112         assertThatIntent(wrappedIntent).hasAction(Intent.ACTION_SEND)
113         assertThatIntent(wrappedIntent).hasData(uri)
114         assertThatIntent(wrappedIntent).hasType("image/png")
115         assertThatIntent(wrappedIntent).extras().doesNotContainKey(Intent.EXTRA_SUBJECT)
116         assertThatIntent(wrappedIntent).extras().string(Intent.EXTRA_TEXT).isEqualTo(extraText)
117         assertThatIntent(wrappedIntent).extras().parcelable<Uri>(Intent.EXTRA_STREAM).isEqualTo(uri)
118     }
119 
120     @Test
testCreateEditnull121     fun testCreateEdit() {
122         val uri = Uri.parse("content://fake")
123         val context = mock<Context>()
124 
125         whenever(context.getString(eq(R.string.config_screenshotEditor))).thenReturn("")
126 
127         val output = ActionIntentCreator.createEdit(uri, context)
128 
129         assertThatIntent(output).hasAction(Intent.ACTION_EDIT)
130         assertThatIntent(output).hasData(uri)
131         assertThatIntent(output).hasType("image/png")
132         assertWithMessage("getComponent()").that(output.component).isNull()
133         assertThat(output.getStringExtra("edit_source")).isEqualTo("screenshot")
134         assertThatIntent(output)
135             .hasFlags(
136                 Intent.FLAG_GRANT_READ_URI_PERMISSION or
137                     Intent.FLAG_GRANT_WRITE_URI_PERMISSION or
138                     Intent.FLAG_ACTIVITY_NEW_TASK or
139                     Intent.FLAG_ACTIVITY_CLEAR_TASK
140             )
141     }
142 
143     @Test
testCreateEdit_embeddedUserIdRemovednull144     fun testCreateEdit_embeddedUserIdRemoved() {
145         val uri = Uri.parse("content://555@fake")
146         val context = mock<Context>()
147         whenever(context.getString(eq(R.string.config_screenshotEditor))).thenReturn("")
148 
149         val output = ActionIntentCreator.createEdit(uri, context)
150 
151         assertThatIntent(output).hasData(Uri.parse("content://fake"))
152     }
153 
154     @Test
testCreateEdit_withEditornull155     fun testCreateEdit_withEditor() {
156         val uri = Uri.parse("content://fake")
157         val context = mock<Context>()
158         val component = ComponentName("com.android.foo", "com.android.foo.Something")
159 
160         whenever(context.getString(eq(R.string.config_screenshotEditor)))
161             .thenReturn(component.flattenToString())
162 
163         val output = ActionIntentCreator.createEdit(uri, context)
164 
165         assertThatIntent(output).hasComponent(component)
166     }
167 }
168