/*
* Copyright 2009 Google Inc.
*
* 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.google.jstestdriver;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.google.jstestdriver.browser.BrowserControl;
import com.google.jstestdriver.browser.BrowserControl.BrowserControlFactory;
import com.google.jstestdriver.browser.BrowserRunner;
import com.google.jstestdriver.model.RunData;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
/**
* Starts a list of browsers when run.
* @author jeremiele@google.com (Jeremie Lenfant-Engelmann)
*/
public class BrowserStartupAction implements Action {
private final Set<BrowserRunner> browsers;
private final String captureAddress;
private final JsTestDriverClient client;
private final ExecutorService executor;
private final BrowserControlFactory browserControlFactory;
@Inject
public BrowserStartupAction(Set<BrowserRunner> browsers,
JsTestDriverClient client,
@Named("captureAddress") String captureAddress,
ExecutorService executor,
BrowserControlFactory browserControlFactory) {
this.browsers = browsers;
this.client = client;
this.captureAddress = captureAddress;
this.executor = executor;
this.browserControlFactory = browserControlFactory;
}
@Override
public RunData run(final RunData runData) {
List<Future<String>> browserIds = Lists.newArrayListWithCapacity(browsers.size());
for (final BrowserRunner browser : browsers) {
browserIds.add(executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return browserControlFactory.create(
browser,
captureAddress,
runData.getTestCases()).captureBrowser(client.getNextBrowserId());
}
}));
}
// check for exceptions. Gotta be a better way.
for (Future<String> futureId : browserIds) {
try {
futureId.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
return runData;
}
public Set<BrowserRunner> getBrowsers() {
return browsers;
}
public String getServerAddress() {
return captureAddress;
}
}