Fix reading period and range of light sensor

This commit fix the bug in factory_LightSensor test.
1.Change the reading period of ALS from 0.3 to 0.6 to fix the bug that gtk
can not redraw properly. Reading period less than 0.5 secwill cause gtk
fail to draw every time. Setting reading period to 0.6 sec and can
successfully draw gtk with a 0.1 sec margin.
2.Add a range_value argument to the test, the test can modify range
value of the light sensor.
This test need another CL on the test_list to run.

BUG=chrome-os-partner:10012, 10013
TEST=run factory_LightSensor test on snow board with modified test_list

Change-Id: I9bdea3a04c0ad54db0249ce491af600135659b50
Reviewed-on: https://gerrit.chromium.org/gerrit/23882
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Tested-by: Cheng-Yi Chiang <cychiang@chromium.org>
diff --git a/client/site_tests/factory_LightSensor/factory_LightSensor.py b/client/site_tests/factory_LightSensor/factory_LightSensor.py
index e8f7cf5..c0e9fba 100644
--- a/client/site_tests/factory_LightSensor/factory_LightSensor.py
+++ b/client/site_tests/factory_LightSensor/factory_LightSensor.py
@@ -45,6 +45,7 @@
     Object to interface to ambient light sensor over iio.
     '''
     PARAMS = {'rd': _DEFAULT_DEVICE_PATH + 'illuminance0_input',
+              'range_setting': _DEFAULT_DEVICE_PATH + 'range',
               'init': '',
               'min': 0,
               'max': math.pow(2, 16),
@@ -52,16 +53,21 @@
               'mindelay': 0.178,
               }
 
-    def __init__(self, device_path):
+    def __init__(self, device_path, range_value):
         self.buf = []
         self.ambient = None
 
         if device_path is not None:
             self.PARAMS['rd'] = device_path + 'illuminance0_input'
+            self.PARAMS['range_setting'] = device_path + 'range'
 
         if not os.path.isfile(self.PARAMS['rd']):
             self.cfg()
 
+        if range_value is not None:
+            with open(self.PARAMS['range_setting'], 'w') as f:
+                f.write('%d\n' % range_value)
+
         self.ambient = self.read('mean', delay=0, samples=10)
         factory.log('ambient light sensor = %d' % self.ambient)
 
@@ -269,13 +275,14 @@
                  timeout_per_subtest=10,
                  subtest_list=None,
                  subtest_cfg=None,
-                 subtest_instruction=None):
+                 subtest_instruction=None,
+                 range_value=None):
 
         factory.log('%s run_once' % self.__class__)
 
         self.get_subtest(subtest_list, subtest_cfg, subtest_instruction)
 
-        self._als = iio_generic(device_path)
+        self._als = iio_generic(device_path, range_value)
 
         self._timeout_per_subtest = timeout_per_subtest
 
@@ -317,7 +324,10 @@
 
         vbox.pack_start(hbox, False, False)
 
-        gobject.timeout_add(300, self.sensor_event, sensor_value)
+        # This function sets the period of getting sensor value in ms.
+        # If the value is too small, gtk may not redraw message box properly.
+
+        gobject.timeout_add(600, self.sensor_event, sensor_value)
 
         self._test_widget = vbox
         ful.run_test_widget(self.job, vbox,