[gnome-logs/wip/test: 16/31] Add behave test for search



commit 9a71e9df97e1ff790b2beacc2570d1c4af974c45
Author: Rashi Aswani <aswanirashi19 gmail com>
Date:   Wed Jul 15 04:27:46 2015 +0530

    Add behave test for search

 tests/common_steps.py   |  134 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/environment.py    |   82 +++++++++++++++++++++++++++++
 tests/general.feature   |    9 +++
 tests/gnome-logs-test.c |    5 +-
 tests/steps/general.py  |   26 +++++++++
 5 files changed, 254 insertions(+), 2 deletions(-)
---
diff --git a/tests/common_steps.py b/tests/common_steps.py
new file mode 100644
index 0000000..b449bbd
--- /dev/null
+++ b/tests/common_steps.py
@@ -0,0 +1,134 @@
+# -*- coding: UTF-8 -*-
+from dogtail.utils import isA11yEnabled, enableA11y, run
+if isA11yEnabled() is False:
+    enableA11y(True)
+
+from time import time, sleep
+from functools import wraps
+from os import strerror, errno, system
+from signal import signal, alarm, SIGALRM
+from subprocess import Popen, PIPE
+from behave import step
+from gi.repository import GLib, Gio
+import fcntl, os
+
+from dogtail.rawinput import keyCombo, absoluteMotion, pressKey
+from dogtail.tree import root
+from unittest import TestCase
+
+
+# Create a dummy unittest class to have nice assertions
+class dummy(TestCase):
+    def runTest(self):  # pylint: disable=R0201
+        assert True
+
+class TimeoutError(Exception):
+    """
+    Timeout exception class for limit_execution_time_to function
+    """
+    pass
+
+class App(object):
+    """
+    This class does all basic events with the app
+    """
+    def __init__(
+        self, appName, shortcut='<Control><Q>', a11yAppName=None,
+            forceKill=True, parameters='', recordVideo=False):
+        """
+        Initialize object App
+        appName     command to run the app
+        shortcut    default quit shortcut
+        a11yAppName app's a11y name is different than binary
+        forceKill   is the app supposed to be kill before/after test?
+        parameters  has the app any params needed to start? (only for startViaCommand)
+        recordVideo start gnome-shell recording while running the app
+        """
+        self.appCommand = appName
+        self.shortcut = shortcut
+        self.forceKill = forceKill
+        self.parameters = parameters
+        self.internCommand = self.appCommand.lower()
+        self.a11yAppName = a11yAppName
+        self.recordVideo = recordVideo
+        self.pid = None
+
+        # a way of overcoming overview autospawn when mouse in 1,1 from start
+        pressKey('Esc')
+        absoluteMotion(100, 100, 2)
+
+        # attempt to make a recording of the test
+        if self.recordVideo:
+            keyCombo('<Control><Alt><Shift>R')
+
+    def isRunning(self):
+        """
+        Is the app running?
+        """
+        if self.a11yAppName is None:
+            self.a11yAppName = self.internCommand
+
+        # Trap weird bus errors
+        for attempt in xrange(0, 30):
+            sleep(1)
+            try:
+                return self.a11yAppName in [x.name for x in root.applications()]
+            except GLib.GError:
+                continue
+        raise Exception("10 at-spi errors, seems that bus is blocked")
+
+    def kill(self):
+        """
+        Kill the app via 'killall'
+        """
+        if self.recordVideo:
+            keyCombo('<Control><Alt><Shift>R')
+
+        try:
+            self.process.kill()
+        except:
+            # Fall back to killall
+            Popen("killall " + self.appCommand, shell=True).wait()
+
+    def startViaCommand(self):
+        """
+        Start the app via command
+        """
+        if self.forceKill and self.isRunning():
+            self.kill()
+            assert not self.isRunning(), "Application cannot be stopped"
+
+        #command = "%s %s" % (self.appCommand, self.parameters)
+        #self.pid = run(command, timeout=5)
+        self.process = Popen(self.appCommand.split() + self.parameters.split(),
+                             stdout=PIPE, stderr=PIPE, bufsize=0)
+        self.pid = self.process.pid
+
+        assert self.isRunning(), "Application failed to start"
+        return root.application(self.a11yAppName)
+
+    def closeViaShortcut(self):
+        """
+        Close the app via shortcut
+        """
+        if not self.isRunning():
+            raise Exception("App is not running")
+
+        keyCombo(self.shortcut)
+        assert not self.isRunning(), "Application cannot be stopped"
+
+
+ step(u'Make sure that gnome-logs-test is running')
+def ensure_app_running(context):
+    context.app = context.app_class.startViaCommand()
+
+def cleanup():
+    # Remove cached data and settings
+    folders = ['~/.local/share/gnome-logs-test', '~/.cache/gnome-logs/test', '~/.config/gnome-logs-test']
+    for folder in folders:
+        system("rm -rf %s > /dev/null" % folder)
+
+    # Reset GSettings
+    schemas = [x for x in Gio.Settings.list_schemas() if 'gnome-logs-test' in x.lower()]
+    for schema in schemas:
+        system("gsettings reset-recursively %s" % schema)
diff --git a/tests/environment.py b/tests/environment.py
new file mode 100644
index 0000000..f0f7bdb
--- /dev/null
+++ b/tests/environment.py
@@ -0,0 +1,82 @@
+# -*- coding: UTF-8 -*-
+
+from time import sleep, localtime, strftime
+from dogtail.utils import isA11yEnabled, enableA11y
+if not isA11yEnabled():
+    enableA11y(True)
+
+from common_steps import App, dummy, cleanup
+from dogtail.config import config
+import os
+
+def before_all(context):
+    """Setup logs stuff
+    Being executed once before any test
+    """
+
+    try:
+        # Close running logs instances
+        os.system("./gnome-logs-test --force-shutdown > /dev/null")
+
+        # Skip dogtail actions to print to stdout
+        config.logDebugToStdOut = False
+        config.typingDelay = 0.2
+
+        # Include assertion object
+        context.assertion = dummy()
+
+        # Cleanup existing data before any test
+        cleanup()
+
+        # Store scenario start time for session logs
+        context.log_start_time = strftime("%Y-%m-%d %H:%M:%S", localtime())
+
+        context.app_class = App('./gnome-logs-test')
+
+    except Exception as e:
+        print("Error in before_all: %s" % e.message)
+
+
+def after_step(context, step):
+    try:
+        if step.status == 'failed' and hasattr(context, "embed"):
+            # Embed screenshot if HTML report is used
+            os.system("dbus-send --print-reply --session --type=method_call " +
+                      "--dest='org.gnome.Shell.Screenshot' " +
+                      "'/org/gnome/Shell/Screenshot' " +
+                      "org.gnome.Shell.Screenshot.Screenshot " +
+                      "boolean:true boolean:false string:/tmp/screenshot.png")
+            context.embed('image/png', open("/tmp/screenshot.png", 'r').read())
+    except Exception as e:
+        print("Error in after_step: %s" % str(e))
+
+
+def after_scenario(context, scenario):
+    """Teardown for each scenario
+    Kill logs (in order to make this reliable we send sigkill)
+    """
+
+    try:
+        # Attach journalctl logs
+        if hasattr(context, "embed"):
+            os.system("journalctl /usr/bin/gnome-session --no-pager -o cat --since='%s'> 
/tmp/journal-session.log" % context.log_start_time)
+            data = open("/tmp/journal-session.log", 'r').read()
+            if data:
+                context.embed('text/plain', data)
+
+            context.app_class.kill()
+
+            stdout = non_block_read(context.app_class.process.stdout)
+            stderr = non_block_read(context.app_class.process.stderr)
+
+            if stdout:
+                context.embed('text/plain', stdout)
+
+            if stderr:
+                context.embed('text/plain', stderr)
+
+        # Make some pause after scenario
+        sleep(1)
+    except Exception as e:
+        # Stupid behave simply crashes in case exception has occurred
+        print("Error in after_scenario: %s" % e.message)
diff --git a/tests/general.feature b/tests/general.feature
new file mode 100644
index 0000000..0794ed4
--- /dev/null
+++ b/tests/general.feature
@@ -0,0 +1,9 @@
+Feature: General
+
+Background:
+  * Make sure that gnome-logs-test is running
+
+ start_logs
+  Scenario: Search
+    * Click on Search
+    Then all selection toolbar buttons are sensitive
diff --git a/tests/gnome-logs-test.c b/tests/gnome-logs-test.c
index ca60090..260277a 100644
--- a/tests/gnome-logs-test.c
+++ b/tests/gnome-logs-test.c
@@ -60,6 +60,7 @@ main (int argc, char** argv)
     g_test_init (&argc, &argv, NULL);
     g_test_add_func ("/util/check_log_message", check_log_message);
     g_object_unref (application);
- 
-   return g_test_run ();
+    //g_application_quit (G_APPLICATION (application));
+     
+    return g_test_run ();
 }
diff --git a/tests/steps/general.py b/tests/steps/general.py
new file mode 100644
index 0000000..55720bc
--- /dev/null
+++ b/tests/steps/general.py
@@ -0,0 +1,26 @@
+from behave import step
+
+from os import system
+from pyatspi import STATE_SENSITIVE
+from time import sleep
+from common_steps import App
+
+ step(u'Run gnome-logs-test')
+def run_gnome_logs_test(context):
+    system("./gnome-logs-test --force-shutdown 2&> /dev/null")
+    context.execute_steps(u'* Start a new Logs instance')
+
+ step(u'Click on search')
+def click_on_search(context):
+    context.app.child('Search').parent.click()
+    
+ then(u'all selection toolbar buttons are sensitive')
+def all_selection_toolbar_buttons_sensitive(context):
+    sleep(0.5)
+    assert context.app.child(translate('Important')).sensitive
+    assert context.app.child(translate('All')).sensitive
+    assert context.app.child(translate('Applications')).sensitive
+    assert context.app.child(translate('System')).sensitive
+    assert context.app.child(translate('Security')).sensitive
+    assert context.app.child(translate('Hardware')).sensitive
+    sleep(0.5) 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]