blob: ede24577ed4074640e8dc03f9962c3cd97257e8e [file] [log] [blame]
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -07001/*
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
17package com.example.android.systemupdatersample.util;
18
Zhomart Mukhamejanovbb8a2152018-05-10 12:19:16 -070019import static org.junit.Assert.assertEquals;
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070020
21import android.content.Context;
koushik panugantid5c7fb52018-12-12 10:39:44 -080022
23import androidx.test.InstrumentationRegistry;
24import androidx.test.filters.SmallTest;
25import androidx.test.runner.AndroidJUnit4;
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070026
27import com.example.android.systemupdatersample.tests.R;
28
29import org.junit.Before;
30import org.junit.Rule;
31import org.junit.Test;
32import org.junit.rules.ExpectedException;
33import org.junit.runner.RunWith;
34
35import java.io.File;
36import java.nio.file.Files;
37import java.nio.file.Paths;
38
39/**
40 * Tests for {@link FileDownloader}
41 */
42@RunWith(AndroidJUnit4.class)
43@SmallTest
44public class FileDownloaderTest {
45
46 @Rule
47 public final ExpectedException thrown = ExpectedException.none();
48
49 private Context mTestContext;
50 private Context mTargetContext;
51
52 @Before
53 public void setUp() {
54 mTestContext = InstrumentationRegistry.getContext();
55 mTargetContext = InstrumentationRegistry.getTargetContext();
56 }
57
58 @Test
59 public void download_downloadsChunkOfZip() throws Exception {
60 // Prepare the target file
61 File packageFile = Paths
62 .get(mTargetContext.getCacheDir().getAbsolutePath(), "ota.zip")
63 .toFile();
Zhomart Mukhamejanovf7a70382018-05-02 20:37:12 -070064 Files.deleteIfExists(packageFile.toPath());
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070065 Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package),
66 packageFile.toPath());
67 String url = "file://" + packageFile.getAbsolutePath();
68 // prepare where to download
69 File outFile = Paths
70 .get(mTargetContext.getCacheDir().getAbsolutePath(), "care_map.txt")
71 .toFile();
Zhomart Mukhamejanovf7a70382018-05-02 20:37:12 -070072 Files.deleteIfExists(outFile.toPath());
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070073 // download a chunk of ota.zip
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070074 FileDownloader downloader = new FileDownloader(url, 1674, 12, outFile);
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070075 downloader.download();
76 String downloadedContent = String.join("\n", Files.readAllLines(outFile.toPath()));
Zhomart Mukhamejanovda7e2372018-05-01 12:50:55 -070077 // archive contains text files with uppercase filenames
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070078 assertEquals("CARE_MAP-TXT", downloadedContent);
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070079 }
80
81}