configurationengine/source/plugins/symbian/ConeHCRPlugin/hcrplugin/tests/unittest_writer.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 
       
    17 import os, unittest
       
    18 import __init__
       
    19 
       
    20 from hcrplugin.hcrrepository import HcrRepository, HcrRecord
       
    21 from hcrplugin.hcr_writer import HcrWriter
       
    22 from hcrplugin import hcr_exceptions
       
    23 
       
    24 class TestHcrWriter(unittest.TestCase):
       
    25     def setUp(self):
       
    26         self.writer = HcrWriter()
       
    27     
       
    28     def test_write_repo_with_duplicate_record(self):
       
    29         records = [
       
    30             HcrRecord(HcrRecord.VALTYPE_INT8, -123, 1, 1, 0),
       
    31             HcrRecord(HcrRecord.VALTYPE_INT8, 124,  2, 1, 0),
       
    32             HcrRecord(HcrRecord.VALTYPE_INT8, 66,   3, 1, 0),
       
    33             HcrRecord(HcrRecord.VALTYPE_INT8, -72,  1, 1, 0),
       
    34             HcrRecord(HcrRecord.VALTYPE_INT8, 171,  1, 2, 0),
       
    35         ]
       
    36         repo = HcrRepository(records, version=2, flags=3)
       
    37         
       
    38         try:
       
    39             self.writer.get_repository_bindata(repo)
       
    40         except hcr_exceptions.DuplicateRecordError:
       
    41             pass
       
    42 
       
    43     def test_record_sorting_by_setting_id(self):
       
    44         records = [
       
    45             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 3, 1, 0),
       
    46             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 1, 0),
       
    47             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 2, 0),
       
    48             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 1, 0),
       
    49             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 2, 0),
       
    50             
       
    51             ]
       
    52 
       
    53         expected = [
       
    54             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 1, 0),
       
    55             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 2, 0),
       
    56             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 1, 0),
       
    57             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 2, 0),
       
    58             HcrRecord(HcrRecord.VALTYPE_INT8, 10, 3, 1, 0),
       
    59             ]
       
    60         
       
    61         self.assertEquals(sorted(records, key=self.writer.get_record_setting_id), expected)
       
    62 
       
    63     def _run_test_write_record_with_invalid_value(self, record_type, record_value):
       
    64         try:
       
    65             record = HcrRecord(record_type, record_value, 0, 0, 0)
       
    66             self.writer.get_record_bindata(record, (0, 0))
       
    67             self.fail("Expected exception not thrown!")
       
    68         except hcr_exceptions.ValueNotInRangeError, e:
       
    69             pass
       
    70 
       
    71     def test_write_numeric_record_with_invalid_value(self):
       
    72         def test(record_type, bits, unsigned):
       
    73             if unsigned:
       
    74                 value1 = -1
       
    75                 value2 = 2**bits
       
    76             else:
       
    77                 value1 = -(2**(bits-1) + 1)
       
    78                 value2 = 2**(bits-1)
       
    79             
       
    80             if record_type in (HcrRecord.VALTYPE_ARRAY_INT32, HcrRecord.VALTYPE_ARRAY_UINT32):
       
    81                 value1 = [1, value1, 2]
       
    82                 value2 = [1, value2, 2]
       
    83             
       
    84             self._run_test_write_record_with_invalid_value(record_type, value1)
       
    85             self._run_test_write_record_with_invalid_value(record_type, value2)
       
    86         
       
    87         test(HcrRecord.VALTYPE_INT8,            8,  False)
       
    88         test(HcrRecord.VALTYPE_UINT8,           8,  True)
       
    89         test(HcrRecord.VALTYPE_INT16,           16, False)
       
    90         test(HcrRecord.VALTYPE_UINT16,          16, True)
       
    91         test(HcrRecord.VALTYPE_INT32,           32, False)
       
    92         test(HcrRecord.VALTYPE_UINT32,          32, True)
       
    93         test(HcrRecord.VALTYPE_LIN_ADDR,        32, True)
       
    94         test(HcrRecord.VALTYPE_INT64,           64, False)
       
    95         test(HcrRecord.VALTYPE_UINT64,          64, True)
       
    96         test(HcrRecord.VALTYPE_ARRAY_INT32,     32, False)
       
    97         test(HcrRecord.VALTYPE_ARRAY_UINT32,    32, True)
       
    98     
       
    99     def test_write_value_with_too_large_lsd_data(self):
       
   100         def test(record_type, value):
       
   101             try:
       
   102                 record = HcrRecord(record_type, value, 0, 0, 0)
       
   103                 self.writer.get_record_lsd_bindata(record)
       
   104                 self.fail("Expected exception not raised!")
       
   105             except hcr_exceptions.TooLargeLsdDataError:
       
   106                 pass
       
   107         
       
   108         test(HcrRecord.VALTYPE_ARRAY_INT32, [i for i in xrange(150)])
       
   109         test(HcrRecord.VALTYPE_ARRAY_UINT32, [i for i in xrange(150)])
       
   110         test(HcrRecord.VALTYPE_BIN_DATA, 513 * ' ')
       
   111         test(HcrRecord.VALTYPE_TEXT8, 200 * u'\u20ac')