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 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()
        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 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()
    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()
        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 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()
    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 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 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 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 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 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 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 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()
    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()