[Tools 1/2] testbot/lib: Add an EnumPropertyDescriptor class for enum properties.

Francois Gouget fgouget at codeweavers.com
Fri Aug 31 10:18:07 CDT 2012


This lets us display the right options in the web interface, thus avoiding the risk of incorrect entry.
This also lets us validate the values before trying to store them in the database. In particular this avoids silently dropping an incorrect 'Type' value when modifying a VM.
---
 testbot/lib/ObjectModel/CGI/FormPage.pm           |   18 +++++
 testbot/lib/ObjectModel/CGI/ItemPage.pm           |    4 +-
 testbot/lib/ObjectModel/EnumPropertyDescriptor.pm |   86 +++++++++++++++++++++
 testbot/lib/ObjectModel/Item.pm                   |    4 +-
 testbot/lib/WineTestBot/Jobs.pm                   |    4 +-
 testbot/lib/WineTestBot/Steps.pm                  |    8 +-
 testbot/lib/WineTestBot/Tasks.pm                  |    4 +-
 testbot/lib/WineTestBot/VMs.pm                    |    8 +-
 testbot/web/admin/VMDetails.pl                    |   26 +------
 9 files changed, 127 insertions(+), 35 deletions(-)
 create mode 100644 testbot/lib/ObjectModel/EnumPropertyDescriptor.pm

diff --git a/testbot/lib/ObjectModel/CGI/FormPage.pm b/testbot/lib/ObjectModel/CGI/FormPage.pm
index a3d2a8d..6a1fbae 100644
--- a/testbot/lib/ObjectModel/CGI/FormPage.pm
+++ b/testbot/lib/ObjectModel/CGI/FormPage.pm
@@ -1,6 +1,7 @@
 # Base class for web pages containing a form
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -222,6 +223,12 @@ sub GetInputType
   my $self = shift;
   my $PropertyDescriptor = shift;
 
+  my $Class=$PropertyDescriptor->GetClass();
+  if ($PropertyDescriptor->GetClass() eq "Enum")
+  {
+    return "select";
+  }
+
   if ($PropertyDescriptor->GetType() eq "B")
   {
     return "checkbox";
@@ -250,6 +257,17 @@ sub GenerateField
       }
       print "/>";
     }
+    elsif ($InputType eq "select")
+    {
+      print "<select name='", $PropertyDescriptor->GetName(), "'>\n";
+      foreach my $V (@{$PropertyDescriptor->GetValues()})
+      {
+        print "  <option value='", $self->CGI->escapeHTML($V), "'";
+        print " selected='selected'" if ($Value eq $V);
+        print ">", $self->CGI->escapeHTML($V), "</option>\n";
+      }
+      print "</select>";
+    }
     elsif ($InputType eq "textarea")
     {
       my $MaxLength = $PropertyDescriptor->GetMaxLength();
diff --git a/testbot/lib/ObjectModel/CGI/ItemPage.pm b/testbot/lib/ObjectModel/CGI/ItemPage.pm
index a65abe7..a3310ec 100644
--- a/testbot/lib/ObjectModel/CGI/ItemPage.pm
+++ b/testbot/lib/ObjectModel/CGI/ItemPage.pm
@@ -1,6 +1,7 @@
 # Base class for web pages containing a db bound form
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -137,7 +138,8 @@ sub SaveProperty
   my $self = shift;
   my ($PropertyDescriptor, $Value) = @_;
 
-  if ($PropertyDescriptor->GetType() eq "B" && $Value)
+  if ($PropertyDescriptor->GetClass() eq "Basic" &&
+      $PropertyDescriptor->GetType() eq "B" && $Value)
   {
     $Value = 1;
   }
diff --git a/testbot/lib/ObjectModel/EnumPropertyDescriptor.pm b/testbot/lib/ObjectModel/EnumPropertyDescriptor.pm
new file mode 100644
index 0000000..b627189
--- /dev/null
+++ b/testbot/lib/ObjectModel/EnumPropertyDescriptor.pm
@@ -0,0 +1,86 @@
+# Copyright 2012 Francois Gouget
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+
+use strict;
+
+=head1 NAME
+
+ObjectModel::EnumPropertyDescriptor - Defines a property that can only take
+a set of values.
+
+=cut
+
+package ObjectModel::EnumPropertyDescriptor;
+
+use ObjectModel::PropertyDescriptor;
+
+use vars qw(@ISA @EXPORT);
+
+require Exporter;
+ at ISA = qw(ObjectModel::PropertyDescriptor Exporter);
+ at EXPORT = qw(&CreateEnumPropertyDescriptor);
+
+sub _initialize
+{
+  my $self = shift;
+  my $Values = shift;
+
+  $self->{Class} = "Enum";
+  $self->{Values} = $Values;
+
+  $self->SUPER::_initialize(@_);
+}
+
+sub GetValues
+{
+  my $self = shift;
+
+  return $self->{Values};
+}
+
+sub GetColNames
+{
+  my $self = shift;
+
+  return [$self->{Name}];
+}
+
+sub ValidateValue
+{
+  my $self = shift;
+  my ($Value, $IsNew) = @_;
+
+  if ($self->GetIsRequired())
+  {
+    if (!$IsNew && (!defined($Value) || $Value eq ""))
+    {
+      return $self->GetDisplayName() .  ": Must be entered";
+    }
+  }
+
+  foreach my $V (@{$self->{Values}})
+  {
+      return undef if ($V eq $Value);
+  }
+  return $self->GetDisplayName() . ": Is not valid";
+}
+
+sub CreateEnumPropertyDescriptor
+{
+  return ObjectModel::EnumPropertyDescriptor->new(@_);
+}
+
+1;
diff --git a/testbot/lib/ObjectModel/Item.pm b/testbot/lib/ObjectModel/Item.pm
index 3dedba8..19b0c94 100644
--- a/testbot/lib/ObjectModel/Item.pm
+++ b/testbot/lib/ObjectModel/Item.pm
@@ -1,6 +1,7 @@
 # Item
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -195,7 +196,8 @@ sub AUTOLOAD
   {
     if ($PropertyName eq $PropertyDescriptor->GetName())
     {
-      if ($PropertyDescriptor->GetClass() eq "Basic")
+      if ($PropertyDescriptor->GetClass() eq "Basic" or
+          $PropertyDescriptor->GetClass() eq "Enum")
       {
         if (@_)
         {
diff --git a/testbot/lib/WineTestBot/Jobs.pm b/testbot/lib/WineTestBot/Jobs.pm
index ccb3817..28d60ea 100644
--- a/testbot/lib/WineTestBot/Jobs.pm
+++ b/testbot/lib/WineTestBot/Jobs.pm
@@ -1,6 +1,7 @@
 # Job collection and items
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -271,6 +272,7 @@ package WineTestBot::Jobs;
 
 use POSIX qw(:errno_h);
 use ObjectModel::BasicPropertyDescriptor;
+use ObjectModel::EnumPropertyDescriptor;
 use ObjectModel::DetailrefPropertyDescriptor;
 use ObjectModel::ItemrefPropertyDescriptor;
 use ObjectModel::PropertyDescriptor;
@@ -299,7 +301,7 @@ BEGIN
     CreateItemrefPropertyDescriptor("Branch", "Branch", !1, 1, \&CreateBranches, ["BranchName"]),
     CreateItemrefPropertyDescriptor("User", "Author", !1, 1, \&WineTestBot::Users::CreateUsers, ["UserName"]),
     CreateBasicPropertyDescriptor("Priority", "Priority", !1, 1, "N", 1),
-    CreateBasicPropertyDescriptor("Status", "Status", !1, 1, "A", 9),
+    CreateEnumPropertyDescriptor("Status", "Status", !1, 1, ['queued', 'running', 'completed', 'failed']),
     CreateBasicPropertyDescriptor("Remarks", "Remarks", !1, !1, "A", 50),
     CreateBasicPropertyDescriptor("Submitted", "Submitted", !1, !1, "DT", 19),
     CreateBasicPropertyDescriptor("Ended", "Ended", !1, !1, "DT", 19),
diff --git a/testbot/lib/WineTestBot/Steps.pm b/testbot/lib/WineTestBot/Steps.pm
index 1ca1abc..c1fd972 100644
--- a/testbot/lib/WineTestBot/Steps.pm
+++ b/testbot/lib/WineTestBot/Steps.pm
@@ -1,6 +1,7 @@
 # Job step collection and items
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -88,6 +89,7 @@ sub HandleStaging
 package WineTestBot::Steps;
 
 use ObjectModel::BasicPropertyDescriptor;
+use ObjectModel::EnumPropertyDescriptor;
 use ObjectModel::DetailrefPropertyDescriptor;
 use ObjectModel::PropertyDescriptor;
 use WineTestBot::Tasks;
@@ -103,10 +105,10 @@ BEGIN
 {
   @PropertyDescriptors = (
     CreateBasicPropertyDescriptor("No", "Step no",  1,  1, "N", 2),
-    CreateBasicPropertyDescriptor("Status", "Status",  !1,  1, "A", 9),
-    CreateBasicPropertyDescriptor("Type", "Step type",  !1,  1, "A", 8),
+    CreateEnumPropertyDescriptor("Status", "Status",  !1,  1, ['queued', 'running', 'completed', 'failed', 'skipped']),
+    CreateEnumPropertyDescriptor("Type", "Step type",  !1,  1, ['suite', 'single', 'build', 'reconfig']),
     CreateBasicPropertyDescriptor("FileName", "File name",  !1,  1, "A", 100),
-    CreateBasicPropertyDescriptor("FileType", "File type",  !1,  1, "A", 13),
+    CreateEnumPropertyDescriptor("FileType", "File type",  !1,  1, ['exe32', 'exe64', 'patchdlls', 'patchprograms', 'dll32', 'dll64', 'zip']),
     CreateBasicPropertyDescriptor("InStaging", "File is in staging area", !1, 1, "B", 1),
     CreateBasicPropertyDescriptor("DebugLevel", "Debug level (WINETEST_DEBUG)", !1, 1, "N", 2),
     CreateBasicPropertyDescriptor("ReportSuccessfulTests", "Report successful tests (WINETEST_REPORT_SUCCESS)", !1, 1, "B", 1),
diff --git a/testbot/lib/WineTestBot/Tasks.pm b/testbot/lib/WineTestBot/Tasks.pm
index b9782ad..cd42ae5 100644
--- a/testbot/lib/WineTestBot/Tasks.pm
+++ b/testbot/lib/WineTestBot/Tasks.pm
@@ -1,6 +1,7 @@
 # Individual task of a job collection and items
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -100,6 +101,7 @@ sub Run
 package WineTestBot::Tasks;
 
 use ObjectModel::BasicPropertyDescriptor;
+use ObjectModel::EnumPropertyDescriptor;
 use ObjectModel::ItemrefPropertyDescriptor;
 use ObjectModel::PropertyDescriptor;
 use WineTestBot::VMs;
@@ -115,7 +117,7 @@ BEGIN
 {
   @PropertyDescriptors = (
     CreateBasicPropertyDescriptor("No", "Task no",  1,  1, "N", 2),
-    CreateBasicPropertyDescriptor("Status", "Status",  !1,  1, "A", 9),
+    CreateEnumPropertyDescriptor("Status", "Status",  !1,  1, ['queued', 'running', 'completed', 'failed', 'skipped']),
     CreateItemrefPropertyDescriptor("VM", "VM", !1,  1, \&CreateVMs, ["VMName"]),
     CreateBasicPropertyDescriptor("Timeout", "Timeout", !1, 1, "N", 4),
     CreateBasicPropertyDescriptor("CmdLineArg", "Command line args", !1, !1, "A", 256),
diff --git a/testbot/lib/WineTestBot/VMs.pm b/testbot/lib/WineTestBot/VMs.pm
index c7d9e5f..6e1145e 100644
--- a/testbot/lib/WineTestBot/VMs.pm
+++ b/testbot/lib/WineTestBot/VMs.pm
@@ -1,6 +1,7 @@
 # VM collection and items
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -534,6 +535,7 @@ package WineTestBot::VMs;
 use VMware::Vix::Simple;
 use VMware::Vix::API::Constants;
 use ObjectModel::BasicPropertyDescriptor;
+use ObjectModel::EnumPropertyDescriptor;
 use ObjectModel::PropertyDescriptor;
 use WineTestBot::WineTestBotObjects;
 
@@ -556,10 +558,10 @@ BEGIN
 {
   @PropertyDescriptors = (
     CreateBasicPropertyDescriptor("Name", "VM name", 1, 1, "A", 20),
-    CreateBasicPropertyDescriptor("Type", "Type of VM", !1, 1, "A", 7),
+    CreateEnumPropertyDescriptor("Type", "Type of VM", !1, 1, ['base', 'extra', 'build', 'retired']),
     CreateBasicPropertyDescriptor("SortOrder", "Display order", !1, 1, "N", 3),
-    CreateBasicPropertyDescriptor("Bits", "32 or 64 bits", !1, 1, "N", 2),
-    CreateBasicPropertyDescriptor("Status", "Current status", !1, 1, "A", 9),
+    CreateEnumPropertyDescriptor("Bits", "32 or 64 bits", !1, 1, ['32', '64']),
+    CreateEnumPropertyDescriptor("Status", "Current status", !1, 1, ['idle', 'reverting', 'sleeping', 'running', 'dirty', 'offline']),
     CreateBasicPropertyDescriptor("VmxHost", "Host where VM is located", !1, !1, "A", 64),
     CreateBasicPropertyDescriptor("VmxFilePath", "Path to .vmx file", !1, 1, "A", 64),
     CreateBasicPropertyDescriptor("IdleSnapshot", "Name of idle snapshot", !1, 1, "A", 32),
diff --git a/testbot/web/admin/VMDetails.pl b/testbot/web/admin/VMDetails.pl
index 519c313..f24061c 100644
--- a/testbot/web/admin/VMDetails.pl
+++ b/testbot/web/admin/VMDetails.pl
@@ -1,6 +1,7 @@
 # VM details page
 #
 # Copyright 2009 Ge van Geldorp
+# Copyright 2012 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -32,31 +33,6 @@ sub _initialize
   $self->SUPER::_initialize(@_, CreateVMs());
 }
 
-sub GenerateField
-{
-  my $self = shift;
-  my ($PropertyDescriptor, $Display) = @_;
-
-  if ($PropertyDescriptor->GetName() ne "Bits" || $Display ne "rw")
-  {
-    $self->SUPER::GenerateField(@_);
-    return;
-  }
-
-  my $Bits = $self->GetPropertyValue($PropertyDescriptor);
-  if (! defined($Bits))
-  {
-    $Bits = 0;
-  }
-  print "<div class='ItemValue'><input type='radio' name='", $PropertyDescriptor->GetName(),
-        "' value='32' ", $Bits == 32 ? "checked='checked' " : "",
-        "/>32 bits<br>\n";
-  print "<input type='radio' name='", $PropertyDescriptor->GetName(),
-        "' value='64' ", $Bits == 64 ? "checked='checked' " : "",
-        "/>64 bits<br>\n";
-  print "</div>\n";
-}
-
 package main;
 
 my $Request = shift;
-- 
1.7.10.4




More information about the wine-patches mailing list