Read about our upcoming Code of Conduct on this issue

UTestManager.cpp 14.2 KB
Newer Older
1
//---------------------------------------------------------------------------
Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
2
3
#ifndef UTESTMANAGER_CPP
#define UTESTMANAGER_CPP
4
5

#include "UTestManager.h"
6
#include "UApplication.h"
7

8
#include <boost/lexical_cast.hpp>
9
#include <boost/algorithm/string.hpp>
10

11
12
namespace RDK {

13
14
///     ,       
/// !    ,   
15
16
17
18
19
20
21
22
23
24
25
26
27
#define TEST_PROPERTY_WITH_TYPE(type) \
  if(property->GetLanguageType() == typeid(type)) \
  { \
    try \
    { \
      if(compareProperties(*(reinterpret_cast<type*>(data)), \
                           testProperty.value, testProperty.delta)) \
      { \
        MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, ("SUCCESS - porperty: "+ testProperty.component + "." + testProperty.property).c_str()); \
        continue; \
      } \
      else \
      { \
28
        MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, ("FAIL - porperty: "+ testProperty.component + "." + testProperty.property +", not the same as the current value! expected value is: "+ testProperty.value).c_str()); \
29
30
31
32
33
34
        ++returnCode; \
        continue; \
      } \
    } \
    catch(boost::bad_lexical_cast &e) \
    { \
35
      MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, ("FAIL - porperty: "+ testProperty.component + "." + testProperty.property + " error, application can't parse value, message :" + e.what()).c_str()); \
36
37
38
39
40
41
      ++returnCode; \
      continue; \
    } \
  } \


42
43
44
// --------------------
//  
// --------------------
45
46
UTest::UTest(void)
{
47
48
  calcDuration = 0;
  stepsMode = true;
49
50
}

51
52
53
54

UTest::UTest(const UEPtr<UApplication> &value)
{
  Application=value;
55
56
  calcDuration = 0;
  stepsMode = true;
57
58
}

Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
59
///     
60
  UEPtr<UApplication> UTest::GetApplication()
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
61
62
63
64
65
66
67
68
69
70
71
72
73
{
  return Application;
}

///   
void UTest::SetApplication(const UEPtr<UApplication> &value)
{
  if(Application == value)
   return;

  Application=value;
}

74
75
76
77
78
79
80
81
82
83
84
85
UTest& UTest::operator = (const UTest &copy)
{
 Application=copy.Application;

 calcDuration=copy.calcDuration;
 stepsMode=copy.stepsMode;
 propertyTests=copy.propertyTests;
 testsFileName=copy.testsFileName;
 testProjectFileName=copy.testProjectFileName;
 return *this;
}

86
87
88
// --------------------
//  
// --------------------
89

Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
90
91
92
///  
int UTest::LoadTest(string testFile)
{
93
  testsFileName = testFile;
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
94
  RDK::USerStorageXML testXML;
95
  if(!testXML.LoadFromFile(testFile, "TestCase"))
96
97
  {
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (testsFileName + " UTest::LoadTest: can't find <TestCase> in Xml file").c_str());
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
98
    return 1;
99
  }
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
100

101
  if(!testXML.SelectNode("Header"))
102
103
  {
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (testsFileName + " UTest::LoadTest: can't find <Header> in Xml file").c_str());
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
104
    return 2;
105
  }
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119

  testXML.SelectNode("CalculateDuration");
  std::string strDuration = testXML.GetNodeAttribute("Steps");

  if(strDuration.empty())
  {
    stepsMode = false;
    strDuration = testXML.GetNodeAttribute("Milliseconds");
  }
  else
  {
    stepsMode = true;
  }

120
121
122
123
124
125
126
127
128
  try
  {
    calcDuration = boost::lexical_cast<int>(strDuration);
  }
  catch(boost::bad_lexical_cast &e)
  {
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (testsFileName + " calcDuration empty, boost::lexical_cast error, message :" + e.what()).c_str());
    calcDuration = 0;
  }
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
129

130
  testXML.SelectUp();
131
132
133
134
135
136
137
138
139
  if(testXML.SelectNode("ConfigFilePath"))
  {
    testProjectFileName = testXML.GetNodeText();
  }
  else
  {
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (testsFileName + " UTest::LoadTest: can't find <ConfigFilePath> in Xml file").c_str());
    return 3;
  }
140

Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
141
  testXML.SelectRoot();
142
  if(testXML.SelectNode("TestProperties"))
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
143
  {
144
    for(int i = 0; i < testXML.GetNumNodes("Property"); ++i)
145
    {
146
147
      testXML.SelectNode("Property", i);
      std::string sValue;
148

149
150
151
152
153
154
155
156
157
158
159
160
      //     xml,    xml,       
      if(testXML.SelectNode(0))
      {
        testXML.SaveFromNode(sValue);
        testXML.SelectUp();
      }
      else
      {
        sValue = testXML.GetNodeText();
      }

      UPropertyTest pt = {
161
162
163
164
        testXML.GetNodeAttribute("Component"),
        testXML.GetNodeAttribute("PropertyName"),
        testXML.GetNodeAttribute("type"),
        testXML.GetNodeAttribute("Delta"),
165
        sValue
166
      };
167
168
169
170
171
172
173
      propertyTests.push_back(pt);
      testXML.SelectUp();
    }
  }
  else
  {
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (testsFileName + " UTest::LoadTest: can't find <TestProperties> in Xml file").c_str());
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
174
  }
175
  return RDK_SUCCESS;
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
176
177
178
}

///  
179
///     
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
180
181
int UTest::ProcessTest()
{
182
183
184
  int returnCode = RDK_SUCCESS;

  try
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
185
  {
186
187
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, std::string("Property test log: ").c_str());

188
    Application->OpenProject(testProjectFileName);
189
    /*std::vector<string> strs;
190
191
    boost::split(strs, testsFileName, boost::is_any_of("/"));
    strs.pop_back();
192
    strs.push_back("TestLog");
193

194
//    std::string oldLogDir = Application->GetLogDir();
195

196
    Application->SetLogDir(boost::join(strs, "/") + "/");
197

198
//    std::string newLogDir = Application->GetLogDir();*/
199

200
201
    if(stepsMode)
    {
202
      for(size_t i = 0; i < static_cast<size_t>(calcDuration); ++i)
203
204
        Application->StepChannel(-1);
    }
205
206
207
208
209
210
    else
    {
      Application->StartChannel(-1);
      Sleep(calcDuration);
      Application->PauseChannel(-1);
    }
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
211

212
213
214
215
216
217
218
219
    UELockPtr<UContainer> model = GetModelLock<UContainer>(Core_GetSelectedChannelIndex());

    if(!model)
    {
      MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, std::string("Model doesn't exist! Testing terminated!").c_str());
      return -1;
    }

220
221
    for(std::vector<UPropertyTest>::iterator i = propertyTests.begin(); i != propertyTests.end(); ++i)
    {
222
223
224
      UPropertyTest testProperty = (*i);
      RDK::UEPtr<RDK::UComponent> component;
      if (testProperty.component.empty())
Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
225
          component = model.Get();
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
      else
          component = model->GetComponentL(testProperty.component, true);

      if(!component)
      {
        MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, ("FAIL - component: "+ testProperty.component +", doesn't exist!").c_str());
        ++returnCode;
        continue;
      }

      UEPtr<UIProperty> property = component->FindProperty(testProperty.property);

      if(!property)
      {
        MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, ("FAIL - porperty: "+ testProperty.component + "." + testProperty.property +", doesn't exist!").c_str());
        ++returnCode;
        continue;
      }

      void* data = const_cast<void*>(property->GetMemoryArea());
246

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
      if(!data)
      {
        MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, ("FAIL - porperty: "+ testProperty.component + "." + testProperty.property +", is NULL!").c_str());
        ++returnCode;
        continue;
      }

      TEST_PROPERTY_WITH_TYPE(bool)
      TEST_PROPERTY_WITH_TYPE(int)
      TEST_PROPERTY_WITH_TYPE(unsigned int)
      TEST_PROPERTY_WITH_TYPE(long long)
      TEST_PROPERTY_WITH_TYPE(unsigned long long)
      TEST_PROPERTY_WITH_TYPE(float)
      TEST_PROPERTY_WITH_TYPE(double)
      TEST_PROPERTY_WITH_TYPE(std::string)
262
263
264
      TEST_PROPERTY_WITH_TYPE(MDMatrix<double>)

//      TEST_PROPERTY_WITH_TYPE(std::vector<RTV::TZoneExt>)
265

266
      MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, ("WEIRD - porperty: "+ testProperty.component + "." + testProperty.property +", haven't compare function!").c_str());
267
268
269
270
271

    }
  }
  catch(RDK::UException& e)
  {
272
273
    ++returnCode;
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_FATAL, (std::string("FAIL, exception catched UTest::ProcessTest - ")+e.what()).c_str());
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
274
  }
275
276
  catch(std::exception& e)
  {
277
278
    ++returnCode;
    MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_FATAL, (std::string("FAIL, exception catched UTest::ProcessTest - ")+e.what()).c_str());
279
280
281
  }

  return returnCode;
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
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
bool UTest::compareProperties(bool value, std::string str, std::string delta)
{
  if(!delta.empty())
    if(boost::lexical_cast<bool>(delta))
      return true;
  return boost::lexical_cast<bool>(str) == value;
}

bool UTest::compareProperties(int value, std::string str, std::string delta)
{
  int intDelta = 0;
  if(!delta.empty())
    intDelta = boost::lexical_cast<int>(delta);

  int intStr = boost::lexical_cast<int>(str);
  return abs(value - intStr) <= intDelta;
}

bool UTest::compareProperties(unsigned int value, std::string str, std::string delta)
{
  unsigned int intDelta = 0;
  if(!delta.empty())
    intDelta = boost::lexical_cast<unsigned int>(delta);

  unsigned int intStr = boost::lexical_cast<unsigned int>(str);
313
314
  unsigned int diff = value > intStr ? value - intStr: intStr - value;
  return diff <= intDelta;
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
}

bool UTest::compareProperties(long long value, std::string str, std::string delta)
{
  long long intDelta = 0;
  if(!delta.empty())
    intDelta = boost::lexical_cast<long long>(delta);

  long long intStr = boost::lexical_cast<long long>(str);
  return static_cast<long long>(abs(value - intStr)) <= intDelta;
}

bool UTest::compareProperties(unsigned long long value, std::string str, std::string delta)
{
  unsigned long long intDelta = 0;
  if(!delta.empty())
    intDelta = boost::lexical_cast<unsigned long long>(delta);

  unsigned long long intStr = boost::lexical_cast<unsigned long long>(str);
334
335
  unsigned long long diff = value > intStr ? value - intStr: intStr - value;
  return diff <= intDelta;
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
}

bool UTest::compareProperties(float value, std::string str, std::string delta)
{
  float floatDelta = 0.f;
  if(!delta.empty())
    floatDelta = boost::lexical_cast<float>(delta);

  float floatStr = boost::lexical_cast<float>(str);
  return fabs(value - floatStr) <= floatDelta;
}

bool UTest::compareProperties(double value, std::string str, std::string delta)
{
  double doubleDelta = 0.;
  if(!delta.empty())
    doubleDelta = boost::lexical_cast<double>(delta);

  double doubleStr = boost::lexical_cast<double>(str);
  return static_cast<double>(fabs(value - doubleStr)) <= doubleDelta;
}

bool UTest::compareProperties(std::string value, std::string str, std::string)
{
  return value.compare(str) == 0;
}

363
364
365
366
367
368
369
370
371
bool UTest::compareProperties(MDMatrix<double> value, string str, string delta)
{
  std::vector<std::string> strs;
  boost::split(strs, str, boost::is_any_of("\t"));

  return static_cast<size_t>(value.GetSize()) == strs.size();
}

/*bool UTest::compareProperties(std::vector<RTV::TZoneExt> value, string str, string delta)
372
373
374
375
{
  USerStorageXML storage;
  storage.Load(str, "");
  storage.SelectNode("Objects");
376
377
378
379
380

  std::string strS = storage.GetNodeAttribute("Size");
  size_t strVectorSize = 0;
  if(!strS.empty())
    strVectorSize = boost::lexical_cast<size_t>(strS);
381
382

  return value.size() == strVectorSize;
383
}*/
384

Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
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
///     
UEPtr<UApplication> UTestManager::GetApplication(void)
{
 return Application;
}

///   
void UTestManager::SetApplication(const UEPtr<UApplication> &value)
{
 if(Application == value)
  return;

 Application=value;
}
/// --------------------

// --------------------
//  
// --------------------
///  
int UTestManager::LoadTests(const std::string &file_name)
{
 if(file_name.empty())
 {
  MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, "Tests description file name is empty!");
  return 1001;
 }

418
419
420
421
422
423
 RDK::USerStorageXML testsXML;
 if(!testsXML.LoadFromFile(file_name, "UTestCases"))
 {
   MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (file_name + " UTestManager::LoadTest: can't find <UTestCases> in Xml file").c_str());
   return 1002;
 }
424

425
 int loadRet = 0;
426
427
 int testsCount = testsXML.GetNumNodes("UTest");
 for(int i = 0; i < testsCount; ++i)
428
429
430
 {
   if(!testsXML.SelectNode("UTest", i))
     continue;
431

432
433
434
435
   tests.push_back(UTest(Application));
   loadRet += tests[i].LoadTest(testsXML.GetNodeText());
   testsXML.SelectUp();
 }
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
436
 if(loadRet)
437
  return 1003;
438

Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
439
 /*std::vector<std::string> test_file_names;
Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
 int decode_main_file_res=DecodeMainTestDescriptionFile(test_file_names);
 if(decode_main_file_res != RDK_SUCCESS)
 {
  MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (std::string("UTestManager::LoadTests - decode main file tests description file FAILED! Error code: ")+sntoa(decode_main_file_res)).c_str());
  return 1002;
 }

 for(size_t i=0;i<test_file_names.size();i++)
 {
  UTest test;
  int decode_res=DecodeTestFile(test_file_names[i],test);
  if(decode_res != RDK_SUCCESS)
  {
   MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, (std::string("UTestManager::LoadTests - decode test file ")+test_file_names[i]+std::string(" FAILED! Error code: ")+sntoa(decode_res)).c_str());
   return 1003;
  }
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
456
 }*/
457
458
459
460
461
462
463
464
465
466
 return RDK_SUCCESS;
}


///  
///      
///    
int UTestManager::ProcessTests(void)
{
 MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, "UTestManager::ProcessTests called");
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
467
468
469
470

 int returnCode = RDK_SUCCESS;

 for(std::vector<UTest>::iterator i = tests.begin(); i != tests.end(); ++i)
471
  returnCode += (*i).ProcessTest();
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
472
473

 return returnCode;
474
475
476
}
// --------------------

Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// --------------------
//  
// --------------------
///     
///       
int UTestManager::DecodeMainTestDescriptionFile(std::vector<std::string> &test_file_names)
{
 MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, "UTestManager::DecodeMainTestDescriptionFile called");
 return RDK_SUCCESS;
}

///  
int UTestManager::DecodeTestFile(const std::string &test_file_name, UTest &test)
{
 MLog_LogMessage(RDK_GLOB_MESSAGE, RDK_EX_INFO, "UTestManager::DecodeTestFile called");
 return RDK_SUCCESS;
}
Viktor_Mikhaylov's avatar
Viktor_Mikhaylov committed
494
495
496



Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
497
// --------------------
498
499

}
Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
500
501

#endif