blob: d55b6f098548181a7b314b3bd6dcab45630c75df [file] [log] [blame]
Ben Kwaaac9e2e2015-04-16 18:14:35 -07001/*
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 McKayc83baa02016-01-06 18:32:13 -080017package com.android.documentsui.services;
Ben Kwaaac9e2e2015-04-16 18:14:35 -070018
Steve McKaybbeba522016-01-13 17:17:39 -080019import static com.android.documentsui.services.FileOperationService.OPERATION_COPY;
20import static com.android.documentsui.services.FileOperations.createBaseIntent;
21import static com.android.documentsui.services.FileOperations.createJobId;
22import static com.google.android.collect.Lists.newArrayList;
Ben Kwaaac9e2e2015-04-16 18:14:35 -070023
Steve McKaybbeba522016-01-13 17:17:39 -080024import android.content.Context;
25import android.content.Intent;
26import android.net.Uri;
27import android.test.ServiceTestCase;
28import android.test.suitebuilder.annotation.MediumTest;
29
Ben Kwaaac9e2e2015-04-16 18:14:35 -070030import com.android.documentsui.model.DocumentInfo;
31import com.android.documentsui.model.DocumentStack;
Steve McKaybbeba522016-01-13 17:17:39 -080032import com.android.documentsui.services.Job.Listener;
Steve McKay99bcc6a2015-10-26 17:03:55 -070033
Ben Kwaaac9e2e2015-04-16 18:14:35 -070034import java.util.ArrayList;
Ben Kwaaac9e2e2015-04-16 18:14:35 -070035import java.util.List;
36
Steve McKaybbeba522016-01-13 17:17:39 -080037/**
38 * TODO: Test progress updates.
39 */
Steve McKaybfd87052015-11-12 10:54:31 +090040@MediumTest
Steve McKayc83baa02016-01-06 18:32:13 -080041public class FileOperationServiceTest extends ServiceTestCase<FileOperationService> {
Ben Kwaaac9e2e2015-04-16 18:14:35 -070042
Steve McKaybbeba522016-01-13 17:17:39 -080043 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 McKayc83baa02016-01-06 18:32:13 -080052 public FileOperationServiceTest() {
53 super(FileOperationService.class);
Steve McKay99bcc6a2015-10-26 17:03:55 -070054 }
55
Steve McKay99bcc6a2015-10-26 17:03:55 -070056 @Override
57 protected void setUp() throws Exception {
58 super.setUp();
Steve McKaybbeba522016-01-13 17:17:39 -080059 setupService(); // must be called first for our test setup to work correctly.
Steve McKay99bcc6a2015-10-26 17:03:55 -070060
Steve McKaybbeba522016-01-13 17:17:39 -080061 mExecutor = new TestScheduledExecutorService();
62 mJobFactory = new TestJobFactory();
Steve McKay99bcc6a2015-10-26 17:03:55 -070063
Steve McKaybbeba522016-01-13 17:17:39 -080064 // Install test doubles.
65 mService = getService();
Steve McKay99bcc6a2015-10-26 17:03:55 -070066
Steve McKaybbeba522016-01-13 17:17:39 -080067 assertNull(mService.executor);
68 mService.executor = mExecutor;
Steve McKayf712a202015-11-19 17:27:12 -080069
Steve McKaybbeba522016-01-13 17:17:39 -080070 assertNull(mService.jobFactory);
71 mService.jobFactory = mJobFactory;
Steve McKay99bcc6a2015-10-26 17:03:55 -070072 }
73
Steve McKaybbeba522016-01-13 17:17:39 -080074 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 McKay99bcc6a2015-10-26 17:03:55 -070080 }
81
Steve McKaybbeba522016-01-13 17:17:39 -080082 public void testRunsJobs_AfterFailure() throws Exception {
83 startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));
84 startService(createCopyIntent(newArrayList(GAMMA_DOC), DELTA_DOC));
Steve McKay99bcc6a2015-10-26 17:03:55 -070085
Steve McKaybbeba522016-01-13 17:17:39 -080086 mJobFactory.jobs.get(0).fail(ALPHA_DOC);
Steve McKay99bcc6a2015-10-26 17:03:55 -070087
Steve McKaybbeba522016-01-13 17:17:39 -080088 mExecutor.runAll();
89 mJobFactory.assertAllJobsStarted();
Steve McKay99bcc6a2015-10-26 17:03:55 -070090 }
91
Steve McKaybbeba522016-01-13 17:17:39 -080092 public void testHoldsWakeLockWhileWorking() throws Exception {
93 startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));
Tomasz Mikolajewskidcec9ac2015-12-16 16:23:00 +090094
Steve McKaybbeba522016-01-13 17:17:39 -080095 assertTrue(mService.holdsWakeLock());
Tomasz Mikolajewskidcec9ac2015-12-16 16:23:00 +090096 }
97
Steve McKaybbeba522016-01-13 17:17:39 -080098 public void testReleasesWakeLock_AfterSuccess() throws Exception {
99 startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));
Steve McKay99bcc6a2015-10-26 17:03:55 -0700100
Steve McKaybbeba522016-01-13 17:17:39 -0800101 assertTrue(mService.holdsWakeLock());
102 mExecutor.runAll();
103 assertFalse(mService.holdsWakeLock());
Steve McKay99bcc6a2015-10-26 17:03:55 -0700104 }
105
Steve McKaybbeba522016-01-13 17:17:39 -0800106 public void testReleasesWakeLock_AfterFailure() throws Exception {
107 startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));
Steve McKay99bcc6a2015-10-26 17:03:55 -0700108
Steve McKaybbeba522016-01-13 17:17:39 -0800109 assertTrue(mService.holdsWakeLock());
110 mExecutor.runAll();
111 assertFalse(mService.holdsWakeLock());
Steve McKay99bcc6a2015-10-26 17:03:55 -0700112 }
113
Steve McKaybbeba522016-01-13 17:17:39 -0800114 public void testShutdownStopsExecutor_AfterSuccess() throws Exception {
115 startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));
Steve McKay99bcc6a2015-10-26 17:03:55 -0700116
Steve McKaybbeba522016-01-13 17:17:39 -0800117 mExecutor.isAlive();
118 mExecutor.runAll();
119 mExecutor.assertShutdown();
Steve McKay99bcc6a2015-10-26 17:03:55 -0700120 }
121
Steve McKaybbeba522016-01-13 17:17:39 -0800122 public void testShutdownStopsExecutor_AfterMixedFailures() throws Exception {
123 startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));
124 startService(createCopyIntent(newArrayList(GAMMA_DOC), DELTA_DOC));
Steve McKayf712a202015-11-19 17:27:12 -0800125
Steve McKaybbeba522016-01-13 17:17:39 -0800126 mJobFactory.jobs.get(0).fail(ALPHA_DOC);
Steve McKayf712a202015-11-19 17:27:12 -0800127
Steve McKaybbeba522016-01-13 17:17:39 -0800128 mExecutor.runAll();
129 mExecutor.assertShutdown();
Steve McKayf712a202015-11-19 17:27:12 -0800130 }
131
Steve McKaybbeba522016-01-13 17:17:39 -0800132 public void testShutdownStopsExecutor_AfterTotalFailure() throws Exception {
133 startService(createCopyIntent(newArrayList(ALPHA_DOC), BETA_DOC));
134 startService(createCopyIntent(newArrayList(GAMMA_DOC), DELTA_DOC));
Steve McKayf712a202015-11-19 17:27:12 -0800135
Steve McKaybbeba522016-01-13 17:17:39 -0800136 mJobFactory.jobs.get(0).fail(ALPHA_DOC);
137 mJobFactory.jobs.get(1).fail(GAMMA_DOC);
Steve McKayf712a202015-11-19 17:27:12 -0800138
Steve McKaybbeba522016-01-13 17:17:39 -0800139 mExecutor.runAll();
140 mExecutor.assertShutdown();
Steve McKayf712a202015-11-19 17:27:12 -0800141 }
142
Steve McKaybbeba522016-01-13 17:17:39 -0800143 private Intent createCopyIntent(ArrayList<DocumentInfo> files, DocumentInfo dest)
144 throws Exception {
Steve McKay99bcc6a2015-10-26 17:03:55 -0700145 DocumentStack stack = new DocumentStack();
Steve McKaybbeba522016-01-13 17:17:39 -0800146 stack.push(dest);
Steve McKay99bcc6a2015-10-26 17:03:55 -0700147
Steve McKaybbeba522016-01-13 17:17:39 -0800148 return createBaseIntent(OPERATION_COPY, getContext(), createJobId(), files, stack);
Steve McKay99bcc6a2015-10-26 17:03:55 -0700149 }
150
Steve McKaybbeba522016-01-13 17:17:39 -0800151 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 McKay99bcc6a2015-10-26 17:03:55 -0700160 }
161
Steve McKaybbeba522016-01-13 17:17:39 -0800162 private static DocumentInfo createDoc(Uri destination) {
163 DocumentInfo destDoc = new DocumentInfo();
164 destDoc.derivedUri = destination;
165 return destDoc;
Steve McKay99bcc6a2015-10-26 17:03:55 -0700166 }
167
Steve McKaybbeba522016-01-13 17:17:39 -0800168 private final class TestJobFactory extends Job.Factory {
Steve McKay99bcc6a2015-10-26 17:03:55 -0700169
Steve McKaybbeba522016-01-13 17:17:39 -0800170 final List<TestJob> jobs = new ArrayList<>();
Steve McKay99bcc6a2015-10-26 17:03:55 -0700171
Steve McKaybbeba522016-01-13 17:17:39 -0800172 void assertAllJobsStarted() {
173 for (TestJob job : jobs) {
174 job.assertStarted();
Ben Kwac06f3fd2015-04-24 15:35:25 -0700175 }
176 }
177
178 @Override
Steve McKaybbeba522016-01-13 17:17:39 -0800179 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 Kwac06f3fd2015-04-24 15:35:25 -0700184 }
Steve McKaybbeba522016-01-13 17:17:39 -0800185 }
Ben Kwaaac9e2e2015-04-16 18:14:35 -0700186}