Author: jmorliaguet Date: Sun Feb 5 11:43:33 2006 New Revision: 2295 Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/__init__.py (contents, props changed) cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/browser.py (contents, props changed) cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/configure.zcml (contents, props changed) cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/cpsskins_chat.pt - copied, changed from r2294, cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/cpsskins_chat.pt cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/configure.zcml (contents, props changed) cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/__init__.py (contents, props changed) cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/browser.py - copied, changed from r2293, cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/browser.py cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/configure.zcml (contents, props changed) cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/cpsskins_quiz.pt (contents, props changed) Removed: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/browser.py cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/cpsskins_chat.pt cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/cpsskins_storage_adapters.pt Modified: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/configure.zcml cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/unit/cpsskins_storage_adapters.pt Log:
- added a quiz/ and a chat/ directory to sort out example applications Modified: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/configure.zcml ============================================================================== --- cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/configure.zcml (original) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/configure.zcml Sun Feb 5 11:43:33 2006 @@ -1,33 +1,4 @@ -<configure - xmlns:browser="http://namespaces.zope.org/browser"> - - <browser:pages - layer="cpsskins" - for="*" - class=".browser.Accessors" - permission="zope.Public"> - - <browser:page - name="getData1" - attribute="getData1" - /> - - <browser:page - name="setData1" - attribute="setData1" - /> - - <browser:page - name="getData2" - attribute="getData2" - /> - - <browser:page - name="setData2" - attribute="setData2" - /> - - </browser:pages> +<configure> <include package=".functional" /> Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/__init__.py ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/__init__.py Sun Feb 5 11:43:33 2006 @@ -0,0 +1 @@ +# Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/browser.py ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/browser.py Sun Feb 5 11:43:33 2006 @@ -0,0 +1,54 @@ + +import time + +from zope.app.publisher.browser import BrowserView +from zope.app.cache.ram import RAMCache +from zope.app.session.interfaces import ISession + +from cpsskins import minjson as json + +cache = RAMCache() + +class Views(BrowserView): + + def __init__(self, context, request): + self.context = context + self.request = request + self.session = ISession(self.request)['cpsskins'] + + def getData(self): + data = cache.query('data2', {}, {}) + return json.write(data) + + def setData(self, data): + data = json.read(data) + + current_data = cache.query('data2', {}, {}) + messages = current_data.get('messages', []) + message = data.get('message') + if message: + ip = self.request.get('REMOTE_ADDR') + messages.append({'message': message, 'ip': ip}); + + data['messages'] = messages + data['last-updated'] = int(time.time()) + cache.set(data, 'data2', {}) + + return json.write(data) + + def getLastUpdated(self): + data = cache.query('data2', {}, {}) + return json.write({'last-updated': data.get('last-updated', 0)}) + + def getPreferences(self): + data = self.session.get('data4', {u'user': u'Guest'}) + return json.write(data) + + def setPreferences(self, data): + data = json.read(data) + user = data.get('user') + if user: + self.session['data1'] = data + + return json.write(data) + Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/configure.zcml ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/configure.zcml Sun Feb 5 11:43:33 2006 @@ -0,0 +1,45 @@ +<configure + xmlns:browser="http://namespaces.zope.org/browser"> + + <browser:page + for="*" + layer="cpsskins" + name="cpsskins_chat.html" + permission="zope.Public" + template="cpsskins_chat.pt" + /> + + <browser:pages + layer="cpsskins" + for="*" + class=".browser.Views" + permission="zope.Public"> + + <browser:page + name="getData" + attribute="getData" + /> + + <browser:page + name="setData" + attribute="setData" + /> + + <browser:page + name="getLastUpdated" + attribute="getLastUpdated" + /> + + <browser:page + name="getPreferences" + attribute="getPreferences" + /> + + <browser:page + name="setPreferences" + attribute="setPreferences" + /> + + </browser:pages> + +</configure> Copied: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/cpsskins_chat.pt (from r2294, cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/cpsskins_chat.pt) ============================================================================== --- cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/cpsskins_chat.pt (original) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/cpsskins_chat.pt Sun Feb 5 11:43:33 2006 @@ -143,8 +143,8 @@ "storage": { "type": "remote", "accessors": { - "get": "@@getData2", - "set": "@@setData2" + "get": "@@getData", + "set": "@@setData" } }} </ins> Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/configure.zcml ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/configure.zcml Sun Feb 5 11:43:33 2006 @@ -0,0 +1,7 @@ +<configure> + + <include package=".quiz" /> + + <include package=".chat" /> + +</configure> Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/__init__.py ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/__init__.py Sun Feb 5 11:43:33 2006 @@ -0,0 +1 @@ +# Copied: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/browser.py (from r2293, cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/browser.py) ============================================================================== --- cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/browser.py (original) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/browser.py Sun Feb 5 11:43:33 2006 @@ -2,25 +2,22 @@ import time from zope.app.publisher.browser import BrowserView -from zope.app.cache.ram import RAMCache from zope.app.session.interfaces import ISession from cpsskins import minjson as json RIGHT_ANSWERS = ('blue', 'red', 'yellow') -cache = RAMCache() - -class Accessors(BrowserView): +class Views(BrowserView): def __init__(self, context, request): self.context = context self.request = request self.session = ISession(self.request)['cpsskins'] - def getData1(self): + def getAnswer(self): # sleep one second to simulate a delay - time.sleep(1.5) + time.sleep(1) # get the data from the session data = self.session.get('data1', {u'content': u'No data available.'}) @@ -28,8 +25,8 @@ # return the data return json.write(data) - def setData1(self, data): - # sleep one second to simulate a delay + def setAnswer(self, data): + # sleep 2 seconds to simulate a delay time.sleep(2) # get the data @@ -51,20 +48,3 @@ # return the results return json.write(data) - def getData2(self): - data = cache.query('data2', {}, {}) - return json.write(data) - - def setData2(self, data): - data = json.read(data) - - current_data = cache.query('data2', {}, {}) - messages = current_data.get('messages', []) - ip = self.request.get('REMOTE_ADDR') - messages.append({'message': data['message'], 'ip': ip}); - - data['messages'] = messages - cache.set(data, 'data2', {}) - - return json.write(data) - Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/configure.zcml ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/configure.zcml Sun Feb 5 11:43:33 2006 @@ -0,0 +1,30 @@ +<configure + xmlns:browser="http://namespaces.zope.org/browser"> + + <browser:page + for="*" + layer="cpsskins" + name="cpsskins_quiz.html" + permission="zope.Public" + template="cpsskins_quiz.pt" + /> + + <browser:pages + layer="cpsskins" + for="*" + class=".browser.Views" + permission="zope.Public"> + + <browser:page + name="getAnswer" + attribute="getAnswer" + /> + + <browser:page + name="setAnswer" + attribute="setAnswer" + /> + + </browser:pages> + +</configure> Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/cpsskins_quiz.pt ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/quiz/cpsskins_quiz.pt Sun Feb 5 11:43:33 2006 @@ -0,0 +1,282 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xml:lang="en" lang="en" + xmlns="http://www.w3.org/1999/xhtml"> +<head> + <title>CPSSkins Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="/++skin++cpsskins/@@/++resource++prototype.js" + type="text/javascript"></script> + <script src="/++skin++cpsskins/@@/++resource++json.js" + type="text/javascript"></script> + <script src="/++skin++cpsskins/@@/++resource++cpsskins.js" + type="text/javascript"></script> + <link rel="stylesheet" type="text/css" + href="/++skin++cpsskins/@@/++resource++cpsskins.css" /> + + <style type="text/css"> + .choices { + font: 1.3em Arial; + } + + div.inputbox { + padding: 0.5em; + width: 300px; + border: 1px solid #666; + background-color: #efc; + } + + input { + margin-bottom: 0.5em; + border-width: 1px; + border-style: solid; + border-color: #000 #999 #999 #000; + width: 175px; + font: 1.3em arial; + } + + div.content { + border: 1px solid #999; + background-color: #ffc; + padding: 1em; + } + + div.message { + border: 1px solid #000; + background-color: #fc0; + padding: 0.5em; + margin-bottom: 1em; + } + </style> + + <script type="text/javascript"> + + Object.extend(Widgets, { + + inputbox: function(def) { + var widget = Canvas.createNode({ + tag: "div", + classes: "inputbox" + }); + + return new InputBox(widget, def); + } + + }); + + InputBox = Class.create(); + InputBox.prototype = Object.extend(new CPSSkins.View(), { + + setup: function() { + this.changeEvent = this.changeEvent.bindAsEventListener(this); + Event.observe(this.widget, "change", this.changeEvent); + }, + + render: function(data) { + var widget = this.widget; + widget.innerHTML = ''; + + var input = Canvas.createNode({ + tag: "input", + attributes: { + "type": "text" + } + }); + + var content = Canvas.createNode({ + tag: "div", + classes: "content", + text: data.content + }); + + if (data.message) { + var message = Canvas.createNode({ + tag: "div", + classes: "message", + text: data.message + }); + widget.appendChild(message); + } + + widget.appendChild(input); + widget.appendChild(content); + }, + + changeEvent: function(e) { + var target = Event.element(e); + var value = target.value; + this.handleAction(target, 'change', value); + } + + }); + + /* Controller */ + + function changeContent(model, context, value) { + model.setData({'message': 'Sending the answer ...', 'content': value}); + } + + CPSSkins.registerHandlers({'changeContent': changeContent}); + + </script> + +</head> +<body> + + <h1>CPSSkins: storage adapters</h1> + + <p>A <strong>storage adapter</strong> can be specified in the model + definition to describe how the model will access the data. + </p> + + <ins class="controller"> + {"id": "controller", + "handlers": { + "change": "changeContent" + }} + </ins> + + + <h2>RAM storage</h2> + + <p>In a <strong>RAM storage</strong> the data is entirely stored in the + client's memory. The server is never accessed. + There is no data persistence.</p> + + <div id="area1"> + + <ins class="model"> + {"id": "local-data-provider", + "data": { + "message": "", + "content": "" + }, + "storage": { + "type": "ram" + } + } + } + </ins> + + <ins class="view"> + {"widget": { + "type": "inputbox" + }, + "model": "local-data-provider", + "controller": "controller" + } + </ins> + + </div> + + <h2>Remote storage</h2> + <p>In this example the data is stored <strong>on the server</strong> inside + the session. + This provides some persistence which means that the page can be reloaded + or the user can leave and return to the page and the current data will be + restored. + </p> + + <p>An extra delay has been added when retrieving and when storing data to + simulate network or database latency. Calls are made + <strong>asynchronously</strong> which means that the browser can load the + page entirely before the data is retrieved.</p> + + <p>The user input is <strong>validated</strong> before the data is getting + stored. + </p> + + <p>Finally the view gets refreshed when the model is updated.</p> + + <h3>Which of these are colors?</h3> + <p class="choices"> + <cite>red</cite> + <cite>light</cite> + <cite>yellow</cite> + <cite>blue</cite> + <cite>dark</cite> + </p> + + <div id="area2"> + + <ins class="model"> + {"id": "remote-data-provider", + "data": { + "message": "", + "content": "Loading data ..." + }, + "storage": { + "type": "remote", + "accessors": { + "get": "@@getAnswer", + "set": "@@setAnswer" + } + } + } + </ins> + + <ins class="view"> + {"widget": { + "type": "inputbox" + }, + "model": "remote-data-provider", + "controller": "controller" + } + </ins> + + </div> + + <h2>Compound storage</h2> + <p>A compound storage is created by combining the storages of different + models.</p> + + <div id="area3"> + <ins class="model"> + {"id": "message", + "data": { + "message": "Please wait ..." + }, + "storage": { + "type": "ram" + } + }} + </ins> + + <ins class="model"> + {"id": "content", + "data": { + "message": "Sending the answer ...", + "content": "Loading data ..." + }, + "storage": { + "type": "remote", + "accessors": { + "get": "@@getAnswer", + "set": "@@setAnswer" + } + }} + </ins> + + <ins class="model"> + {"id": "compound-data-provider", + "data": { + "message": "", + "content": "" + }, + "storage": { + "type": "compound", + "partitions": ["message", "content"] + }} + </ins> + + <ins class="view"> + {"widget": { + "type": "inputbox" + }, + "model": "compound-data-provider", + "controller": "controller" + } + </ins> + +</body> +</html> Modified: cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/unit/cpsskins_storage_adapters.pt ============================================================================== --- cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/unit/cpsskins_storage_adapters.pt (original) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/unit/cpsskins_storage_adapters.pt Sun Feb 5 11:43:33 2006 @@ -34,7 +34,7 @@ {"id": "m1", "data": {"a": 1}, "storage": { - "type": "volatile" + "type": "ram" }} </ins> @@ -42,16 +42,29 @@ {"id": "m2", "data": {"b": 2}, "storage": { - "type": "volatile" + "type": "ram" }} </ins> <ins class="model"> + {"id": "m3", + "data": {"c": 3}, + "storage": { + "type": "remote", + "accessors": { + "get": "@@getData2", + "set": "@@setData2" + } + } + } + </ins> + + <ins class="model"> {"id": "c1", - "data": {"a": 0, "b": 0}, + "data": {"a": 0, "b": 0, "c": 0}, "storage": { "type": "compound", - "partitions": ["m1", "m2"] + "partitions": ["m1", "m2", "m3"] }} </ins> @@ -61,28 +74,62 @@ <script type="text/javascript"> // <![CDATA[ + var model_c1; + var model_m1; + var model_m2; + var model_m3; + + // Tests are run asynchronously, results are recorded: + var results = []; + var dummy = new Object(); + + var expected = [ + {'a': 1}, + {'a': 1, 'b': 2}, + {'a': 1, 'b': 2, 'c': 1}, + ]; + new Test.Unit.Runner({ setup: function() { + model_c1 = CPSSkins.getModelById('c1'); + model_m1 = CPSSkins.getModelById('m1'); + model_m2 = CPSSkins.getModelById('m2'); + model_m3 = CPSSkins.getModelById('m3'); + + CPSSkins.registerEventHandler('modified', dummy, function(event) { + // record modifications + results.push($H(event.target.def.data)); + debug($H(event.target.def.data)); + }); + + CPSSkins.subscribe('modified', + {'subscriber': dummy, 'target': model_m3} + ); + + model_c1.getData(); + model_c1.getData(); + model_c1.setData({"a": 10, "b": 1, "c": 10}); + + model_c1.getData(); }, testGetData: function() { with(this) { - var model_c1 = CPSSkins.getModelById('c1'); - - assertEqual($H(model_c1.getData()).inspect(), - $H({"a": 1,"b": 2}).inspect()); + $A(expected).each(function(r, index) { + assertEqual($H(r).inspect(), $H(results[index]).inspect()); + }); }}, + /* testSetData: function() { with(this) { - var model_m1 = CPSSkins.getModelById('m1'); - var model_m2 = CPSSkins.getModelById('m2'); - var model_c1 = CPSSkins.getModelById('c1'); assertEqual($H(model_c1.getData()).inspect(), - $H({"a": 1, "b": 2}).inspect()); + $H({"a": 1, "b": 2, "c": 3}).inspect()); + }}, + testGetData2: function() { with(this) { assertEqual($H(model_c1.schema).inspect(), - $H({'a': 'number', 'b': 'number'}).inspect()); + $H({'a': 'number', 'b': 'number', 'c': number}).inspect()); assertEqual($H(model_m1.schema).inspect(), $H({'a': 'number'}).inspect()); @@ -90,6 +137,9 @@ assertEqual($H(model_m2.schema).inspect(), $H({'b': 'number'}).inspect()); + assertEqual($H(model_m3.schema).inspect(), + $H({'c': 'number'}).inspect()); + model_c1.setData({"a": 3, "b": 1}); assertEqual($H(model_c1.getData()).inspect(), $H({"a": 3, "b": 1}).inspect()); @@ -107,6 +157,7 @@ $H({}).inspect()); }} + */ }); -- http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins