aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowPosixTest.java
blob: 437c313b7cae051db2b4220b087af5beced6d0b1 (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
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static com.google.common.truth.Truth.assertThat;

import android.system.StructStat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.File;
import java.io.FileOutputStream;
import java.time.Duration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;

/** Unit tests for ShadowPosix to check values returned from stat() call. */
@RunWith(AndroidJUnit4.class)
public final class ShadowPosixTest {
  private File file;
  private String path;

  @Before
  public void setUp() throws Exception {
    file = File.createTempFile("ShadowPosixTest", null);
    path = file.getAbsolutePath();
    try (FileOutputStream outputStream = new FileOutputStream(file)) {
      outputStream.write(1234);
    }
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void getStatAtLeastLollipop_returnCorrectMode() throws Exception {
    StructStat stat = (StructStat) ShadowPosix.stat(path);
    assertThat(stat.st_mode).isEqualTo(OsConstantsValues.S_IFREG_VALUE);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void getStatAtLeastLollipop_returnCorrectSize() throws Exception {
    StructStat stat = (StructStat) ShadowPosix.stat(path);
    assertThat(stat.st_size).isEqualTo(file.length());
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void getStatAtLeastLollipop_returnCorrectModifiedTime() throws Exception {
    StructStat stat = (StructStat) ShadowPosix.stat(path);
    assertThat(stat.st_mtime).isEqualTo(Duration.ofMillis(file.lastModified()).getSeconds());
  }

  @Test
  public void getStatBelowLollipop_returnCorrectMode() throws Exception {
    Object stat = ShadowPosix.stat(path);
    int mode = ReflectionHelpers.getField(stat, "st_mode");
    assertThat(mode).isEqualTo(OsConstantsValues.S_IFREG_VALUE);
  }

  @Test
  public void getStatBelowLollipop_returnCorrectSize() throws Exception {
    Object stat = ShadowPosix.stat(path);
    long size = ReflectionHelpers.getField(stat, "st_size");
    assertThat(size).isEqualTo(file.length());
  }

  @Test
  public void getStatBelowtLollipop_returnCorrectModifiedTime() throws Exception {
    Object stat = ShadowPosix.stat(path);
    long modifiedTime = ReflectionHelpers.getField(stat, "st_mtime");
    assertThat(modifiedTime).isEqualTo(Duration.ofMillis(file.lastModified()).getSeconds());
  }
}