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
|
/*
* libssc: Library to expose Qualcomm Sensor Core sensors
* Copyright (C) 2022-2026 Dylan Van Assche
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <locale.h>
#include "libssc-client-private.h"
#include "libssc-sensor.h"
#include "libssc-sensor-proximity.h"
#include "libssc-sensor-light.h"
#include "libssc-sensor-accelerometer.h"
#include "libssc-sensor-magnetometer.h"
#include "libssc-sensor-compass.h"
#define TIMEOUT 2
typedef struct {
SSCSensor *sensor;
GArray *measurements;
GMainLoop *loop;
} SensorData;
static void
sensor_unavailable_open_ready (SSCSensor *self, GAsyncResult *result, gpointer user_data)
{
GError *error = NULL;
GMainLoop *loop = user_data;
gboolean success = FALSE;
success = ssc_sensor_open_finish (self, result, &error);
g_assert_false (success);
g_main_loop_quit (loop);
}
static void
sensor_unavailable_ready (SSCClient *self, GAsyncResult *result, gpointer user_data)
{
GError *error = NULL;
SSCSensor *sensor = NULL;
sensor = ssc_sensor_new_finish (result, &error);
g_assert_no_error (error);
ssc_sensor_open (sensor, NULL, (GAsyncReadyCallback) sensor_unavailable_open_ready, user_data);
}
static void
test_libssc_sensor_unavailable (void)
{
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
/* Create a sensor which is unavailable according to attribute */
ssc_sensor_new ("unavailable", NULL, (GAsyncReadyCallback) sensor_unavailable_ready, loop);
/* Run main loop to process signals and timers */
g_main_loop_run (loop);
}
static void
sensor_unsupported_ready (SSCClient *self, GAsyncResult *result, gpointer user_data)
{
GMainLoop *loop = user_data;
GError *error = NULL;
SSCSensor *sensor = NULL;
sensor = ssc_sensor_new_finish (result, &error);
g_assert_true (sensor == NULL);
g_main_loop_quit (loop);
}
static void
test_libssc_sensor_unsupported (void)
{
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
/* Discover a sensor which is unsupported by DSP */
ssc_sensor_new ("unsupported", NULL, (GAsyncReadyCallback) sensor_unsupported_ready, loop);
/* Run main loop to process signals and timers */
g_main_loop_run (loop);
}
static void
sensor_no_sample_rate_open_ready (SSCSensor *self, GAsyncResult *result, gpointer user_data)
{
GError *error = NULL;
GMainLoop *loop = user_data;
gboolean success = FALSE;
success = ssc_sensor_open_finish (self, result, &error);
g_assert_false (success);
g_main_loop_quit (loop);
}
static void
sensor_no_sample_rate_ready (SSCClient *self, GAsyncResult *result, gpointer user_data)
{
GError *error = NULL;
SSCSensor *sensor = NULL;
sensor = ssc_sensor_new_finish (result, &error);
g_assert_no_error (error);
ssc_sensor_open (sensor, NULL, (GAsyncReadyCallback) sensor_no_sample_rate_open_ready, user_data);
}
static void
test_libssc_sensor_no_sample_rate (void)
{
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
/* Discover a sensor in continuous mode with missing required sample rate */
ssc_sensor_new ("no-sample-rate", NULL, (GAsyncReadyCallback) sensor_no_sample_rate_ready, loop);
/* Run main loop to process signals and timers */
g_main_loop_run (loop);
}
int main (int argc, char *argv[])
{
GLogLevelFlags mask;
setlocale (LC_ALL, "");
/* Initialize test framework */
g_test_init (&argc, &argv, NULL);
/* Allow warnings */
mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
mask = (GLogLevelFlags) (mask & (~G_LOG_LEVEL_WARNING));
g_log_set_always_fatal ((GLogLevelFlags) mask);
/* Tests */
g_test_add_func("/libssc/sensor/unsupported", test_libssc_sensor_unsupported);
g_test_add_func("/libssc/sensor/unavailable", test_libssc_sensor_unavailable);
g_test_add_func("/libssc/sensor/no-sample-rate", test_libssc_sensor_no_sample_rate);
/* Execute tests */
return g_test_run();
}
|