blob: 80506ee6d328bc659c624b9ab4ddbbac43fc7801 [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
19import static junit.framework.Assert.assertEquals;
20
21import android.content.Context;
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import com.example.android.systemupdatersample.tests.R;
27
28import org.junit.Before;
29import org.junit.Rule;
30import org.junit.Test;
31import org.junit.rules.ExpectedException;
32import org.junit.runner.RunWith;
33
34import java.io.File;
35import java.nio.file.Files;
36import java.nio.file.Paths;
37
38/**
39 * Tests for {@link FileDownloader}
40 */
41@RunWith(AndroidJUnit4.class)
42@SmallTest
43public 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 Mukhamejanovf7a70382018-05-02 20:37:12 -070063 Files.deleteIfExists(packageFile.toPath());
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070064 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 Mukhamejanovf7a70382018-05-02 20:37:12 -070071 Files.deleteIfExists(outFile.toPath());
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070072 // download a chunk of ota.zip
73 FileDownloader downloader = new FileDownloader(url, 160, 8, outFile);
74 downloader.download();
75 String downloadedContent = String.join("\n", Files.readAllLines(outFile.toPath()));
Zhomart Mukhamejanovda7e2372018-05-01 12:50:55 -070076 // archive contains text files with uppercase filenames
Zhomart Mukhamejanov93535dd2018-04-26 16:10:12 -070077 assertEquals("CARE_MAP", downloadedContent);
78 }
79
80}