<commit_before>/*
 * Copyright 2012-2013 eBay Software Foundation and selendroid committers.
 * 
 * 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 io.selendroid;

import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
import static org.openqa.selenium.remote.CapabilityType.VERSION;
import io.selendroid.device.DeviceTargetPlatform;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SelendroidCapabilities extends DesiredCapabilities {
  public static final String ANDROID_TARGET = "androidTarget";
  public static final String AUT = "aut";
  public static final String EMULATOR = "emulator";
  public static final String DISPLAY = "display";
  public static final String LANGUAGE = "language";
  public static final String LOCALE = "locale";
  public static final String SCREEN_SIZE = "screenSize";
  private static final long serialVersionUID = -7061568919298342362L;
  public static final String PRE_SESSION_ADB_COMMANDS = "preSessionAdbCommands";
  public static final String SERIAL = "serial";

  public SelendroidCapabilities(Map<String, ?> from) {
    for (String key : from.keySet()) {
      setCapability(key, from.get(key));
    }
  }

  public String getSerial() {
    if (getRawCapabilities().get(SERIAL) == null
        || getRawCapabilities().get(SERIAL).equals(JSONObject.NULL)) return null;
    return (String) getRawCapabilities().get(SERIAL);
  }

  public String getAndroidTarget() {
    return (String) getRawCapabilities().get(ANDROID_TARGET);
  }

  public String getAut() {
    return (String) getRawCapabilities().get(AUT);
  }

  public Boolean getEmulator() {
    if (getRawCapabilities().get(EMULATOR) == null
        || getRawCapabilities().get(EMULATOR).equals(JSONObject.NULL)) return null;
    return (Boolean) getRawCapabilities().get(EMULATOR);
  }

  public String getLanguage() {
    return (String) getRawCapabilities().get(LANGUAGE);
  }

  public String getLocale() {
    return (String) getRawCapabilities().get(LOCALE);
  }

  public Map<String, Object> getRawCapabilities() {
    return (Map<String, Object>) asMap();
  }

  public String getScreenSize() {
    return (String) getRawCapabilities().get(SCREEN_SIZE);
  }

  public void setSerial(String serial) {
    setCapability(SERIAL, serial);
  }

  public void setAndroidTarget(String androidTarget) {
    setCapability(ANDROID_TARGET, androidTarget);
  }

  public void setAut(String aut) {
    setCapability(AUT, aut);
  }

  public void setEmulator(Boolean emulator) {
    setCapability(EMULATOR, emulator);
  }

  public void setLanguage(String language) {
    setCapability(LANGUAGE, language);
  }

  public void setLocale(String locale) {
    setCapability(LOCALE, locale);
  }

  public void setScreenSize(String screenSize) {
    setCapability(SCREEN_SIZE, screenSize);
  }

  public SelendroidCapabilities(JSONObject source) throws JSONException {
    Iterator<String> iter = source.keys();
    while (iter.hasNext()) {
      String key = iter.next();
      Object value = source.get(key);

      setCapability(key, decode(value));
    }
    if (source.has(CapabilityType.BROWSER_NAME) && !source.has(AUT)) {
      setAut(source.getString(CapabilityType.BROWSER_NAME));
    }
  }

  public SelendroidCapabilities() {
    super();
    setBrowserName("selendroid");
    setEmulator(true);
    setLocale("en_US");
  }

  public SelendroidCapabilities(String serial, String aut) {
    setAut(aut);
    setSerial(serial);
    if (serial == null) {
      setEmulator(null);
    } else if (serial.startsWith("emulator")) {
      setEmulator(true);
    } else {
      setEmulator(false);
    }
  }

  /**
   * 
   * @param aut The application under test. Expected format is basePackage:version. E.g.:
   *        io.selendroid.testapp:0.4
   * @return Desired Capabilities of an emulator.
   */
  public static SelendroidCapabilities emulator(String aut) {
    SelendroidCapabilities caps = new SelendroidCapabilities();
    caps.setAut(aut);
    return caps;
  }

  /**
   * 
   * @param platform The Android target platform to use.
   * @param aut The application under test. Expected format is basePackage:version. E.g.:
   *        io.selendroid.testapp:0.4
   * @return Desired Capabilities of an emulator.
   */
  public static SelendroidCapabilities emulator(DeviceTargetPlatform platform, String aut) {
    SelendroidCapabilities caps = new SelendroidCapabilities();
    caps.setAndroidTarget(platform.name());
    caps.setLocale("en_US");
    caps.setAut(aut);
    return caps;
  }

  public static DesiredCapabilities android(DeviceTargetPlatform platform) {
    SelendroidCapabilities capabilities = new SelendroidCapabilities();
    capabilities.setCapability(BROWSER_NAME, BrowserType.ANDROID);
    capabilities.setCapability(VERSION, "");
    capabilities.setCapability(PLATFORM, platform);
    capabilities.setCapability(ANDROID_TARGET, platform);
    return capabilities;
  }

  /**
   * @return The list of ADB commands that will be executed before the test session starts on the<commit_msg>Adding comstructor just for app testing if users have no preference with regards to device type.<commit_after>/*
 * Copyright 2012-2013 eBay Software Foundation and selendroid committers.
 * 
 * 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 io.selendroid;

import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
import static org.openqa.selenium.remote.CapabilityType.VERSION;
import io.selendroid.device.DeviceTargetPlatform;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SelendroidCapabilities extends DesiredCapabilities {
  public static final String ANDROID_TARGET = "androidTarget";
  public static final String AUT = "aut";
  public static final String EMULATOR = "emulator";
  public static final String DISPLAY = "display";
  public static final String LANGUAGE = "language";
  public static final String LOCALE = "locale";
  public static final String SCREEN_SIZE = "screenSize";
  private static final long serialVersionUID = -7061568919298342362L;
  public static final String PRE_SESSION_ADB_COMMANDS = "preSessionAdbCommands";
  public static final String SERIAL = "serial";

  public SelendroidCapabilities(Map<String, ?> from) {
    for (String key : from.keySet()) {
      setCapability(key, from.get(key));
    }
  }

  public String getSerial() {
    if (getRawCapabilities().get(SERIAL) == null
        || getRawCapabilities().get(SERIAL).equals(JSONObject.NULL)) return null;
    return (String) getRawCapabilities().get(SERIAL);
  }

  public String getAndroidTarget() {
    return (String) getRawCapabilities().get(ANDROID_TARGET);
  }

  public String getAut() {
    return (String) getRawCapabilities().get(AUT);
  }

  public Boolean getEmulator() {
    if (getRawCapabilities().get(EMULATOR) == null
        || getRawCapabilities().get(EMULATOR).equals(JSONObject.NULL)) return null;
    return (Boolean) getRawCapabilities().get(EMULATOR);
  }

  public String getLanguage() {
    return (String) getRawCapabilities().get(LANGUAGE);
  }

  public String getLocale() {
    return (String) getRawCapabilities().get(LOCALE);
  }

  public Map<String, Object> getRawCapabilities() {
    return (Map<String, Object>) asMap();
  }

  public String getScreenSize() {
    return (String) getRawCapabilities().get(SCREEN_SIZE);
  }

  public void setSerial(String serial) {
    setCapability(SERIAL, serial);
  }

  public void setAndroidTarget(String androidTarget) {
    setCapability(ANDROID_TARGET, androidTarget);
  }

  public void setAut(String aut) {
    setCapability(AUT, aut);
  }

  public void setEmulator(Boolean emulator) {
    setCapability(EMULATOR, emulator);
  }

  public void setLanguage(String language) {
    setCapability(LANGUAGE, language);
  }

  public void setLocale(String locale) {
    setCapability(LOCALE, locale);
  }

  public void setScreenSize(String screenSize) {
    setCapability(SCREEN_SIZE, screenSize);
  }

  public SelendroidCapabilities(JSONObject source) throws JSONException {
    Iterator<String> iter = source.keys();
    while (iter.hasNext()) {
      String key = iter.next();
      Object value = source.get(key);

      setCapability(key, decode(value));
    }
    if (source.has(CapabilityType.BROWSER_NAME) && !source.has(AUT)) {
      setAut(source.getString(CapabilityType.BROWSER_NAME));
    }
  }

  public SelendroidCapabilities() {
    super();
    setBrowserName("selendroid");
    setLocale("en_US");
  }

  public SelendroidCapabilities(String aut) {
    setAut(aut);
  }
  
  public SelendroidCapabilities(String serial, String aut) {
    setAut(aut);
    setSerial(serial);
    if (serial == null) {
      setEmulator(null);
    } else if (serial.startsWith("emulator")) {
      setEmulator(true);
    } else {
      setEmulator(false);
    }
  }

  /**
   * 
   * @param aut The application under test. Expected format is basePackage:version. E.g.:
   *        io.selendroid.testapp:0.4
   * @return Desired Capabilities of an emulator.
   */
  public static SelendroidCapabilities emulator(String aut) {
    SelendroidCapabilities caps = new SelendroidCapabilities();
    caps.setAut(aut);
    caps.setEmulator(true);
    return caps;
  }

  /**
   * 
   * @param platform The Android target platform to use.
   * @param aut The application under test. Expected format is basePackage:version. E.g.:
   *        io.selendroid.testapp:0.4
   * @return Desired Capabilities of an emulator.
   */
  public static SelendroidCapabilities emulator(DeviceTargetPlatform platform, String aut) {
    SelendroidCapabilities caps = new SelendroidCapabilities();
    caps.setAndroidTarget(platform.name());
    caps.setLocale("en_US");
    caps.setAut(aut);
    caps.setEmulator(true);
    return caps;
  }

  public static DesiredCapabilities android(DeviceTargetPlatform platform) {
    SelendroidCapabilities capabilities = new SelendroidCapabilities();
    capabilities.setCapability(BROWSER_NAME, BrowserType.ANDROID);
    capabilities.setCapability(VERSION, "");
    capabilities.setCapability(PLATFORM, platform);
    capabilities.setCapability(ANDROID_TARGET, platform);
    return capabilities;
  }

  /**
   * @return The list of ADB commands that will be executed before the test session starts on the
{'fraction_non_alphanumeric': 0.10121222185320491, 'fraction_numerical': 0.005812022583859183, 'mean_word_length': 4.154043645699615, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6962905', 'n_tokens_mistral': 3625, 'n_tokens_neox': 3517, 'n_words': 923}