1 /* 2 * Copyright (C) 2020 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 static com.android.systemui.screenshot.ScreenshotController.ACTION_TYPE_DELETE; 20 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_ID; 21 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED; 22 import static com.android.systemui.screenshot.ScreenshotController.SCREENSHOT_URI_ID; 23 24 import static junit.framework.Assert.assertEquals; 25 import static junit.framework.Assert.assertNotNull; 26 27 import static org.mockito.ArgumentMatchers.any; 28 import static org.mockito.ArgumentMatchers.anyBoolean; 29 import static org.mockito.Mockito.never; 30 import static org.mockito.Mockito.verify; 31 32 import android.content.ContentResolver; 33 import android.content.ContentValues; 34 import android.content.Intent; 35 import android.database.Cursor; 36 import android.net.Uri; 37 import android.os.Environment; 38 import android.provider.MediaStore; 39 import android.testing.AndroidTestingRunner; 40 41 import androidx.test.filters.SmallTest; 42 43 import com.android.systemui.SysuiTestCase; 44 import com.android.systemui.util.concurrency.FakeExecutor; 45 import com.android.systemui.util.time.FakeSystemClock; 46 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.Mock; 51 import org.mockito.MockitoAnnotations; 52 53 import java.io.File; 54 import java.util.concurrent.Executor; 55 56 @RunWith(AndroidTestingRunner.class) 57 @SmallTest 58 public class DeleteScreenshotReceiverTest extends SysuiTestCase { 59 60 @Mock 61 private ScreenshotSmartActions mMockScreenshotSmartActions; 62 @Mock 63 private Executor mMockExecutor; 64 65 private DeleteScreenshotReceiver mDeleteScreenshotReceiver; 66 private FakeExecutor mFakeExecutor = new FakeExecutor(new FakeSystemClock()); 67 68 @Before setup()69 public void setup() { 70 MockitoAnnotations.initMocks(this); 71 mDeleteScreenshotReceiver = 72 new DeleteScreenshotReceiver(mMockScreenshotSmartActions, mMockExecutor); 73 } 74 75 @Test testNoUriProvided()76 public void testNoUriProvided() { 77 Intent intent = new Intent(mContext, DeleteScreenshotReceiver.class); 78 79 mDeleteScreenshotReceiver.onReceive(mContext, intent); 80 81 verify(mMockExecutor, never()).execute(any(Runnable.class)); 82 verify(mMockScreenshotSmartActions, never()).notifyScreenshotAction( 83 any(String.class), any(String.class), anyBoolean(), 84 any(Intent.class)); 85 } 86 87 @Test testFileDeleted()88 public void testFileDeleted() { 89 DeleteScreenshotReceiver deleteScreenshotReceiver = 90 new DeleteScreenshotReceiver(mMockScreenshotSmartActions, mFakeExecutor); 91 ContentResolver contentResolver = mContext.getContentResolver(); 92 final Uri testUri = contentResolver.insert( 93 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, getFakeContentValues()); 94 assertNotNull(testUri); 95 96 try { 97 Cursor cursor = 98 contentResolver.query(testUri, null, null, null, null); 99 assertEquals(1, cursor.getCount()); 100 Intent intent = new Intent(mContext, DeleteScreenshotReceiver.class) 101 .putExtra(SCREENSHOT_URI_ID, testUri.toString()); 102 103 deleteScreenshotReceiver.onReceive(mContext, intent); 104 int runCount = mFakeExecutor.runAllReady(); 105 106 assertEquals(1, runCount); 107 cursor = 108 contentResolver.query(testUri, null, null, null, null); 109 assertEquals(0, cursor.getCount()); 110 } finally { 111 contentResolver.delete(testUri, null, null); 112 } 113 114 // ensure smart actions not called by default 115 verify(mMockScreenshotSmartActions, never()).notifyScreenshotAction( 116 any(String.class), any(String.class), anyBoolean(), any(Intent.class)); 117 } 118 119 @Test testNotifyScreenshotAction()120 public void testNotifyScreenshotAction() { 121 Intent intent = new Intent(mContext, DeleteScreenshotReceiver.class); 122 String uriString = "testUri"; 123 String testId = "testID"; 124 intent.putExtra(SCREENSHOT_URI_ID, uriString); 125 intent.putExtra(EXTRA_ID, testId); 126 intent.putExtra(EXTRA_SMART_ACTIONS_ENABLED, true); 127 128 mDeleteScreenshotReceiver.onReceive(mContext, intent); 129 130 verify(mMockExecutor).execute(any(Runnable.class)); 131 verify(mMockScreenshotSmartActions).notifyScreenshotAction(testId, 132 ACTION_TYPE_DELETE, false, null); 133 } 134 getFakeContentValues()135 private static ContentValues getFakeContentValues() { 136 final ContentValues values = new ContentValues(); 137 values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES 138 + File.separator + Environment.DIRECTORY_SCREENSHOTS); 139 values.put(MediaStore.MediaColumns.DISPLAY_NAME, "test_screenshot"); 140 values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png"); 141 values.put(MediaStore.MediaColumns.DATE_ADDED, 0); 142 values.put(MediaStore.MediaColumns.DATE_MODIFIED, 0); 143 return values; 144 } 145 } 146