Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 17 | package com.android.documentsui.services; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 18 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 19 | import static com.android.documentsui.services.FileOperationService.OPERATION_COPY; |
| 20 | import static com.android.documentsui.services.FileOperations.createBaseIntent; |
| 21 | import static com.android.documentsui.services.FileOperations.createJobId; |
| 22 | import static com.google.android.collect.Lists.newArrayList; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 23 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.net.Uri; |
| 27 | import android.test.ServiceTestCase; |
| 28 | import android.test.suitebuilder.annotation.MediumTest; |
| 29 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 30 | import com.android.documentsui.model.DocumentInfo; |
| 31 | import com.android.documentsui.model.DocumentStack; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 32 | import com.android.documentsui.services.Job.Listener; |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 33 | |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 35 | import java.util.List; |
| 36 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 37 | /** |
| 38 | * TODO: Test progress updates. |
| 39 | */ |
Steve McKay | bfd8705 | 2015-11-12 10:54:31 +0900 | [diff] [blame] | 40 | @MediumTest |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 41 | public class FileOperationServiceTest extends ServiceTestCase<FileOperationService> { |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 42 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 43 | private static final DocumentInfo ALPHA_DOC = createDoc("alpha"); |
| 44 | private static final DocumentInfo BETA_DOC = createDoc("alpha"); |
| 45 | private static final DocumentInfo GAMMA_DOC = createDoc("gamma"); |
| 46 | private static final DocumentInfo DELTA_DOC = createDoc("delta"); |
| 47 | |
| 48 | private FileOperationService mService; |
| 49 | private TestScheduledExecutorService mExecutor; |
| 50 | private TestJobFactory mJobFactory; |
| 51 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 52 | public FileOperationServiceTest() { |
| 53 | super(FileOperationService.class); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 56 | @Override |
| 57 | protected void setUp() throws Exception { |
| 58 | super.setUp(); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 59 | setupService(); // must be called first for our test setup to work correctly. |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 60 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 61 | mExecutor = new TestScheduledExecutorService(); |
| 62 | mJobFactory = new TestJobFactory(); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 63 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 64 | // Install test doubles. |
| 65 | mService = getService(); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 66 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 67 | assertNull(mService.executor); |
| 68 | mService.executor = mExecutor; |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 69 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 70 | assertNull(mService.jobFactory); |
| 71 | mService.jobFactory = mJobFactory; |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 74 | public void testRunsJobs() throws Exception { |
| 75 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
| 76 | startService(createCopyIntent(newArrayList(GAMMA_DOC), DELTA_DOC)); |
| 77 | |
| 78 | mExecutor.runAll(); |
| 79 | mJobFactory.assertAllJobsStarted(); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 82 | public void testRunsJobs_AfterFailure() throws Exception { |
| 83 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
| 84 | startService(createCopyIntent(newArrayList(GAMMA_DOC), DELTA_DOC)); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 85 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 86 | mJobFactory.jobs.get(0).fail(ALPHA_DOC); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 87 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 88 | mExecutor.runAll(); |
| 89 | mJobFactory.assertAllJobsStarted(); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 92 | public void testHoldsWakeLockWhileWorking() throws Exception { |
| 93 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 94 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 95 | assertTrue(mService.holdsWakeLock()); |
Tomasz Mikolajewski | dcec9ac | 2015-12-16 16:23:00 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 98 | public void testReleasesWakeLock_AfterSuccess() throws Exception { |
| 99 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 100 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 101 | assertTrue(mService.holdsWakeLock()); |
| 102 | mExecutor.runAll(); |
| 103 | assertFalse(mService.holdsWakeLock()); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 106 | public void testReleasesWakeLock_AfterFailure() throws Exception { |
| 107 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 108 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 109 | assertTrue(mService.holdsWakeLock()); |
| 110 | mExecutor.runAll(); |
| 111 | assertFalse(mService.holdsWakeLock()); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 114 | public void testShutdownStopsExecutor_AfterSuccess() throws Exception { |
| 115 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 116 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 117 | mExecutor.isAlive(); |
| 118 | mExecutor.runAll(); |
| 119 | mExecutor.assertShutdown(); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 122 | public void testShutdownStopsExecutor_AfterMixedFailures() throws Exception { |
| 123 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
| 124 | startService(createCopyIntent(newArrayList(GAMMA_DOC), DELTA_DOC)); |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 125 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 126 | mJobFactory.jobs.get(0).fail(ALPHA_DOC); |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 127 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 128 | mExecutor.runAll(); |
| 129 | mExecutor.assertShutdown(); |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 132 | public void testShutdownStopsExecutor_AfterTotalFailure() throws Exception { |
| 133 | startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC)); |
| 134 | startService(createCopyIntent(newArrayList(GAMMA_DOC), DELTA_DOC)); |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 135 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 136 | mJobFactory.jobs.get(0).fail(ALPHA_DOC); |
| 137 | mJobFactory.jobs.get(1).fail(GAMMA_DOC); |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 138 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 139 | mExecutor.runAll(); |
| 140 | mExecutor.assertShutdown(); |
Steve McKay | f712a20 | 2015-11-19 17:27:12 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 143 | private Intent createCopyIntent(ArrayList<DocumentInfo> files, DocumentInfo dest) |
| 144 | throws Exception { |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 145 | DocumentStack stack = new DocumentStack(); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 146 | stack.push(dest); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 147 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 148 | return createBaseIntent(OPERATION_COPY, getContext(), createJobId(), files, stack); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 151 | private static DocumentInfo createDoc(String name) { |
| 152 | // Doesn't need to be valid content Uri, just some urly looking thing. |
| 153 | Uri uri = new Uri.Builder() |
| 154 | .scheme("content") |
| 155 | .authority("com.android.documentsui.testing") |
| 156 | .path(name) |
| 157 | .build(); |
| 158 | |
| 159 | return createDoc(uri); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 162 | private static DocumentInfo createDoc(Uri destination) { |
| 163 | DocumentInfo destDoc = new DocumentInfo(); |
| 164 | destDoc.derivedUri = destination; |
| 165 | return destDoc; |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 168 | private final class TestJobFactory extends Job.Factory { |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 169 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 170 | final List<TestJob> jobs = new ArrayList<>(); |
Steve McKay | 99bcc6a | 2015-10-26 17:03:55 -0700 | [diff] [blame] | 171 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 172 | void assertAllJobsStarted() { |
| 173 | for (TestJob job : jobs) { |
| 174 | job.assertStarted(); |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | @Override |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 179 | Job createCopy(Context service, Context appContext, Listener listener, String id, |
| 180 | DocumentStack stack, List<DocumentInfo> srcs) { |
| 181 | TestJob job = new TestJob(service, appContext, listener, OPERATION_COPY, id, stack); |
| 182 | jobs.add(job); |
| 183 | return job; |
Ben Kwa | c06f3fd | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 184 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 185 | } |
Ben Kwa | aac9e2e | 2015-04-16 18:14:35 -0700 | [diff] [blame] | 186 | } |