[Libreoffice-commits] .: 2 commits - bug/bug bug/bug.xhtml
Loic Dachary
loic at kemper.freedesktop.org
Sat Sep 17 04:22:00 PDT 2011
bug/bug.xhtml | 3 ++
bug/bug/bug.js | 40 +++++++++++++++++++++++++---------
bug/bug/test.html | 7 +++---
bug/bug/test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++----------
4 files changed, 87 insertions(+), 25 deletions(-)
New commits:
commit 7fff3afdb25fa0575a4172a97b606a4d4437c484
Author: Loic Dachary <loic at dachary.org>
Date: Sat Sep 17 13:21:43 2011 +0200
Implement lookup_result to factorize the logic of looking at a HTML page returned by bugzilla looking for errors and then looking up a meaningfull result if no error is found. The error are handled by throwing an exception and adding an error message in the .error element. On success it returns the value extracted from the page.
The .error element is supposed to be a central place for errors that is going to be used by all states of the bug submission assistant. Although it would be better to display contextual errors, for instance next to a given field, this would lead to more complex development and most probably a user interface that require significant work to design properly.
The state_signin function is converted to use lookup_result. Because lookup_result is a filter, the http://api.jquery.com/deferred.pipe/ function is used to chain the jQuery.post and pass it to the function that will actually display the username somewhere in the page when the login is successfull. Unlike the twisted deferred, the jQuery pipe function does not catch exception and does not turn them into failures. The second argument of pipe is therefore ignored and exceptions are not caught to be passed to the next pipe.
diff --git a/bug/bug/bug.js b/bug/bug/bug.js
index 3f3ab29..2aeec07 100644
--- a/bug/bug/bug.js
+++ b/bug/bug/bug.js
@@ -22,25 +22,43 @@
get: $.get,
+ lookup_result: function(data, error_regexp, success_regexp) {
+ var error = data.match(error_regexp);
+ if(error !== null) {
+ $('.error').text(error[1]);
+ throw error;
+ } else {
+ var success = data.match(success_regexp);
+ if(success !== null) {
+ return success[1];
+ } else {
+ $('.error').text("could not match " + success_regexp + " on the string returned by the server " + data);
+ throw data;
+ }
+ }
+ },
+
// if this string is found in the page returned when
// trying to login, it means the login / password combination
// is invalid.
- state_signin_error_string: 'throw_error',
+ state_signin_error_regexp: 'class="throw_error">([^<]*)',
+ state_signin_success_regexp: 'Log out</a>([^<]*)',
state_signin: function() {
var element = $('.signin');
$('.go', element).click(function() {
- $('.error', element).empty();
+ $('.error').empty();
$.bug.post('/index.cgi', {
Bugzilla_login: $('.user', element).val(),
Bugzilla_password: $('.password', element).val()
- }, function(data) {
- if(data.indexOf($.bug.state_signin_error_string) < 0) {
- element.hide();
- $.bug.state_component();
- } else {
- $('.error', element).text('invalid user or password');
- }
+ }).pipe(function(data) {
+ return $.bug.lookup_result(data,
+ $.bug.state_signin_error_regexp,
+ $.bug.state_signin_success_regexp);
+ }).pipe(function(data) {
+ $('.username').text(data);
+ element.hide();
+ $.bug.state_component();
});
});
element.show();
@@ -129,7 +147,7 @@
}, function(data) {
var error = data.indexOf($.bug.state_submit_error_string);
if(error >= 0) {
- $('.error', element).text(data.substring(error + $.bug.state_submit_error_string.length, data.indexOf('<', error)));
+ $('.error').text(data.substring(error + $.bug.state_submit_error_string.length, data.indexOf('<', error)));
} else {
var success = data.indexOf($.bug.state_submit_success_string);
var start = success + $.bug.state_submit_success_string.length;
@@ -154,7 +172,7 @@
$('form', element).iframePostForm({ complete: function(data) {
var error = data.indexOf($.bug.state_attach_error_string);
if(error >= 0) {
- $('.error', element).text(data.substring(error + $.bug.state_attach_error_string.length, data.indexOf('<', error)));
+ $('.error').text(data.substring(error + $.bug.state_attach_error_string.length, data.indexOf('<', error)));
} else {
var success = data.indexOf($.bug.state_attach_success_string);
var attachment = data.substring(success + $.bug.state_attach_success_string.length, data.indexOf('<', success));
diff --git a/bug/bug/test.html b/bug/bug/test.html
index 964a20e..dacf1f9 100644
--- a/bug/bug/test.html
+++ b/bug/bug/test.html
@@ -21,11 +21,14 @@
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
+ <div class="error"></div>
+
+ <div class="username"></div>
+
<div class="signin">
<input class='user' type="text" name="user"></input>
<input class='password' type="password" name="password"></input>
<div class="go">Sign in</div>
- <div class="error"></div>
</div>
<div class="state_component">
@@ -81,7 +84,6 @@
<div class="state state_submit">
<div class="go"></div>
- <div class="error"></div>
<div class="bug"></div>
</div>
@@ -101,7 +103,6 @@
<input type="file" name="data"></input>
<input type="submit" value="Upload"></input>
</form>
- <div class="error"></div>
<img alt="SCREENSHOT" title="SCREENSHOT" />
</div>
diff --git a/bug/bug/test.js b/bug/bug/test.js
index 2cce333..6cc946a 100644
--- a/bug/bug/test.js
+++ b/bug/bug/test.js
@@ -16,34 +16,75 @@
//
module("bug");
-test("state_signin", function() {
+test("lookup_result", function() {
expect(7);
+ var caught = false;
+ var what = 'WHAT';
+ var error_regexp = /ERR_(.*)_OR/;
+ var value = 'VALUE';
+ var success_regexp = /SUC_(.*)_ESS/;
+
+ // error
+ try {
+ $.bug.lookup_result('ERR_' + what + '_OR', error_regexp, success_regexp);
+ } catch(e) {
+ equal(e[1], what);
+ equal($('.error').text(), what);
+ caught = true;
+ }
+ ok(caught, 'caught exception');
+
+ // output is not as expected
+ var bugous = 'BUGOUS OUTPUT';
+ try {
+ $.bug.lookup_result(bugous, error_regexp, success_regexp);
+ } catch(e) {
+ equal(e, bugous);
+ ok($('.error').text().indexOf(success_regexp) >= 0, 'error displayed');
+ caught = true;
+ }
+ ok(caught, 'caught exception');
+
+ // success
+ equal($.bug.lookup_result('SUC_' + value + '_ESS', error_regexp, success_regexp), value);
+});
+
+test("state_signin", function() {
+ expect(9);
+
equal($('.signin').css('display'), 'none');
var user = 'gooduser';
var password = 'goodpassword';
$.bug.post = function(url, data, callback) {
+ var d = $.Deferred();
if(data.Bugzilla_login == user &&
data.Bugzilla_password == password) {
- callback('ok');
+ d.resolve('Log out</a>' + data.Bugzilla_login + '<');
} else {
- callback($.bug.state_signin_error_string);
+ d.resolve('class="throw_error">ERROR<');
}
+ return d;
};
var state_component = $.bug.state_component;
$.bug.state_component = function() { ok(true, 'state_component'); };
$.bug.state_signin();
equal($('.signin').css('display'), 'block');
// fail to login, shows error
- equal($('.signin .error').text().length, 0, 'no error');
- $('.signin .go').click();
- ok($('.signin .error').text().length > 0, 'error message');
+ equal($('.error').text().length, 0, 'no error');
+ try {
+ $('.signin .go').click();
+ } catch(e) {
+ ok(true, 'caught error');
+ }
+ ok($('.error').text().length > 0, 'error message');
// successfull login
$('.signin .user').val(user);
$('.signin .password').val(password);
$('.signin .go').click();
equal($('.signin').css('display'), 'none');
- equal($('.signin .error').text().length, 0, 'no error');
+ equal($('.error').text().length, 0, 'no error');
+ equal($('.username').text(), user);
$.bug.post = $.post;
$.bug.state_component = state_component;
@@ -161,12 +202,12 @@ test("state_submit", function() {
equal($('.bug', element).text(), bug, 'bug number');
var error = ' ERROR ';
- equal($('.error', element).text(), '', 'error is not set');
+ equal($('.error').text(), '', 'error is not set');
$.bug.post = function(url, data, callback) {
callback('<table cellpadding="20"> <tr> <td bgcolor="#ff0000"> <font size="+2">' + error + '</font> </td> </tr> </table>');
};
$('.go', element).click();
- equal($('.error', element).text(), error, 'error is set');
+ equal($('.error').text(), error, 'error is set');
$.bug.post = $.post;
$.bug.state_success = state_success;
@@ -211,7 +252,7 @@ test("state_attach", function() {
var error = 'ERROR';
data = $.bug.state_attach_error_string + error + '<';
$('form', element).submit();
- equal($('.error', element).text(), error);
+ equal($('.error').text(), error);
var attachment = '888';
data = $.bug.state_attach_success_string + attachment + '<';
@@ -224,7 +265,6 @@ test("state_attach", function() {
test("logged_in", function() {
expect(2);
-
$.bug.get = function(url) {
return $.Deferred().resolve($.bug.logged_in_false);
};
commit 1fbe95665706479bdd6826bec1c6aa85f77263e2
Author: Loic Dachary <loic at dachary.org>
Date: Wed Sep 14 21:14:03 2011 +0200
license notice GPLv3+
diff --git a/bug/bug.xhtml b/bug/bug.xhtml
index 21c928b..077178b 100644
--- a/bug/bug.xhtml
+++ b/bug/bug.xhtml
@@ -132,6 +132,9 @@
<img title="SCREENSHOT" />
</div>
+ <div class="copyright">
+ Bug Submission Assistant is copyright (C) 2011 Loic Dachary published under <a href="http://www.gnu.org/licenses/gpl.txt">GNU GPLv3+</a> : download the <a href="bug.js">sources</a>.
+ </div>
</div>
</div>
<script>
More information about the Libreoffice-commits
mailing list