File: unit_test.cpp

package info (click to toggle)
ros-image-common 1.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 736 kB
  • sloc: cpp: 3,310; xml: 186; python: 38; makefile: 6
file content (739 lines) | stat: -rw-r--r-- 23,414 bytes parent folder | download
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/* $Id$ */

/*********************************************************************
* Software License Agreement (BSD License)
*
*  Copyright (c) 2010 Jack O'Quin
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the author nor other contributors may be
*     used to endorse or promote products derived from this software
*     without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/

#include <unistd.h>
#include <stdlib.h>
#include <ros/ros.h>
#include <ros/package.h>
#include "camera_info_manager/camera_info_manager.h"
#include <sensor_msgs/distortion_models.h>
#include <string>
#include <gtest/gtest.h>

///////////////////////////////////////////////////////////////
// global test data
///////////////////////////////////////////////////////////////

namespace
{
  const std::string g_package_name("camera_info_manager");
  const std::string g_test_name("test_calibration");
  const std::string g_package_filename("/tests/" + g_test_name +".yaml");
  const std::string g_package_url("package://" + g_package_name
                                  + g_package_filename);
  const std::string g_package_name_url("package://" + g_package_name
                                       + "/tests/${NAME}.yaml");
  const std::string g_default_url("file://${ROS_HOME}/camera_info/${NAME}.yaml");
  const std::string g_camera_name("08144361026320a0");
}

///////////////////////////////////////////////////////////////
// utility functions
///////////////////////////////////////////////////////////////

// compare CameraInfo fields that are saved and loaded for calibration
void compare_calibration(const sensor_msgs::CameraInfo &exp,
                         const sensor_msgs::CameraInfo &ci)
{
  // check image size
  EXPECT_EQ(exp.width, ci.width);
  EXPECT_EQ(exp.height, ci.height);

  // check distortion coefficients
  EXPECT_EQ(exp.distortion_model, ci.distortion_model);
  EXPECT_EQ(exp.D.size(), ci.D.size());
  for (unsigned i = 0; i < ci.D.size(); ++i)
    {
      EXPECT_EQ(exp.D[i], ci.D[i]);
    }

  // check camera matrix
  for (unsigned i = 0; i < ci.K.size(); ++i)
    {
      EXPECT_EQ(exp.K[i], ci.K[i]);
    }

  // check rectification matrix
  for (unsigned i = 0; i < ci.R.size(); ++i)
    {
      EXPECT_EQ(exp.R[i], ci.R[i]);
    }

  // check projection matrix
  for (unsigned i = 0; i < ci.P.size(); ++i)
    {
      EXPECT_EQ(exp.P[i], ci.P[i]);
    }

  // check binning
  EXPECT_EQ(exp.binning_x, ci.binning_x);
  EXPECT_EQ(exp.binning_y, ci.binning_y);

  // check region of interest
  EXPECT_EQ(exp.roi.x_offset, ci.roi.x_offset);
  EXPECT_EQ(exp.roi.y_offset, ci.roi.y_offset);
  EXPECT_EQ(exp.roi.height, ci.roi.height);
  EXPECT_EQ(exp.roi.width, ci.roi.width);
  EXPECT_EQ(exp.roi.do_rectify, ci.roi.do_rectify);
}

// make sure this file does not exist
void delete_file(std::string filename)
{
  int rc = unlink(filename.c_str());
  if (rc != 0)
    {
      if (errno != ENOENT)
        ROS_INFO_STREAM("unexpected unlink() error: " << errno);
    }
}

void delete_default_file(void)
{
  std::string ros_home("/tmp");
  setenv("ROS_HOME", ros_home.c_str(), true);
  std::string tmpFile(ros_home + "/camera_info/camera.yaml");
  delete_file(tmpFile);
}

void do_system(const std::string &command)
{
  int rc = system(command.c_str());
  if (rc)
    std::cout << command << " returns " << rc;
}

void delete_tmp_camera_info_directory(void)
{
  do_system(std::string("rm -rf /tmp/camera_info"));
}

void make_tmp_camera_info_directory(void)
{
  do_system(std::string("mkdir -p /tmp/camera_info"));
}

// These data must match the contents of test_calibration.yaml.
sensor_msgs::CameraInfo expected_calibration(void)
{
  sensor_msgs::CameraInfo ci;

  ci.width = 640u;
  ci.height = 480u;

  // set distortion coefficients
  ci.distortion_model = sensor_msgs::distortion_models::PLUMB_BOB;
  ci.D.resize(5);
  ci.D[0] = -1.04482;
  ci.D[1] = 1.59252;
  ci.D[2] = -0.0196308;
  ci.D[3] = 0.0287906;
  ci.D[4] = 0.0;

  // set camera matrix
  ci.K[0] = 1168.68;
  ci.K[1] = 0.0;
  ci.K[2] = 295.015;
  ci.K[3] = 0.0;
  ci.K[4] = 1169.01;
  ci.K[5] = 252.247;
  ci.K[6] = 0.0;
  ci.K[7] = 0.0;
  ci.K[8] = 1.0;

  // set rectification matrix
  ci.R[0] = 1.0;
  ci.R[1] = 0.0;
  ci.R[2] = 0.0;
  ci.R[3] = 0.0;
  ci.R[4] = 1.0;
  ci.R[5] = 0.0;
  ci.R[6] = 0.0;
  ci.R[7] = 0.0;
  ci.R[8] = 1.0;

  // set projection matrix
  ci.P[0] = ci.K[0];
  ci.P[1] = ci.K[1];
  ci.P[2] = ci.K[2];
  ci.P[3] = 0.0;
  ci.P[4] = ci.K[3];
  ci.P[5] = ci.K[4];
  ci.P[6] = ci.K[5];
  ci.P[7] = 0.0;
  ci.P[8] = ci.K[6];
  ci.P[9] = ci.K[7];
  ci.P[10] = ci.K[8];
  ci.P[11] = 0.0;

  ci.binning_x = 1;
  ci.binning_y = 2;

  ci.roi.x_offset = 20;
  ci.roi.y_offset = 40;
  ci.roi.height = 400;
  ci.roi.width = 600;

  return ci;
}

// issue SetCameraInfo service request
bool set_calibration(ros::NodeHandle node,
                     const sensor_msgs::CameraInfo &calib)
{
  ros::ServiceClient client =
    node.serviceClient<sensor_msgs::SetCameraInfo>("set_camera_info");
  sensor_msgs::SetCameraInfo set_camera_info;
  set_camera_info.request.camera_info = calib;
  bool success;
  EXPECT_TRUE((success = client.call(set_camera_info)));
  return success;
}

// resolve URL string, result should be as expected
void check_url_substitution(ros::NodeHandle node,
                            const std::string &url,
                            const std::string &exp_url,
                            const std::string &camera_name)
{
  camera_info_manager::CameraInfoManager cinfo(node, camera_name, url);
  std::string sub_url = cinfo.resolveURL(url, camera_name);
  EXPECT_EQ(sub_url, exp_url);
}

///////////////////////////////////////////////////////////////
// Test cases
///////////////////////////////////////////////////////////////

// Test that valid camera names are accepted
TEST(CameraName, validNames)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  EXPECT_TRUE(cinfo.setCameraName(std::string("a")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("1")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("_")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("abcdefghijklmnopqrstuvwxyz")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("ABCDEFGHIJKLMNOPQRSTUVWXYZ")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("0123456789")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("0123456789abcdef")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("A1")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("9z")));
  EXPECT_TRUE(cinfo.setCameraName(std::string("08144361026320a0_640x480_mono8")));

}

// Test that invalid camera names are rejected
TEST(CameraName, invalidNames)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  EXPECT_FALSE(cinfo.setCameraName(std::string("")));
  EXPECT_FALSE(cinfo.setCameraName(std::string("-21")));
  EXPECT_FALSE(cinfo.setCameraName(std::string("C++")));
  EXPECT_FALSE(cinfo.setCameraName(std::string("file:///tmp/url.yaml")));
  EXPECT_FALSE(cinfo.setCameraName(std::string("file://${INVALID}/xxx.yaml")));
}

// Test that valid URLs are accepted
TEST(UrlValidation, validURLs)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  EXPECT_TRUE(cinfo.validateURL(std::string("")));
  EXPECT_TRUE(cinfo.validateURL(std::string("file:///")));
  EXPECT_TRUE(cinfo.validateURL(std::string("file:///tmp/url.yaml")));
  EXPECT_TRUE(cinfo.validateURL(std::string("File:///tmp/url.ini")));
  EXPECT_TRUE(cinfo.validateURL(std::string("FILE:///tmp/url.yaml")));
  EXPECT_TRUE(cinfo.validateURL(g_default_url));
  EXPECT_TRUE(cinfo.validateURL(g_package_url));
  EXPECT_TRUE(cinfo.validateURL(std::string("package://no_such_package/calibration.yaml")));
  EXPECT_TRUE(cinfo.validateURL(std::string("packAge://camera_info_manager/x")));
}

// Test that invalid URLs are rejected
TEST(UrlValidation, invalidURLs)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  EXPECT_FALSE(cinfo.validateURL(std::string("file://")));
  EXPECT_FALSE(cinfo.validateURL(std::string("flash:///")));
  EXPECT_FALSE(cinfo.validateURL(std::string("html://ros.org/wiki/camera_info_manager")));
  EXPECT_FALSE(cinfo.validateURL(std::string("package://")));
  EXPECT_FALSE(cinfo.validateURL(std::string("package:///")));
  EXPECT_FALSE(cinfo.validateURL(std::string("package://calibration.yaml")));
  EXPECT_FALSE(cinfo.validateURL(std::string("package://camera_info_manager/")));
}

// Test ability to provide uncalibrated CameraInfo
TEST(GetInfo, uncalibrated)
{
  ros::NodeHandle node;

  delete_default_file();

  camera_info_manager::CameraInfoManager cinfo(node);
  EXPECT_FALSE(cinfo.isCalibrated());

  sensor_msgs::CameraInfo ci(cinfo.getCameraInfo());
  sensor_msgs::CameraInfo exp;
  compare_calibration(exp, ci);
}

// Test ability to load calibrated CameraInfo
TEST(GetInfo, calibrated)
{
  ros::NodeHandle node;

  delete_default_file();

  camera_info_manager::CameraInfoManager cinfo(node);
  EXPECT_FALSE(cinfo.isCalibrated());

  std::string pkgPath(ros::package::getPath(g_package_name));
  std::string url("file://" + pkgPath + "/tests/test_calibration.yaml");
  EXPECT_TRUE(cinfo.loadCameraInfo(url));
  EXPECT_TRUE(cinfo.isCalibrated());

  sensor_msgs::CameraInfo ci(cinfo.getCameraInfo());
  sensor_msgs::CameraInfo exp(expected_calibration());
  compare_calibration(exp, ci);
}

// Test ability to load calibrated CameraInfo from package
TEST(GetInfo, fromPackage)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  EXPECT_TRUE(cinfo.loadCameraInfo(g_package_url));
  EXPECT_TRUE(cinfo.isCalibrated());

  sensor_msgs::CameraInfo ci(cinfo.getCameraInfo());
  sensor_msgs::CameraInfo exp(expected_calibration());
  compare_calibration(exp, ci);
}

// Test ability to access named calibrated CameraInfo from package
TEST(GetInfo, fromPackageWithName)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node, g_test_name,
                                               g_package_name_url);
  EXPECT_TRUE(cinfo.isCalibrated());

  sensor_msgs::CameraInfo ci(cinfo.getCameraInfo());
  sensor_msgs::CameraInfo exp(expected_calibration());
  compare_calibration(exp, ci);
}

// Test load of unresolved "package:" URL files
TEST(GetInfo, unresolvedLoads)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  EXPECT_FALSE(cinfo.loadCameraInfo(std::string("package://")));
  EXPECT_FALSE(cinfo.isCalibrated());

  EXPECT_FALSE(cinfo.loadCameraInfo(std::string("package:///calibration.yaml")));
  EXPECT_FALSE(cinfo.isCalibrated());

  EXPECT_FALSE(cinfo.loadCameraInfo(std::string("package://no_such_package/calibration.yaml")));
  EXPECT_FALSE(cinfo.isCalibrated());

  EXPECT_FALSE(cinfo.loadCameraInfo(std::string("package://camera_info_manager/no_such_file.yaml")));
  EXPECT_FALSE(cinfo.isCalibrated());
}

// Test load of "package:" URL after changing name
TEST(GetInfo, nameChange)
{
  ros::NodeHandle node;
  const std::string missing_file("no_such_file");

  // first declare using non-existent camera name
  camera_info_manager::CameraInfoManager cinfo(node, missing_file,
                                               g_package_name_url);
  EXPECT_FALSE(cinfo.isCalibrated());

  // set name so it resolves to a test file that does exist
  EXPECT_TRUE(cinfo.setCameraName(g_test_name));
  EXPECT_TRUE(cinfo.isCalibrated());
  sensor_msgs::CameraInfo ci(cinfo.getCameraInfo());
  sensor_msgs::CameraInfo exp(expected_calibration());
  compare_calibration(exp, ci);
}

// Test load of invalid CameraInfo URLs
TEST(GetInfo, invalidLoads)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  EXPECT_FALSE(cinfo.loadCameraInfo(std::string("flash:///")));
  EXPECT_FALSE(cinfo.isCalibrated());

  EXPECT_FALSE(cinfo.loadCameraInfo(std::string("html://ros.org/wiki/camera_info_manager")));
  EXPECT_FALSE(cinfo.isCalibrated());

  sensor_msgs::CameraInfo ci(cinfo.getCameraInfo());
  sensor_msgs::CameraInfo exp;
  compare_calibration(exp, ci);
}

// Test ability to set CameraInfo directly
TEST(SetInfo, setCameraInfo)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  // issue calibration service request
  sensor_msgs::CameraInfo exp(expected_calibration());
  bool success = cinfo.setCameraInfo(exp);
  EXPECT_TRUE(success);

  // only check results if the service succeeded, avoiding confusing
  // and redundant failure messages
  if (success)
    {
      // check that it worked
      EXPECT_TRUE(cinfo.isCalibrated());
      sensor_msgs::CameraInfo ci = cinfo.getCameraInfo();
      compare_calibration(exp, ci);
    }
}

// Test ability to set calibrated CameraInfo
TEST(SetInfo, setCalibration)
{
  ros::NodeHandle node;
  camera_info_manager::CameraInfoManager cinfo(node);

  // issue calibration service request
  sensor_msgs::CameraInfo exp(expected_calibration());
  bool success = set_calibration(node, exp);

  // only check results if the service succeeded, avoiding confusing
  // and redundant failure messages
  if (success)
    {
      // check that it worked
      EXPECT_TRUE(cinfo.isCalibrated());
      sensor_msgs::CameraInfo ci = cinfo.getCameraInfo();
      compare_calibration(exp, ci);
    }
}

// Test ability to save calibrated CameraInfo in default URL
TEST(SetInfo, saveCalibrationDefault)
{
  ros::NodeHandle node;
  sensor_msgs::CameraInfo exp(expected_calibration());
  bool success;

  // Set ${ROS_HOME} to /tmp, then delete the /tmp/camera_info
  // directory and everything in it.
  setenv("ROS_HOME", "/tmp", true);
  delete_tmp_camera_info_directory();

  {
    // create instance to save calibrated data
    camera_info_manager::CameraInfoManager cinfo(node);
    EXPECT_FALSE(cinfo.isCalibrated());

    // issue calibration service request
    success = set_calibration(node, exp);
    EXPECT_TRUE(cinfo.isCalibrated());
  }

  // only check results if the service succeeded, avoiding confusing
  // and redundant failure messages
  if (success)
    {
      // create a new instance to load saved calibration
      camera_info_manager::CameraInfoManager cinfo2(node);
      EXPECT_TRUE(cinfo2.isCalibrated());
      if (cinfo2.isCalibrated())
        {
          sensor_msgs::CameraInfo ci(cinfo2.getCameraInfo());
          compare_calibration(exp, ci);
        }
    }
}

// Test ability to save calibrated CameraInfo to default location with
// explicit camera name
TEST(SetInfo, saveCalibrationCameraName)
{
  ros::NodeHandle node;
  sensor_msgs::CameraInfo exp(expected_calibration());
  bool success;

  // set ${ROS_HOME} to /tmp, delete the calibration file
  std::string ros_home("/tmp");
  setenv("ROS_HOME", ros_home.c_str(), true);
  std::string tmpFile(ros_home + "/camera_info/" + g_camera_name + ".yaml");
  delete_file(tmpFile);

  {
    // create instance to save calibrated data
    camera_info_manager::CameraInfoManager cinfo(node, g_camera_name);
    success = set_calibration(node, exp);
    EXPECT_TRUE(cinfo.isCalibrated());
  }

  // only check results if the service succeeded, avoiding confusing
  // and redundant failure messages
  if (success)
    {
      // create a new instance to load saved calibration
      camera_info_manager::CameraInfoManager cinfo2(node);
      std::string url = "file://" + tmpFile;
      cinfo2.loadCameraInfo(std::string(url));
      EXPECT_TRUE(cinfo2.isCalibrated());
      if (cinfo2.isCalibrated())
        {
          sensor_msgs::CameraInfo ci(cinfo2.getCameraInfo());
          compare_calibration(exp, ci);
        }
    }
}

// Test ability to save calibrated CameraInfo in a file
TEST(SetInfo, saveCalibrationFile)
{
  return;

  ros::NodeHandle node;
  sensor_msgs::CameraInfo exp(expected_calibration());
  std::string cname("camera");
  std::string tmpFile("/tmp/camera_info_manager_calibration_test.yaml");
  std::string url = "file://" + tmpFile;
  bool success;

  // first, delete the file
  delete_file(tmpFile);

  {
    // create instance to save calibrated data
    camera_info_manager::CameraInfoManager cinfo(node, cname, url);
    success = set_calibration(node, exp);
    EXPECT_TRUE(cinfo.isCalibrated());
  }

  // only check results if the service succeeded, avoiding confusing
  // and redundant failure messages
  if (success)
    {
      // create a new instance to load saved calibration
      camera_info_manager::CameraInfoManager cinfo2(node, cname, url);
      EXPECT_TRUE(cinfo2.isCalibrated());
      if (cinfo2.isCalibrated())
        {
          sensor_msgs::CameraInfo ci(cinfo2.getCameraInfo());
          compare_calibration(exp, ci);
        }
    }
}

// Test ability to save calibrated CameraInfo in a package
// (needs write access).
TEST(SetInfo, saveCalibrationPackage)
{
  GTEST_SKIP();
  ros::NodeHandle node;
  sensor_msgs::CameraInfo exp(expected_calibration());
  std::string pkgPath(ros::package::getPath(g_package_name));
  std::string filename(pkgPath + g_package_filename);
  bool success;

  // first, delete the file
  delete_file(filename);

  {
    // create instance to save calibrated data
    camera_info_manager::CameraInfoManager cinfo(node, g_camera_name,
                                                 g_package_url);
    success = set_calibration(node, exp);
    EXPECT_TRUE(cinfo.isCalibrated());
  }

  // only check results if the service succeeded, avoiding confusing
  // and redundant failure messages
  if (success)
    {
      // create a new instance to load saved calibration
      camera_info_manager::CameraInfoManager cinfo2(node, g_camera_name,
                                                    g_package_url);
      EXPECT_TRUE(cinfo2.isCalibrated());
      if (cinfo2.isCalibrated())
        {
          sensor_msgs::CameraInfo ci(cinfo2.getCameraInfo());
          compare_calibration(exp, ci);
        }
    }
}

TEST(UrlSubstitution, cameraName)
{
  ros::NodeHandle node;
  std::string name_url;
  std::string exp_url;

  // resolve a GUID camera name
  name_url = "package://" + g_package_name + "/tests/${NAME}.yaml";
  exp_url = "package://" + g_package_name + "/tests/" + g_camera_name + ".yaml";
  check_url_substitution(node, name_url, exp_url, g_camera_name);

  // substitute camera name "test"
  name_url = "package://" + g_package_name + "/tests/${NAME}_calibration.yaml";
  std::string test_name("test");
  exp_url = "package://" + g_package_name + "/tests/" + test_name
    + "_calibration.yaml";
  check_url_substitution(node, name_url, exp_url, test_name);

  // with an '_' in the name
  test_name = "camera_1024x768";
  exp_url = "package://" + g_package_name + "/tests/" + test_name
    + "_calibration.yaml";
  check_url_substitution(node, name_url, exp_url, test_name);

  // substitute empty camera name
  name_url = "package://" + g_package_name + "/tests/${NAME}_calibration.yaml";
  std::string empty_name("");
  exp_url = "package://" + g_package_name + "/tests/" + empty_name
    + "_calibration.yaml";
  check_url_substitution(node, name_url, exp_url, empty_name);

  // substitute test camera calibration from this package
  check_url_substitution(node, g_package_name_url, g_package_url, g_test_name);
}

TEST(UrlSubstitution, rosHome)
{
  ros::NodeHandle node;
  std::string name_url;
  std::string exp_url;
  char *home_env = getenv("HOME");
  std::string home(home_env);

  // resolve ${ROS_HOME} with environment variable undefined
  unsetenv("ROS_HOME");
  name_url = "file://${ROS_HOME}/camera_info/test_camera.yaml";
  exp_url = "file://" + home + "/.ros/camera_info/test_camera.yaml";
  check_url_substitution(node, name_url, exp_url, g_camera_name);

  // resolve ${ROS_HOME} with environment variable defined
  setenv("ROS_HOME", "/my/ros/home", true);
  name_url = "file://${ROS_HOME}/camera_info/test_camera.yaml";
  exp_url = "file:///my/ros/home/camera_info/test_camera.yaml";
  check_url_substitution(node, name_url, exp_url, g_camera_name);
}

TEST(UrlSubstitution, unmatchedDollarSigns)
{
  ros::NodeHandle node;

  // test for "$$" in the URL (NAME should be resolved)
  std::string name_url("file:///tmp/$${NAME}.yaml");
  std::string exp_url("file:///tmp/$" + g_camera_name + ".yaml");
  check_url_substitution(node, name_url, exp_url, g_camera_name);

  // test for "$" in middle of string
  name_url = "file:///$whatever.yaml";
  check_url_substitution(node, name_url, name_url, g_camera_name);

  // test for "$$" in middle of string
  name_url = "file:///something$$whatever.yaml";
  check_url_substitution(node, name_url, name_url, g_camera_name);

  // test for "$$" at end of string
  name_url = "file:///$$";
  check_url_substitution(node, name_url, name_url, g_camera_name);
}

TEST(UrlSubstitution, emptyURL)
{
  // test that empty URL is handled correctly
  ros::NodeHandle node;
  std::string empty_url("");
  check_url_substitution(node, empty_url, empty_url, g_camera_name);
}

TEST(UrlSubstitution, invalidVariables)
{
  ros::NodeHandle node;
  std::string name_url;

  // missing "{...}"
  name_url = "file:///tmp/$NAME.yaml";
  check_url_substitution(node, name_url, name_url, g_camera_name);

  // invalid substitution variable name
  name_url = "file:///tmp/${INVALID}/calibration.yaml";
  check_url_substitution(node, name_url, name_url, g_camera_name);

  // truncated substitution variable
  name_url = "file:///tmp/${NAME";
  check_url_substitution(node, name_url, name_url, g_camera_name);

  // missing substitution variable
  name_url = "file:///tmp/${}";
  check_url_substitution(node, name_url, name_url, g_camera_name);

  // no exception thrown for single "$" at end of string
  name_url = "file:///$";
  check_url_substitution(node, name_url, name_url, g_camera_name);
}

// Run all the tests that were declared with TEST()
int main(int argc, char **argv)
{
  ros::init(argc, argv, "camera_info_manager_unit_test");
  testing::InitGoogleTest(&argc, argv);

  // create asynchronous thread for handling service requests
  ros::AsyncSpinner spinner(1);
  spinner.start();

  // run the tests in this thread
  return RUN_ALL_TESTS();
}