Zhomart Mukhamejanov | 93535dd | 2018-04-26 16:10:12 -0700 | [diff] [blame] | 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.example.android.systemupdatersample.util; |
| 18 | |
Zhomart Mukhamejanov | bb8a215 | 2018-05-10 12:19:16 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
Zhomart Mukhamejanov | 93535dd | 2018-04-26 16:10:12 -0700 | [diff] [blame] | 20 | |
| 21 | import android.content.Context; |
| 22 | import android.support.test.InstrumentationRegistry; |
| 23 | import android.support.test.filters.SmallTest; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | |
| 26 | import com.example.android.systemupdatersample.tests.R; |
| 27 | |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Rule; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.rules.ExpectedException; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | import java.io.File; |
| 35 | import java.nio.file.Files; |
| 36 | import java.nio.file.Paths; |
| 37 | |
| 38 | /** |
| 39 | * Tests for {@link FileDownloader} |
| 40 | */ |
| 41 | @RunWith(AndroidJUnit4.class) |
| 42 | @SmallTest |
| 43 | public class FileDownloaderTest { |
| 44 | |
| 45 | @Rule |
| 46 | public final ExpectedException thrown = ExpectedException.none(); |
| 47 | |
| 48 | private Context mTestContext; |
| 49 | private Context mTargetContext; |
| 50 | |
| 51 | @Before |
| 52 | public void setUp() { |
| 53 | mTestContext = InstrumentationRegistry.getContext(); |
| 54 | mTargetContext = InstrumentationRegistry.getTargetContext(); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void download_downloadsChunkOfZip() throws Exception { |
| 59 | // Prepare the target file |
| 60 | File packageFile = Paths |
| 61 | .get(mTargetContext.getCacheDir().getAbsolutePath(), "ota.zip") |
| 62 | .toFile(); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 63 | Files.deleteIfExists(packageFile.toPath()); |
Zhomart Mukhamejanov | 93535dd | 2018-04-26 16:10:12 -0700 | [diff] [blame] | 64 | Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package), |
| 65 | packageFile.toPath()); |
| 66 | String url = "file://" + packageFile.getAbsolutePath(); |
| 67 | // prepare where to download |
| 68 | File outFile = Paths |
| 69 | .get(mTargetContext.getCacheDir().getAbsolutePath(), "care_map.txt") |
| 70 | .toFile(); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 71 | Files.deleteIfExists(outFile.toPath()); |
Zhomart Mukhamejanov | 93535dd | 2018-04-26 16:10:12 -0700 | [diff] [blame] | 72 | // download a chunk of ota.zip |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 73 | FileDownloader downloader = new FileDownloader(url, 1674, 12, outFile); |
Zhomart Mukhamejanov | 93535dd | 2018-04-26 16:10:12 -0700 | [diff] [blame] | 74 | downloader.download(); |
| 75 | String downloadedContent = String.join("\n", Files.readAllLines(outFile.toPath())); |
Zhomart Mukhamejanov | da7e237 | 2018-05-01 12:50:55 -0700 | [diff] [blame] | 76 | // archive contains text files with uppercase filenames |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 77 | assertEquals("CARE_MAP-TXT", downloadedContent); |
Zhomart Mukhamejanov | 93535dd | 2018-04-26 16:10:12 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } |