Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move settings to config page as default #119 #189

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
return true;
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public FormValidation doCheckFontSize(@QueryParameter String value) {
try {
float val = Float.parseFloat(value);
if (val >= 0.3 && val <= 2) {
return FormValidation.ok();
} else {
return FormValidation.error("Must be >= 0.3 and <= 2");
}
} catch (NumberFormatException e) {
return FormValidation.error("Must be float");
}
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public FormValidation doCheckNumberOfColumns(@QueryParameter String value) {
try {
int val = Integer.parseInt(value);
if (val >= 1 && val <= 8) {
return FormValidation.ok();
} else {
return FormValidation.error("Must be >= 1 and <= 8");
}
} catch (NumberFormatException e) {
return FormValidation.error("Must be integer");
}
}

private boolean permissionToCollectAnonymousUsageStatistics = true;

public boolean getPermissionToCollectAnonymousUsageStatistics() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ public String currentOrder() {
return currentConfig().getOrder().getClass().getSimpleName();
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public String currentFontSize() {
return String.valueOf(currentConfig().getFontSize());
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public String currentNumberOfColumns() {
return String.valueOf(currentConfig().getNumberOfColumns());
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public boolean isColourBlindMode() {
return currentConfig().isColourBlindMode();
}

@SuppressWarnings("unused") // used in the configure-entries.jelly form
public String getColourBlindModeValue() {
return isColourBlindMode() ? "1" : "0";
}

private static final BuildMonitorInstallation installation = new BuildMonitorInstallation();

@SuppressWarnings("unused") // used in index.jelly
Expand All @@ -95,11 +115,45 @@ public boolean collectAnonymousUsageStatistics() {
@Override
protected void submit(StaplerRequest req) throws ServletException, IOException, FormException {
super.submit(req);
title = req.getParameter("title");

String requestedOrdering = req.getParameter("order");
submitFontSize(req);
submitNumberOfColumns(req);
submitColourBlindMode(req);
submitOrder(req);
}

title = req.getParameter("title");
private void submitFontSize(StaplerRequest req) throws FormException {
String fontSize = req.getParameter("fontSize");
try {
float val = Float.parseFloat(fontSize);
if (val >= 0.3 && val <= 2) {
currentConfig().setFontSize(val);
}
} catch (NumberFormatException e) {
throw new FormException("Font size must be float and not null", fontSize);
}
}

private void submitNumberOfColumns(StaplerRequest req) throws FormException {
String numberOfColumns = req.getParameter("numberOfColumns");
try {
int val = Integer.parseInt(numberOfColumns);
if (val >= 1 && val <= 8) {
currentConfig().setNumberOfColumns(val);
}
} catch (NumberFormatException e) {
throw new FormException("Number of columns must be integer and not null", numberOfColumns);
}
}

private void submitColourBlindMode(StaplerRequest req) throws FormException {
String colourBlindMode = req.getParameter("colourBlindMode");
currentConfig().setColourBlindMode(isGiven(colourBlindMode));
}

private void submitOrder(StaplerRequest req) throws FormException {
String requestedOrdering = req.getParameter("order");
try {
currentConfig().setOrder(orderIn(requestedOrdering));
} catch (Exception e) {
Expand Down Expand Up @@ -175,7 +229,7 @@ private boolean deserailisingFromAnOlderFormat() {

// If an older version of config.xml is loaded, "config" field is missing, but "order" is present
private void migrateFromOldToNewConfigFormat() {
Config c = new Config();
Config c = Config.defaultConfig();
c.setOrder(order);

config = c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import com.google.common.base.Objects;
import com.smartcodeltd.jenkinsci.plugins.buildmonitor.order.ByName;
import hudson.model.AbstractProject;
import hudson.model.Job;

import java.util.Comparator;

public class Config {

public static Config defaultConfig() {
return new Config();
Config config = new Config();
config.setFontSize(1);
config.setNumberOfColumns(2);
config.setColourBlindMode(false);
return config;
}

public Comparator<Job<?, ?>> getOrder() {
Expand All @@ -21,6 +24,33 @@ public void setOrder(Comparator<Job<?, ?>> order) {
this.order = order;
}

public float getFontSize() {
return fontSize;
}

public void setFontSize(float fontSize) {
this.fontSize = fontSize;
}

public int getNumberOfColumns() {
return numberOfColumns;
}

public void setNumberOfColumns(int numberOfColumns) {
this.numberOfColumns = numberOfColumns;
}

public boolean isColourBlindMode() {
return colourBlindMode;
}

public void setColourBlindMode(boolean colourBlindMode) {
this.colourBlindMode = colourBlindMode;
}

public Config() {
}

@Override
public String toString() {
return Objects.toStringHelper(this)
Expand All @@ -47,4 +77,10 @@ private <T> T getOrElse(T value, T defaultValue) {
}

private Comparator<Job<?, ?>> order;

private float fontSize;

private int numberOfColumns;

private boolean colourBlindMode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
</select>
</f:entry>

<f:entry title="${%Font size}">
<f:textbox name="fontSize" field="fontSize" value="${it.currentFontSize()}" />
</f:entry>

<f:entry title="${%Number of columns}">
<f:textbox name="numberOfColumns" field="numberOfColumns" value="${it.currentNumberOfColumns()}" />
</f:entry>

<f:entry title="${%Colour blind mode ?}">
<f:checkbox name="colourBlindMode" checked="${it.isColourBlindMode()}"/>
</f:entry>

</f:section>

<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@

constant('BUILD_MONITOR_VERSION', '${it.installation.buildMonitorVersion()}').

constant('DEFAULT_SETTINGS_FONTSIZE', '${it.currentFontSize()}').
constant('DEFAULT_SETTINGS_NUMBEROFCOLUMNS', '${it.currentNumberOfColumns()}').
constant('DEFAULT_SETTINGS_COLOURBLINDMODE', '${it.getColourBlindModeValue()}').

config(function(proxyProvider, cookieJarProvider, hashCodeProvider) {
var hashCodeOf = hashCodeProvider.hashCodeOf;

Expand Down
10 changes: 5 additions & 5 deletions src/main/webapp/scripts/settings.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
angular.
module('buildMonitor.settings', [ 'buildMonitor.services', 'uiSlider']).

controller('controlPanel', ['$scope', 'cookieJar', 'townCrier',
function ($scope, cookieJar, townCrier) {
controller('controlPanel', ['$scope', 'cookieJar', 'townCrier', 'DEFAULT_SETTINGS_FONTSIZE', 'DEFAULT_SETTINGS_NUMBEROFCOLUMNS', 'DEFAULT_SETTINGS_COLOURBLINDMODE',
function ($scope, cookieJar, townCrier, DEFAULT_SETTINGS_FONTSIZE, DEFAULT_SETTINGS_NUMBEROFCOLUMNS, DEFAULT_SETTINGS_COLOURBLINDMODE) {
'use strict';

$scope.settings.fontSize = cookieJar.get('fontSize', 1);
$scope.settings.numberOfColumns = cookieJar.get('numberOfColumns', 2);
$scope.settings.colourBlind = cookieJar.get('colourBlind', 0);
$scope.settings.fontSize = cookieJar.get('fontSize', DEFAULT_SETTINGS_FONTSIZE);
$scope.settings.numberOfColumns = cookieJar.get('numberOfColumns', DEFAULT_SETTINGS_NUMBEROFCOLUMNS);
$scope.settings.colourBlind = cookieJar.get('colourBlind', DEFAULT_SETTINGS_COLOURBLINDMODE);

angular.forEach($scope.settings, function(value, name) {
$scope.$watch('settings.' + name, function(currentValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,68 @@ public void form_validator_should_advise_how_a_regex_could_be_improved() throws
assertThat(htmlDecoded(result.getMessage()), containsString("Unmatched closing ')'"));
}

@Test
public void form_validator_should_allow_fontsize_in_good_range() throws Exception {
FormValidation result = validator.doCheckFontSize("1.0");

assertThat(result.kind, is(OK));
}

@Test
public void form_validator_should_refuse_fontsize_under_0_3() throws Exception {
FormValidation result = validator.doCheckFontSize("0");

assertThat(result.kind, is(ERROR));
assertThat(htmlDecoded(result.getMessage()), containsString("Must be >= 0.3 and <= 2"));
}

@Test
public void form_validator_should_refuse_fontsize_over_2() throws Exception {
FormValidation result = validator.doCheckFontSize("2.1");

assertThat(result.kind, is(ERROR));
assertThat(htmlDecoded(result.getMessage()), containsString("Must be >= 0.3 and <= 2"));
}

@Test
public void form_validator_should_refuse_fontsize_not_float() throws Exception {
FormValidation result = validator.doCheckFontSize("a");

assertThat(result.kind, is(ERROR));
assertThat(htmlDecoded(result.getMessage()), containsString("Must be float"));
}

@Test
public void form_validator_should_allow_numberofcolumns_in_good_range() throws Exception {
FormValidation result = validator.doCheckNumberOfColumns("1");

assertThat(result.kind, is(OK));
}

@Test
public void form_validator_should_refuse_numberofcolumns_under_1() throws Exception {
FormValidation result = validator.doCheckNumberOfColumns("0");

assertThat(result.kind, is(ERROR));
assertThat(htmlDecoded(result.getMessage()), containsString("Must be >= 1 and <= 8"));
}

@Test
public void form_validator_should_refuse_numberofcolumns_over_8() throws Exception {
FormValidation result = validator.doCheckNumberOfColumns("10");

assertThat(result.kind, is(ERROR));
assertThat(htmlDecoded(result.getMessage()), containsString("Must be >= 1 and <= 8"));
}

@Test
public void form_validator_should_refuse_numberofcolumns_not_integer() throws Exception {
FormValidation result = validator.doCheckNumberOfColumns("a");

assertThat(result.kind, is(ERROR));
assertThat(htmlDecoded(result.getMessage()), containsString("Must be integer"));
}

private String htmlDecoded(String message) {
return StringEscapeUtils.unescapeHtml(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import static com.smartcodeltd.jenkinsci.plugins.buildmonitor_acceptance.scenarios.prerequisites.FreestyleProjectExists.aFreestyleProject;
import static com.smartcodeltd.jenkinsci.plugins.buildmonitor_acceptance.scenarios.tasks.ConfigureJobFilters.includesAllTheJobs;
import static com.smartcodeltd.jenkinsci.plugins.buildmonitor_acceptance.scenarios.tasks.CreateBuildMonitorView.createABuildMonitorView;
import static org.junit.Assert.assertThat;
import static com.smartcodeltd.jenkinsci.plugins.buildmonitor_acceptance.scenarios.tasks.OpenSettingsPanel.openSettingsPanel;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

// @TODO: Work in progress
public class OutOfTheBoxBehaviourTest extends AcceptanceTest {
Expand All @@ -32,4 +34,18 @@ public void correctly_displays_successful_and_failing_jobs() throws Exception {
assertThat(buildMonitor.job("example-build"), isSuccessful());
assertThat(buildMonitor.job("example-acceptance"), isFailing());
}

@Test
@With(plugins = { "buildgraph-view-1.0.hpi", "git-1.5.0.hpi", "git-client-1.8.0.jpi" })
public void correctly_displays_default_settings() throws Exception {

given.WhenI(createABuildMonitorView("Build Monitor"));

buildMonitor = buildMonitorView("Build Monitor");

given.WhenI(openSettingsPanel());

assertThat(buildMonitor.settings().fontSize(), is("1.0"));
assertThat(buildMonitor.settings().numberOfColumns(), is("2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public BuildMonitor(WebElement root) {
public Job job(String name) {
return new Job(root.findElement(By.id(name)));
}

public Settings settings() {
return new Settings(root.findElement(By.className("showSettings")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.smartcodeltd.jenkinsci.plugins.buildmonitor_acceptance.pageobjects.buildmonitor;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class Settings {
private final WebElement root;

public Settings(WebElement root) {
this.root = root;
}

public String fontSize() {
return root.findElement(By.xpath("//ul/li[2]/slider/span[contains(@class, 'bubble ng-binding')]")).getText();
}

public String numberOfColumns() {
return root.findElement(By.xpath("//ul/li[3]/slider/span[contains(@class, 'bubble ng-binding')]")).getText();
}
}
Loading