platform_sensor_reader_winrt_unittests.cc 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/device/generic_sensor/platform_sensor_reader_winrt.h"
  5. #include <objbase.h>
  6. #include "base/numerics/math_constants.h"
  7. #include "base/test/bind.h"
  8. #include "base/test/task_environment.h"
  9. #include "base/win/core_winrt_util.h"
  10. #include "base/win/scoped_com_initializer.h"
  11. #include "services/device/generic_sensor/generic_sensor_consts.h"
  12. #include "services/device/generic_sensor/platform_sensor_reader_win_base.h"
  13. #include "services/device/public/cpp/generic_sensor/sensor_reading.h"
  14. #include "testing/gmock/include/gmock/gmock.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "ui/gfx/geometry/quaternion.h"
  17. #include "ui/gfx/geometry/vector3d_f.h"
  18. namespace device {
  19. static constexpr unsigned long kExpectedMinimumReportInterval = 213;
  20. static constexpr double kExpectedReportFrequencySet = 25.0;
  21. static constexpr unsigned long kExpectedReportIntervalSet = 40;
  22. // Base mock class for the Windows::Devices::Sensors::I*SensorReadings
  23. template <class ISensorReading>
  24. class FakeSensorReadingWinrt
  25. : public Microsoft::WRL::RuntimeClass<
  26. Microsoft::WRL::RuntimeClassFlags<
  27. Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
  28. ISensorReading> {
  29. public:
  30. FakeSensorReadingWinrt(ABI::Windows::Foundation::DateTime time_stamp)
  31. : time_stamp_(time_stamp) {}
  32. IFACEMETHODIMP get_Timestamp(
  33. ABI::Windows::Foundation::DateTime* time_stamp) override {
  34. *time_stamp = time_stamp_;
  35. return get_timestamp_return_code_;
  36. }
  37. void SetGetTimestampReturnCode(HRESULT return_code) {
  38. get_timestamp_return_code_ = return_code;
  39. }
  40. protected:
  41. ABI::Windows::Foundation::DateTime time_stamp_;
  42. HRESULT get_timestamp_return_code_ = S_OK;
  43. };
  44. class FakeLightSensorReadingWinrt
  45. : public FakeSensorReadingWinrt<
  46. ABI::Windows::Devices::Sensors::ILightSensorReading> {
  47. public:
  48. FakeLightSensorReadingWinrt(ABI::Windows::Foundation::DateTime time_stamp,
  49. float lux)
  50. : FakeSensorReadingWinrt(time_stamp), lux_(lux) {}
  51. ~FakeLightSensorReadingWinrt() override = default;
  52. IFACEMETHODIMP get_IlluminanceInLux(float* lux) override {
  53. *lux = lux_;
  54. return get_illuminance_in_lux_return_code_;
  55. }
  56. void SetGetIlluminanceInLuxReturnCode(HRESULT return_code) {
  57. get_illuminance_in_lux_return_code_ = return_code;
  58. }
  59. private:
  60. float lux_;
  61. HRESULT get_illuminance_in_lux_return_code_ = S_OK;
  62. };
  63. class FakeAccelerometerReadingWinrt
  64. : public FakeSensorReadingWinrt<
  65. ABI::Windows::Devices::Sensors::IAccelerometerReading> {
  66. public:
  67. FakeAccelerometerReadingWinrt(ABI::Windows::Foundation::DateTime time_stamp,
  68. double x,
  69. double y,
  70. double z)
  71. : FakeSensorReadingWinrt(time_stamp), x_(x), y_(y), z_(z) {}
  72. ~FakeAccelerometerReadingWinrt() override = default;
  73. IFACEMETHODIMP get_AccelerationX(double* x) override {
  74. *x = x_;
  75. return get_x_return_code_;
  76. }
  77. IFACEMETHODIMP get_AccelerationY(double* y) override {
  78. *y = y_;
  79. return get_y_return_code_;
  80. }
  81. IFACEMETHODIMP get_AccelerationZ(double* z) override {
  82. *z = z_;
  83. return get_z_return_code_;
  84. }
  85. void SetGetXReturnCode(HRESULT return_code) {
  86. get_x_return_code_ = return_code;
  87. }
  88. void SetGetYReturnCode(HRESULT return_code) {
  89. get_y_return_code_ = return_code;
  90. }
  91. void SetGetZReturnCode(HRESULT return_code) {
  92. get_z_return_code_ = return_code;
  93. }
  94. private:
  95. double x_;
  96. double y_;
  97. double z_;
  98. HRESULT get_x_return_code_ = S_OK;
  99. HRESULT get_y_return_code_ = S_OK;
  100. HRESULT get_z_return_code_ = S_OK;
  101. };
  102. class FakeGyrometerReadingWinrt
  103. : public FakeSensorReadingWinrt<
  104. ABI::Windows::Devices::Sensors::IGyrometerReading> {
  105. public:
  106. FakeGyrometerReadingWinrt(ABI::Windows::Foundation::DateTime time_stamp,
  107. double x,
  108. double y,
  109. double z)
  110. : FakeSensorReadingWinrt(time_stamp), x_(x), y_(y), z_(z) {}
  111. ~FakeGyrometerReadingWinrt() override = default;
  112. IFACEMETHODIMP get_AngularVelocityX(double* x) override {
  113. *x = x_;
  114. return get_x_return_code_;
  115. }
  116. IFACEMETHODIMP get_AngularVelocityY(double* y) override {
  117. *y = y_;
  118. return get_y_return_code_;
  119. }
  120. IFACEMETHODIMP get_AngularVelocityZ(double* z) override {
  121. *z = z_;
  122. return get_z_return_code_;
  123. }
  124. void SetGetXReturnCode(HRESULT return_code) {
  125. get_x_return_code_ = return_code;
  126. }
  127. void SetGetYReturnCode(HRESULT return_code) {
  128. get_y_return_code_ = return_code;
  129. }
  130. void SetGetZReturnCode(HRESULT return_code) {
  131. get_z_return_code_ = return_code;
  132. }
  133. private:
  134. double x_;
  135. double y_;
  136. double z_;
  137. HRESULT get_x_return_code_ = S_OK;
  138. HRESULT get_y_return_code_ = S_OK;
  139. HRESULT get_z_return_code_ = S_OK;
  140. };
  141. class FakeInclinometerReadingWinrt
  142. : public FakeSensorReadingWinrt<
  143. ABI::Windows::Devices::Sensors::IInclinometerReading> {
  144. public:
  145. FakeInclinometerReadingWinrt(ABI::Windows::Foundation::DateTime time_stamp,
  146. float x,
  147. float y,
  148. float z)
  149. : FakeSensorReadingWinrt(time_stamp), x_(x), y_(y), z_(z) {}
  150. ~FakeInclinometerReadingWinrt() override = default;
  151. IFACEMETHODIMP get_PitchDegrees(float* x) override {
  152. *x = x_;
  153. return get_x_return_code_;
  154. }
  155. IFACEMETHODIMP get_RollDegrees(float* y) override {
  156. *y = y_;
  157. return get_y_return_code_;
  158. }
  159. IFACEMETHODIMP get_YawDegrees(float* z) override {
  160. *z = z_;
  161. return get_z_return_code_;
  162. }
  163. void SetGetXReturnCode(HRESULT return_code) {
  164. get_x_return_code_ = return_code;
  165. }
  166. void SetGetYReturnCode(HRESULT return_code) {
  167. get_y_return_code_ = return_code;
  168. }
  169. void SetGetZReturnCode(HRESULT return_code) {
  170. get_z_return_code_ = return_code;
  171. }
  172. private:
  173. float x_;
  174. float y_;
  175. float z_;
  176. HRESULT get_x_return_code_ = S_OK;
  177. HRESULT get_y_return_code_ = S_OK;
  178. HRESULT get_z_return_code_ = S_OK;
  179. };
  180. class FakeMagnetometerReadingWinrt
  181. : public FakeSensorReadingWinrt<
  182. ABI::Windows::Devices::Sensors::IMagnetometerReading> {
  183. public:
  184. FakeMagnetometerReadingWinrt(ABI::Windows::Foundation::DateTime time_stamp,
  185. float x,
  186. float y,
  187. float z)
  188. : FakeSensorReadingWinrt(time_stamp), x_(x), y_(y), z_(z) {}
  189. ~FakeMagnetometerReadingWinrt() override = default;
  190. IFACEMETHODIMP get_MagneticFieldX(float* x) override {
  191. *x = x_;
  192. return get_x_return_code_;
  193. }
  194. IFACEMETHODIMP get_MagneticFieldY(float* y) override {
  195. *y = y_;
  196. return get_y_return_code_;
  197. }
  198. IFACEMETHODIMP get_MagneticFieldZ(float* z) override {
  199. *z = z_;
  200. return get_z_return_code_;
  201. }
  202. IFACEMETHODIMP get_DirectionalAccuracy(
  203. ABI::Windows::Devices::Sensors::MagnetometerAccuracy*) override {
  204. return E_NOTIMPL;
  205. }
  206. void SetGetXReturnCode(HRESULT return_code) {
  207. get_x_return_code_ = return_code;
  208. }
  209. void SetGetYReturnCode(HRESULT return_code) {
  210. get_y_return_code_ = return_code;
  211. }
  212. void SetGetZReturnCode(HRESULT return_code) {
  213. get_z_return_code_ = return_code;
  214. }
  215. private:
  216. float x_;
  217. float y_;
  218. float z_;
  219. HRESULT get_x_return_code_ = S_OK;
  220. HRESULT get_y_return_code_ = S_OK;
  221. HRESULT get_z_return_code_ = S_OK;
  222. };
  223. class FakeSensorQuaternion
  224. : public Microsoft::WRL::RuntimeClass<
  225. Microsoft::WRL::RuntimeClassFlags<
  226. Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
  227. ABI::Windows::Devices::Sensors::ISensorQuaternion> {
  228. public:
  229. FakeSensorQuaternion(float w, float x, float y, float z)
  230. : w_(w), x_(x), y_(y), z_(z) {}
  231. ~FakeSensorQuaternion() override = default;
  232. IFACEMETHODIMP get_W(float* w) override {
  233. *w = w_;
  234. return S_OK;
  235. }
  236. IFACEMETHODIMP get_X(float* x) override {
  237. *x = x_;
  238. return S_OK;
  239. }
  240. IFACEMETHODIMP get_Y(float* y) override {
  241. *y = y_;
  242. return S_OK;
  243. }
  244. IFACEMETHODIMP get_Z(float* z) override {
  245. *z = z_;
  246. return S_OK;
  247. }
  248. private:
  249. float w_;
  250. float x_;
  251. float y_;
  252. float z_;
  253. };
  254. class FakeOrientationSensorReadingWinrt
  255. : public FakeSensorReadingWinrt<
  256. ABI::Windows::Devices::Sensors::IOrientationSensorReading> {
  257. public:
  258. FakeOrientationSensorReadingWinrt(
  259. ABI::Windows::Foundation::DateTime time_stamp,
  260. float w,
  261. float x,
  262. float y,
  263. float z)
  264. : FakeSensorReadingWinrt(time_stamp) {
  265. quaternion_ = Microsoft::WRL::Make<FakeSensorQuaternion>(w, x, y, z);
  266. }
  267. ~FakeOrientationSensorReadingWinrt() override = default;
  268. IFACEMETHODIMP get_Quaternion(
  269. ABI::Windows::Devices::Sensors::ISensorQuaternion** quaternion) override {
  270. quaternion_.CopyTo(quaternion);
  271. return S_OK;
  272. }
  273. IFACEMETHODIMP get_RotationMatrix(
  274. ABI::Windows::Devices::Sensors::ISensorRotationMatrix** ppMatrix)
  275. override {
  276. return E_NOTIMPL;
  277. }
  278. void SetGetQuaternionReturnCode(HRESULT return_code) {
  279. get_quaternion_return_code_ = return_code;
  280. }
  281. private:
  282. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Sensors::ISensorQuaternion>
  283. quaternion_;
  284. HRESULT get_quaternion_return_code_ = S_OK;
  285. };
  286. // Mock class for Windows::Devices::Sensors::ISensorReadingChangedEventArgs,
  287. // allows reading changed events to return mock sensor readings.
  288. template <class ISensorReading, class ISensorReadingChangedEventArgs>
  289. class FakeSensorReadingChangedEventArgsWinrt
  290. : public Microsoft::WRL::RuntimeClass<
  291. Microsoft::WRL::RuntimeClassFlags<
  292. Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
  293. ISensorReadingChangedEventArgs> {
  294. public:
  295. FakeSensorReadingChangedEventArgsWinrt(
  296. const Microsoft::WRL::ComPtr<ISensorReading>& reading)
  297. : reading_(reading) {}
  298. ~FakeSensorReadingChangedEventArgsWinrt() override = default;
  299. IFACEMETHODIMP get_Reading(ISensorReading** reading) override {
  300. return reading_.CopyTo(reading);
  301. }
  302. private:
  303. Microsoft::WRL::ComPtr<ISensorReading> reading_;
  304. };
  305. // Mock client used to receive sensor data callbacks
  306. class MockClient : public PlatformSensorReaderWinBase::Client {
  307. public:
  308. MOCK_METHOD1(OnReadingUpdated, void(const SensorReading& reading));
  309. MOCK_METHOD0(OnSensorError, void());
  310. protected:
  311. ~MockClient() override = default;
  312. };
  313. // Base mock class for Windows.Devices.Sensors.ISensor*, injected into
  314. // PlatformSensorReaderWinrt* to mock out the underlying
  315. // Windows.Devices.Sensors dependency.
  316. template <class ISensor,
  317. class Sensor,
  318. class ISensorReading,
  319. class ISensorReadingChangedEventArgs,
  320. class SensorReadingChangedEventArgs>
  321. class FakeSensorWinrt
  322. : public Microsoft::WRL::RuntimeClass<
  323. Microsoft::WRL::RuntimeClassFlags<
  324. Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
  325. ISensor> {
  326. public:
  327. ~FakeSensorWinrt() override = default;
  328. IFACEMETHODIMP GetCurrentReading(ISensorReading** ppReading) override {
  329. return E_NOTIMPL;
  330. }
  331. IFACEMETHODIMP get_MinimumReportInterval(UINT32* pValue) override {
  332. *pValue = kExpectedMinimumReportInterval;
  333. return get_minimum_report_interval_return_code_;
  334. }
  335. IFACEMETHODIMP get_ReportInterval(UINT32* pValue) override {
  336. return E_NOTIMPL;
  337. }
  338. IFACEMETHODIMP put_ReportInterval(UINT32 value) override {
  339. EXPECT_EQ(value, kExpectedReportIntervalSet);
  340. return put_report_interval_return_code_;
  341. }
  342. IFACEMETHODIMP add_ReadingChanged(
  343. ABI::Windows::Foundation::
  344. ITypedEventHandler<Sensor*, SensorReadingChangedEventArgs*>* pHandler,
  345. EventRegistrationToken* pToken) override {
  346. handler_ = pHandler;
  347. return add_reading_changed_return_code_;
  348. }
  349. IFACEMETHODIMP remove_ReadingChanged(EventRegistrationToken iToken) override {
  350. handler_.Reset();
  351. return remove_reading_changed_return_code_;
  352. }
  353. // Makes any clients registered via add_ReadingChanged() to trigger with
  354. // the given sensor reading.
  355. void TriggerFakeSensorReading(
  356. Microsoft::WRL::ComPtr<ISensorReading> reading) {
  357. EXPECT_TRUE(handler_);
  358. Microsoft::WRL::ComPtr<ISensorReadingChangedEventArgs> reading_event_args =
  359. Microsoft::WRL::Make<FakeSensorReadingChangedEventArgsWinrt<
  360. ISensorReading, ISensorReadingChangedEventArgs>>(reading);
  361. EXPECT_HRESULT_SUCCEEDED(handler_->Invoke(this, reading_event_args.Get()));
  362. }
  363. // Returns true if any clients are registered for readings via
  364. // add_ReadingChanged(), false otherwise.
  365. bool IsSensorStarted() { return handler_; }
  366. void SetGetMinimumReportIntervalReturnCode(HRESULT return_code) {
  367. get_minimum_report_interval_return_code_ = return_code;
  368. }
  369. void SetPutReportIntervalReturnCode(HRESULT return_code) {
  370. put_report_interval_return_code_ = return_code;
  371. }
  372. void SetAddReadingChangedReturnCode(HRESULT return_code) {
  373. add_reading_changed_return_code_ = return_code;
  374. }
  375. void SetRemoveReadingChangedReturnCode(HRESULT return_code) {
  376. remove_reading_changed_return_code_ = return_code;
  377. }
  378. private:
  379. Microsoft::WRL::ComPtr<ABI::Windows::Foundation::ITypedEventHandler<
  380. Sensor*,
  381. SensorReadingChangedEventArgs*>>
  382. handler_;
  383. HRESULT put_report_interval_return_code_ = S_OK;
  384. HRESULT get_minimum_report_interval_return_code_ = S_OK;
  385. HRESULT add_reading_changed_return_code_ = S_OK;
  386. HRESULT remove_reading_changed_return_code_ = S_OK;
  387. };
  388. class FakeAccelerometerSensorWinrt
  389. : public FakeSensorWinrt<
  390. ABI::Windows::Devices::Sensors::IAccelerometer,
  391. ABI::Windows::Devices::Sensors::Accelerometer,
  392. ABI::Windows::Devices::Sensors::IAccelerometerReading,
  393. ABI::Windows::Devices::Sensors::IAccelerometerReadingChangedEventArgs,
  394. ABI::Windows::Devices::Sensors::
  395. AccelerometerReadingChangedEventArgs> {
  396. public:
  397. FakeAccelerometerSensorWinrt() = default;
  398. ~FakeAccelerometerSensorWinrt() override = default;
  399. IFACEMETHODIMP add_Shaken(
  400. ABI::Windows::Foundation::ITypedEventHandler<
  401. ABI::Windows::Devices::Sensors::Accelerometer*,
  402. ABI::Windows::Devices::Sensors::AccelerometerShakenEventArgs*>*,
  403. EventRegistrationToken*) override {
  404. return E_NOTIMPL;
  405. }
  406. IFACEMETHODIMP remove_Shaken(EventRegistrationToken) override {
  407. return E_NOTIMPL;
  408. }
  409. };
  410. template <class ISensorStatics,
  411. class ISensor,
  412. class Sensor,
  413. class ISensorReading,
  414. class ISensorReadingChangedEventArgs,
  415. class SensorReadingChangedEventArgs>
  416. class FakeSensorFactoryWinrt
  417. : public Microsoft::WRL::RuntimeClass<
  418. Microsoft::WRL::RuntimeClassFlags<
  419. Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
  420. ISensorStatics> {
  421. public:
  422. FakeSensorFactoryWinrt() {
  423. fake_sensor_ =
  424. Microsoft::WRL::Make<FakeSensorWinrt<ISensor, Sensor, ISensorReading,
  425. ISensorReadingChangedEventArgs,
  426. SensorReadingChangedEventArgs>>();
  427. }
  428. FakeSensorFactoryWinrt(
  429. Microsoft::WRL::ComPtr<FakeSensorWinrt<ISensor,
  430. Sensor,
  431. ISensorReading,
  432. ISensorReadingChangedEventArgs,
  433. SensorReadingChangedEventArgs>>
  434. fake_sensor)
  435. : fake_sensor_(fake_sensor) {}
  436. ~FakeSensorFactoryWinrt() override = default;
  437. IFACEMETHODIMP GetDefault(ISensor** ppResult) override {
  438. if (fake_sensor_ && SUCCEEDED(get_default_return_code_)) {
  439. return fake_sensor_.CopyTo(ppResult);
  440. }
  441. return get_default_return_code_;
  442. }
  443. Microsoft::WRL::ComPtr<FakeSensorWinrt<ISensor,
  444. Sensor,
  445. ISensorReading,
  446. ISensorReadingChangedEventArgs,
  447. SensorReadingChangedEventArgs>>
  448. fake_sensor_;
  449. void SetGetDefaultReturnCode(HRESULT return_code) {
  450. get_default_return_code_ = return_code;
  451. }
  452. private:
  453. HRESULT get_default_return_code_ = S_OK;
  454. };
  455. class PlatformSensorReaderTestWinrt : public testing::Test {
  456. private:
  457. base::test::TaskEnvironment task_environment_;
  458. base::win::ScopedCOMInitializer scoped_com_initializer_;
  459. };
  460. // Tests that PlatformSensorReaderWinrtBase returns the expected error
  461. // if it could not create the underlying sensor.
  462. TEST_F(PlatformSensorReaderTestWinrt, FailedSensorCreate) {
  463. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  464. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  465. ABI::Windows::Devices::Sensors::ILightSensor,
  466. ABI::Windows::Devices::Sensors::LightSensor,
  467. ABI::Windows::Devices::Sensors::ILightSensorReading,
  468. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  469. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  470. // Case: sensor was created successfully
  471. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  472. sensor->InitForTesting(base::BindLambdaForTesting(
  473. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  474. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  475. EXPECT_TRUE(sensor->Initialize());
  476. EXPECT_TRUE(sensor->IsUnderlyingWinrtObjectValidForTesting());
  477. // Case: failed to query sensor
  478. fake_sensor_factory->SetGetDefaultReturnCode(E_FAIL);
  479. EXPECT_FALSE(sensor->Initialize());
  480. EXPECT_FALSE(sensor->IsUnderlyingWinrtObjectValidForTesting());
  481. fake_sensor_factory->SetGetDefaultReturnCode(S_OK);
  482. // Case: Sensor does not exist on system
  483. fake_sensor_factory->fake_sensor_ = nullptr;
  484. EXPECT_FALSE(sensor->Initialize());
  485. EXPECT_FALSE(sensor->IsUnderlyingWinrtObjectValidForTesting());
  486. // Case:: failed to activate sensor factory
  487. sensor->InitForTesting(base::BindLambdaForTesting(
  488. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  489. -> HRESULT { return E_FAIL; }));
  490. EXPECT_FALSE(sensor->Initialize());
  491. EXPECT_FALSE(sensor->IsUnderlyingWinrtObjectValidForTesting());
  492. }
  493. // Tests that PlatformSensorReaderWinrtBase returns the right
  494. // minimum report interval.
  495. TEST_F(PlatformSensorReaderTestWinrt, SensorMinimumReportInterval) {
  496. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  497. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  498. ABI::Windows::Devices::Sensors::ILightSensor,
  499. ABI::Windows::Devices::Sensors::LightSensor,
  500. ABI::Windows::Devices::Sensors::ILightSensorReading,
  501. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  502. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  503. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  504. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  505. sensor->InitForTesting(base::BindLambdaForTesting(
  506. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  507. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  508. EXPECT_TRUE(sensor->Initialize());
  509. EXPECT_EQ(sensor->GetMinimalReportingInterval().InMilliseconds(),
  510. kExpectedMinimumReportInterval);
  511. }
  512. // Tests that PlatformSensorReaderWinrtBase returns a 0 report interval
  513. // when it fails to query the sensor.
  514. TEST_F(PlatformSensorReaderTestWinrt, FailedSensorMinimumReportInterval) {
  515. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  516. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  517. ABI::Windows::Devices::Sensors::ILightSensor,
  518. ABI::Windows::Devices::Sensors::LightSensor,
  519. ABI::Windows::Devices::Sensors::ILightSensorReading,
  520. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  521. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  522. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  523. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  524. fake_sensor->SetGetMinimumReportIntervalReturnCode(E_FAIL);
  525. sensor->InitForTesting(base::BindLambdaForTesting(
  526. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  527. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  528. EXPECT_TRUE(sensor->Initialize());
  529. EXPECT_EQ(sensor->GetMinimalReportingInterval().InMilliseconds(), 0);
  530. }
  531. // Tests that PlatformSensorReaderWinrtBase converts the timestamp correctly
  532. TEST_F(PlatformSensorReaderTestWinrt, SensorTimestampConversion) {
  533. static constexpr double expectedTimestampDeltaSecs = 19.0;
  534. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  535. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  536. ABI::Windows::Devices::Sensors::ILightSensor,
  537. ABI::Windows::Devices::Sensors::LightSensor,
  538. ABI::Windows::Devices::Sensors::ILightSensorReading,
  539. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  540. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  541. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  542. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  543. sensor->InitForTesting(base::BindLambdaForTesting(
  544. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  545. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  546. EXPECT_TRUE(sensor->Initialize());
  547. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  548. double lastReportedTimestamp = 0.0;
  549. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  550. .WillRepeatedly(testing::Invoke([&](const SensorReading& reading) {
  551. lastReportedTimestamp = reading.als.timestamp;
  552. EXPECT_EQ(reading.als.value, 0.0f);
  553. }));
  554. sensor->SetClient(mock_client.get());
  555. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  556. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  557. // Trigger a sensor reading at time 0 (epoch), converted timestamp should
  558. // also be 0.
  559. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Sensors::ILightSensorReading>
  560. reading = Microsoft::WRL::Make<FakeLightSensorReadingWinrt>(
  561. ABI::Windows::Foundation::DateTime{}, 0.0f);
  562. fake_sensor->TriggerFakeSensorReading(reading);
  563. EXPECT_EQ(lastReportedTimestamp, 0);
  564. auto second_timestamp =
  565. base::Seconds(expectedTimestampDeltaSecs).ToWinrtDateTime();
  566. reading =
  567. Microsoft::WRL::Make<FakeLightSensorReadingWinrt>(second_timestamp, 0.0f);
  568. fake_sensor->TriggerFakeSensorReading(reading);
  569. // Verify the reported time stamp has ticked forward
  570. // expectedTimestampDeltaSecs
  571. EXPECT_EQ(lastReportedTimestamp, expectedTimestampDeltaSecs);
  572. }
  573. // Tests that PlatformSensorReaderWinrtBase starts and stops the
  574. // underlying sensor when Start() and Stop() are called.
  575. TEST_F(PlatformSensorReaderTestWinrt, StartStopSensorCallbacks) {
  576. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  577. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  578. ABI::Windows::Devices::Sensors::ILightSensor,
  579. ABI::Windows::Devices::Sensors::LightSensor,
  580. ABI::Windows::Devices::Sensors::ILightSensorReading,
  581. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  582. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  583. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  584. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  585. sensor->InitForTesting(base::BindLambdaForTesting(
  586. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  587. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  588. EXPECT_TRUE(sensor->Initialize());
  589. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  590. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  591. EXPECT_TRUE(fake_sensor->IsSensorStarted());
  592. // Calling Start() contiguously should not cause any errors/exceptions
  593. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  594. EXPECT_TRUE(fake_sensor->IsSensorStarted());
  595. sensor->StopSensor();
  596. EXPECT_FALSE(fake_sensor->IsSensorStarted());
  597. // Calling Stop() contiguously should not cause any errors/exceptions
  598. sensor->StopSensor();
  599. EXPECT_FALSE(fake_sensor->IsSensorStarted());
  600. }
  601. // Tests that PlatformSensorReaderWinrtBase stops the underlying sensor
  602. // even if Start() is called without a following Stop() upon destruction.
  603. TEST_F(PlatformSensorReaderTestWinrt, StartWithoutStopSensorCallbacks) {
  604. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  605. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  606. ABI::Windows::Devices::Sensors::ILightSensor,
  607. ABI::Windows::Devices::Sensors::LightSensor,
  608. ABI::Windows::Devices::Sensors::ILightSensorReading,
  609. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  610. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  611. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  612. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  613. sensor->InitForTesting(base::BindLambdaForTesting(
  614. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  615. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  616. EXPECT_TRUE(sensor->Initialize());
  617. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  618. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  619. EXPECT_TRUE(fake_sensor->IsSensorStarted());
  620. sensor.reset();
  621. EXPECT_FALSE(fake_sensor->IsSensorStarted());
  622. }
  623. // Tests that PlatformSensorReaderWinrtBase::StartSensor() returns false
  624. // when setting the report interval or registering for sensor events fails.
  625. TEST_F(PlatformSensorReaderTestWinrt, FailedSensorStart) {
  626. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  627. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  628. ABI::Windows::Devices::Sensors::ILightSensor,
  629. ABI::Windows::Devices::Sensors::LightSensor,
  630. ABI::Windows::Devices::Sensors::ILightSensorReading,
  631. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  632. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  633. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  634. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  635. sensor->InitForTesting(base::BindLambdaForTesting(
  636. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  637. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  638. EXPECT_TRUE(sensor->Initialize());
  639. fake_sensor->SetPutReportIntervalReturnCode(E_FAIL);
  640. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  641. EXPECT_FALSE(sensor->StartSensor(sensor_config));
  642. fake_sensor->SetPutReportIntervalReturnCode(S_OK);
  643. fake_sensor->SetAddReadingChangedReturnCode(E_FAIL);
  644. EXPECT_FALSE(sensor->StartSensor(sensor_config));
  645. }
  646. // Tests that PlatformSensorReaderWinrtBase::StopSensor() swallows
  647. // unregister sensor change errors.
  648. TEST_F(PlatformSensorReaderTestWinrt, FailedSensorStop) {
  649. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  650. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  651. ABI::Windows::Devices::Sensors::ILightSensor,
  652. ABI::Windows::Devices::Sensors::LightSensor,
  653. ABI::Windows::Devices::Sensors::ILightSensorReading,
  654. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  655. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  656. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  657. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  658. sensor->InitForTesting(base::BindLambdaForTesting(
  659. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  660. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  661. EXPECT_TRUE(sensor->Initialize());
  662. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  663. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  664. fake_sensor->SetRemoveReadingChangedReturnCode(E_FAIL);
  665. sensor->StopSensor();
  666. }
  667. // Tests that PlatformSensorReaderWinrtLightSensor does not notify the
  668. // client if an error occurs during sensor sample parsing.
  669. TEST_F(PlatformSensorReaderTestWinrt, FailedLightSensorSampleParse) {
  670. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  671. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  672. ABI::Windows::Devices::Sensors::ILightSensor,
  673. ABI::Windows::Devices::Sensors::LightSensor,
  674. ABI::Windows::Devices::Sensors::ILightSensorReading,
  675. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  676. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  677. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  678. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  679. sensor->InitForTesting(base::BindLambdaForTesting(
  680. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  681. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  682. EXPECT_TRUE(sensor->Initialize());
  683. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  684. // This test relies on the NiceMock to work, if the sensor notifies
  685. // the client despite not being able to parse the sensor sample then
  686. // the NiceMock will throw an error as no EXPECT_CALL has been set.
  687. sensor->SetClient(mock_client.get());
  688. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  689. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  690. auto reading = Microsoft::WRL::Make<FakeLightSensorReadingWinrt>(
  691. ABI::Windows::Foundation::DateTime{}, 0.0f);
  692. reading->SetGetTimestampReturnCode(E_FAIL);
  693. fake_sensor->TriggerFakeSensorReading(reading);
  694. reading->SetGetTimestampReturnCode(S_OK);
  695. reading->SetGetIlluminanceInLuxReturnCode(E_FAIL);
  696. fake_sensor->TriggerFakeSensorReading(reading);
  697. }
  698. // Tests that PlatformSensorReaderWinrtLightSensor notifies the client
  699. // and gives it the correct sensor data when a new sensor sample arrives.
  700. TEST_F(PlatformSensorReaderTestWinrt, SensorClientNotification) {
  701. static constexpr float expected_lux = 123.0f;
  702. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  703. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  704. ABI::Windows::Devices::Sensors::ILightSensor,
  705. ABI::Windows::Devices::Sensors::LightSensor,
  706. ABI::Windows::Devices::Sensors::ILightSensorReading,
  707. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  708. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  709. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  710. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  711. sensor->InitForTesting(base::BindLambdaForTesting(
  712. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  713. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  714. EXPECT_TRUE(sensor->Initialize());
  715. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  716. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  717. .WillOnce(testing::Invoke([&](const SensorReading& reading) {
  718. EXPECT_EQ(expected_lux, reading.als.value);
  719. }));
  720. sensor->SetClient(mock_client.get());
  721. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  722. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  723. auto reading = Microsoft::WRL::Make<FakeLightSensorReadingWinrt>(
  724. ABI::Windows::Foundation::DateTime{}, expected_lux);
  725. fake_sensor->TriggerFakeSensorReading(reading);
  726. sensor->StopSensor();
  727. }
  728. // Tests if PlatformSensorReaderWinrtAccelerometer correctly converts sensor
  729. // readings from Windows.Devices.Sensors.Accelerometer.
  730. TEST_F(PlatformSensorReaderTestWinrt, CheckAccelerometerReadingConversion) {
  731. constexpr double expected_x = 0.25;
  732. constexpr double expected_y = -0.25;
  733. constexpr double expected_z = -0.5;
  734. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  735. ABI::Windows::Devices::Sensors::IAccelerometerStatics,
  736. ABI::Windows::Devices::Sensors::IAccelerometer,
  737. ABI::Windows::Devices::Sensors::Accelerometer,
  738. ABI::Windows::Devices::Sensors::IAccelerometerReading,
  739. ABI::Windows::Devices::Sensors::IAccelerometerReadingChangedEventArgs,
  740. ABI::Windows::Devices::Sensors::AccelerometerReadingChangedEventArgs>>(
  741. Microsoft::WRL::Make<FakeAccelerometerSensorWinrt>());
  742. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  743. auto sensor = std::make_unique<PlatformSensorReaderWinrtAccelerometer>();
  744. sensor->InitForTesting(base::BindLambdaForTesting(
  745. [&](ABI::Windows::Devices::Sensors::IAccelerometerStatics**
  746. sensor_factory) -> HRESULT {
  747. return fake_sensor_factory.CopyTo(sensor_factory);
  748. }));
  749. EXPECT_TRUE(sensor->Initialize());
  750. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  751. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  752. .WillOnce(testing::Invoke([&](const SensorReading& reading) {
  753. EXPECT_EQ(-expected_x * base::kMeanGravityDouble, reading.accel.x);
  754. EXPECT_EQ(-expected_y * base::kMeanGravityDouble, reading.accel.y);
  755. EXPECT_EQ(-expected_z * base::kMeanGravityDouble, reading.accel.z);
  756. }));
  757. sensor->SetClient(mock_client.get());
  758. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  759. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  760. auto reading = Microsoft::WRL::Make<FakeAccelerometerReadingWinrt>(
  761. ABI::Windows::Foundation::DateTime{}, expected_x, expected_y, expected_z);
  762. fake_sensor->TriggerFakeSensorReading(reading);
  763. sensor->StopSensor();
  764. }
  765. // Tests that PlatformSensorReaderWinrtAccelerometer does not notify the
  766. // client if an error occurs during sensor sample parsing.
  767. TEST_F(PlatformSensorReaderTestWinrt, FailedAccelerometerSampleParse) {
  768. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  769. ABI::Windows::Devices::Sensors::IAccelerometerStatics,
  770. ABI::Windows::Devices::Sensors::IAccelerometer,
  771. ABI::Windows::Devices::Sensors::Accelerometer,
  772. ABI::Windows::Devices::Sensors::IAccelerometerReading,
  773. ABI::Windows::Devices::Sensors::IAccelerometerReadingChangedEventArgs,
  774. ABI::Windows::Devices::Sensors::AccelerometerReadingChangedEventArgs>>(
  775. Microsoft::WRL::Make<FakeAccelerometerSensorWinrt>());
  776. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  777. auto sensor = std::make_unique<PlatformSensorReaderWinrtAccelerometer>();
  778. sensor->InitForTesting(base::BindLambdaForTesting(
  779. [&](ABI::Windows::Devices::Sensors::IAccelerometerStatics**
  780. sensor_factory) -> HRESULT {
  781. return fake_sensor_factory.CopyTo(sensor_factory);
  782. }));
  783. EXPECT_TRUE(sensor->Initialize());
  784. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  785. sensor->SetClient(mock_client.get());
  786. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  787. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  788. auto reading = Microsoft::WRL::Make<FakeAccelerometerReadingWinrt>(
  789. ABI::Windows::Foundation::DateTime{}, 0, 0, 0);
  790. reading->SetGetTimestampReturnCode(E_FAIL);
  791. fake_sensor->TriggerFakeSensorReading(reading);
  792. reading->SetGetTimestampReturnCode(S_OK);
  793. reading->SetGetXReturnCode(E_FAIL);
  794. fake_sensor->TriggerFakeSensorReading(reading);
  795. reading->SetGetXReturnCode(S_OK);
  796. reading->SetGetYReturnCode(E_FAIL);
  797. fake_sensor->TriggerFakeSensorReading(reading);
  798. reading->SetGetYReturnCode(S_OK);
  799. reading->SetGetZReturnCode(E_FAIL);
  800. fake_sensor->TriggerFakeSensorReading(reading);
  801. }
  802. // Tests if PlatformSensorReaderWinrtGyrometer correctly converts sensor
  803. // readings from Windows.Devices.Sensors.Gyrometer.
  804. TEST_F(PlatformSensorReaderTestWinrt, CheckGyrometerReadingConversion) {
  805. constexpr double expected_x = 0.0;
  806. constexpr double expected_y = -1.8;
  807. constexpr double expected_z = -98.7;
  808. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  809. ABI::Windows::Devices::Sensors::IGyrometerStatics,
  810. ABI::Windows::Devices::Sensors::IGyrometer,
  811. ABI::Windows::Devices::Sensors::Gyrometer,
  812. ABI::Windows::Devices::Sensors::IGyrometerReading,
  813. ABI::Windows::Devices::Sensors::IGyrometerReadingChangedEventArgs,
  814. ABI::Windows::Devices::Sensors::GyrometerReadingChangedEventArgs>>();
  815. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  816. auto sensor = std::make_unique<PlatformSensorReaderWinrtGyrometer>();
  817. sensor->InitForTesting(base::BindLambdaForTesting(
  818. [&](ABI::Windows::Devices::Sensors::IGyrometerStatics** sensor_factory)
  819. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  820. EXPECT_TRUE(sensor->Initialize());
  821. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  822. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  823. .WillOnce(testing::Invoke([&](const SensorReading& reading) {
  824. EXPECT_EQ(gfx::DegToRad(expected_x), reading.gyro.x);
  825. EXPECT_EQ(gfx::DegToRad(expected_y), reading.gyro.y);
  826. EXPECT_EQ(gfx::DegToRad(expected_z), reading.gyro.z);
  827. }));
  828. sensor->SetClient(mock_client.get());
  829. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  830. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  831. auto reading = Microsoft::WRL::Make<FakeGyrometerReadingWinrt>(
  832. ABI::Windows::Foundation::DateTime{}, expected_x, expected_y, expected_z);
  833. fake_sensor->TriggerFakeSensorReading(reading);
  834. sensor->StopSensor();
  835. }
  836. // Tests that PlatformSensorReaderWinrtGyrometer does not notify the
  837. // client if an error occurs during sensor sample parsing.
  838. TEST_F(PlatformSensorReaderTestWinrt, FailedGyrometerSampleParse) {
  839. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  840. ABI::Windows::Devices::Sensors::IGyrometerStatics,
  841. ABI::Windows::Devices::Sensors::IGyrometer,
  842. ABI::Windows::Devices::Sensors::Gyrometer,
  843. ABI::Windows::Devices::Sensors::IGyrometerReading,
  844. ABI::Windows::Devices::Sensors::IGyrometerReadingChangedEventArgs,
  845. ABI::Windows::Devices::Sensors::GyrometerReadingChangedEventArgs>>();
  846. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  847. auto sensor = std::make_unique<PlatformSensorReaderWinrtGyrometer>();
  848. sensor->InitForTesting(base::BindLambdaForTesting(
  849. [&](ABI::Windows::Devices::Sensors::IGyrometerStatics** sensor_factory)
  850. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  851. EXPECT_TRUE(sensor->Initialize());
  852. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  853. sensor->SetClient(mock_client.get());
  854. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  855. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  856. auto reading = Microsoft::WRL::Make<FakeGyrometerReadingWinrt>(
  857. ABI::Windows::Foundation::DateTime{}, 0, 0, 0);
  858. reading->SetGetTimestampReturnCode(E_FAIL);
  859. fake_sensor->TriggerFakeSensorReading(reading);
  860. reading->SetGetTimestampReturnCode(S_OK);
  861. reading->SetGetXReturnCode(E_FAIL);
  862. fake_sensor->TriggerFakeSensorReading(reading);
  863. reading->SetGetXReturnCode(S_OK);
  864. reading->SetGetYReturnCode(E_FAIL);
  865. fake_sensor->TriggerFakeSensorReading(reading);
  866. reading->SetGetYReturnCode(S_OK);
  867. reading->SetGetZReturnCode(E_FAIL);
  868. fake_sensor->TriggerFakeSensorReading(reading);
  869. }
  870. // Tests if PlatformSensorReaderWinrtMagnetometer correctly converts sensor
  871. // readings from Windows.Sensors.Devices.Magnetometer.
  872. TEST_F(PlatformSensorReaderTestWinrt, CheckMagnetometerReadingConversion) {
  873. constexpr double expected_x = 112.0;
  874. constexpr double expected_y = 112.0;
  875. constexpr double expected_z = 457.0;
  876. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  877. ABI::Windows::Devices::Sensors::IMagnetometerStatics,
  878. ABI::Windows::Devices::Sensors::IMagnetometer,
  879. ABI::Windows::Devices::Sensors::Magnetometer,
  880. ABI::Windows::Devices::Sensors::IMagnetometerReading,
  881. ABI::Windows::Devices::Sensors::IMagnetometerReadingChangedEventArgs,
  882. ABI::Windows::Devices::Sensors::MagnetometerReadingChangedEventArgs>>();
  883. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  884. auto sensor = std::make_unique<PlatformSensorReaderWinrtMagnetometer>();
  885. sensor->InitForTesting(base::BindLambdaForTesting(
  886. [&](ABI::Windows::Devices::Sensors::IMagnetometerStatics** sensor_factory)
  887. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  888. EXPECT_TRUE(sensor->Initialize());
  889. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  890. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  891. .WillOnce(testing::Invoke([&](const SensorReading& reading) {
  892. EXPECT_EQ(expected_x, reading.magn.x);
  893. EXPECT_EQ(expected_y, reading.magn.y);
  894. EXPECT_EQ(expected_z, reading.magn.z);
  895. }));
  896. sensor->SetClient(mock_client.get());
  897. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  898. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  899. auto reading = Microsoft::WRL::Make<FakeMagnetometerReadingWinrt>(
  900. ABI::Windows::Foundation::DateTime{}, expected_x, expected_y, expected_z);
  901. fake_sensor->TriggerFakeSensorReading(reading);
  902. sensor->StopSensor();
  903. }
  904. // Tests that PlatformSensorReaderWinrtMagnetometer does not notify the
  905. // client if an error occurs during sensor sample parsing.
  906. TEST_F(PlatformSensorReaderTestWinrt, FailedMagnetometerSampleParse) {
  907. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  908. ABI::Windows::Devices::Sensors::IMagnetometerStatics,
  909. ABI::Windows::Devices::Sensors::IMagnetometer,
  910. ABI::Windows::Devices::Sensors::Magnetometer,
  911. ABI::Windows::Devices::Sensors::IMagnetometerReading,
  912. ABI::Windows::Devices::Sensors::IMagnetometerReadingChangedEventArgs,
  913. ABI::Windows::Devices::Sensors::MagnetometerReadingChangedEventArgs>>();
  914. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  915. auto sensor = std::make_unique<PlatformSensorReaderWinrtMagnetometer>();
  916. sensor->InitForTesting(base::BindLambdaForTesting(
  917. [&](ABI::Windows::Devices::Sensors::IMagnetometerStatics** sensor_factory)
  918. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  919. EXPECT_TRUE(sensor->Initialize());
  920. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  921. sensor->SetClient(mock_client.get());
  922. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  923. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  924. auto reading = Microsoft::WRL::Make<FakeMagnetometerReadingWinrt>(
  925. ABI::Windows::Foundation::DateTime{}, 0, 0, 0);
  926. reading->SetGetTimestampReturnCode(E_FAIL);
  927. fake_sensor->TriggerFakeSensorReading(reading);
  928. reading->SetGetTimestampReturnCode(S_OK);
  929. reading->SetGetXReturnCode(E_FAIL);
  930. fake_sensor->TriggerFakeSensorReading(reading);
  931. reading->SetGetXReturnCode(S_OK);
  932. reading->SetGetYReturnCode(E_FAIL);
  933. fake_sensor->TriggerFakeSensorReading(reading);
  934. reading->SetGetYReturnCode(S_OK);
  935. reading->SetGetZReturnCode(E_FAIL);
  936. fake_sensor->TriggerFakeSensorReading(reading);
  937. }
  938. // Tests if PlatformSensorReaderWinrtAbsOrientationEulerAngles correctly
  939. // converts sensor readings from Windows.Devices.Sensors.Inclinometer.
  940. TEST_F(PlatformSensorReaderTestWinrt, CheckInclinometerReadingConversion) {
  941. constexpr double expected_x = 10.0;
  942. constexpr double expected_y = 20.0;
  943. constexpr double expected_z = 30.0;
  944. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  945. ABI::Windows::Devices::Sensors::IInclinometerStatics,
  946. ABI::Windows::Devices::Sensors::IInclinometer,
  947. ABI::Windows::Devices::Sensors::Inclinometer,
  948. ABI::Windows::Devices::Sensors::IInclinometerReading,
  949. ABI::Windows::Devices::Sensors::IInclinometerReadingChangedEventArgs,
  950. ABI::Windows::Devices::Sensors::InclinometerReadingChangedEventArgs>>();
  951. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  952. auto sensor =
  953. std::make_unique<PlatformSensorReaderWinrtAbsOrientationEulerAngles>();
  954. sensor->InitForTesting(base::BindLambdaForTesting(
  955. [&](ABI::Windows::Devices::Sensors::IInclinometerStatics** sensor_factory)
  956. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  957. EXPECT_TRUE(sensor->Initialize());
  958. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  959. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  960. .WillOnce(testing::Invoke([&](const SensorReading& reading) {
  961. EXPECT_EQ(expected_x, reading.orientation_euler.x);
  962. EXPECT_EQ(expected_y, reading.orientation_euler.y);
  963. EXPECT_EQ(expected_z, reading.orientation_euler.z);
  964. }));
  965. sensor->SetClient(mock_client.get());
  966. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  967. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  968. auto reading = Microsoft::WRL::Make<FakeInclinometerReadingWinrt>(
  969. ABI::Windows::Foundation::DateTime{}, expected_x, expected_y, expected_z);
  970. fake_sensor->TriggerFakeSensorReading(reading);
  971. sensor->StopSensor();
  972. }
  973. // Tests that PlatformSensorReaderWinrtAbsOrientationEulerAngles does not notify
  974. // the client if an error occurs during sensor sample parsing.
  975. TEST_F(PlatformSensorReaderTestWinrt, FailedInclinometerSampleParse) {
  976. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  977. ABI::Windows::Devices::Sensors::IInclinometerStatics,
  978. ABI::Windows::Devices::Sensors::IInclinometer,
  979. ABI::Windows::Devices::Sensors::Inclinometer,
  980. ABI::Windows::Devices::Sensors::IInclinometerReading,
  981. ABI::Windows::Devices::Sensors::IInclinometerReadingChangedEventArgs,
  982. ABI::Windows::Devices::Sensors::InclinometerReadingChangedEventArgs>>();
  983. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  984. auto sensor =
  985. std::make_unique<PlatformSensorReaderWinrtAbsOrientationEulerAngles>();
  986. sensor->InitForTesting(base::BindLambdaForTesting(
  987. [&](ABI::Windows::Devices::Sensors::IInclinometerStatics** sensor_factory)
  988. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  989. EXPECT_TRUE(sensor->Initialize());
  990. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  991. sensor->SetClient(mock_client.get());
  992. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  993. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  994. auto reading = Microsoft::WRL::Make<FakeInclinometerReadingWinrt>(
  995. ABI::Windows::Foundation::DateTime{}, 0, 0, 0);
  996. reading->SetGetTimestampReturnCode(E_FAIL);
  997. fake_sensor->TriggerFakeSensorReading(reading);
  998. reading->SetGetTimestampReturnCode(S_OK);
  999. reading->SetGetXReturnCode(E_FAIL);
  1000. fake_sensor->TriggerFakeSensorReading(reading);
  1001. reading->SetGetXReturnCode(S_OK);
  1002. reading->SetGetYReturnCode(E_FAIL);
  1003. fake_sensor->TriggerFakeSensorReading(reading);
  1004. reading->SetGetYReturnCode(S_OK);
  1005. reading->SetGetZReturnCode(E_FAIL);
  1006. fake_sensor->TriggerFakeSensorReading(reading);
  1007. }
  1008. // Tests if PlatformSensorReaderWinrtAbsOrientationQuaternion correctly
  1009. // converts sensor readings from Windows.Devices.Sensors.OrientationSensor.
  1010. TEST_F(PlatformSensorReaderTestWinrt, CheckOrientationSensorReadingConversion) {
  1011. constexpr double expected_w = 11.0;
  1012. constexpr double expected_x = 22.0;
  1013. constexpr double expected_y = 33.0;
  1014. constexpr double expected_z = 44.0;
  1015. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1016. ABI::Windows::Devices::Sensors::IOrientationSensorStatics,
  1017. ABI::Windows::Devices::Sensors::IOrientationSensor,
  1018. ABI::Windows::Devices::Sensors::OrientationSensor,
  1019. ABI::Windows::Devices::Sensors::IOrientationSensorReading,
  1020. ABI::Windows::Devices::Sensors::IOrientationSensorReadingChangedEventArgs,
  1021. ABI::Windows::Devices::Sensors::
  1022. OrientationSensorReadingChangedEventArgs>>();
  1023. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1024. auto sensor =
  1025. std::make_unique<PlatformSensorReaderWinrtAbsOrientationQuaternion>();
  1026. sensor->InitForTesting(base::BindLambdaForTesting(
  1027. [&](ABI::Windows::Devices::Sensors::IOrientationSensorStatics**
  1028. sensor_factory) -> HRESULT {
  1029. return fake_sensor_factory.CopyTo(sensor_factory);
  1030. }));
  1031. EXPECT_TRUE(sensor->Initialize());
  1032. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1033. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  1034. .WillOnce(testing::Invoke([&](const SensorReading& reading) {
  1035. EXPECT_EQ(expected_w, reading.orientation_quat.w);
  1036. EXPECT_EQ(expected_x, reading.orientation_quat.x);
  1037. EXPECT_EQ(expected_y, reading.orientation_quat.y);
  1038. EXPECT_EQ(expected_z, reading.orientation_quat.z);
  1039. }));
  1040. sensor->SetClient(mock_client.get());
  1041. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1042. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1043. auto reading = Microsoft::WRL::Make<FakeOrientationSensorReadingWinrt>(
  1044. ABI::Windows::Foundation::DateTime{}, expected_w, expected_x, expected_y,
  1045. expected_z);
  1046. fake_sensor->TriggerFakeSensorReading(reading);
  1047. sensor->StopSensor();
  1048. }
  1049. // Tests that PlatformSensorReaderWinrtAbsOrientationQuaternion does not notify
  1050. // the client if an error occurs during sensor sample parsing.
  1051. TEST_F(PlatformSensorReaderTestWinrt, FailedOrientationSampleParse) {
  1052. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1053. ABI::Windows::Devices::Sensors::IOrientationSensorStatics,
  1054. ABI::Windows::Devices::Sensors::IOrientationSensor,
  1055. ABI::Windows::Devices::Sensors::OrientationSensor,
  1056. ABI::Windows::Devices::Sensors::IOrientationSensorReading,
  1057. ABI::Windows::Devices::Sensors::IOrientationSensorReadingChangedEventArgs,
  1058. ABI::Windows::Devices::Sensors::
  1059. OrientationSensorReadingChangedEventArgs>>();
  1060. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1061. auto sensor =
  1062. std::make_unique<PlatformSensorReaderWinrtAbsOrientationQuaternion>();
  1063. sensor->InitForTesting(base::BindLambdaForTesting(
  1064. [&](ABI::Windows::Devices::Sensors::IOrientationSensorStatics**
  1065. sensor_factory) -> HRESULT {
  1066. return fake_sensor_factory.CopyTo(sensor_factory);
  1067. }));
  1068. EXPECT_TRUE(sensor->Initialize());
  1069. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1070. sensor->SetClient(mock_client.get());
  1071. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1072. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1073. auto reading = Microsoft::WRL::Make<FakeOrientationSensorReadingWinrt>(
  1074. ABI::Windows::Foundation::DateTime{}, 0, 0, 0, 0);
  1075. reading->SetGetTimestampReturnCode(E_FAIL);
  1076. fake_sensor->TriggerFakeSensorReading(reading);
  1077. reading->SetGetTimestampReturnCode(S_OK);
  1078. reading->SetGetQuaternionReturnCode(E_FAIL);
  1079. fake_sensor->TriggerFakeSensorReading(reading);
  1080. }
  1081. TEST_F(PlatformSensorReaderTestWinrt, LightSensorThresholding) {
  1082. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1083. ABI::Windows::Devices::Sensors::ILightSensorStatics,
  1084. ABI::Windows::Devices::Sensors::ILightSensor,
  1085. ABI::Windows::Devices::Sensors::LightSensor,
  1086. ABI::Windows::Devices::Sensors::ILightSensorReading,
  1087. ABI::Windows::Devices::Sensors::ILightSensorReadingChangedEventArgs,
  1088. ABI::Windows::Devices::Sensors::LightSensorReadingChangedEventArgs>>();
  1089. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1090. auto sensor = std::make_unique<PlatformSensorReaderWinrtLightSensor>();
  1091. sensor->InitForTesting(base::BindLambdaForTesting(
  1092. [&](ABI::Windows::Devices::Sensors::ILightSensorStatics** sensor_factory)
  1093. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  1094. EXPECT_TRUE(sensor->Initialize());
  1095. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1096. bool expected_callback = false;
  1097. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  1098. .WillRepeatedly(testing::Invoke(
  1099. [&](const SensorReading&) { EXPECT_TRUE(expected_callback); }));
  1100. sensor->SetClient(mock_client.get());
  1101. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1102. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1103. float last_sent_lux = 1.0f;
  1104. auto threshold_helper = [&](bool expect_callback) {
  1105. expected_callback = expect_callback;
  1106. auto reading = Microsoft::WRL::Make<FakeLightSensorReadingWinrt>(
  1107. ABI::Windows::Foundation::DateTime{}, last_sent_lux);
  1108. fake_sensor->TriggerFakeSensorReading(reading);
  1109. };
  1110. // Expect callback, first sample
  1111. threshold_helper(true);
  1112. // No callback, threshold has not been met
  1113. last_sent_lux +=
  1114. PlatformSensorReaderWinrtLightSensor::kLuxPercentThreshold * 0.5f;
  1115. threshold_helper(false);
  1116. // Expect callback, threshold has been met since last reported sample
  1117. last_sent_lux +=
  1118. PlatformSensorReaderWinrtLightSensor::kLuxPercentThreshold * 0.6f;
  1119. threshold_helper(true);
  1120. // Expect callback, threshold has been met exactly
  1121. last_sent_lux += PlatformSensorReaderWinrtLightSensor::kLuxPercentThreshold;
  1122. threshold_helper(true);
  1123. sensor->StopSensor();
  1124. }
  1125. TEST_F(PlatformSensorReaderTestWinrt, AccelerometerThresholding) {
  1126. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1127. ABI::Windows::Devices::Sensors::IAccelerometerStatics,
  1128. ABI::Windows::Devices::Sensors::IAccelerometer,
  1129. ABI::Windows::Devices::Sensors::Accelerometer,
  1130. ABI::Windows::Devices::Sensors::IAccelerometerReading,
  1131. ABI::Windows::Devices::Sensors::IAccelerometerReadingChangedEventArgs,
  1132. ABI::Windows::Devices::Sensors::AccelerometerReadingChangedEventArgs>>(
  1133. Microsoft::WRL::Make<FakeAccelerometerSensorWinrt>());
  1134. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1135. auto sensor = std::make_unique<PlatformSensorReaderWinrtAccelerometer>();
  1136. sensor->InitForTesting(base::BindLambdaForTesting(
  1137. [&](ABI::Windows::Devices::Sensors::IAccelerometerStatics**
  1138. sensor_factory) -> HRESULT {
  1139. return fake_sensor_factory.CopyTo(sensor_factory);
  1140. }));
  1141. EXPECT_TRUE(sensor->Initialize());
  1142. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1143. bool expected_callback = false;
  1144. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  1145. .WillRepeatedly(testing::Invoke(
  1146. [&](const SensorReading&) { EXPECT_TRUE(expected_callback); }));
  1147. sensor->SetClient(mock_client.get());
  1148. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1149. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1150. double last_sent_x = 1.0f;
  1151. double last_sent_y = 2.0f;
  1152. double last_sent_z = 3.0f;
  1153. auto threshold_helper = [&](bool expect_callback) {
  1154. expected_callback = expect_callback;
  1155. auto reading = Microsoft::WRL::Make<FakeAccelerometerReadingWinrt>(
  1156. ABI::Windows::Foundation::DateTime{}, last_sent_x, last_sent_y,
  1157. last_sent_z);
  1158. fake_sensor->TriggerFakeSensorReading(reading);
  1159. };
  1160. // Expect callback, first sample
  1161. threshold_helper(true);
  1162. // No callback, threshold has not been met
  1163. last_sent_x += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold * 0.5f;
  1164. threshold_helper(false);
  1165. last_sent_y += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold * 0.5f;
  1166. threshold_helper(false);
  1167. last_sent_z += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold * 0.5f;
  1168. threshold_helper(false);
  1169. // Expect callback, threshold has been met since last reported sample
  1170. last_sent_x += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold * 0.6f;
  1171. threshold_helper(true);
  1172. last_sent_y += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold * 0.6f;
  1173. threshold_helper(true);
  1174. last_sent_z += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold * 0.6f;
  1175. threshold_helper(true);
  1176. // Expect callback, threshold has been met exactly
  1177. last_sent_x += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold;
  1178. threshold_helper(true);
  1179. last_sent_y += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold;
  1180. threshold_helper(true);
  1181. last_sent_z += PlatformSensorReaderWinrtAccelerometer::kAxisThreshold;
  1182. threshold_helper(true);
  1183. sensor->StopSensor();
  1184. }
  1185. TEST_F(PlatformSensorReaderTestWinrt, GyrometerThresholding) {
  1186. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1187. ABI::Windows::Devices::Sensors::IGyrometerStatics,
  1188. ABI::Windows::Devices::Sensors::IGyrometer,
  1189. ABI::Windows::Devices::Sensors::Gyrometer,
  1190. ABI::Windows::Devices::Sensors::IGyrometerReading,
  1191. ABI::Windows::Devices::Sensors::IGyrometerReadingChangedEventArgs,
  1192. ABI::Windows::Devices::Sensors::GyrometerReadingChangedEventArgs>>();
  1193. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1194. auto sensor = std::make_unique<PlatformSensorReaderWinrtGyrometer>();
  1195. sensor->InitForTesting(base::BindLambdaForTesting(
  1196. [&](ABI::Windows::Devices::Sensors::IGyrometerStatics** sensor_factory)
  1197. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  1198. EXPECT_TRUE(sensor->Initialize());
  1199. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1200. bool expected_callback = false;
  1201. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  1202. .WillRepeatedly(testing::Invoke(
  1203. [&](const SensorReading&) { EXPECT_TRUE(expected_callback); }));
  1204. sensor->SetClient(mock_client.get());
  1205. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1206. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1207. double last_sent_x = 3.0f;
  1208. double last_sent_y = 4.0f;
  1209. double last_sent_z = 5.0f;
  1210. auto threshold_helper = [&](bool expect_callback) {
  1211. expected_callback = expect_callback;
  1212. auto reading = Microsoft::WRL::Make<FakeGyrometerReadingWinrt>(
  1213. ABI::Windows::Foundation::DateTime{}, last_sent_x, last_sent_y,
  1214. last_sent_z);
  1215. fake_sensor->TriggerFakeSensorReading(reading);
  1216. };
  1217. // Expect callback, first sample
  1218. threshold_helper(true);
  1219. // No callback, threshold has not been met
  1220. last_sent_x += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold * 0.5f;
  1221. threshold_helper(false);
  1222. last_sent_y += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold * 0.5f;
  1223. threshold_helper(false);
  1224. last_sent_z += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold * 0.5f;
  1225. threshold_helper(false);
  1226. // Expect callback, threshold has been met since last reported sample
  1227. last_sent_x += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold * 0.6f;
  1228. threshold_helper(true);
  1229. last_sent_y += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold * 0.6f;
  1230. threshold_helper(true);
  1231. last_sent_z += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold * 0.6f;
  1232. threshold_helper(true);
  1233. // Expect callback, threshold has been met exactly
  1234. last_sent_x += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold;
  1235. threshold_helper(true);
  1236. last_sent_y += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold;
  1237. threshold_helper(true);
  1238. last_sent_z += PlatformSensorReaderWinrtGyrometer::kDegreeThreshold;
  1239. threshold_helper(true);
  1240. sensor->StopSensor();
  1241. }
  1242. TEST_F(PlatformSensorReaderTestWinrt, MagnetometerThresholding) {
  1243. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1244. ABI::Windows::Devices::Sensors::IMagnetometerStatics,
  1245. ABI::Windows::Devices::Sensors::IMagnetometer,
  1246. ABI::Windows::Devices::Sensors::Magnetometer,
  1247. ABI::Windows::Devices::Sensors::IMagnetometerReading,
  1248. ABI::Windows::Devices::Sensors::IMagnetometerReadingChangedEventArgs,
  1249. ABI::Windows::Devices::Sensors::MagnetometerReadingChangedEventArgs>>();
  1250. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1251. auto sensor = std::make_unique<PlatformSensorReaderWinrtMagnetometer>();
  1252. sensor->InitForTesting(base::BindLambdaForTesting(
  1253. [&](ABI::Windows::Devices::Sensors::IMagnetometerStatics** sensor_factory)
  1254. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  1255. EXPECT_TRUE(sensor->Initialize());
  1256. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1257. bool expected_callback = false;
  1258. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  1259. .WillRepeatedly(testing::Invoke(
  1260. [&](const SensorReading&) { EXPECT_TRUE(expected_callback); }));
  1261. sensor->SetClient(mock_client.get());
  1262. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1263. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1264. double last_sent_x = 3.0f;
  1265. double last_sent_y = 4.0f;
  1266. double last_sent_z = 5.0f;
  1267. auto threshold_helper = [&](bool expect_callback) {
  1268. expected_callback = expect_callback;
  1269. auto reading = Microsoft::WRL::Make<FakeMagnetometerReadingWinrt>(
  1270. ABI::Windows::Foundation::DateTime{}, last_sent_x, last_sent_y,
  1271. last_sent_z);
  1272. fake_sensor->TriggerFakeSensorReading(reading);
  1273. };
  1274. // Expect callback, first sample
  1275. threshold_helper(true);
  1276. // No callback, threshold has not been met
  1277. last_sent_x +=
  1278. PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold * 0.5f;
  1279. threshold_helper(false);
  1280. last_sent_y +=
  1281. PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold * 0.5f;
  1282. threshold_helper(false);
  1283. last_sent_z +=
  1284. PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold * 0.5f;
  1285. threshold_helper(false);
  1286. // Expect callback, threshold has been met since last reported sample
  1287. last_sent_x +=
  1288. PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold * 0.6f;
  1289. threshold_helper(true);
  1290. last_sent_y +=
  1291. PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold * 0.6f;
  1292. threshold_helper(true);
  1293. last_sent_z +=
  1294. PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold * 0.6f;
  1295. threshold_helper(true);
  1296. // Expect callback, threshold has been met exactly
  1297. last_sent_x += PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold;
  1298. threshold_helper(true);
  1299. last_sent_y += PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold;
  1300. threshold_helper(true);
  1301. last_sent_z += PlatformSensorReaderWinrtMagnetometer::kMicroteslaThreshold;
  1302. threshold_helper(true);
  1303. sensor->StopSensor();
  1304. }
  1305. TEST_F(PlatformSensorReaderTestWinrt, AbsOrientationEulerThresholding) {
  1306. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1307. ABI::Windows::Devices::Sensors::IInclinometerStatics,
  1308. ABI::Windows::Devices::Sensors::IInclinometer,
  1309. ABI::Windows::Devices::Sensors::Inclinometer,
  1310. ABI::Windows::Devices::Sensors::IInclinometerReading,
  1311. ABI::Windows::Devices::Sensors::IInclinometerReadingChangedEventArgs,
  1312. ABI::Windows::Devices::Sensors::InclinometerReadingChangedEventArgs>>();
  1313. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1314. auto sensor =
  1315. std::make_unique<PlatformSensorReaderWinrtAbsOrientationEulerAngles>();
  1316. sensor->InitForTesting(base::BindLambdaForTesting(
  1317. [&](ABI::Windows::Devices::Sensors::IInclinometerStatics** sensor_factory)
  1318. -> HRESULT { return fake_sensor_factory.CopyTo(sensor_factory); }));
  1319. EXPECT_TRUE(sensor->Initialize());
  1320. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1321. bool expected_callback = false;
  1322. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  1323. .WillRepeatedly(testing::Invoke(
  1324. [&](const SensorReading&) { EXPECT_TRUE(expected_callback); }));
  1325. sensor->SetClient(mock_client.get());
  1326. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1327. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1328. double last_sent_x = 3.0f;
  1329. double last_sent_y = 4.0f;
  1330. double last_sent_z = 5.0f;
  1331. auto threshold_helper = [&](bool expect_callback) {
  1332. expected_callback = expect_callback;
  1333. auto reading = Microsoft::WRL::Make<FakeInclinometerReadingWinrt>(
  1334. ABI::Windows::Foundation::DateTime{}, last_sent_x, last_sent_y,
  1335. last_sent_z);
  1336. fake_sensor->TriggerFakeSensorReading(reading);
  1337. };
  1338. // Expect callback, first sample
  1339. threshold_helper(true);
  1340. // No callback, threshold has not been met
  1341. last_sent_x +=
  1342. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold *
  1343. 0.5f;
  1344. threshold_helper(false);
  1345. last_sent_y +=
  1346. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold *
  1347. 0.5f;
  1348. threshold_helper(false);
  1349. last_sent_z +=
  1350. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold *
  1351. 0.5f;
  1352. threshold_helper(false);
  1353. // Expect callback, threshold has been met since last reported sample
  1354. last_sent_x +=
  1355. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold *
  1356. 0.6f;
  1357. threshold_helper(true);
  1358. last_sent_y +=
  1359. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold *
  1360. 0.6f;
  1361. threshold_helper(true);
  1362. last_sent_z +=
  1363. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold *
  1364. 0.6f;
  1365. threshold_helper(true);
  1366. // Expect callback, threshold has been met exactly
  1367. last_sent_x +=
  1368. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold;
  1369. threshold_helper(true);
  1370. last_sent_y +=
  1371. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold;
  1372. threshold_helper(true);
  1373. last_sent_z +=
  1374. PlatformSensorReaderWinrtAbsOrientationEulerAngles::kDegreeThreshold;
  1375. threshold_helper(true);
  1376. sensor->StopSensor();
  1377. }
  1378. TEST_F(PlatformSensorReaderTestWinrt, AbsOrientationQuatThresholding) {
  1379. auto fake_sensor_factory = Microsoft::WRL::Make<FakeSensorFactoryWinrt<
  1380. ABI::Windows::Devices::Sensors::IOrientationSensorStatics,
  1381. ABI::Windows::Devices::Sensors::IOrientationSensor,
  1382. ABI::Windows::Devices::Sensors::OrientationSensor,
  1383. ABI::Windows::Devices::Sensors::IOrientationSensorReading,
  1384. ABI::Windows::Devices::Sensors::IOrientationSensorReadingChangedEventArgs,
  1385. ABI::Windows::Devices::Sensors::
  1386. OrientationSensorReadingChangedEventArgs>>();
  1387. auto fake_sensor = fake_sensor_factory->fake_sensor_;
  1388. auto sensor =
  1389. std::make_unique<PlatformSensorReaderWinrtAbsOrientationQuaternion>();
  1390. sensor->InitForTesting(base::BindLambdaForTesting(
  1391. [&](ABI::Windows::Devices::Sensors::IOrientationSensorStatics**
  1392. sensor_factory) -> HRESULT {
  1393. return fake_sensor_factory.CopyTo(sensor_factory);
  1394. }));
  1395. EXPECT_TRUE(sensor->Initialize());
  1396. auto mock_client = std::make_unique<testing::NiceMock<MockClient>>();
  1397. bool expected_callback = false;
  1398. EXPECT_CALL(*mock_client, OnReadingUpdated(::testing::_))
  1399. .WillRepeatedly(testing::Invoke(
  1400. [&](const SensorReading&) { EXPECT_TRUE(expected_callback); }));
  1401. sensor->SetClient(mock_client.get());
  1402. PlatformSensorConfiguration sensor_config(kExpectedReportFrequencySet);
  1403. EXPECT_TRUE(sensor->StartSensor(sensor_config));
  1404. double last_sent_rad = 1.0;
  1405. auto threshold_helper = [&](bool expect_callback) {
  1406. expected_callback = expect_callback;
  1407. auto quat = gfx::Quaternion(gfx::Vector3dF(1.0, 0, 0), last_sent_rad);
  1408. auto reading = Microsoft::WRL::Make<FakeOrientationSensorReadingWinrt>(
  1409. ABI::Windows::Foundation::DateTime{}, quat.w(), quat.x(), quat.y(),
  1410. quat.z());
  1411. fake_sensor->TriggerFakeSensorReading(reading);
  1412. };
  1413. // Expect callback, first sample
  1414. threshold_helper(true);
  1415. // No callback, threshold has not been met
  1416. last_sent_rad +=
  1417. PlatformSensorReaderWinrtAbsOrientationQuaternion::kRadianThreshold *
  1418. 0.5f;
  1419. threshold_helper(false);
  1420. // Expect callback, threshold has been met since last reported sample
  1421. last_sent_rad +=
  1422. PlatformSensorReaderWinrtAbsOrientationQuaternion::kRadianThreshold *
  1423. 0.6f;
  1424. threshold_helper(true);
  1425. // Expect callback, threshold has been met exactly
  1426. last_sent_rad +=
  1427. PlatformSensorReaderWinrtAbsOrientationQuaternion::kRadianThreshold;
  1428. threshold_helper(true);
  1429. sensor->StopSensor();
  1430. }
  1431. } // namespace device