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