summaryrefslogtreecommitdiff
path: root/src/xorg-gtest_main.cpp
blob: 5e5a7486fb77fb99f97301871b4ab222bfbec29c (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
125
126
127
128
129
130
131
/*******************************************************************************
 *
 * X testing environment - Google Test environment feat. dummy x server
 *
 * Copyright (C) 2011, 2012 Canonical Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 ******************************************************************************/

#include <getopt.h>

#include <gtest/gtest.h>

#include "xorg/gtest/environment.h"
#include "defines.h"

namespace {

int help = false;
int no_dummy_server = false;
int xorg_conf_specified = false;
int xorg_display_specified = false;
int xorg_logfile_specified = false;
int server_specified = false;

const struct option longopts[] = {
  { "help", no_argument, &help, true, },
  { "no-dummy-server", no_argument, &no_dummy_server, true, },
  { "xorg-conf", required_argument, &xorg_conf_specified, true, },
  { "xorg-display", required_argument, &xorg_display_specified, true, },
  { "xorg-logfile", required_argument, &xorg_logfile_specified, true, },
  { "server", required_argument, &server_specified, true, },
  { NULL, 0, NULL, 0 }
};

} // namespace

int main(int argc, char *argv[]) {
  std::string xorg_conf_path;
  std::string xorg_log_file_path;
  int xorg_display = -1;
  std::string server;

  testing::InitGoogleTest(&argc, argv);

  /* Reset getopt state */
  optind = 0;

  while (true) {
    int ret;
    int index;
    ret = getopt_long(argc, argv, "", longopts, &index);

    if (ret == -1)
      break;

    if (ret == '?')
      exit(-1);

    switch (index) {
      case 2:
        xorg_conf_path = optarg;
        break;

      case 3:
        xorg_display = atoi(optarg);
        break;

      case 4:
        xorg_log_file_path = optarg;
        break;

      case 5:
        server = optarg;
        break;

      default:
        break;
    }
  }

  if (help) {
    std::cout << "\nAdditional options:\n";
    std::cout << "    --no-dummy-server: Use the currently running X server "
        "for testing\n";
    std::cout << "    --xorg-conf: Path to xorg dummy configuration file\n";
    std::cout << "    --server: Path to X server executable\n";
    std::cout << "    --xorg-display: xorg dummy display port\n";
    std::cout << "    --xorg-logfile: xorg logfile filename. See -logfile in \"man Xorg\".\n"
                 "                    Its default value is "DEFAULT_XORG_LOGFILE".\n";
    exit(-1);
  }

  if (!no_dummy_server) {
    xorg::testing::Environment* environment = new xorg::testing::Environment;

    if (xorg_conf_specified)
      environment->set_conf_file(xorg_conf_path);

    if (server_specified)
      environment->set_server(server);

    if (xorg_display_specified)
      environment->set_display(xorg_display);

    if (xorg_logfile_specified)
        environment->set_log_file(xorg_log_file_path);

    testing::AddGlobalTestEnvironment(environment);
  }

  return RUN_ALL_TESTS();
}