class CordovaPluginOCFDemoAppButtonTest(oeRuntimeTest): '''Automatize the Cordova plugin OCF demo app tests like checking if the resources are found, and resource information is readonly. ''' pkg_id = 'com.example.CordovaPluginOcfDemo' button_item = None device_found = False resource_found = False details_btn = None id_item = None button_value_item = None button_value = None appmgr = AppMgr() @classmethod def setUpClass(cls): ''' Launch the app and find the OCF resources. ''' cls.appmgr.kill_app(cls.pkg_id) cls.appmgr.launch_app(cls.pkg_id) time.sleep(data_settings.app_launch_and_wait) def init_button_sensor(self): ''' Go to the detailed page of the OCF resource. ''' self.button_item = d(className='android.view.View', descriptionContains='Path: /a/button') self.button_item.click() time.sleep(1) self.details_btn = d(className='android.widget.Button') self.id_item = d(className='android.view.View', descriptionStartsWith='id') self.button_value_item = d(className='android.view.View', descriptionStartsWith='value') self.button_value = self.button_value_item.description.split( ':')[-1].strip() def test_button_resource_found(self): '''Check if the button resource can be found.''' self.appmgr.go_to_resources_for_ocfdemo() time.sleep(data_settings.app_found_res_and_wait) self.resource_found = d.exists(className='android.view.View', descriptionContains='/a/button') self.assertTrue(self.resource_found, 'Button resource is not found.') def test_button_resource_properties_readonly(self): '''Check if the properties of button resource are readonly.''' self.init_button_sensor() self.assertEqual(len(self.details_btn), 1, 'The properties of button are not readonly.') self.assertEqual( self.id_item.description.split(':')[-1].strip(), 'button', 'Id of button resource not found.') self.assertEqual(self.button_value, 'false', 'Initial button value is not false!') def test_button_z_device_found(self): '''Check if the OCF device can be found.''' self.appmgr.go_to_devices_for_ocfdemo() time.sleep(data_settings.app_found_dev_and_wait) self.device_found = d.exists(descriptionContains='UUID') self.device_found = self.device_found and d.exists( descriptionContains='URL') self.device_found = self.device_found and d.exists( descriptionContains='Name') self.device_found = self.device_found and d.exists( descriptionContains='Data models') self.device_found = self.device_found and d.exists( descriptionContains='Core spec version') self.device_found = self.device_found and d.exists( descriptionContains='Role') self.assertTrue(self.device_found, 'OCF device is not found.') @classmethod def tearDownClass(cls): '''Terminate the app.''' cls.appmgr.kill_app(cls.pkg_id)
class CordovaPluginOCFDemoAppPowerTest(oeRuntimeTest): '''Automatize the Cordova plugin OCF demo app tests like checking if the resources are found, and resource information is readonly. ''' pkg_id = 'com.example.CordovaPluginOcfDemo' power_item = None device_found = False resource_found = False details_btn = None power1_item = None power1_value = 0.0 id_item = None power2_item = None power2_value = 0.0 appmgr = AppMgr() @classmethod def setUpClass(cls): ''' Launch the app and find the OCF resources. ''' cls.appmgr.kill_app(cls.pkg_id) cls.appmgr.launch_app(cls.pkg_id) time.sleep(data_settings.app_launch_and_wait) def init_power_sensor(self): ''' Go to the detaipower page of the OCF resource. ''' self.power_item = d(className='android.view.View', descriptionContains='Path: /a/power') self.power_item.click() time.sleep(1) self.details_btn = d(className='android.widget.Button') self.power1_item = d(className='android.view.View', descriptionStartsWith='power1') self.power1_value = float(self.power1_item.description.split(':')[-1].strip()) self.id_item = d(className='android.view.View', descriptionStartsWith='id') self.power2_item = d(className='android.view.View', descriptionStartsWith='power2') self.power2_value = float(self.power2_item.description.split(':')[-1].strip()) def test_power_resource_found(self): '''Check if the power resources can be found.''' self.appmgr.go_to_resources_for_ocfdemo() time.sleep(data_settings.app_found_res_and_wait) self.resource_found = d.exists(className='android.view.View', descriptionContains='/a/power') self.assertTrue(self.resource_found, 'The power resource is not found.') def test_power_resource_has_properties(self): '''Check if the power resource has properties like id and value.''' self.init_power_sensor() self.assertEqual(len(self.details_btn), 1) self.assertEqual(self.id_item.description.split(':')[-1].strip(), 'power', 'Id of power resource not found.', ) self.assertTrue(self.power1_value >= 0.0, 'power1 value seems not sensible') self.assertTrue(self.power2_value >= 0.0, 'power2 value seems not sensible') def test_power_z_device_found(self): ''''Check if the OCF device can be found.''' self.appmgr.go_to_devices_for_ocfdemo() time.sleep(data_settings.app_found_dev_and_wait) self.device_found = d.exists(descriptionContains='UUID') self.device_found = self.device_found and d.exists(descriptionContains='URL') self.device_found = self.device_found and d.exists(descriptionContains='Name') self.device_found = self.device_found and d.exists(descriptionContains='Data models') self.device_found = self.device_found and d.exists(descriptionContains='Core spec version') self.device_found = self.device_found and d.exists(descriptionContains='Role') self.assertTrue(self.device_found, 'OCF device is not found.') @classmethod def tearDownClass(cls): '''Terminate the app.''' cls.appmgr.kill_app(cls.pkg_id)
class CordovaPluginOCFDemoAppRGBLedTest(oeRuntimeTest): '''Automatize the Cordova plugin OCF demo app tests like checking if the resources are found, and resource information is readonly. ''' pkg_id = 'com.example.CordovaPluginOcfDemo' rgb_led_item = None device_found = False resource_found = False details_btn = None id_item = None range_item = None rgb_led_value_item = None rgb_led_value = None appmgr = AppMgr() @classmethod def setUpClass(cls): ''' Launch the app and find the OCF resources. ''' cls.appmgr.kill_app(cls.pkg_id) cls.appmgr.launch_app(cls.pkg_id) time.sleep(data_settings.app_launch_and_wait) def init_rgb_led_sensor(self): ''' Go to the detairgb_led page of the OCF resource. ''' self.rgb_led_item = d(className='android.view.View', descriptionContains='Path: /a/rgbled') self.rgb_led_item.click() time.sleep(1) self.details_btn = d(className='android.widget.Button') self.id_item = d(className='android.view.View', descriptionStartsWith='id') self.range_item = d(className='android.view.View', descriptionStartsWith='range') self.rgb_led_value_item = d(className='android.view.View', index=15).child( className='android.view.View', index=1) self.rgb_led_value = self.rgb_led_value_item.description def test_rgb_led_resource_found(self): '''Check if the rgb_led resources can be found.''' self.appmgr.go_to_resources_for_ocfdemo() time.sleep(data_settings.app_found_res_and_wait) self.resource_found = d.exists(className='android.view.View', descriptionContains='/a/rgbled') self.assertTrue(self.resource_found, 'The motion resource is not found.') def test_rgb_led_resource_has_properties(self): '''Check if the rgb_led resource has properties like id and value.''' self.init_rgb_led_sensor() self.assertEqual(len(self.details_btn), 2) self.assertEqual( self.id_item.description.split(':')[-1].strip(), 'rgbled', 'Id of rgb_led resource not found.', ) self.assertEqual( self.range_item.description.split(':')[-1].strip(), '[0, 255]', 'RGB led range is empty!') self.assertTrue(self.rgb_led_value.strip() <= '255, 255, 255', 'rgb_led value is invalid!') def test_rgb_led_z_device_found(self): ''''Check if the OCF device can be found.''' self.appmgr.go_to_devices_for_ocfdemo() time.sleep(data_settings.app_found_dev_and_wait) self.device_found = d.exists(descriptionContains='UUID') self.device_found = self.device_found and d.exists( descriptionContains='URL') self.device_found = self.device_found and d.exists( descriptionContains='Name') self.device_found = self.device_found and d.exists( descriptionContains='Data models') self.device_found = self.device_found and d.exists( descriptionContains='Core spec version') self.device_found = self.device_found and d.exists( descriptionContains='Role') self.assertTrue(self.device_found, 'OCF device is not found.') @classmethod def tearDownClass(cls): '''Terminate the app.''' cls.appmgr.kill_app(cls.pkg_id)
class CordovaPluginOCFDemoAppTemperatureTest(oeRuntimeTest): '''Automatize the Cordova plugin OCF demo app tests like checking if the resources are found, and resource information is readonly.''' pkg_id = 'com.example.CordovaPluginOcfDemo' temperature_item = None device_found = False resource_found = False details_btn = None id_item = None range_item = None temperature_value_item = None temperature_value = 0.0 units_item = None appmgr = AppMgr() @classmethod def setUpClass(cls): ''' Launch the app and find the OCF resources. ''' cls.appmgr.kill_app(cls.pkg_id) cls.appmgr.launch_app(cls.pkg_id) time.sleep(data_settings.app_launch_and_wait) def init_temperature_sensor(self): ''' Go to the detailed page of the OCF resource. ''' self.temperature_item = d(className='android.view.View', descriptionContains='Path: /a/temperature') self.temperature_item.click() time.sleep(1) self.details_btn = d(className='android.widget.Button') self.id_item = d(className='android.view.View', descriptionStartsWith='id') self.range_item = d(className='android.view.View', descriptionStartsWith='range') self.temperature_value_item = d(className='android.view.View', descriptionStartsWith='temperature') self.temperature_value = float( self.temperature_value_item.description.split(':')[-1].strip()) self.units_item = d(className='android.view.View', descriptionStartsWith='units') def test_temeperature_resource_found(self): '''Check if the temperature resources can be found.''' self.appmgr.go_to_resources_for_ocfdemo() time.sleep(data_settings.app_found_res_and_wait) self.resource_found = d.exists(className='android.view.View', descriptionContains='/a/temperature') self.assertTrue(self.resource_found, 'The temperature resource is not found.') def test_temeperature_resource_properties_readonly(self): '''Check if the properties of temperature resources are readonly.''' self.init_temperature_sensor() self.assertEqual(len(self.details_btn), 1, 'The properties of temperature are not readonly.') self.assertEqual( self.id_item.description.split(':')[-1].strip(), 'temperature', 'Device of temperature resource not found.') self.assertEqual( self.range_item.description.split(':')[-1].strip(), '-40,125', 'Temperature range is invalid.') self.assertTrue(self.temperature_value > 10.0, 'Temperature value seems not sensible in Zhizu1 Lab.') self.assertTrue( self.units_item.description.split(':')[-1].strip() in ('C', 'F', 'K'), 'Temperature units is invalid.') def test_temeperature_z_device_found(self): ''''Check if the OCF device can be found.''' self.appmgr.go_to_devices_for_ocfdemo() time.sleep(data_settings.app_found_dev_and_wait) self.device_found = d.exists(descriptionContains='UUID') self.device_found = self.device_found and d.exists( descriptionContains='URL') self.device_found = self.device_found and d.exists( descriptionContains='Name') self.device_found = self.device_found and d.exists( descriptionContains='Data models') self.device_found = self.device_found and d.exists( descriptionContains='Core spec version') self.device_found = self.device_found and d.exists( descriptionContains='Role') self.assertTrue(self.device_found, 'OCF device is not found.') @classmethod def tearDownClass(cls): '''Terminate the app.''' cls.appmgr.kill_app(cls.pkg_id)
class OCFDemoAppBuildAndLaunchTest(oeRuntimeTest): ''' Build/Install/Launch the CordovaPluginOCFDemo app. ''' workspace_dir = os.path.dirname(__file__) cordova_pluin_ocf_demo_url = 'https://github.com/siovene/cordova-plugin-ocf-demo.git' pkg_id = 'com.example.CordovaPluginOcfDemo' repo_dir = 'cordova-plugin-ocf-demo' prj_dir = os.path.join(workspace_dir, repo_dir) apk_path = 'platforms/android/build/outputs/apk/android-debug.apk' clean_up = False uninstall_app = False appmgr = AppMgr() def setUp(self): ''' Check if the grunt-cli and bower are installed. Install them globally if not. ''' self.appmgr.uninstall_app(self.pkg_id) cli_cmd = ['grunt', 'bower'] for cmd in cli_cmd: detect_cmd = ['which'] + [cmd] proc = subprocess.Popen(detect_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc.wait() if proc.returncode != 0: if cmd == 'grunt': cli = 'grunt-cli' else: cli = cmd print('{} not installed, install it...'.format(cli)) proc_cli = subprocess.Popen(['sudo', '-E', 'npm', cli, '-g']) proc_cli.communicate() else: print('{} has been installed'.format(cmd)) detect_dev = ['adb', 'devices'] proc_adb = subprocess.Popen(detect_dev, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc_adb.wait() adb_info = proc_adb.stdout.read().decode('utf8').strip() if proc_adb.returncode != 0 or not adb_info.endswith('device'): print( 'Please check if the android device is connected to the host.') def test_ocfdemo_build_install_launch(self): ''' Check if there're any errors during the following steps: 1. git clone the cordova-plugin-ocf-demo 2. build the app 3. install and launch it ''' clone_cmd = [ 'git', 'clone', '--depth', '1', '--single-branch', '--branch', 'master', self.cordova_pluin_ocf_demo_url ] clone_proc = subprocess.Popen(clone_cmd, cwd=self.workspace_dir) clone_proc.wait() self.assertEqual(clone_proc.returncode, 0, 'clone the cordova_pluin_ocf_demo failed.') build_cmds = [['npm', 'install'], ['bower', 'install'], ['grunt', 'platform:add:android'], ['grunt', 'build']] for build_cmd in build_cmds: build_proc = subprocess.Popen(build_cmd, cwd=self.prj_dir) build_proc.wait() self.assertEqual( build_proc.returncode, 0, 'Failed to build when running [{0}]'.format(build_cmd)) self.assertTrue(os.path.exists(self.prj_dir), 'cordova-plugin-ocf-demo apk is not found!') install_cmd = ['adb', 'install', self.apk_path] install_proc = subprocess.Popen(install_cmd, cwd=self.prj_dir) install_proc.wait() self.assertEqual(install_proc.returncode, 0, 'Failed to install OCF Demo apk') self.appmgr.launch_app(self.pkg_id) time.sleep(5) btn = d(className='android.widget.Button', index=0) btn.click() title_found = d.exists(className='android.view.View', descriptionContains='OCF Demo') self.assertTrue(title_found, 'Launching the app OK?') def tearDown(self): ''' Remove the cordova-plugin-ocf-demo project as it not needed any more. ''' if self.clean_up: os.chdir(self.workspace_dir) os.system('rm -fr {}'.format(self.repo_dir)) if self.uninstall_app: self.appmgr.uninstall_app(self.pkg_id)
from __future__ import unicode_literals import sys import redis import logging from configparser import ConfigParser from appmgr import AppMgr console_out = logging.StreamHandler(sys.stdout) console_out.setLevel(logging.DEBUG) console_err = logging.StreamHandler(sys.stderr) console_err.setLevel(logging.ERROR) logging_handlers = [console_out, console_err] logging_format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' logging_datefmt = '%a, %d %b %Y %H:%M:%S' logging.basicConfig(level=logging.DEBUG, format=logging_format, datefmt=logging_datefmt, handlers=logging_handlers) config = ConfigParser() config.read('../config.ini') redis_srv = config.get('redis', 'url', fallback='redis://127.0.0.1:6379') redis_sts = redis.Redis.from_url( redis_srv + "/9", decode_responses=True) # device status (online or offline) mgr = AppMgr(config) mgr.start() mgr.join()
class CordovaPluginOCFDemoAppEnvTest(oeRuntimeTest): '''Automatize the Cordova plugin OCF demo app tests like checking if the resources are found, and resource information is readonly. ''' pkg_id = 'com.example.CordovaPluginOcfDemo' env_item = None device_found = False resource_found = False details_btn = None humidity_item = None humidity_value = 0.0 id_item = None pressure_item = None pressure_value = 0.0 temperature_item = None temperature_value = 0.0 unindex_item = None uvindex_value = None appmgr = AppMgr() @classmethod def setUpClass(cls): ''' Launch the app and find the OCF resources. ''' cls.appmgr.kill_app(cls.pkg_id) cls.appmgr.launch_app(cls.pkg_id) time.sleep(data_settings.app_launch_and_wait) def init_env_sensor(self): ''' Go to the detaienv page of the OCF resource. ''' self.env_item = d(className='android.view.View', descriptionContains='Path: /a/env') self.env_item.click() time.sleep(1) self.details_btn = d(className='android.widget.Button') self.humidity_item = d(className='android.view.View', descriptionStartsWith='humidity') self.humidity_value = float( self.humidity_item.description.split(':')[-1].strip()) self.id_item = d(className='android.view.View', descriptionStartsWith='id') self.pressure_item = d(className='android.view.View', descriptionStartsWith='pressure') self.pressure_value = float( self.pressure_item.description.split(':')[-1].strip()) self.temperature_item = d(className='android.view.View', descriptionStartsWith='temperature') self.temperature_value = float( self.temperature_item.description.split(':')[-1].strip()) self.uvindex_item = d(className='android.view.View', descriptionStartsWith='uvIndex') self.uvindex_value = int( self.uvindex_item.description.split(':')[-1].strip()) def test_env_resource_found(self): '''Check if the env resources can be found.''' self.appmgr.go_to_resources_for_ocfdemo() time.sleep(data_settings.app_found_res_and_wait) self.resource_found = d.exists(className='android.view.View', descriptionContains='/a/env') self.assertTrue(self.resource_found, 'The environment resource is not found.') def test_env_resource_has_properties(self): '''Check if the env resource has properties like id and value.''' self.init_env_sensor() self.assertEqual(len(self.details_btn), 1) self.assertTrue(self.humidity_value >= 0.0, 'Invalid humidity number.') self.assertEqual( self.id_item.description.split(':')[-1].strip(), 'environmentalSensor', 'Id of env resource not found.', ) self.assertTrue(self.pressure_value >= 1000.0, 'Pressure seems not sensible!') self.assertTrue(self.temperature_value >= 15.0, 'Temperature seems not sensible!') self.assertTrue(self.uvindex_value >= 0, 'env value is empty!') def test_env_z_device_found(self): ''''Check if the OCF device can be found.''' self.appmgr.go_to_devices_for_ocfdemo() time.sleep(data_settings.app_found_dev_and_wait) self.device_found = d.exists(descriptionContains='UUID') self.device_found = self.device_found and d.exists( descriptionContains='URL') self.device_found = self.device_found and d.exists( descriptionContains='Name') self.device_found = self.device_found and d.exists( descriptionContains='Data models') self.device_found = self.device_found and d.exists( descriptionContains='Core spec version') self.device_found = self.device_found and d.exists( descriptionContains='Role') self.assertTrue(self.device_found, 'OCF device is not found.') @classmethod def tearDownClass(cls): '''Terminate the app.''' cls.appmgr.kill_app(cls.pkg_id)
class CordovaPluginOCFDemoAppSolarTest(oeRuntimeTest): '''Automatize the Cordova plugin OCF demo app tests like checking if the resources are found, and resource information is readonly. ''' pkg_id = 'com.example.CordovaPluginOcfDemo' solar_item = None device_found = False resource_found = False details_btn = None id_item = None lcd1_item = None lcd2_item = None simu_item = None solar_value_item = None solar_value = None appmgr = AppMgr() @classmethod def setUpClass(cls): ''' Launch the app and find the OCF resources. ''' cls.appmgr.kill_app(cls.pkg_id) cls.appmgr.launch_app(cls.pkg_id) time.sleep(data_settings.app_launch_and_wait) def init_solar_sensor(self): ''' Go to the detaisolar page of the OCF resource. ''' self.solar_item = d(className='android.view.View', descriptionContains='Path: /a/solar') self.solar_item.click() time.sleep(1) self.details_btn = d(className='android.widget.Button') self.id_item = d(className='android.view.View', descriptionStartsWith='id') self.lcd1_item = d(className='android.view.View', descriptionStartsWith='lcd1') self.lcd2_item = d(className='android.view.View', descriptionStartsWith='lcd2') self.simu_item = d(className='android.view.View', descriptionStartsWith='simulationMode') self.solar_value_item = d(className='android.view.View', index=17).child( className='android.view.View', index=1) self.solar_value = self.solar_value_item.description def test_solar_resource_found(self): '''Check if the solar resources can be found.''' self.appmgr.go_to_resources_for_ocfdemo() time.sleep(data_settings.app_found_res_and_wait) self.resource_found = d.exists(className='android.view.View', descriptionContains='/a/solar') self.assertTrue(self.resource_found, 'The solar resource is not found.') def test_solar_resource_has_properties(self): '''Check if the solar resource has properties like id and value.''' self.init_solar_sensor() self.assertEqual(len(self.details_btn), 3) self.assertEqual( self.id_item.description.split(':')[-1].strip(), 'solar', 'Id of solar resource not found.', ) self.assertEqual( self.lcd1_item.description.split(':')[-1].strip(), 'Solar Connected!!', 'LCD1 text is empty!') self.assertEqual( self.lcd2_item.description.split(':')[-1].strip(), '', 'LCD2 text is not empty!') self.assertEqual(self.solar_value, '0', 'solar value is empty!') def test_solar_z_device_found(self): ''''Check if the OCF device can be found.''' self.appmgr.go_to_devices_for_ocfdemo() time.sleep(data_settings.app_found_dev_and_wait) self.device_found = d.exists(descriptionContains='UUID') self.device_found = self.device_found and d.exists( descriptionContains='URL') self.device_found = self.device_found and d.exists( descriptionContains='Name') self.device_found = self.device_found and d.exists( descriptionContains='Data models') self.device_found = self.device_found and d.exists( descriptionContains='Core spec version') self.device_found = self.device_found and d.exists( descriptionContains='Role') self.assertTrue(self.device_found, 'OCF device is not found.') @classmethod def tearDownClass(cls): '''Terminate the app.''' cls.appmgr.kill_app(cls.pkg_id)