aboutsummaryrefslogtreecommitdiff
path: root/test/navigator.c
blob: 55ab392f077df062f88a4fc310ee8a542fb75a46 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cmocka.h>
#include <errno.h>

#include "navigator.h"

#define FILENAME1 "example.file.1"
#define FILENAME2 "example.file.2"
#define FILENAME3 "example.file.3"
#define FILENAME4 "example.file.4"
#define FILENAME5 "example.file.5"
#define FILENAME6 "example.file.6"

static void test_navigator_add_remove(void **state)
{
  (void)state;
  struct imv_navigator nav;
  imv_navigator_init(&nav);

  /* Check poll_changed */
  assert_false(imv_navigator_poll_changed(&nav, 0));

  /* Add 6 paths, one non-existant should fail */
  assert_false(imv_navigator_add(&nav, FILENAME1, 0));
  assert_false(imv_navigator_add(&nav, FILENAME2, 0));
  assert_false(imv_navigator_add(&nav, FILENAME3, 0));
  assert_false(imv_navigator_add(&nav, FILENAME4, 0));
  assert_false(imv_navigator_add(&nav, FILENAME5, 0));
  assert_false(imv_navigator_add(&nav, FILENAME6, 0));
  assert_int_equal(nav.num_paths, 6);

  /* Check poll_changed */
  assert_true(imv_navigator_poll_changed(&nav, 0));

  /* Make sure current selection  is #1 */
  assert_string_equal(imv_navigator_selection(&nav), FILENAME1);

  /* Move right and remove current file (#2); should get to #3 */
  imv_navigator_select_rel(&nav, 1);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME2);
  imv_navigator_remove(&nav, FILENAME2);
  assert_int_equal(nav.num_paths, 5);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME3);

  /* Move left and remove current file (#1); should get to #6 */
  imv_navigator_select_rel(&nav, -1);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME1);
  imv_navigator_remove(&nav, FILENAME1);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME6);

  /* Move left, right, remove current file (#6); should get to #3 */
  imv_navigator_select_rel(&nav, -1);
  imv_navigator_select_rel(&nav, 1);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME6);
  imv_navigator_remove(&nav, FILENAME6);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME3);

  /* Remove #4; should not move */
  imv_navigator_remove(&nav, FILENAME4);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME3);

  /* Verify that #4 is removed by moving left; should get to #5 */
  imv_navigator_select_rel(&nav, 1);
  assert_string_equal(imv_navigator_selection(&nav), FILENAME5);

  imv_navigator_destroy(&nav);
}

static void test_navigator_file_changed(void **state)
{
  int fd;
  struct imv_navigator nav;
  struct timespec times[2] = { {0, 0}, {0, 0} };

  (void)state;
  imv_navigator_init(&nav);

  fd = open(FILENAME1, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    imv_navigator_destroy(&nav);
    (void)unlink(FILENAME1);
    skip();
  }
  assert_false(futimens(fd, times) == -1);

  assert_false(imv_navigator_add(&nav, FILENAME1, 0));
  assert_true(imv_navigator_poll_changed(&nav, 0));
  assert_false(imv_navigator_poll_changed(&nav, 0));

  assert_false(sleep(1));

  fd = open(FILENAME1, O_RDWR);
  assert_false(fd == -1);

  times[0].tv_nsec = UTIME_NOW;
  times[0].tv_sec = UTIME_NOW;
  times[1].tv_nsec = UTIME_NOW;
  times[1].tv_sec = UTIME_NOW;
  assert_false(futimens(fd, times) == -1);

  assert_true(imv_navigator_poll_changed(&nav, 0));

  (void)close(fd);
  (void)unlink(FILENAME1);
  imv_navigator_destroy(&nav);
}

int main(void)
{
  const struct CMUnitTest tests[] = {
    cmocka_unit_test(test_navigator_add_remove),
    cmocka_unit_test(test_navigator_file_changed),
  };

  return cmocka_run_group_tests(tests, NULL, NULL);
}


/* vim:set ts=2 sts=2 sw=2 et: */