[libcamera-devel,v4,12/13] libcamera: test: Add ControlList tests

Message ID 20190701201504.28487-13-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera Controls
Related show

Commit Message

Laurent Pinchart July 1, 2019, 8:15 p.m. UTC
From: Kieran Bingham <kieran.bingham@ideasonboard.com>

Add tests of the ControlList infrastructure and public API.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 test/controls/control_list.cpp | 213 +++++++++++++++++++++++++++++++++
 test/controls/meson.build      |   1 +
 2 files changed, 214 insertions(+)
 create mode 100644 test/controls/control_list.cpp

Comments

Niklas Söderlund July 2, 2019, 12:28 a.m. UTC | #1
Hi Kieran,

Thanks for your patch.

On 2019-07-01 23:15:03 +0300, Laurent Pinchart wrote:
> From: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> Add tests of the ControlList infrastructure and public API.
> 
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  test/controls/control_list.cpp | 213 +++++++++++++++++++++++++++++++++
>  test/controls/meson.build      |   1 +
>  2 files changed, 214 insertions(+)
>  create mode 100644 test/controls/control_list.cpp
> 
> diff --git a/test/controls/control_list.cpp b/test/controls/control_list.cpp
> new file mode 100644
> index 000000000000..c834edc352f5
> --- /dev/null
> +++ b/test/controls/control_list.cpp
> @@ -0,0 +1,213 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +/*
> + * Copyright (C) 2019, Google Inc.
> + *
> + * control_list.cpp - ControlList tests
> + */
> +
> +#include <iostream>
> +
> +#include <libcamera/camera.h>
> +#include <libcamera/camera_manager.h>
> +#include <libcamera/controls.h>
> +
> +#include "test.h"
> +
> +using namespace std;
> +using namespace libcamera;
> +
> +class ControlListTest : public Test
> +{
> +protected:
> +	int init()
> +	{
> +		cm_ = CameraManager::instance();
> +
> +		if (cm_->start()) {
> +			cout << "Failed to start camera manager" << endl;
> +			return TestFail;
> +		}
> +
> +		camera_ = cm_->get("VIMC Sensor B");
> +		if (!camera_) {
> +			cout << "Can not find VIMC camera" << endl;
> +			return TestSkip;
> +		}
> +
> +		return TestPass;
> +	}
> +
> +	int run()
> +	{
> +		ControlList list(camera_.get());
> +
> +		/* Test that the list is initially empty. */
> +		if (!list.empty()) {
> +			cout << "List should to be empty" << endl;
> +			return TestFail;
> +		}
> +
> +		if (list.size() != 0) {
> +			cout << "List should contain zero items" << endl;
> +			return TestFail;
> +		}
> +
> +		if (list.contains(Brightness)) {
> +			cout << "List should not contain Brightness control" << endl;
> +			return TestFail;
> +		}
> +
> +		unsigned int count = 0;
> +		for (auto iter = list.begin(); iter != list.end(); ++iter)
> +			count++;
> +
> +		if (count != 0) {
> +			cout << "List iteration should not produce any item" << endl;
> +			return TestFail;
> +		}
> +
> +		/*
> +		 * Set a control, and verify that the list now contains it, and
> +		 * nothing else.
> +		 */
> +		list[Brightness] = 255;
> +
> +		if (list.empty()) {
> +			cout << "List should not be empty" << endl;
> +			return TestFail;
> +		}
> +
> +		if (list.size() != 1) {
> +			cout << "List should contain one item" << endl;
> +			return TestFail;
> +		}
> +
> +		if (!list.contains(Brightness)) {
> +			cout << "List should contain Brightness control" << endl;
> +			return TestFail;
> +		}
> +
> +		count = 0;
> +		for (auto iter = list.begin(); iter != list.end(); ++iter)
> +			count++;
> +
> +		if (count != 1) {
> +			cout << "List iteration should produce one item" << endl;
> +			return TestFail;
> +		}
> +
> +		if (list[Brightness].getInt() != 255) {
> +			cout << "Incorrest Brightness control value" << endl;
> +			return TestFail;
> +		}
> +
> +		if (list.contains(Contrast)) {
> +			cout << "List should not contain Contract control" << endl;
> +			return TestFail;
> +		}
> +
> +		/*
> +		 * Set a second control through ControlInfo and retrieve it
> +		 * through both controlId and ControlInfo.
> +		 */
> +		const ControlInfoMap &controls = camera_->controls();
> +		const ControlInfo *brightness = &controls.find(Brightness)->second;
> +		const ControlInfo *contrast = &controls.find(Contrast)->second;
> +
> +		list[brightness] = 64;
> +		list[contrast] = 128;
> +
> +		if (!list.contains(Contrast) || !list.contains(contrast)) {
> +			cout << "List should contain Contrast control" << endl;
> +			return TestFail;
> +		}
> +
> +		/*
> +		 * Test control value retrieval and update through ControlInfo.
> +		 */
> +		if (list[brightness].getInt() != 64 ||
> +		    list[contrast].getInt() != 128) {
> +			cout << "Failed to retrieve control value" << endl;
> +			return TestFail;
> +		}
> +
> +		list[brightness] = 10;
> +		list[contrast] = 20;
> +
> +		if (list[brightness].getInt() != 10 ||
> +		    list[contrast].getInt() != 20) {
> +			cout << "Failed to update control value" << endl;
> +			return TestFail;
> +		}
> +
> +		/*
> +		 * Assert that the container has not grown with the control
> +		 * updated.
> +		 */
> +		if (list.size() != 2) {
> +			cout << "List should contain two elements" << endl;
> +			return TestFail;
> +		}
> +
> +		/*
> +		 * Test list merging. Create a new list, add two controls with
> +		 * one overlapping the existing list, merge the lists and clear
> +		 * the old list. Verify that the new list is empty and that the
> +		 * new list contains the expected items and values.
> +		 */
> +		ControlList newList(camera_.get());
> +
> +		newList[Brightness] = 128;
> +		newList[Saturation] = 255;
> +		newList.update(list);
> +
> +		list.clear();
> +
> +		if (list.size() != 0) {
> +			cout << "Old List should contain zero items" << endl;
> +			return TestFail;
> +		}
> +
> +		if (!list.empty()) {
> +			cout << "Old List should be empty" << endl;
> +			return TestFail;
> +		}
> +
> +		if (newList.size() != 3) {
> +			cout << "New list has incorrect size" << endl;
> +			return TestFail;
> +		}
> +
> +		if (!newList.contains(Brightness) ||
> +		    !newList.contains(Contrast) ||
> +		    !newList.contains(Saturation)) {
> +			cout << "New list contains incorrect items" << endl;
> +			return TestFail;
> +		}
> +
> +		if (newList[Brightness].getInt() != 10 ||
> +		    newList[Contrast].getInt() != 20 ||
> +		    newList[Saturation].getInt() != 255) {
> +			cout << "New list contains incorrect values" << endl;
> +			return TestFail;
> +		}
> +
> +		return TestPass;
> +	}
> +
> +	void cleanup()
> +	{
> +		if (camera_) {
> +			camera_->release();
> +			camera_.reset();
> +		}
> +
> +		cm_->stop();
> +	}
> +
> +private:
> +	CameraManager *cm_;
> +	std::shared_ptr<Camera> camera_;
> +};
> +
> +TEST_REGISTER(ControlListTest)
> diff --git a/test/controls/meson.build b/test/controls/meson.build
> index f8cda2877e73..f4fc7b947dd9 100644
> --- a/test/controls/meson.build
> +++ b/test/controls/meson.build
> @@ -1,5 +1,6 @@
>  control_tests = [
>      [ 'control_info',   'control_info.cpp' ],
> +    [ 'control_list',   'control_list.cpp' ],
>      [ 'control_value',  'control_value.cpp' ],
>  ]
>  
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/test/controls/control_list.cpp b/test/controls/control_list.cpp
new file mode 100644
index 000000000000..c834edc352f5
--- /dev/null
+++ b/test/controls/control_list.cpp
@@ -0,0 +1,213 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * control_list.cpp - ControlList tests
+ */
+
+#include <iostream>
+
+#include <libcamera/camera.h>
+#include <libcamera/camera_manager.h>
+#include <libcamera/controls.h>
+
+#include "test.h"
+
+using namespace std;
+using namespace libcamera;
+
+class ControlListTest : public Test
+{
+protected:
+	int init()
+	{
+		cm_ = CameraManager::instance();
+
+		if (cm_->start()) {
+			cout << "Failed to start camera manager" << endl;
+			return TestFail;
+		}
+
+		camera_ = cm_->get("VIMC Sensor B");
+		if (!camera_) {
+			cout << "Can not find VIMC camera" << endl;
+			return TestSkip;
+		}
+
+		return TestPass;
+	}
+
+	int run()
+	{
+		ControlList list(camera_.get());
+
+		/* Test that the list is initially empty. */
+		if (!list.empty()) {
+			cout << "List should to be empty" << endl;
+			return TestFail;
+		}
+
+		if (list.size() != 0) {
+			cout << "List should contain zero items" << endl;
+			return TestFail;
+		}
+
+		if (list.contains(Brightness)) {
+			cout << "List should not contain Brightness control" << endl;
+			return TestFail;
+		}
+
+		unsigned int count = 0;
+		for (auto iter = list.begin(); iter != list.end(); ++iter)
+			count++;
+
+		if (count != 0) {
+			cout << "List iteration should not produce any item" << endl;
+			return TestFail;
+		}
+
+		/*
+		 * Set a control, and verify that the list now contains it, and
+		 * nothing else.
+		 */
+		list[Brightness] = 255;
+
+		if (list.empty()) {
+			cout << "List should not be empty" << endl;
+			return TestFail;
+		}
+
+		if (list.size() != 1) {
+			cout << "List should contain one item" << endl;
+			return TestFail;
+		}
+
+		if (!list.contains(Brightness)) {
+			cout << "List should contain Brightness control" << endl;
+			return TestFail;
+		}
+
+		count = 0;
+		for (auto iter = list.begin(); iter != list.end(); ++iter)
+			count++;
+
+		if (count != 1) {
+			cout << "List iteration should produce one item" << endl;
+			return TestFail;
+		}
+
+		if (list[Brightness].getInt() != 255) {
+			cout << "Incorrest Brightness control value" << endl;
+			return TestFail;
+		}
+
+		if (list.contains(Contrast)) {
+			cout << "List should not contain Contract control" << endl;
+			return TestFail;
+		}
+
+		/*
+		 * Set a second control through ControlInfo and retrieve it
+		 * through both controlId and ControlInfo.
+		 */
+		const ControlInfoMap &controls = camera_->controls();
+		const ControlInfo *brightness = &controls.find(Brightness)->second;
+		const ControlInfo *contrast = &controls.find(Contrast)->second;
+
+		list[brightness] = 64;
+		list[contrast] = 128;
+
+		if (!list.contains(Contrast) || !list.contains(contrast)) {
+			cout << "List should contain Contrast control" << endl;
+			return TestFail;
+		}
+
+		/*
+		 * Test control value retrieval and update through ControlInfo.
+		 */
+		if (list[brightness].getInt() != 64 ||
+		    list[contrast].getInt() != 128) {
+			cout << "Failed to retrieve control value" << endl;
+			return TestFail;
+		}
+
+		list[brightness] = 10;
+		list[contrast] = 20;
+
+		if (list[brightness].getInt() != 10 ||
+		    list[contrast].getInt() != 20) {
+			cout << "Failed to update control value" << endl;
+			return TestFail;
+		}
+
+		/*
+		 * Assert that the container has not grown with the control
+		 * updated.
+		 */
+		if (list.size() != 2) {
+			cout << "List should contain two elements" << endl;
+			return TestFail;
+		}
+
+		/*
+		 * Test list merging. Create a new list, add two controls with
+		 * one overlapping the existing list, merge the lists and clear
+		 * the old list. Verify that the new list is empty and that the
+		 * new list contains the expected items and values.
+		 */
+		ControlList newList(camera_.get());
+
+		newList[Brightness] = 128;
+		newList[Saturation] = 255;
+		newList.update(list);
+
+		list.clear();
+
+		if (list.size() != 0) {
+			cout << "Old List should contain zero items" << endl;
+			return TestFail;
+		}
+
+		if (!list.empty()) {
+			cout << "Old List should be empty" << endl;
+			return TestFail;
+		}
+
+		if (newList.size() != 3) {
+			cout << "New list has incorrect size" << endl;
+			return TestFail;
+		}
+
+		if (!newList.contains(Brightness) ||
+		    !newList.contains(Contrast) ||
+		    !newList.contains(Saturation)) {
+			cout << "New list contains incorrect items" << endl;
+			return TestFail;
+		}
+
+		if (newList[Brightness].getInt() != 10 ||
+		    newList[Contrast].getInt() != 20 ||
+		    newList[Saturation].getInt() != 255) {
+			cout << "New list contains incorrect values" << endl;
+			return TestFail;
+		}
+
+		return TestPass;
+	}
+
+	void cleanup()
+	{
+		if (camera_) {
+			camera_->release();
+			camera_.reset();
+		}
+
+		cm_->stop();
+	}
+
+private:
+	CameraManager *cm_;
+	std::shared_ptr<Camera> camera_;
+};
+
+TEST_REGISTER(ControlListTest)
diff --git a/test/controls/meson.build b/test/controls/meson.build
index f8cda2877e73..f4fc7b947dd9 100644
--- a/test/controls/meson.build
+++ b/test/controls/meson.build
@@ -1,5 +1,6 @@ 
 control_tests = [
     [ 'control_info',   'control_info.cpp' ],
+    [ 'control_list',   'control_list.cpp' ],
     [ 'control_value',  'control_value.cpp' ],
 ]