Merge lp:~pitti/qtubuntu-sensors/crash-without-hw into lp:qtubuntu-sensors

Proposed by Martin Pitt
Status: Merged
Approved by: Sergio Schvezov
Approved revision: 52
Merged at revision: 50
Proposed branch: lp:~pitti/qtubuntu-sensors/crash-without-hw
Merge into: lp:qtubuntu-sensors
Diff against target: 246 lines (+127/-11)
4 files modified
debian/control (+1/-1)
plugins/sensors/core_shared_accelerometer.cpp (+10/-3)
plugins/sensors/core_shared_accelerometer.h (+1/-0)
tests/test_sensor_api.cpp (+115/-7)
To merge this branch: bzr merge lp:~pitti/qtubuntu-sensors/crash-without-hw
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+203931@code.launchpad.net

Commit message

Fix crash if there are no available sensors.

Description of the change

This provides the qtubuntu-sensors part of the crash fix for LP#1272294. See
the commit logs for details.

Note that this depends on ua_sensors_accelerometer_new() actually returning
NULL in the "no sensors" case instead of crashing. That part needs to get fixed
in platform-api, hence blocking this MP until that lands.

To post a comment you must log in.
Revision history for this message
Martin Pitt (pitti) wrote :

Now that the platform-api fix has finally landed, I bumped the build dependency accordingly (otherwise this new test would just crash), it's now ready to land.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Sergio Schvezov (sergiusens) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'debian/control'
2--- debian/control 2014-01-15 09:48:40 +0000
3+++ debian/control 2014-02-11 20:16:48 +0000
4@@ -5,7 +5,7 @@
5 Build-Depends: debhelper (>= 9),
6 cmake,
7 libgtest-dev,
8- libplatform-api1-dev,
9+ libplatform-api1-dev (>= 0.20+14.04.20140211),
10 libprocess-cpp-dev (>= 0.0.1+14.04.20131212),
11 libubuntu-application-api-test1,
12 pkg-config,
13
14=== modified file 'plugins/sensors/core_shared_accelerometer.cpp'
15--- plugins/sensors/core_shared_accelerometer.cpp 2014-01-08 11:32:40 +0000
16+++ plugins/sensors/core_shared_accelerometer.cpp 2014-02-11 20:16:48 +0000
17@@ -32,10 +32,13 @@
18 m_minDelay(-1),
19 m_minValue(0.0),
20 m_maxValue(0.0),
21- m_resolution(0.0)
22+ m_resolution(0.0),
23+ m_available(false)
24 {
25 qRegisterMetaType<QSharedPointer<QAccelerometerReading> >("QSharedPointer<QAccelerometerReading>");
26 m_accelerometer = ua_sensors_accelerometer_new();
27+ if (m_accelerometer == NULL)
28+ return;
29
30 ua_sensors_accelerometer_set_reading_cb(
31 m_accelerometer,
32@@ -47,16 +50,20 @@
33 m_minValue = static_cast<qreal>(ua_sensors_accelerometer_get_min_value(m_accelerometer));
34 m_maxValue = static_cast<qreal>(ua_sensors_accelerometer_get_max_value(m_accelerometer));
35 m_resolution = static_cast<qreal>(ua_sensors_accelerometer_get_resolution(m_accelerometer));
36+ m_available = true;
37+
38 }
39
40 void core::SharedAccelerometer::start()
41 {
42- ua_sensors_accelerometer_enable(m_accelerometer);
43+ if (m_available)
44+ ua_sensors_accelerometer_enable(m_accelerometer);
45 }
46
47 void core::SharedAccelerometer::stop()
48 {
49- ua_sensors_accelerometer_disable(m_accelerometer);
50+ if (m_available)
51+ ua_sensors_accelerometer_disable(m_accelerometer);
52 }
53
54 qreal core::SharedAccelerometer::getMinDelay() const
55
56=== modified file 'plugins/sensors/core_shared_accelerometer.h'
57--- plugins/sensors/core_shared_accelerometer.h 2014-01-08 11:32:40 +0000
58+++ plugins/sensors/core_shared_accelerometer.h 2014-02-11 20:16:48 +0000
59@@ -52,6 +52,7 @@
60 qreal m_minValue;
61 qreal m_maxValue;
62 qreal m_resolution;
63+ bool m_available;
64
65 // Gets called by the underlying platform when there is a new accelerometer reading
66 static void onAccelerometerReadingCb(UASAccelerometerEvent *event, void *context);
67
68=== modified file 'tests/test_sensor_api.cpp'
69--- tests/test_sensor_api.cpp 2014-01-23 14:47:07 +0000
70+++ tests/test_sensor_api.cpp 2014-02-11 20:16:48 +0000
71@@ -48,8 +48,13 @@
72 chrono::time_point<chrono::system_clock> time;
73 };
74
75+/*******************************************
76+ *
77+ * Tests with simulated sensor backend
78+ *
79+ *******************************************/
80
81-class APITest : public testing::Test
82+class SimBackendTest : public testing::Test
83 {
84 protected:
85 virtual void SetUp()
86@@ -126,7 +131,7 @@
87 queue<struct OrientationEvent> orientation_events;
88 };
89
90-TESTP_F(APITest, CreateAccelerator, {
91+TESTP_F(SimBackendTest, CreateAccelerometer, {
92 set_data("create accel 0.5 1000 0.1");
93
94 QAccelerometer sensor;
95@@ -151,7 +156,7 @@
96 EXPECT_FLOAT_EQ(reading->z(), 0.0);
97 })
98
99-TESTP_F(APITest, CreateOrientation, {
100+TESTP_F(SimBackendTest, CreateOrientation, {
101 // orientation sensor is based on acceleration
102 set_data("create accel -500 500 1");
103
104@@ -168,7 +173,7 @@
105 EXPECT_EQ(sensor.reading()->orientation(), QOrientationReading::Undefined);
106 })
107
108-TESTP_F(APITest, AcceleratorEvents, {
109+TESTP_F(SimBackendTest, AccelerometerEvents, {
110 set_data("create accel -500 500 0.1\n"
111 "10 accel 0 -9.9 0\n"
112 "200 accel 1.5 400 0.5\n"
113@@ -195,7 +200,7 @@
114 check_accel_event(-1.0, -9.8, -0.5, 510);
115 })
116
117-TESTP_F(APITest, AcceleratorReadings, {
118+TESTP_F(SimBackendTest, AccelerometerReadings, {
119 set_data("create accel -500 500 0.1\n"
120 "10 accel 0 -9.9 0\n"
121 "200 accel 1.5 400 0.5\n"
122@@ -232,7 +237,7 @@
123 EXPECT_FLOAT_EQ(reading->z(), -0.5);
124 })
125
126-TESTP_F(APITest, OrientationEvents, {
127+TESTP_F(SimBackendTest, OrientationEvents, {
128 /* test some "parallel to coordinate axes" conditions, as well as some
129 * ~ 45 degrees angles; we want a hysteresis there, i. e. it should not
130 * flip back and forth when wiggling around the diagonals but only when
131@@ -276,7 +281,7 @@
132 check_orientation_event(QOrientationReading::FaceDown, 520);
133 })
134
135-TESTP_F(APITest, OrientationReading, {
136+TESTP_F(SimBackendTest, OrientationReading, {
137 set_data("create accel -500 500 0.1\n"
138 "10 accel 0 9.8 0\n" // TopUp
139 "20 accel 6.9 6.9 0\n" // turning left
140@@ -302,3 +307,106 @@
141 run_events(20);
142 EXPECT_EQ(orientation_sensor.reading()->orientation(), QOrientationReading::RightUp);
143 })
144+
145+
146+/*******************************************
147+ *
148+ * Tests with real sensor backend
149+ *
150+ *******************************************/
151+
152+class RealBackendTest : public testing::Test
153+{
154+ virtual void SetUp()
155+ {
156+ unsetenv("UBUNTU_PLATFORM_API_BACKEND");
157+ }
158+};
159+
160+TESTP_F(RealBackendTest, CreateAccelerometer, {
161+ QAccelerometer sensor;
162+ // connect to the qtubuntu-sensors backend; default is dummy, and there
163+ // does not seem to be a way to use data/Sensors.conf
164+ sensor.setIdentifier("core.accelerometer");
165+
166+ // this can succeed for fail depending on whether the hardware we run this
167+ // on actually exists; but it should never crash
168+ if (sensor.start()) {
169+ EXPECT_EQ(sensor.error(), 0);
170+ EXPECT_EQ(sensor.isConnectedToBackend(), true);
171+ EXPECT_EQ(sensor.accelerationMode(), QAccelerometer::Combined);
172+ } else {
173+ EXPECT_NE(sensor.error(), 0);
174+ EXPECT_EQ(sensor.isConnectedToBackend(), false);
175+ }
176+
177+ EXPECT_EQ(sensor.identifier(), "core.accelerometer");
178+})
179+
180+TESTP_F(RealBackendTest, CreateOrientation, {
181+ QOrientationSensor sensor;
182+ // connect to the qtubuntu-sensors backend; default is dummy, and there
183+ // does not seem to be a way to use data/Sensors.conf
184+ sensor.setIdentifier("core.orientation");
185+
186+ // this can succeed for fail depending on whether the hardware we run this
187+ // on actually exists; but it should never crash
188+ if (sensor.start()) {
189+ EXPECT_EQ(sensor.error(), 0);
190+ EXPECT_EQ(sensor.isConnectedToBackend(), true);
191+ } else {
192+ EXPECT_NE(sensor.error(), 0);
193+ EXPECT_EQ(sensor.isConnectedToBackend(), false);
194+ }
195+
196+ EXPECT_EQ(sensor.identifier(), "core.orientation");
197+})
198+
199+/*******************************************
200+ *
201+ * Tests with default sensor backend
202+ *
203+ *******************************************/
204+
205+class DefaultBackendTest : public testing::Test
206+{
207+ virtual void SetUp()
208+ {
209+ unsetenv("UBUNTU_PLATFORM_API_BACKEND");
210+ }
211+};
212+
213+TESTP_F(DefaultBackendTest, CreateAccelerometer, {
214+ QAccelerometer sensor;
215+ // don't set any particular identifier here
216+
217+ // this can succeed for fail depending on whether the hardware we run this
218+ // on actually exists; but it should never crash
219+ if (sensor.start()) {
220+ EXPECT_EQ(sensor.error(), 0);
221+ EXPECT_EQ(sensor.isConnectedToBackend(), true);
222+ } else {
223+ EXPECT_NE(sensor.error(), 0);
224+ EXPECT_EQ(sensor.isConnectedToBackend(), false);
225+ }
226+
227+ cout << "default backend connected to " << sensor.identifier().constData() << endl;
228+})
229+
230+TESTP_F(DefaultBackendTest, CreateOrientation, {
231+ QOrientationSensor sensor;
232+ // don't set any particular identifier here
233+
234+ // this can succeed for fail depending on whether the hardware we run this
235+ // on actually exists; but it should never crash
236+ if (sensor.start()) {
237+ EXPECT_EQ(sensor.error(), 0);
238+ EXPECT_EQ(sensor.isConnectedToBackend(), true);
239+ } else {
240+ EXPECT_NE(sensor.error(), 0);
241+ EXPECT_EQ(sensor.isConnectedToBackend(), false);
242+ }
243+
244+ cout << "default backend connected to " << sensor.identifier().constData() << endl;
245+})
246+

Subscribers

People subscribed via source and target branches