summaryrefslogtreecommitdiffstats
path: root/tests/camera2/TestSettings.cpp
blob: f07adc8c5d4e8ddc12bc29df0799f10467382d4e (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstdlib>
#include <getopt.h>
#include <cstring>
#include <iostream>

#include "TestSettings.h"

#include "TestForkerEventListener.h"

namespace android {
namespace camera2 {
namespace tests {

bool TestSettings::mForkingDisabled     = false;
int  TestSettings::mDeviceId            = 0;
char* const* TestSettings::mArgv;

// --forking-disabled, false by default
bool TestSettings::ForkingDisabled() {
    return mForkingDisabled;
}

// reverse of --forking-disabled (not a flag), true by default
bool TestSettings::ForkingEnabled() {
    return !ForkingDisabled();
}

// --device-id, 0 by default
int TestSettings::DeviceId() {
    return mDeviceId;
}

// returns false if usage should be printed and we should exit early
bool TestSettings::ParseArgs(int argc, char* const argv[])
{
    {
        char *env = getenv("CAMERA2_TEST_FORKING_DISABLED");
        if (env) {
            mForkingDisabled = atoi(env);
        }

        env = getenv("CAMERA2_TEST_DEVICE_ID");
        if (env) {
            mDeviceId = atoi(env);
        }
    }

    bool printHelp = false;
    bool unknownArgs = false;

    opterr = 0; // do not print errors for unknown arguments
    while (true) {
        int c;
        int option_index = 0;

        static struct option long_options[] = {
            /* name              has_arg          flag val */
            {"forking-disabled", optional_argument, 0,  0  },
            {"device-id",        required_argument, 0,  0  },
            {"help",             no_argument,       0, 'h' },
            {0,                  0,                 0,  0  }
        };

        // Note: '+' in optstring means do not mutate argv
        c = getopt_long(argc, argv, "+h", long_options, &option_index);

        if (c == -1) { // All arguments exhausted
            break;
        }
        if (c == '?') { // Argument not in option lists
            const char *arg = argv[optind-1];
            // Anything beginning with gtest_ will get handled by gtest
            if (strstr(arg, "--gtest_") != arg) {
                std::cerr << "Unknown argument: " << arg << std::endl;
                unknownArgs = true;
            }
            continue;
        }

        switch (c) {
        case 0: // long option
            switch (option_index) {
            case 0: {
                const char *arg = optarg ?: "1";
                mForkingDisabled = atoi(arg);
                break;
            }
            case 1: {
                mDeviceId = atoi(optarg);
                break;
            }
            default:
                std::cerr << "Unknown long option: " << option_index << std::endl;
                break;
            }
            break; // case 0
        case 'h': // help
            printHelp = true;
            break;
        default: // case '?'
            std::cerr << "Unknown option: " << optarg << std::endl;
        }
    }

    if (unknownArgs) {
        std::cerr << std::endl;
    }

    mArgv = argv;

    if (printHelp || unknownArgs) {
        return false;
    }

    std::cerr << "Forking Disabled: "
              << (mForkingDisabled ? "yes" : "no") << std::endl;

    std::cerr << "Device ID: " << mDeviceId << std::endl;

    return true;
}

// print usage/help list of commands (non-gtest)
void TestSettings::PrintUsage() {
    std::cerr << "Usage: " << mArgv[0] << " [OPTIONS]" << std::endl;
    std::cerr << std::endl;

    std::cerr << "Main modes of operation:"
              << std::endl;
    std::cerr << "   --forking-disabled[=1]  don't fork process before "
              << std::endl
              << "                           running a new test."
              << std::endl
              << "                           (default enabled)"
              << std::endl;
    std::cerr << "   --device-id=ID          specify a different camera ID"
              << std::endl
              << "                           (default 0)"
              << std::endl;

    std::cerr << "   -h, --help              print this help listing"
              << std::endl;


    std::cerr << std::endl;
}

}
}
}