Author: titmuss
Date: Tue Apr 22 04:32:27 2008
New Revision: 2307

URL: http://svn.slimdevices.com?rev=2307&root=Jive&view=rev
Log:
Bug: N/A
Description:
Exhanced MacroPlay for automated testing. This now supports multiple test 
scripts, and will do screen grabbing and comparison.


Modified:
    
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayApplet.lua
    
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayMeta.lua
    7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h
    7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c
    7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_surface.c
    7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c
    7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.pkg

Modified: 
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayApplet.lua
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayApplet.lua?rev=2307&root=Jive&r1=2306&r2=2307&view=diff
==============================================================================
--- 
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayApplet.lua
 (original)
+++ 
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayApplet.lua
 Tue Apr 22 04:32:27 2008
@@ -13,75 +13,125 @@
 
 
 -- stuff we use
-local ipairs, pairs, tostring = ipairs, pairs, tostring
+local getfenv, loadfile, ipairs, package, pairs, require, setfenv, 
setmetatable, tostring = getfenv, loadfile, ipairs, package, pairs, require, 
setfenv, setmetatable, tostring
 
 local oo               = require("loop.simple")
+local lfs              = require("lfs")
 local math             = require("math")
+local string           = require("string")
 local table            = require("jive.utils.table")
+
 
 local Applet           = require("jive.Applet")
 local Event            = require("jive.ui.Event")
 local Framework        = require("jive.ui.Framework")
 local Menu             = require("jive.ui.Menu")
+local SimpleMenu       = require("jive.ui.SimpleMenu")
+local Surface          = require("jive.ui.Surface")
 local Task             = require("jive.ui.Task")
+local Textarea         = require("jive.ui.Textarea")
 local Timer            = require("jive.ui.Timer")
-
+local Window           = require("jive.ui.Window")
+
+local debug            = require("jive.utils.debug")
 local log              = require("jive.utils.log").logger("applets.misc")
 
-
-local EVENT_KEY_PRESS           = jive.ui.EVENT_KEY_PRESS
-local EVENT_KEY_DOWN            = jive.ui.EVENT_KEY_DOWN
-local EVENT_KEY_UP              = jive.ui.EVENT_KEY_UP
-local EVENT_KEY_HOLD            = jive.ui.EVENT_KEY_HOLD
-local EVENT_SCROLL              = jive.ui.EVENT_SCROLL
-local EVENT_CONSUME             = jive.ui.EVENT_CONSUME
-local EVENT_ACTION              = jive.ui.EVENT_ACTION
-local EVENT_WINDOW_POP          = jive.ui.EVENT_WINDOW_POP
-
-local KEY_BACK                  = jive.ui.KEY_BACK
-local KEY_DOWN                  = jive.ui.KEY_DOWN
-local KEY_HOME                  = jive.ui.KEY_HOME
-local KEY_GO                    = jive.ui.KEY_GO
-local KEY_PLAY                  = jive.ui.KEY_PLAY
+local jive = jive
 
 
 module(...)
 oo.class(_M, Applet)
 
 
--- XXX Load script from file
-local script = function()
-       log:info("Starting macro script")
-
-       -- key HOME
-       macroEvent(100, EVENT_KEY_PRESS, KEY_HOME)
-
-       while true do
-               -- key down until Choose Player
-               while not macroIsMenuItem("Choose Player") do
-                       macroEvent(100, EVENT_KEY_PRESS, KEY_DOWN)
-               end
-
-               -- key go into Choose Player
-               macroEvent(10000, EVENT_KEY_PRESS, KEY_GO)
-
-               -- key back from Choose Player
-               macroEvent(100, EVENT_KEY_PRESS, KEY_BACK)
-
-               -- key down
-               macroEvent(100, EVENT_KEY_PRESS, KEY_DOWN)
-       end
-end
-
-
--- macro state
+-- Declare as module variables, not locals. This allows visiblity to
+-- loaded macro functions using setfenv.
+EVENT_KEY_PRESS           = jive.ui.EVENT_KEY_PRESS
+EVENT_KEY_DOWN            = jive.ui.EVENT_KEY_DOWN
+EVENT_KEY_UP              = jive.ui.EVENT_KEY_UP
+EVENT_KEY_HOLD            = jive.ui.EVENT_KEY_HOLD
+EVENT_SCROLL              = jive.ui.EVENT_SCROLL
+EVENT_CONSUME             = jive.ui.EVENT_CONSUME
+EVENT_ACTION              = jive.ui.EVENT_ACTION
+EVENT_WINDOW_POP          = jive.ui.EVENT_WINDOW_POP
+
+KEY_BACK                  = jive.ui.KEY_BACK
+KEY_DOWN                  = jive.ui.KEY_DOWN
+KEY_HOME                  = jive.ui.KEY_HOME
+KEY_GO                    = jive.ui.KEY_GO
+KEY_PLAY                  = jive.ui.KEY_PLAY
+
+
+-- macro (global) state
 local task = false
 local timer = false
+local macrodir = false
+
+
+local function loadmacro(file)
+       for dir in package.path:gmatch("([^;]*)%?[^;]*;") do
+               -- FIXME file test first
+
+               local f, err = loadfile(dir .. file)
+               if err == nil then
+                       -- Set chunk environment to be contained in the
+                       -- MacroPlay applet.
+                       setfenv(f, getfenv(1))
+                       return f, string.match(dir .. file, "(.*[/\]).+")
+               end
+       end
+
+       return nil, err
+end
+
+
+function settingsShow(self)
+       -- Create window
+       local window = Window("window", self:string("MACRO_PLAY"))
+       local menu = SimpleMenu("menu", items)
+       local help = Textarea("help", "")
+
+       window:addWidget(help)
+       window:addWidget(menu)
+
+       -- Load macro configuration
+       local f = loadmacro("applets/MacroPlay/Macros.lua")
+       if f then
+               -- Defines self.macros
+               f()
+
+               for i, v in ipairs(self.macros) do
+                       local item = {
+                               text = self:string(v.name),
+                               sound = "WINDOWSHOW",
+                               callback = function(event, menuItem)
+                                       self:play(v.file)
+                               end,
+                               focusGained = function()
+                                       help:setValue(v.desc)
+                               end,
+                       }
+                       menu:addItem(item)
+               end
+       end
+
+       -- FIXME can't tie applet due to global macro state
+       window:show()
+end
 
 
 -- play the macro
-function play(self)
-       task = Task("MacroPlay", self, script)
+function play(self, file)
+       task = Task("MacroPlay", self,
+               function()
+                       local f, dir = loadmacro(file)
+                       if f then
+                               log:info("Macro starting: ", file)
+                               macrodir = dir
+                               f()
+                       else
+                               log:warn("Macro error: ", err)
+                       end
+               end)
        task:addTask()
 
        timer = Timer(0, function()
@@ -139,6 +189,48 @@
 end
 
 
+-- capture or verify a screenshot
+function macroScreenshot(interval, file, limit)
+       local pass = false
+
+       limit = limit or 100
+
+       -- create screenshot
+       local w, h = Framework:getScreenSize()
+
+       local window = Framework.windowStack[1]
+
+       local screen = Surface:newRGB(w, h)
+       window:draw(screen, JIVE_LAYER_ALL)
+
+
+       local reffile = macrodir .. file .. ".bmp"
+       if lfs.attributes(reffile, "mode") == "file" then
+               -- verify screenshot
+               log:debug("Loading reference screenshot " .. reffile)
+               local ref = Surface:loadImage(reffile)
+
+               local match = ref:compare(screen, 0xFF00FF)
+
+               if match < limit then
+                       -- failure
+                       log:warn("Screenshot FAILED " .. file .. " match=" .. 
match .. " limt=" .. limit)
+                       failfile = macrodir .. file .. "_fail.bmp"
+                       screen:saveBMP(failfile)
+               else
+                       log:info("Screenshot PASSED " .. file)
+                       pass = true
+               end
+       else
+               log:debug("Saving reference screenshot " .. reffile)
+               screen:saveBMP(reffile)
+       end
+
+       macroDelay(interval)
+       return pass
+end
+
+
 --[[
 
 =head1 LICENSE

Modified: 
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayMeta.lua
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayMeta.lua?rev=2307&root=Jive&r1=2306&r2=2307&view=diff
==============================================================================
--- 
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayMeta.lua 
(original)
+++ 
7.1/trunk/squeezeplay/src/squeezeplay/share/applets/MacroPlay/MacroPlayMeta.lua 
Tue Apr 22 04:32:27 2008
@@ -39,6 +39,6 @@
 
 function registerApplet(meta)
        -- menu item to start
-       jiveMain:addItem(meta:menuItem('macroPlay', 'advancedSettings', 
'MACRO_PLAY', function(applet, ...) applet:play(...) end))
+       jiveMain:addItem(meta:menuItem('macroPlay', 'advancedSettings', 
'MACRO_PLAY', function(applet, ...) applet:settingsShow(...) end))
 end
 

Modified: 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h?rev=2307&root=Jive&r1=2306&r2=2307&view=diff
==============================================================================
--- 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h (original)
+++ 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive.h Tue Apr 22 04:32:27 2008
@@ -280,6 +280,7 @@
 JiveSurface *jive_surface_load_image(const char *path);
 JiveSurface *jive_surface_load_image_data(const char *data, size_t len);
 int jive_surface_save_bmp(JiveSurface *srf, const char *file);
+int jive_surface_cmp(JiveSurface *a, JiveSurface *b, Uint32 key);
 void jive_surface_set_offset(JiveSurface *src, Sint16 x, Sint16 y);
 void jive_surface_get_clip(JiveSurface *srf, SDL_Rect *r);
 void jive_surface_set_clip(JiveSurface *srf, SDL_Rect *r);

Modified: 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c?rev=2307&root=Jive&r1=2306&r2=2307&view=diff
==============================================================================
--- 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c (original)
+++ 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_framework.c Tue Apr 22 
04:32:27 2008
@@ -796,11 +796,21 @@
 
 
 int jive_find_file(const char *path, char *fullpath) {
-       char *resource_path = strdup(jive_resource_path);
-
-       char *ptr = strtok(resource_path, ";");
+       char *resource_path, *ptr;
+       FILE *fp;
+
+       /* absolute/relative path */
+       fp = fopen(path, "r");
+       if (fp) {
+               fclose(fp);
+               strcpy(fullpath, path);
+               return 1;
+       }
+
+       /* search lua path */
+       resource_path = strdup(jive_resource_path);
+       ptr = strtok(resource_path, ";");
        while (ptr) {
-               FILE *fp;
 #if defined(WIN32)
                char *tmp;
 #endif

Modified: 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_surface.c
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_surface.c?rev=2307&root=Jive&r1=2306&r2=2307&view=diff
==============================================================================
--- 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_surface.c (original)
+++ 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/jive_surface.c Tue Apr 22 
04:32:27 2008
@@ -158,6 +158,89 @@
        return SDL_SaveBMP(srf->sdl, file);
 }
 
+
+static int _getPixel(SDL_Surface *s, Uint16 x, Uint16 y) {
+       Uint8 R, G, B;
+
+       switch (s->format->BytesPerPixel) {
+       case 1: { /* 8-bpp */
+               Uint8 *p;
+               p = (Uint8 *)s->pixels + y*s->pitch + x;
+
+               SDL_GetRGB(*p, s->format, &R, &G, &B);
+               return (R << 16) | (G << 8) | B;
+       }
+
+       case 2: { /* 15-bpp or 16-bpp */
+               Uint16 *p;
+               p = (Uint16 *)s->pixels + y*s->pitch/2 + x;
+
+               SDL_GetRGB(*p, s->format, &R, &G, &B);
+               return (R << 16) | (G << 8) | B;
+       }
+
+       case 3: { /* 24-bpp */
+               /* FIXME */
+               assert(0);
+       }
+
+       case 4: { /* 32-bpp */
+               Uint32 *p;
+               p = (Uint32 *)s->pixels + y*s->pitch/4 + x;
+
+               SDL_GetRGB(*p, s->format, &R, &G, &B);
+               return (R << 16) | (G << 8) | B;
+       }
+       }
+
+       return 0;
+}
+
+
+int jive_surface_cmp(JiveSurface *a, JiveSurface *b, Uint32 key) {
+       SDL_Surface *sa = a->sdl;
+       SDL_Surface *sb = b->sdl;
+       Uint32 pa, pb;
+       int x, y;
+       int count=0, equal=0;
+
+       if (!sa || !sb) {
+               return 0;
+       }
+
+       if (sa->w != sb->w || sa->h != sb->h) {
+               return 0;
+       }
+
+       if (SDL_MUSTLOCK(sa)) {
+               SDL_LockSurface(sa);
+       }
+       if (SDL_MUSTLOCK(sb)) {
+               SDL_LockSurface(sb);
+       }
+       
+       for (x=0; x<sa->w; x++) {
+               for (y=0; y<sa->h; y++) {
+                       pa = _getPixel(sa, x, y);
+                       pb = _getPixel(sb, x ,y);
+                       
+                       count++;
+                       if (pa == pb || pa == key || pb == key) {
+                               equal++;
+                       }
+               }
+       }
+
+       if (SDL_MUSTLOCK(sb)) {
+               SDL_UnlockSurface(sb);
+       }
+       if (SDL_MUSTLOCK(sa)) {
+               SDL_UnlockSurface(sa);
+       }
+
+       return (int)(((float)equal / count) * 100);
+}
+
 void jive_surface_set_offset(JiveSurface *srf, Sint16 x, Sint16 y) {
        srf->offset_x = x;
        srf->offset_y = y;

Modified: 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c?rev=2307&root=Jive&r1=2306&r2=2307&view=diff
==============================================================================
--- 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c (original)
+++ 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.c Tue Apr 22 
04:32:27 2008
@@ -1,6 +1,6 @@
 /*
 ** Lua binding: jive
-** Generated automatically by tolua++-1.0.92 on Wed Jan 30 21:18:12 2008.
+** Generated automatically by tolua++-1.0.92 on Fri Apr 11 00:01:23 2008.
 */
 
 #ifndef __cplusplus
@@ -397,6 +397,42 @@
 #ifndef TOLUA_RELEASE
  tolua_lerror:
  tolua_error(tolua_S,"#ferror in function 'saveBMP'.",&tolua_err);
+ return 0;
+#endif
+}
+#endif //#ifndef TOLUA_DISABLE
+
+/* method: jive_surface_cmp of class  Surface */
+#ifndef TOLUA_DISABLE_tolua_jive_jive_ui_Surface_compare00
+static int tolua_jive_jive_ui_Surface_compare00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertype(tolua_S,1,"Surface",0,&tolua_err) ||
+ !tolua_isusertype(tolua_S,2,"Surface",0,&tolua_err) ||
+ !tolua_isinteger(tolua_S,3,0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,4,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+  Surface* self = (Surface*)  tolua_tousertype(tolua_S,1,0);
+  Surface* b = ((Surface*)  tolua_tousertype(tolua_S,2,0));
+  unsigned int key = (( unsigned int)  tolua_tointeger(tolua_S,3,0));
+#ifndef TOLUA_RELEASE
+ if (!self) tolua_error(tolua_S,"invalid 'self' in function 
'jive_surface_cmp'",NULL);
+#endif
+ {
+  tolua_outside int tolua_ret = (tolua_outside int)  
jive_surface_cmp(self,b,key);
+ tolua_pushinteger(tolua_S,(lua_Integer)tolua_ret);
+ }
+ }
+ return 1;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'compare'.",&tolua_err);
  return 0;
 #endif
 }
@@ -2194,6 +2230,7 @@
     tolua_function(tolua_S,"drawText",tolua_jive_jive_ui_Surface_drawText00);
     tolua_function(tolua_S,"free",tolua_jive_jive_ui_Surface_free00);
     tolua_function(tolua_S,"saveBMP",tolua_jive_jive_ui_Surface_saveBMP00);
+    tolua_function(tolua_S,"compare",tolua_jive_jive_ui_Surface_compare00);
     tolua_function(tolua_S,"setOffset",tolua_jive_jive_ui_Surface_setOffset00);
     tolua_function(tolua_S,"setClip",tolua_jive_jive_ui_Surface_setClip00);
     tolua_function(tolua_S,"getClip",tolua_jive_jive_ui_Surface_getClip00);

Modified: 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.pkg
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.pkg?rev=2307&root=Jive&r1=2306&r2=2307&view=diff
==============================================================================
--- 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.pkg (original)
+++ 7.1/trunk/squeezeplay/src/squeezeplay/src/ui/lua_jiveui.pkg Tue Apr 22 
04:32:27 2008
@@ -155,6 +155,8 @@
        tolua_destroy void jive_surface_free @ free();
 
        tolua_outside int jive_surface_save_bmp @ saveBMP(const char *file);
+       tolua_outside int jive_surface_cmp @ compare(Surface *b, Uint32 key);
+
        tolua_outside void jive_surface_set_offset @ setOffset(Sint16 x, Sint16 
y);
 
        tolua_outside void jive_surface_set_clip_arg @ setClip(Uint16 x, Uint16 
y, Uint16 w, Uint16 h);

_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins

Reply via email to