aboutsummaryrefslogtreecommitdiff
path: root/testrunner/src/test/java/com/galois/fiveui/chrome/RuleEvaluationTest.java
blob: 52705b5c675a74ba17cf86602e1200b71dd211cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * Module     : RuleEvaluationTest.java
 * Copyright  : (c) 2011-2012, Galois, Inc.
 *
 * Maintainer :
 * Stability  : Provisional
 * Portability: Portable
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.galois.fiveui.chrome;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import com.galois.fiveui.drivers.Drivers;
import com.galois.fiveui.testrunner.FileServer;
import com.galois.fiveui.testrunner.FiveUINav;
import com.google.common.base.Predicate;
import com.google.common.io.Files;

@Ignore
public class RuleEvaluationTest {
	private static ChromeDriver _driver;
	private static FiveUINav _fiveui;
	private static FileServer _server;
	
	@BeforeClass
	public static void setUp() {
		//_driver = Drivers.buildChromeDriver();
		_fiveui = new FiveUINav(_driver);
		
		try {
			// set up a file server to serve from the exampleData dir:
			_server = new FileServer("../");
			_server.start();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	@AfterClass
	public static void tearDown() {
		try {
			_driver.quit();
			_server.stop();
		} catch (Exception e) {
			System.err.println("exception shutting down test");
			e.printStackTrace();
		}
	}

	@Before
	public void preTest() throws InterruptedException {
		_fiveui.clearOptions();
	}
	
	/**
	 * Check to see that a very basic rule will evaluate in the context of a matching url.
	 * @throws InterruptedException 
	 */
	@Ignore
	@Test
	public void ruleEvaluates() throws InterruptedException {
		String rsName = "Test rule set";
		String ruleSet = "{ 'name': '"+rsName+"', " +
				         "  'description': 'test'," +
				         "  'rules': [ { 'name': 'Test rule'," +
				         "               'description': 'An empty test rule'," +
				         "               'rule': function() { report('test error'); }" +
				         "             } ]" +
				         "}";
			
		_fiveui.addRuleSet(ruleSet);
		
		String pattern = "*localhost*";
		_fiveui.addUrlPat(pattern, rsName);
		_fiveui.setDisplayDefault(true);
		
		_driver.get("http://localhost:"+_server.getPort()+"/exampleData/basic/headings.html");
				
		_fiveui.expectCondition("FiveUI Window should be showing", 5, new Predicate<WebDriver>() {
			public boolean apply(WebDriver input) {
				List<WebElement> uicWindow = _driver.findElements(By.id("uic-top"));
				return uicWindow.size() != 0 && uicWindow.get(0).isDisplayed();
			}
		});
		
		_fiveui.expectCondition("Wrong number of problems.", 5, new Predicate<WebDriver>() {
			public boolean apply(WebDriver input) {
				List<WebElement> problems = _driver.findElements(By.cssSelector("#problemList>.pr"));
				return problems.size() != 1;
			}
		});
	}
	
	/**
	 * Check that selecting a rule highlights the correct element of the dom.
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	@Ignore
	@Test
	public void rulesHighlight() throws InterruptedException, IOException {
		String rsName = "reportOnSampleText";
		File rsFile = new File("../exampleData/ruleSets/"+rsName+".json");
		String ruleSet = Files.toString(rsFile, Charset.defaultCharset());
		
		_fiveui.addRuleSet(ruleSet);
		
		String pattern = "*localhost*";
		_fiveui.addUrlPat(pattern, rsName);
		_fiveui.setDisplayDefault(true);
		
		_driver.get("http://localhost:"+_server.getPort()+"/exampleData/basic/headings.html");
		
		
		_fiveui.expectCondition("Wrong number of problems.", 5, new Predicate<WebDriver>() {
			public boolean apply(WebDriver input) {
			    //List<WebElement> problems = _driver.findElements(By.cssSelector("#problemList .prExpand"));
			    List<WebElement> problems = _driver.findElements(By.className("prExpand"));
				return problems.size() == 1;
			}
		});
		
		WebElement expand = _driver.findElement(By.cssSelector("#problemList .prExpand"));
		expand.click();
		
		_fiveui.expectCondition("Nothing was tagged as a uic-problem.", 5, new Predicate<WebDriver>() {
			public boolean apply(WebDriver input) {
				List<WebElement> problems = _driver.findElements(By.className("uic-problem"));
				return problems.size() == 1;
			}
		});
	}
}