summaryrefslogtreecommitdiff
path: root/test/camera-manager.cpp
blob: 9e9c494af21c5a15b236247eca8273fc02990149 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * libcamera Camera Manager API tests
 */

#include <iostream>
#include <memory>

#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>

#include "test.h"

using namespace libcamera;
using namespace std;

class CameraManagerTest : public Test
{
protected:
	int validate()
	{
		std::shared_ptr<Camera> camera;

		if (cm_->start()) {
			cerr << "Failed to start camera manager" << endl;
			return TestFail;
		}

		if (cm_->cameras().size() <= 0) {
			cerr << "No cameras available" << endl;
			return TestFail;
		}

		camera = cm_->cameras()[0];
		if (!camera) {
			cerr << "Can not obtain a camera at index 0" << endl;
			return TestFail;
		}

		/* Store the camera id that we get */
		cameraId_ = camera->id();

		return TestPass;
	}

	int run()
	{
		std::string firstCamera;

		/* Construct and validate the CameraManager */
		cm_ = new CameraManager();
		if (validate()) {
			cerr << "Failed first construction" << endl;
			return TestFail;
		}

		/* Get camera ID stored by validate */
		firstCamera = cameraId_;

		/* Now stop everything and reconstruct the CameraManager */
		cm_->stop();
		delete cm_;

		/* Restart and assert we can still get a camera */
		cm_ = new CameraManager();
		if (validate()) {
			cerr << "Failed after re-construction" << endl;
			return TestFail;
		}

		if (firstCamera != cameraId_) {
			cerr << "Expected to get the same camera after re-construction" << endl;
			return TestFail;
		}

		/* Test stop and start (without re-create) */
		cm_->stop();

		/* validate will call start() */
		if (validate()) {
			cerr << "Failed after re-starting CameraManager" << endl;
			return TestFail;
		}

		/*
		 * Creating a second camera manager is not permitted
		 *
		 * This will fail with a FATAL in constructing a second IPA
		 * Manager, even though we also have a FATAL in the
		 * CameraManager construction, but the CameraManager tries
		 * to construct an IPA manager, which fails before the
		 * CameraManager executes any of it's constructor.
		 */
		//CameraManager *cm2 = new CameraManager();

		return TestPass;
	}

private:
	CameraManager *cm_;
	std::string cameraId_;
};

TEST_REGISTER(CameraManagerTest)