/*
Copyright 2012 Selenium committers
Copyright 2012 Software Freedom Conservancy

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 org.openqa.selenium.qtwebkit;

import java.net.URL;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.HasTouchScreen;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.TouchScreen;
import org.openqa.selenium.html5.LocalStorage;
import org.openqa.selenium.html5.SessionStorage;
import org.openqa.selenium.html5.WebStorage;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.html5.RemoteLocalStorage;
import org.openqa.selenium.remote.html5.RemoteSessionStorage;


public class QtWebKitDriver extends RemoteWebDriver implements TakesScreenshot, WebStorage, HasTouchScreen {

    private RemoteLocalStorage localStorage;
    private RemoteSessionStorage sessionStorage;
    private TouchScreen touchScreen;

    public QtWebKitDriver(CommandExecutor executor, Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
        super(executor, desiredCapabilities, requiredCapabilities);
        localStorage = new RemoteLocalStorage(getExecuteMethod());
        sessionStorage = new RemoteSessionStorage(getExecuteMethod());
        touchScreen = new RemoteTouchScreen(getExecuteMethod());
      setElementConverter(new QtWebKitJsonToWebElementConverter(this));
    }

    public QtWebKitDriver(CommandExecutor executor, Capabilities desiredCapabilities) {
	    this(executor, desiredCapabilities, null);
    }

    public QtWebKitDriver(Capabilities desiredCapabilities) {
	    this((URL) null, desiredCapabilities);
    }

    public QtWebKitDriver(URL remoteAddress, Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
	    this(new QtWebDriverExecutor(remoteAddress), desiredCapabilities, requiredCapabilities);
    }

    public QtWebKitDriver(URL remoteAddress, Capabilities desiredCapabilities) {
	    this(new QtWebDriverExecutor(remoteAddress), desiredCapabilities, null);
    }

    public <X> X getScreenshotAs(OutputType<X> target) {
        // Get the screenshot as base64.
        String base64 = (String) execute(DriverCommand.SCREENSHOT).getValue();
        // ... and convert it.
        return target.convertFromBase64Png(base64);
    }

    @Override
    public LocalStorage getLocalStorage() {
        return localStorage;
    }

    @Override
    public SessionStorage getSessionStorage() {
        return sessionStorage;
    }

    @Override
    public TouchScreen getTouch() {
        return touchScreen;
    }
}
@@ -677,167 +677,48 @@
 ort 
-org.openqa.selenium.Capabilities;%0Aimport org.openqa.selenium.HasTouchScreen;%0Aimport org.openqa.selenium.OutputType;%0Aimport org.openqa.selenium.TakesScreenshot;
+com.google.common.collect.ImmutableMap;%0A
 %0Aimp
@@ -741,27 +741,17 @@
 elenium.
-TouchScreen
+*
 ;%0Aimport
@@ -1319,16 +1319,27 @@
 chScreen
+, Rotatable
  %7B%0A%0A    
@@ -3122,10 +3122,382 @@
 n;%0A    %7D
+%0A%0A    @Override%0A    public void rotate(ScreenOrientation orientation) %7B%0A        execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of(%22orientation%22, orientation));%0A    %7D%0A%0A    @Override%0A    public ScreenOrientation getOrientation() %7B%0A        return ScreenOrientation.valueOf(%0A                (String) execute(DriverCommand.GET_SCREEN_ORIENTATION).getValue());%0A    %7D
 %0A%7D

enable Rotatable tests for QtWebKitDriver