aboutsummaryrefslogtreecommitdiff
path: root/library/src/androidTest/java/com/bumptech/glide/tests/Util.java
blob: 73220f4518bdcd375490ff28792a92a20deaf5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.bumptech.glide.tests;

import android.os.Build;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.Robolectric;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static org.junit.Assert.assertEquals;

public class Util {

    public static String getExpectedClassId(Class clazz) {
        return clazz.getSimpleName() + "." + clazz.getPackage().getName();
    }

    public static void assertClassHasValidId(Class clazz, String id) {
        assertEquals(getExpectedClassId(clazz), id);
    }

    public static boolean isWindows() {
        return System.getProperty("os.name").startsWith("Windows");
    }

    public static void writeFile(File file, byte[] data) throws IOException {
        OutputStream out = new FileOutputStream(file);
        try {
            out.write(data);
            out.flush();
            out.close();
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                // Do nothing.
            }
        }
    }

    public static byte[] readFile(File file, int expectedLength) throws IOException {
        InputStream is = new FileInputStream(file);
        byte[] result = new byte[expectedLength];
        try {
            assertEquals(expectedLength, is.read(result));
            assertEquals(-1, is.read());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                // Do nothing.
            }
        }
        return result;
    }

    public static <T> Answer<T> arg(final int argumentIndex) {
        return new Answer<T>() {
            @SuppressWarnings("unchecked")
            @Override
            public T answer(InvocationOnMock invocation) {
                if (argumentIndex >= invocation.getArguments().length) {
                    throw new IllegalArgumentException("Cannot invoke argument " + argumentIndex
                            + " greater than arguments length " + invocation.getArguments().length);
                }
                return (T) invocation.getArguments()[argumentIndex];
            }
        };
    }

    public static void setSdkVersionInt(int version) {
        Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT", version);
    }

}