• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.textclassifier.downloader;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 import android.os.IBinder;
22 import com.android.textclassifier.common.base.TcLog;
23 import java.io.File;
24 import java.nio.file.Files;
25 import java.util.concurrent.CountDownLatch;
26 import javax.annotation.Nullable;
27 
28 // TODO(licha): Find another way to test the service. (E.g. CtsTextClassifierService.java)
29 /** Test Service of IModelDownloaderService. */
30 public final class TestModelDownloaderService extends Service {
31   private static final String TAG = "TestModelDownloaderService";
32 
33   public static final String GOOD_URI = "good_uri";
34   public static final String BAD_URI = "bad_uri";
35   public static final long BYTES_WRITTEN = 1L;
36   public static final int DOWNLOADER_LIB_ERROR_CODE = 500;
37   public static final String ERROR_MSG = "not good uri";
38 
39   public enum DownloadResult {
40     SUCCEEDED,
41     FAILED,
42     DO_NOTHING
43   }
44 
45   // Obviously this does not work when considering concurrency, but probably fine for test purpose
46   private static boolean boundBefore = false;
47   private static boolean boundNow = false;
48   private static CountDownLatch onBindInvokedLatch = new CountDownLatch(1);
49   private static CountDownLatch onUnbindInvokedLatch = new CountDownLatch(1);
50 
51   private static boolean bindSucceed = false;
52   private static String expectedUrl = null;
53   private static DownloadResult downloadResult = DownloadResult.SUCCEEDED;
54   private static byte[] fileContent = null;
55 
hasEverBeenBound()56   public static boolean hasEverBeenBound() {
57     return boundBefore;
58   }
59 
isBound()60   public static boolean isBound() {
61     return boundNow;
62   }
63 
getOnBindInvokedLatch()64   public static CountDownLatch getOnBindInvokedLatch() {
65     return onBindInvokedLatch;
66   }
67 
getOnUnbindInvokedLatch()68   public static CountDownLatch getOnUnbindInvokedLatch() {
69     return onUnbindInvokedLatch;
70   }
71 
setBindSucceed(boolean bindSucceed)72   public static void setBindSucceed(boolean bindSucceed) {
73     TestModelDownloaderService.bindSucceed = bindSucceed;
74   }
75 
setDownloadResult( String url, DownloadResult result, @Nullable byte[] fileContent)76   public static void setDownloadResult(
77       String url, DownloadResult result, @Nullable byte[] fileContent) {
78     TestModelDownloaderService.expectedUrl = url;
79     TestModelDownloaderService.downloadResult = result;
80     TestModelDownloaderService.fileContent = fileContent;
81   }
82 
reset()83   public static void reset() {
84     boundBefore = false;
85     boundNow = false;
86     onBindInvokedLatch = new CountDownLatch(1);
87     onUnbindInvokedLatch = new CountDownLatch(1);
88     bindSucceed = false;
89   }
90 
91   @Override
onBind(Intent intent)92   public IBinder onBind(Intent intent) {
93     try {
94       if (bindSucceed) {
95         boundBefore = true;
96         boundNow = true;
97         return new TestModelDownloaderServiceImpl();
98       } else {
99         return null;
100       }
101     } finally {
102       onBindInvokedLatch.countDown();
103     }
104   }
105 
106   @Override
onUnbind(Intent intent)107   public boolean onUnbind(Intent intent) {
108     try {
109       boundNow = false;
110       return false;
111     } finally {
112       onUnbindInvokedLatch.countDown();
113     }
114   }
115 
116   private static final class TestModelDownloaderServiceImpl extends IModelDownloaderService.Stub {
117     @Override
download(String url, String targetFilePath, IModelDownloaderCallback callback)118     public void download(String url, String targetFilePath, IModelDownloaderCallback callback) {
119       if (expectedUrl == null || !expectedUrl.equals(url)) {
120         throw new IllegalStateException("url does not match");
121       }
122       TcLog.d(TAG, String.format("Test Request: %s, %s, %s", url, targetFilePath, downloadResult));
123       try {
124         switch (downloadResult) {
125           case SUCCEEDED:
126             File targetFile = new File(targetFilePath);
127             targetFile.createNewFile();
128             Files.write(targetFile.toPath(), fileContent);
129             callback.onSuccess(BYTES_WRITTEN);
130             break;
131           case FAILED:
132             callback.onFailure(DOWNLOADER_LIB_ERROR_CODE, ERROR_MSG);
133             break;
134           case DO_NOTHING:
135             // Do nothing
136         }
137       } catch (Throwable t) {
138         // The test would timeout if failing to get the callback result
139       }
140     }
141   }
142 }
143