13564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay/* 23564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * Copyright (C) 2016 The Android Open Source Project 33564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * 43564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * Licensed under the Apache License, Version 2.0 (the "License"); 53564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * you may not use this file except in compliance with the License. 63564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * You may obtain a copy of the License at 73564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * 83564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * http://www.apache.org/licenses/LICENSE-2.0 93564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * 103564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * Unless required by applicable law or agreed to in writing, software 113564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * distributed under the License is distributed on an "AS IS" BASIS, 123564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * See the License for the specific language governing permissions and 143564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay * limitations under the License. 153564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay */ 163564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 173564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKaypackage com.android.documentsui.services; 183564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 193564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport static com.android.documentsui.StubProvider.ROOT_0_ID; 203564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport static com.android.documentsui.StubProvider.ROOT_1_ID; 213564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 223564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.content.ContentProviderClient; 233564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.content.ContentResolver; 243564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.content.Context; 253564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.net.Uri; 263564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.os.RemoteException; 273564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.provider.DocumentsContract; 283564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.test.AndroidTestCase; 293564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport android.test.suitebuilder.annotation.MediumTest; 303564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 313564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport com.android.documentsui.DocumentsProviderHelper; 323564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport com.android.documentsui.StubProvider; 333564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport com.android.documentsui.model.DocumentInfo; 343564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport com.android.documentsui.model.DocumentStack; 353564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport com.android.documentsui.model.RootInfo; 363564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 373564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport com.google.common.collect.Lists; 383564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 393564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKayimport java.util.List; 403564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 413564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay@MediumTest 423564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKaypublic abstract class AbstractJobTest<T extends Job> extends AndroidTestCase { 433564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 443564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay static String AUTHORITY = StubProvider.DEFAULT_AUTHORITY; 453564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay static final byte[] HAM_BYTES = "ham and cheese".getBytes(); 463564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay static final byte[] FRUITY_BYTES = "I love fruit cakes!".getBytes(); 473564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 483564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay Context mContext; 493564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay ContentResolver mResolver; 503564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay ContentProviderClient mClient; 513564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay DocumentsProviderHelper mDocs; 523564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay TestJobListener mJobListener; 533564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay RootInfo mSrcRoot; 543564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay RootInfo mDestRoot; 553564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 563564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay @Override 573564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay protected void setUp() throws Exception { 583564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay super.setUp(); 593564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 603564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mJobListener = new TestJobListener(); 613564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 623564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay // NOTE: Must be the "target" context, else security checks in content provider will fail. 633564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mContext = getContext(); 643564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mResolver = mContext.getContentResolver(); 653564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 663564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mClient = mResolver.acquireContentProviderClient(AUTHORITY); 673564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mDocs = new DocumentsProviderHelper(AUTHORITY, mClient); 683564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 693564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay initTestFiles(); 703564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay } 713564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 723564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay @Override 733564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay protected void tearDown() throws Exception { 743564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay resetStorage(); 753564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mClient.release(); 763564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay super.tearDown(); 773564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay } 783564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 793564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay private void resetStorage() throws RemoteException { 803564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mClient.call("clear", null, null); 813564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay } 823564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 833564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay private void initTestFiles() throws RemoteException { 843564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mSrcRoot = mDocs.getRoot(ROOT_0_ID); 853564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay mDestRoot = mDocs.getRoot(ROOT_1_ID); 863564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay } 873564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 88b8436af1b84a31b2ba3d50510ebf10bb721f2b6cTomasz Mikolajewski final T createJob(List<Uri> srcs, Uri srcParent, Uri destination) throws Exception { 893564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay DocumentStack stack = new DocumentStack(); 903564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay stack.push(DocumentInfo.fromUri(mResolver, destination)); 913564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 923564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay List<DocumentInfo> srcDocs = Lists.newArrayList(); 933564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay for (Uri src : srcs) { 943564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay srcDocs.add(DocumentInfo.fromUri(mResolver, src)); 953564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay } 963564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 97b8436af1b84a31b2ba3d50510ebf10bb721f2b6cTomasz Mikolajewski return createJob(srcDocs, DocumentInfo.fromUri(mResolver, srcParent), stack); 983564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay } 993564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay 100b8436af1b84a31b2ba3d50510ebf10bb721f2b6cTomasz Mikolajewski abstract T createJob(List<DocumentInfo> srcs, DocumentInfo srcParent, DocumentStack destination) 101b8436af1b84a31b2ba3d50510ebf10bb721f2b6cTomasz Mikolajewski throws Exception; 1023564543e1ece2211a2d5b729fbbf8dd7ac94944cSteve McKay} 103