def testCommanderFunctionality(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "42") provider.send("dump") self.assert_(provider.expect("Wrote")) # wait for it listen = CLTool("context-listen", "test.int", "test.string") listen.expect("Available commands") # wait for starting commander = CLTool("context-provide", "--v2") # check type-check commander.send("add string test.int foobar") commander.expect("Added key: test.int") self.assert_(listen.expect("Type check failed for \"test.int\""), "Type check didn't work") # check the non-existent property commander.send("add string test.string barfoo") commander.expect("Added key: test.string") self.assert_(listen.expect(wanted("test.string", "QString", "\"barfoo\"")), "Non-existent property couldn't be commanded") # change the type of the non-existent property commander.send("add int test.string 42") self.assert_(listen.expect(wanted("test.string", "int", "42")), "Non-existent property's type couldn't be overwritten") commander.wait() listen.wait() provider.wait()
def testTypes(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth", "test.fake") listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]) # wait for it # test querying the type of all properties listen.send("type test.int") self.assert_( listen.expect("^type: INT$")) listen.send("type test.truth") self.assert_( listen.expect("^type: TRUTH$")) listen.send("type test.string") self.assert_( listen.expect("^type: STRING$")) listen.send("type test.fake") self.assert_( listen.expect("^type:$")) listen.wait() provider.wait()
class Subscription(unittest.TestCase): def tearDown(self): os.remove('time.context') def testChangingPlugin(self): os.environ["CONTEXT_PROVIDERS"] = "." if os.path.isdir("../testplugins"): # for your local machine os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] = "../testplugins" else: # if libcontextsubscriber-tests is installed os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] = "/usr/lib/contextkit/subscriber-test-plugins" if not os.path.exists(os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] + "/" + "contextsubscribertime1.so") \ or not os.path.exists(os.environ["CONTEXT_SUBSCRIBER_PLUGINS"] + "/" + "contextsubscribertime2.so"): self.assert_(False, "Couldn't find the test time plugins") self.context_client = CLTool("context-listen", "Test.Time") self.context_client.expect("Available commands") # wait for it # Copy the declaration file, declaring libcontextsubscribertime1 plugin. os.system('cp time1.context.temp time.context.temp') os.system('mv time.context.temp time.context') #print "now reading" # Expect value coming from plugin libcontextsubscribertime1 self.assert_(self.context_client.expect("Test.Time = QString:\"Time1:")) # Modify the registry so that the key is now provided by libcontextsubscribertime2 os.system('cp time2.context.temp time.context.temp') os.system('mv time.context.temp time.context') # Expect value coming from plugin libcontextsubscribertime2 self.assert_(self.context_client.expect("Test.Time = QString:\"Time2:")) self.context_client.wait()
def testCommanderFunctionality(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "42") provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.int") self.assert_(listen.expect(wanted("test.int", "int", "42")), "Bad value initially from the real provider, wanted 42") commander = CLTool("context-provide", "--v2") commander.send("add int test.int 4242") commander.send("start") commander.expect("Added") # wait for it self.assert_(listen.expect(wanted("test.int", "int", "4242")), "Value after commander has been started is wrong, wanted 4242") commander.send("unset test.int") listen.comment("commander commanded test.int to unknown") self.assert_(listen.expect(wantedUnknown("test.int")), "Value after commander has changed it to unknown is wrong") commander.send("test.int = 1235") self.assert_(listen.expect(wanted("test.int", "int", "1235")), "Value after commander has changed it is wrong, wanted 1235") commander.wait() listen.comment("Commander killed") self.assert_(listen.expect(wanted("test.int", "int", "42")), "Value after killing the commander is wrong, wanted 42") listen.wait() provider.wait()
class Orientation(unittest.TestCase): def setUp(self): self.fpath = "/tmp/fakedsensors/accelerometer" self.datafaker = "/usr/bin/datafaker" self.context_client_stable = CLTool("context-listen", "Position.Stable") def tearDown(self): self.context_client_stable.atexit() def testStationary(self): index = 0 # Verify that buffer of similar samples results in stable = true while index < 62: os.system("echo -36 -90 953 | " + self.datafaker + " " + self.fpath) index = index + 1 time.sleep(1.1) time.sleep(2) self.assert_(self.context_client_stable.expect('Position.Stable = bool:true')) # Verify that two samples are enough to set stable = false os.system("echo -500 -100 -300 | " + self.datafaker + " " + self.fpath) time.sleep(2) os.system("echo -500 -100 -300 | " + self.datafaker + " " + self.fpath) time.sleep(2) self.assert_(self.context_client_stable.expect('Position.Stable = bool:false')) # ...and that we return when those two samples disappear from the buffer while index > 0: os.system("echo -500 -100 -300 | " + self.datafaker + " " + self.fpath) index = index - 1 time.sleep(1.1) time.sleep(2) self.assert_(self.context_client_stable.expect('Position.Stable = bool:true')) # Go back to stable = false os.system("echo -36 -90 953 | " + self.datafaker + " " + self.fpath) time.sleep(2) os.system("echo -36 -90 953 | " + self.datafaker + " " + self.fpath) time.sleep(2) self.assert_(self.context_client_stable.expect('Position.Stable = bool:false')) # Verify that timer works #Todo: Here should find the configuration file according to device type timeout = int(os.popen("cat `ls /etc/sensorfw/sensord.conf.d/* /etc/sensorfw/sensord-ncdk.conf` | grep stability_timeout | head -n1 | cut -f2 -d=", "r").read()) if (timeout <= 0): # Timeout not set in config, 60s is the default timeout = 60 time.sleep(timeout) self.assert_(self.context_client_stable.expect('Position.Stable = bool:true'))
def testProviders(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth", "test.fake") listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]) # wait for it # test querying the provider(s) listen.send("providers test.int") self.assert_( listen.expect("^providers: session:com.nokia.test@contextkit-dbus$")) listen.send("providers test.fake") self.assert_( listen.expect("^providers:$")) listen.wait() provider.wait()
def testNoCommanding(self): # CONTEXT_COMMANDING is not set del os.environ['CONTEXT_COMMANDING'] provider = CLTool("context-provide", "--v2", "contextkit.test", "int", "test.int", "42") provider.send("dump") provider.expect("Wrote") # wait for it commander = CLTool("context-provide", "--v2") commander.send("add int test.int 4242") commander.send("start") commander.expect("Added") # wait for it os.environ["CONTEXT_CLI_IGNORE_COMMANDER"] = "" listen = CLTool("context-listen", "test.int") self.assert_(listen.expect(wanted("test.int", "int", "42")), "Provider provided value is wrong") listen.wait() commander.wait() provider.wait()
def testRapidChanges(self): """ Description This test verifies that the subscriber library doesn't emit unnecessary valueChanged signals (in spite of receiving multiple D-Bus messages in the same main loop iteration). Steps 1. starts up a provider with one property 2. starts a client, verifies that the value arrives 3. suspends the client (so it stops processing D-Bus messages) 4. sets new value for the property twice in the provider 5. resumes the client 6. verifies that the client receives only one valueChanged signal """ provider_fast = CLTool("context-provide", "--v2", "com.nokia.fast", "int","test.fast","44") provider_fast.expect("Setting key") context_client = CLTool("context-listen", "test.fast") self.assert_(context_client.expect(wanted("test.fast", "int", "44")), "Bad value for the fast property, wanted 44") context_client.suspend() provider_fast.send("test.fast = 34") provider_fast.expect("Setting key") provider_fast.send("test.fast = 54") provider_fast.expect("Setting key") context_client.resume() context_client.expect(wanted("test.fast", "int", "54")) # the two value changes can happen very close in time, so we # have to check that beyond the good value there are no other # value(s) if len(re.findall("test.fast =", context_client.last_output)) != 1: context_client.printio() self.assert_(False, "expected a single valueChanged") # not even after waiting one second self.assertFalse(context_client.expect("test.fast =", wantdump = False, timeout=1), "expected a single valueChanged") context_client.wait() provider_fast.wait()
def testInfos(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Assert the returned information for key, description and type Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth") listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]) # wait for it listen.send("ikey test.int") self.assert_( listen.expect("^ikey: test.int$"), "ikey didn't work") listen.send("man test.truth") self.assert_( listen.expect("^man: A phony but very flexible property.$"), "man didn't work") listen.send("type test.truth") self.assert_( listen.expect("^type: TRUTH$"), "type didn't work") listen.wait() provider.wait()
class ProfilePlugin(unittest.TestCase): def setUp(self): # Set the initial value before subscribe set_profile("general") self.context_client = CLTool("context-listen", "Profile.Name") def tearDown(self): # Restore some default values for Profile set_profile("general") def testProfileName(self): self.assert_(self.context_client.expect("Profile.Name = QString:\"general\"")) # silent set_profile("silent") self.assert_(self.context_client.expect("Profile.Name = QString:\"silent\"")) # meeting set_profile("meeting") self.assert_(self.context_client.expect("Profile.Name = QString:\"meeting\"")) # outdoors set_profile("outdoors") self.assert_(self.context_client.expect("Profile.Name = QString:\"outdoors\"")) def testInvalidProfileName(self): self.assert_(self.context_client.expect("Profile.Name = QString:\"general\"")) set_profile("") self.assertEqual(get_profile(),"general") self.assertFalse(self.context_client.expect("Profile.Name = QString:\"general\"", wantdump=False, timeout=10)) self.context_client.send("value Profile.Name") self.assert_(self.context_client.expect("value: QString:\"general\""))
def testToggleFullAndNormal(self): program = CLTool("screentoggler") self.assert_(program.expect("ready")) self.assert_(self.context_client.expect('Session.State = QString:"normal"')) program.send("full") # wait for the change to happen and the helper program to print out "full" self.assert_(program.expect("full")) self.assert_(self.context_client.expect('Session.State = QString:"fullscreen"')) program.send("normal") self.assert_(program.expect("normal")) self.assert_(self.context_client.expect('Session.State = QString:"normal"')) program.close()
def testStartFull(self): self.assert_(self.context_client.expect('Session.State = QString:"normal"')) program = CLTool("screentoggler", "full") self.assert_(program.expect("ready")) self.assert_(self.context_client.expect('Session.State = QString:"fullscreen"')) program.close() self.assert_(self.context_client.expect('Session.State = QString:"normal"'))
def test_has_provider(self): global property_name # start listening to the property context_client = CLTool("context-listen", property_name) context_client.send("providers " + property_name) # assert that there is at least one provider; plugin and # constructionstring are separated with @ in the provider # desription self.assert_(context_client.expect("@"))
def testProperties(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "-5", "string", "test.string", "something", "double", "test.double", "4.231", "truth", "test.truth", "False") provider.send("dump") self.assert_(provider.expect("Wrote ./context-provide.context"), "context-provide.context couldn't been written by context-provide") info_client = CLTool("context-ls","-l","-d","test.*") self.assert_(info_client.expect(["^test.int\tinteger\tcontextkit-dbus\tsession:com.nokia.test$", "^test.double\tnumber\tcontextkit-dbus\tsession:com.nokia.test$", "^test.truth\tbool\tcontextkit-dbus\tsession:com.nokia.test$", "^test.string\tstring\tcontextkit-dbus\tsession:com.nokia.test$", "^Documentation: A phony but very flexible property.$"]), "Bad introspection result from context-ls") info_client.wait() provider.wait()
class SessionPlugin(unittest.TestCase): def setUp(self): self.provider = CLTool("context-provide", "--v2", "--session", "org.maemo.fake.provider", "bool", "Screen.Blanked", "false") self.context_client = CLTool("context-listen", "Session.State") def tearDown(self): self.context_client.close() self.provider.close() def testInitial(self): self.assert_(self.context_client.expect('Session.State = QString:"normal"')) def testBlanking(self): self.assert_(self.context_client.expect('Session.State = QString:"normal"')) self.provider.send("Screen.Blanked=true") self.assert_(self.context_client.expect('Session.State = QString:"blanked"')) self.provider.send("Screen.Blanked=false") self.assert_(self.context_client.expect('Session.State = QString:"normal"')) self.provider.send("Screen.Blanked=true") self.assert_(self.context_client.expect('Session.State = QString:"blanked"')) self.provider.send("unset Screen.Blanked") self.assert_(self.context_client.expect('Session.State = QString:"normal"'))
def testStartNormal(self): self.assert_(self.context_client.expect('Session.State = QString:"normal"')) program = CLTool("screentoggler") self.assert_(program.expect("ready")) # no change signals should be delivered, so we need to query the value self.context_client.send("v Session.State") self.assert_(self.context_client.expect('value: QString:"normal"')) program.close() self.context_client.send("v Session.State") self.assert_(self.context_client.expect('value: QString:"normal"'))
def testReturnValue(self): provider = CLTool("context-provide", "--v2", "com.nokia.test", "int", "test.int", "-5", "string", "test.string", "something", "double", "test.double", "4.231", "truth", "test.truth", "False") provider.send("dump") self.assert_(provider.expect("Wrote ./context-provide.context")) # wait for it info_client = CLTool("context-ls","test.*") returnValue = info_client.wait() self.assertEqual(returnValue, 0, "context-ls exited with return value != 0") info_client.wait() provider.wait()
def testAllDataTypes(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Properties values Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth") self.assert_( listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]), "Actual key values pairs do not match expected") listen.wait() provider.wait()
def testTruthTypePermutations(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Change property of type bool to False, None and True Property value of type bool is updated to False, None and True Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "truth", "test.truth", "False") provider.expect("Setting key: test.truth") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it provider.send("test.truth = False") provider.expect("Setting") # wait for it listen = CLTool("context-listen", "test.truth") self.assert_( listen.expect(wanted("test.truth", "bool", "false")), "setting to false didn't work") provider.send("unset test.truth") self.assert_( listen.expect(wantedUnknown("test.truth")), "setting to unknown didn't work") provider.send("test.truth = True") self.assert_( listen.expect(wanted("test.truth", "bool", "true")), "setting to true didn't work") listen.wait() provider.wait()
def testMultipleProviders2(self): """ Description This test verifies correct client behavior in the presence of multiple providers. In this test we always start the client after the providers have already been started and values have been set. Steps 1. starts two providers (X and Y) providing the same P property 2. X sets P to V1 3. Y sets P to V2 4. starts a client 5. commands X to sleep, to make sure the client gets the value from Y first checks that value for P is V2 even though it got first V2 and then V1 """ provider_x = CLTool("context-provide", "--v2", "test.x", "int", "test.prop", "44") provider_x.send("dump x.context") provider_x.expect("Wrote") provider_y = CLTool("context-provide", "--v2", "test.y", "int", "test.prop", "22") provider_y.send("dump y.context") provider_y.expect("Wrote") client = CLTool("context-listen") client.expect("Available commands") provider_x.send("sleep 2") provider_x.expect("Sleeping") client.send("n test.prop") time.sleep(4) client.send("value test.prop") self.assert_(client.expect("^value: qulonglong:22$")) client.wait() provider_y.wait() provider_y.wait()
class Orientation(unittest.TestCase): def setUp(self): self.fpath = "/tmp/fakedsensors/als" self.context_client_dark = CLTool('context-listen', 'Environment.IsDark') self.context_client_bright = CLTool('context-listen', 'Environment.IsBright') def tearDown(self): self.context_client_dark.atexit() self.context_client_bright.atexit() def testIsDark(self): # Normal os.system("echo -en '\x94' >" + self.fpath) # 148 self.assert_( self.context_client_dark.expect('Environment.IsDark = bool:false')) time.sleep(0.5) # Dark os.system("echo -en '\x09' >" + self.fpath) # 9 self.assert_( self.context_client_dark.expect('Environment.IsDark = bool:true')) time.sleep(0.5) # Bright os.system("echo -en '\x33\x01' >" + self.fpath) # 307 self.assert_( self.context_client_dark.expect('Environment.IsDark = bool:false')) time.sleep(0.5) def testIsBright(self): # Normal os.system("echo -en '\x94' >" + self.fpath) # 148 self.assert_( self.context_client_bright.expect( 'Environment.IsBright = bool:false')) time.sleep(0.5) # Bright os.system("echo -en '\x33\x01' >" + self.fpath) # 307 self.assert_( self.context_client_bright.expect( 'Environment.IsBright = bool:true')) time.sleep(0.5) # Dark os.system("echo -en '\x09' >" + self.fpath) # 9 self.assert_( self.context_client_bright.expect( 'Environment.IsBright = bool:false')) time.sleep(0.5)
def testWaitForSubscribeFail(self): """ Description Subscriber blocks until WaitForSubsciption returns, even if provider is not on DBus (specific to ContextKitPlugin) Pre-conditions Provider does exist for the property in the registry but it is not running Steps Subscribe and wait for the property Post-conditions The wait returned in 3 seconds (enough for testing that provider is not on D-Bus) """ context_client = CLTool("context-listen") context_client.send("n test.nonexistent") context_client.send("w test.nonexistent") self.assert_(context_client.expect("wait finished for test.nonexistent"), "Wait for subscription is probably in a dead state") context_client.wait()
def testStringTypePermutations(self): """ Description Subscribe to 4 properties covering basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Change property of type string to empty string and None Property value of type bool is updated to empty string and None Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "string", "test.string", "something") provider.expect("Setting key: test.string") # wait for it provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.string") self.assert_( listen.expect("^test.string = QString:\"something\"$"), "setting to 'something' didn't work") provider.send('test.string = ""') self.assert_( listen.expect("^test.string = QString:\"\"$"), "setting to empty string didn't work") provider.send('unset test.string') self.assert_( listen.expect(wantedUnknown("test.string")), "setting to null didn't work") listen.wait() provider.wait()
class Orientation(unittest.TestCase): def setUp(self): self.fpath = "/tmp/fakedsensors/als" self.context_client_dark = CLTool('context-listen', 'Environment.IsDark') self.context_client_bright = CLTool('context-listen', 'Environment.IsBright') def tearDown(self): self.context_client_dark.atexit() self.context_client_bright.atexit() def testIsDark(self): # Normal os.system("echo -en '\x94' >" + self.fpath) # 148 self.assert_(self.context_client_dark.expect('Environment.IsDark = bool:false')) time.sleep(0.5) # Dark os.system("echo -en '\x09' >" + self.fpath) # 9 self.assert_(self.context_client_dark.expect('Environment.IsDark = bool:true')) time.sleep(0.5) # Bright os.system("echo -en '\x33\x01' >" + self.fpath) # 307 self.assert_(self.context_client_dark.expect('Environment.IsDark = bool:false')) time.sleep(0.5) def testIsBright(self): # Normal os.system("echo -en '\x94' >" + self.fpath) # 148 self.assert_(self.context_client_bright.expect('Environment.IsBright = bool:false')) time.sleep(0.5) # Bright os.system("echo -en '\x33\x01' >" + self.fpath) # 307 self.assert_(self.context_client_bright.expect('Environment.IsBright = bool:true')) time.sleep(0.5) # Dark os.system("echo -en '\x09' >" + self.fpath) # 9 self.assert_(self.context_client_bright.expect('Environment.IsBright = bool:false')) time.sleep(0.5)
class BluezPlugin(unittest.TestCase): def setUp(self): # Make Bluetooth invisible and un-enabled # Note: This test will alter the bluetooth settings of the system! os.system("dbusnamewatcher --system org.bluez 10") os.system("hciconfig hci0 up") set_bluez_property("Discoverable", "false") set_bluez_property("Powered", "false") self.context_client = CLTool("context-listen", "Bluetooth.Enabled", "Bluetooth.Visible") def tearDown(self): # Restore some default values for Bluez set_bluez_property("Discoverable", "false") set_bluez_property("Powered", "true") def testInitial(self): self.assert_(self.context_client.expect("Bluetooth.Visible = bool:false\nBluetooth.Enabled = bool:false")) def testEnabledAndVisible(self): self.assert_(self.context_client.expect("Bluetooth.Visible = bool:false\nBluetooth.Enabled = bool:false")) # Enable set_bluez_property("Powered", "true") self.assert_(self.context_client.expect("Bluetooth.Enabled = bool:true")) # Set visible set_bluez_property("Discoverable", "true") self.assert_(self.context_client.expect("Bluetooth.Visible = bool:true")) # Set invisible set_bluez_property("Discoverable", "false") self.assert_(self.context_client.expect("Bluetooth.Visible = bool:false")) # Disable set_bluez_property("Powered", "false") self.assert_(self.context_client.expect("Bluetooth.Enabled = bool:false"))
def testAsynchronicity(self): """ Description This test verifies that the asynchronicity of the subscriber library Pre-conditions 2 Providers started with context-provide tool. Each provider offers one property. 1 provider is delayed (3s). Steps Subscribe to both properties Verify that return values are correct Measure the time elapsed between the reception of the 2 values. Post-conditions Kill providers References None """ # start the client provider_slow = CLTool("context-provide", "--v2", "com.nokia.slow", "int","test.slow","42") provider_slow.expect("Setting key") # wait for it provider_fast = CLTool("context-provide", "--v2", "com.nokia.fast", "int","test.fast","44") provider_fast.expect("Setting key") # wait for it context_client = CLTool("context-listen") context_client.expect("Available commands") # wait for it provider_slow.comment("provider_slow sleep time started at" + str(time.time())) provider_slow.send("sleep 3") provider_slow.expect("Sleeping") # wait for it context_client.send("n test.slow ; n test.fast") # check the fast property self.assert_(context_client.expect(wanted("test.fast", "int", "44")), # timeout == 3 seconds "Bad value for the fast property, wanted 44") fast_time = time.time() context_client.comment("Fast property arrived with good value at: " + str(fast_time)) # check the slow property self.assert_(context_client.expect(wanted("test.slow", "int", "42")), # timeout == 10 seconds max, but 5 is enough usually "Bad value for the slow property, wanted 42") slow_time = time.time() context_client.comment("Slow property arrived with good value at: " + str(slow_time)) if slow_time - fast_time < 1.5: provider_slow.printio() context_client.printio() self.assert_(False, "The arrival time of the fast and slow property is not far enough from each other") # context_client.printio() context_client.wait() provider_fast.wait() provider_slow.wait()
class SubscriptionPause (unittest.TestCase): def tearDown(self): self.listen.wait() self.provider.wait() os.unlink('context-provide.context') def testPause(self): """ Description A subscriber successively subscribes and unsubscribes from a property Pre-conditions Provider started via context-provide tool. 2 properties of data type int Steps Subscribe to a property Subscriber receives updated value Unsubscribe from the property Subscriber does not receive updated value Resubscribe to the property Subscriber receives updated value Post-conditions Kill provider References None """ self.provider = CLTool("context-provide", "--v2","com.nokia.test", "int","test.int","1") self.provider.expect("Setting key: test.int") # wait for it self.provider.send("dump") self.provider.expect("Wrote") # wait for it self.listen = CLTool("context-listen","test.int") self.assert_( self.listen.expect(wanted("test.int", "int", "1"))) self.provider.send("test.int = -5") self.assert_( self.listen.expect(wanted("test.int", "int", "-5"))) self.listen.send("unsubscribe test.int") self.provider.send("test.int = 3") self.assertFalse( self.listen.expect(wanted("test.int", "int", "3"), wantdump=False)) self.listen.send("subscribe test.int") self.assert_( self.listen.expect(wanted("test.int", "int", "3"))) self.provider.send("test.int = 6") self.assert_( self.listen.expect(wanted("test.int", "int", "6"))) def testWaitForSubscribe(self): """ Description Subscriber blocks until WaitForSubsciption returns Pre-conditions Provider started via context-provide tool. 1 data type supported int Steps Delete the property test.int Delay the provider Subscribe to the property test.int Subscriber blocks until the subscription is complete Post-conditions Kill provider References None """ self.provider = CLTool("context-provide", "--v2","com.nokia.test", "int","test.int","1") self.provider.expect("Setting key: test.int") # wait for it self.provider.send("dump") self.provider.expect("Wrote") # wait for it self.provider.send("sleep 3") self.provider.expect("Sleep") # wait for it self.listen = CLTool("context-listen") self.listen.send("new test.int") self.listen.send("waitforsubscription test.int") expected = [wanted("test.int", "int", "1"), "^wait finished for test.int$"] # I don't get it quickly self.assertFalse( self.listen.expect(expected, wantdump=False, timeout=1)) # but get it after a while self.assert_(self.listen.expect(expected))
def testTwoProviders(self): provider1 = CLTool("context-provide", "--v2","com.nokia.test", "truth","test.truth","True") provider1.expect("Setting key: test.truth") # wait for it provider1.send("dump context-provide1.context") provider1.expect("Wrote") # wait for it provider2 = CLTool("context-provide", "--v2","com.nokia.test2", "int","test.int","24") provider2.expect("Setting key: test.int") # wait for it provider2.send("dump context-provide2.context") provider2.expect("Wrote") # wait for it listen = CLTool("context-listen","test.int","test.truth") listen.expect("Available commands") # wait for it provider2.send("test.int = -68") self.assert_( listen.expect(wanted("test.int", "int", "-68"))) provider1.send("test.truth = False") self.assert_( listen.expect(wanted("test.truth", "bool", "false"))) listen.wait() provider2.wait() provider1.wait()
def testValue(self): """ Description Subscribe to 4 properties covering basic data types Query the value of the property (when it's known or unknown, with and without a default value) Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Change value of type int from the provider to a known value Query the value (both with and without a default value) and assert the result Change value of type int from the provider to unknown Query the value (both with and without a default value) and assert the result Post-conditions Kill provider References None """ provider = CLTool("context-provide", "--v2", "com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") provider.send("dump") provider.expect("Wrote") # wait for it listen = CLTool("context-listen", "test.double", "test.string", "test.int", "test.truth") self.assert_( listen.expect([wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")]), "Actual key values pairs do not match expected") provider.send("test.int = 100") listen.expect(wanted("test.int", "int", "100")) # wait for it listen.send("value test.int") self.assert_( listen.expect("^value: qulonglong:100$"), "Value command returned wrong value") listen.send("value test.int \"defaultValue\"") self.assert_( listen.expect("^value: qulonglong:100$"), "Value command returned wrong value") provider.send("unset test.int") listen.expect("Unknown") # wait for it listen.send("value test.int") self.assert_( listen.expect("^value: Unknown$"), "Value command returned wrong value") listen.send("value test.int \"defaultValue\"") self.assert_( listen.expect("^value: QString:\"defaultValue\"$"), "Value command returned wrong value") listen.send("key test.int") self.assert_( listen.expect("^key: test.int$"), "Key command returned wrong value") # Because of a python threading / process / CLTool # issue, processes need to be waited for in the # opposite order as they are started. listen.wait() provider.wait()
class MultipleSubscribers(unittest.TestCase): def setUp(self): self.flexiprovider = CLTool("context-provide", "--v2","com.nokia.test", "int","test.int","1", "string","test.string","foobar", "double","test.double","2.5", "truth","test.truth","True") self.flexiprovider.expect("Setting key: test.truth") # wait for it self.flexiprovider.send("dump") self.flexiprovider.expect("Wrote") # wait for it self.context_client1 = CLTool("context-listen","test.int","test.double","test.string","test.truth") self.context_client2 = CLTool("context-listen","test.int","test.double") self.context_client3 = CLTool("context-listen","test.int","test.string","test.truth") self.context_client4 = CLTool("context-listen","test.int","test.double","test.string") def tearDown(self): self.context_client4.wait() self.context_client3.wait() self.context_client2.wait() self.context_client1.wait() self.flexiprovider.wait() os.unlink('./context-provide.context') def testInitialSubscription(self): """ Description 4 subscribers instantiate multiple properties of 4 basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Subscribers receive initial values from provider Post-conditions Kill provider References None """ client1_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client2_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1")] client3_expected = [wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client4_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\"")] self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client2.expect(client2_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client4.expect(client4_expected), "Actual key values pairs do not match expected") def testValueChanged(self): """ Description 4 subscribers instantiate multiple properties of 4 basic data types Pre-conditions Provider started via context-provide tool. 4 data types supported int, bool, double and string Steps Subscribe to the properties, int, bool, double, string Values are changed successively from the provider Subscribers receive updated values Post-conditions Kill provider References None """ client1_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client2_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1")] client3_expected = [wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\""), wanted("test.truth", "bool", "true")] client4_expected = [wanted("test.double", "double", "2.5"), wanted("test.int", "int", "1"), wanted("test.string", "QString", "\"foobar\"")] self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client2.expect(client2_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client4.expect(client4_expected), "Actual key values pairs do not match expected") self.flexiprovider.send("test.double = -5.3") client1_expected = wanted("test.double", "double", "-5.3") client2_expected = wanted("test.double", "double", "-5.3") client4_expected = wanted("test.double", "double", "-5.3") self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client2.expect(client2_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client4.expect(client4_expected), "Actual key values pairs do not match expected") self.context_client3.send("type test.truth") client3_expected = "^type: TRUTH$" self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected") self.flexiprovider.send("unset test.truth") client1_expected = wantedUnknown("test.truth") client3_expected = wantedUnknown("test.truth") self.assert_(self.context_client1.expect(client1_expected), "Actual key values pairs do not match expected") self.assert_(self.context_client3.expect(client3_expected), "Actual key values pairs do not match expected")
def testMultipleProviders(self): """ Description This test verifies correct client behavior in the presence of multiple providers. Steps 1. starts up a client 2. starts two providers (X and Y) providing the same P property 3. X sets P to V1 and verifies that the client got it 4. Y sets P to V2 and likewise verifies in the client 5. Y sets P to NULL, the client verifies that P goes back to V1 6. Y sets P to V3, the client verifies P == V3 7. Y is removed from the registry, client verifies that P == V1 8. X is removed from the registry, client verifies that P == NULL """ client = CLTool("context-listen", "test.prop") client.expect("Available commands") provider_x = CLTool("context-provide", "--v2", "test.X", "int", "test.prop", "44") provider_x.send("dump x.context") provider_x.expect("Wrote") provider_y = CLTool("context-provide", "--v2", "test.Y", "int", "test.prop", "22") provider_y.send("dump y.context") provider_y.expect("Wrote") provider_x.send("test.prop = 55"); provider_x.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "55"))) provider_y.send("test.prop = 77"); provider_y.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "77"))) provider_y.send("unset test.prop"); provider_y.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "55"))) provider_y.send("test.prop = 99"); provider_y.expect("Setting key") self.assert_(client.expect(wanted("test.prop", "int", "99"))) provider_y.wait() os.unlink("y.context") self.assert_(client.expect(wanted("test.prop", "int", "55"))) provider_x.wait() os.unlink("x.context") self.assert_(client.expect(wantedUnknown("test.prop"))) client.wait()
class Orientation(unittest.TestCase): def setUp(self): self.fpath = "/tmp/fakedsensors/accelerometer" self.datafaker = "/usr/bin/datafaker" self.context_client_edge = CLTool("context-listen", "Screen.TopEdge", "Position.IsFlat") self.context_client_cover = CLTool("context-listen", "Screen.IsCovered") # Get angle thresholds from config #Todo: Here should find the configuration file according to device type landscape_angle = int( os.popen( "cat `ls /etc/sensorfw/sensord.conf.d/* /etc/sensorfw/sensord-ncdk.conf` | grep threshold_landscape | head -n1 | cut -f2 -d=", "r").read()) portrait_angle = int( os.popen( "cat `ls /etc/sensorfw/sensord.conf.d/* /etc/sensorfw/sensord-ncdk.conf` | grep threshold_portrait | head -n1 | cut -f2 -d=", "r").read()) print("Using thresholds for orientation changes:\n Landscape: " + str(landscape_angle) + "\n Portrait: " + str(portrait_angle) + "\n") # Create data sets self.dataSet = [] self.expectSet = [] dataSet_top = [] dataSet_left = [] # TopEdge = top (U, U, T, U, T), starting from Unknown for angle in [ 0, portrait_angle - 1, portrait_angle + 1, portrait_angle - 1, 90 ]: dataSet_top.append([ 0, int(1000 * math.cos(math.radians(90 - angle))), int(1000 * math.cos(math.radians(angle))) ]) self.dataSet += dataSet_top self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('') self.expectSet.append('Screen.TopEdge = QString:"top"') self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('Position.IsFlat = bool:false') # TopEdge = left (U, U, L, U, L) for angle in [ 0, landscape_angle - 1, landscape_angle + 1, landscape_angle - 1, 90 ]: dataSet_left.append([ -int(1000 * math.cos(math.radians(90 - angle))), 0, int(1000 * math.cos(math.radians(angle))) ]) self.dataSet += dataSet_left self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('') self.expectSet.append('Screen.TopEdge = QString:"left"') self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('Position.IsFlat = bool:false') # TopEdge = bottom, (U, U, B, U, B) for v in dataSet_top[:]: u = v[:] u[1] = -u[1] self.dataSet.append(u) self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('') self.expectSet.append('Screen.TopEdge = QString:"bottom"') self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('Position.IsFlat = bool:false') # TopEdge = right (U, U, R, U, R) for v in dataSet_left[:]: u = v[:] u[0] = -u[0] self.dataSet.append(u) self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('') self.expectSet.append('Screen.TopEdge = QString:"right"') self.expectSet.append('Position.IsFlat = bool:true') self.expectSet.append('Position.IsFlat = bool:false') # TopEdge: left -> top -> left (should represent bottom and right well enough) for angle in [ 90, portrait_angle - 1, portrait_angle + 1, 90 - portrait_angle, 0 ]: self.dataSet.append([ -int(1000 * math.cos(math.radians(angle))), int(1000 * math.cos(math.radians(90 - angle))), 0 ]) self.expectSet.append('Screen.TopEdge = QString:"top"') self.expectSet.append('Screen.TopEdge = QString:"left"') self.expectSet.append('') self.expectSet.append('Screen.TopEdge = QString:"top"') self.expectSet.append('Screen.TopEdge = QString:"left"') def tearDown(self): self.context_client_edge.atexit() self.context_client_cover.atexit() def testOrientation(self): # Set the starting position to bottom (0, -1000, 0) os.system("echo 0 -1000 0 | " + self.datafaker + " " + self.fpath) index = 0 for v in self.dataSet[:]: time.sleep(0.9) if self.expectSet[index] != '': os.system("echo " + str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + " | " + self.datafaker + " " + self.fpath) self.assert_( self.context_client_edge.expect(self.expectSet[index])) index += 1 # Set the starting position os.system("echo 0 0 -1000 | " + self.datafaker + " " + self.fpath) time.sleep(0.9) # On the table os.system("echo -36 -90 953 | " + self.datafaker + " " + self.fpath) self.assert_( self.context_client_cover.expect('Screen.IsCovered = bool:false')) time.sleep(0.9) # On the table upside down os.system("echo 270 216 -972 | " + self.datafaker + " " + self.fpath) self.assert_( self.context_client_cover.expect('Screen.IsCovered = bool:true'))