# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Test expectation list for WebDriver Java acceptance tests.

It is evaluated through Python.
"""

import fnmatch


_REVISION_NEGATIVE_FILTER = {}
_REVISION_NEGATIVE_FILTER['HEAD'] = [
    'AlertsTest.*',
    'AuthenticatedPageLoadingTest.*',
    'AvailableLogsTest.driverLogShouldBeEnabledByDefault',
    'BasicMouseInterfaceTest.testContextClick',
    'BasicMouseInterfaceTest.testMousePositionIsNotPreservedInActionsChain',
    'BasicMouseInterfaceTest.testMovingIntoAnImageEnclosedInALink',
    'BasicMouseInterfaceTest.testMovingMouseBackAndForthPastViewPort',
    'BasicMouseInterfaceTest.testMovingMousePastViewPort',
    'ChromeOptionsFunctionalTest.canStartChromeWithCustomOptions',
    # https://code.google.com/p/selenium/source/diff?spec=svn5e856ea3d0ee07db00aa41293f68ce2566c16888&r=a823e0a2ead8b5430a9a92a14452d0d47cb05f61&format=side&path=/java/client/test/org/openqa/selenium/ClickScrollingTest.java&old_path=/java/client/test/org/openqa/selenium/ClickScrollingTest.java&old=ee8ae7d15794146d24a744ec96d1192ff851f0fc
    'ClickScrollingTest.testShouldBeAbleToClickOnAnElementHiddenByDoubleOverflow',
    'ClickScrollingTest.testShouldNotBeAbleToClickElementThatIsOutOfViewInANonScrollableFrame',
    'ClickScrollingTest.testShouldNotScrollIfAlreadyScrolledAndElementIsInView',
    'ClickTest.testCanClickAnImageMapArea',
    # https://code.google.com/p/selenium/source/diff?spec=svn5e856ea3d0ee07db00aa41293f68ce2566c16888&r=9ec284bff77c06198af6097ca6703a48e1b0736b&format=side&path=/java/client/test/org/openqa/selenium/ClickTest.java&old_path=/java/client/test/org/openqa/selenium/ClickTest.java&old=5a0284c1a625ed8507f6d98857958e286a449bb7
    'ClickTest.testShouldBeAbleToClickOnAnElementInFrameGreaterThanTwoViewports',
    'CombinedInputActionsTest.testClickingOnFormElements',
    'CoordinatesTest.testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort',
    'CorrectEventFiringTest.testShouldFireEventsInTheRightOrder',
    'CorrectEventFiringTest.testShouldFireFocusEventWhenClicking',
    'DragAndDropTest.testDragTooFar',
    'ExecutingAsyncJavascriptTest.includesAlertTextInUnhandledAlertException',
    # https://code.google.com/p/selenium/source/diff?spec=svn5e856ea3d0ee07db00aa41293f68ce2566c16888&r=0e6a710cceed3037e8e44ae81579cf3ceca33fb6&format=side&path=/java/client/test/org/openqa/selenium/ExecutingAsyncJavascriptTest.java&old_path=/java/client/test/org/openqa/selenium/ExecutingAsyncJavascriptTest.java&old=ee8ae7d15794146d24a744ec96d1192ff851f0fc
    'ExecutingAsyncJavascriptTest.shouldCatchErrorsWithMessageAndStacktraceWhenExecutingInitialScript',
    'ExecutingAsyncJavascriptTest.throwsIfAlertHappensDuringScript',
    'ExecutingAsyncJavascriptTest.throwsIfAlertHappensDuringScriptWhichTimesOut',
    'ExecutingAsyncJavascriptTest.throwsIfScriptTriggersAlert',
    'ExecutingAsyncJavascriptTest.throwsIfScriptTriggersAlertWhichTimesOut',
    'ExecutingJavascriptTest.testShouldThrowExceptionIfExecutingOnNoPage',
    # https://code.google.com/p/selenium/source/diff?spec=svn5e856ea3d0ee07db00aa41293f68ce2566c16888&r=0e6a710cceed3037e8e44ae81579cf3ceca33fb6&format=side&path=/java/client/test/org/openqa/selenium/ExecutingJavascriptTest.java&old_path=/java/client/test/org/openqa/selenium/ExecutingJavascriptTest.java&old=bf0d4b7d040315e9311efaa3a91a2a59777608da
    'ExecutingJavascriptTest.testShouldThrowAnExceptionWithMessageAndStacktraceWhenTheJavascriptIsBad',
    'FormHandlingTest.handleFormWithJavascriptAction',
    'FormHandlingTest.testShouldNotBeAbleToSubmitAFormThatDoesNotExist',
    'FrameSwitchingTest.testShouldNotBeAbleToDoAnythingTheFrameIsDeletedFromUnderUs',
    'I18nTest.testShouldBeAbleToActivateIMEEngine',
    # Broken because AddWebStorage.java is broken.
    'LocalStorageTest.*',
    'LocationContextTest.*',
    'MiscTest.testShouldBeAbleToGetTheSourceOfAnXmlDocument',
    'PageLoadingTest.shouldBeAbleToDisableAcceptOfInsecureSslCertsWithRequiredCapability',
    'PageLoadingTest.testShouldNotWaitIndefinitelyIfAnExternalResourceFailsToLoad',
    'PageLoadingTest.testShouldTimeoutIfAPageTakesTooLongToLoad',
    # PerformanceLoggingTest is for firefox only; "performance" log deprecated.
    'PerformanceLoggingTest.*',
    'ProxySettingTest.canConfigureProxyWithRequiredCapability',
    'ProxySettingTest.requiredProxyCapabilityShouldHavePriority',
    'RenderedWebElementTest.testShouldHandleNonIntegerPositionAndSize',
    'SessionHandlingTest.callingQuitAfterClosingTheLastWindowIsANoOp',
    # Broken because AddWebStorage.java is broken.
    'SessionStorageTest.*',
    'SqlDatabaseTest.*',
    # https://code.google.com/p/selenium/source/diff?spec=svn6bbba240f54370803189ce28149072c59f5d5312&r=6bbba240f54370803189ce28149072c59f5d5312&format=side&path=/java/client/test/org/openqa/selenium/SvgDocumentTest.java
    'SvgDocumentTest.testClickOnSvgElement',
    # https://code.google.com/p/selenium/source/diff?spec=svn5e856ea3d0ee07db00aa41293f68ce2566c16888&r=859a1df06d56b4f2ba247f1dadbfceac8066efcb&format=side&path=/java/client/test/org/openqa/selenium/TakesScreenshotTest.java&old_path=/java/client/test/org/openqa/selenium/TakesScreenshotTest.java&old=cfea793d0410db2100a40daf88483b83d58af435
    'TakesScreenshotTest.testShouldCaptureScreenshot',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithLongX',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithLongY',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithTooLong',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithTooLongX',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithTooLongY',
    'TextHandlingTest.testShouldNotReturnLtrMarks',
    'TextPagesTest.testShouldBeAbleToLoadASimplePageOfText',
    'TextPagesTest.testShouldThrowExceptionWhenAddingCookieToAPageThatIsNotHtml',
    'TypingTest.testNonPrintableCharactersShouldWorkWithContentEditableOrDesignModeSet',
    'TypingTest.testShouldBeAbleToTypeIntoContentEditableElementWithExistingValue',
    'TypingTest.testShouldNotTypeIntoElementsThatPreventKeyDownEvents',
    'TypingTest.testTypingIntoAnIFrameWithContentEditableOrDesignModeSet',
    'UnexpectedAlertBehaviorTest.*',
    # https://code.google.com/p/selenium/source/diff?spec=svn5e856ea3d0ee07db00aa41293f68ce2566c16888&r=e923c1389b85331d2c98aaeb6bbca88bc57821cd&format=side&path=/java/client/test/org/openqa/selenium/VisibilityTest.java&old_path=/java/client/test/org/openqa/selenium/VisibilityTest.java&old=ee8ae7d15794146d24a744ec96d1192ff851f0fc
    'VisibilityTest.testElementHiddenByOverflowXIsNotVisible',
    'VisibilityTest.testElementHiddenByOverflowYIsNotVisible',
    'VisibilityTest.tooSmallAWindowWithOverflowHiddenIsNotAProblem',
    'WindowTest.*',
]
_REVISION_NEGATIVE_FILTER['28'] = [] + _REVISION_NEGATIVE_FILTER['HEAD']
_REVISION_NEGATIVE_FILTER['27'] = [] + _REVISION_NEGATIVE_FILTER['HEAD']
_REVISION_NEGATIVE_FILTER['26'] = [
    'UploadTest.testFileUploading',
    'CorrectEventFiringTest.testUploadingFileShouldFireOnChangeEvent',
    'FormHandlingTest.testShouldBeAbleToAlterTheContentsOfAFileUploadInputElement',
    'FormHandlingTest.testShouldBeAbleToUploadTheSameFileTwice',
    'FormHandlingTest.testShouldBeAbleToSendKeysToAFileUploadInputElementInAnXhtmlDocument',
] + _REVISION_NEGATIVE_FILTER['HEAD']

_OS_NEGATIVE_FILTER = {}
_OS_NEGATIVE_FILTER['win'] = [
    # Flaky: https://code.google.com/p/chromedriver/issues/detail?id=282
    'PageLoadingTest.testShouldNotHangIfDocumentOpenCallIsNeverFollowedByDocumentCloseCall',
]
_OS_NEGATIVE_FILTER['linux'] = []
_OS_NEGATIVE_FILTER['mac'] = []
_OS_NEGATIVE_FILTER['android'] = [
    'ChromeOptionsFunctionalTest.canStartChromeWithCustomOptions',
    'ClickScrollingTest.testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrameThatIsOutOfView',
    'ClickScrollingTest.testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrame',
    'ClickTest.testCanClickOnAnElementWithTopSetToANegativeNumber',
    'ClickTest.testShouldOnlyFollowHrefOnce',
    'CombinedInputActionsTest.testCombiningShiftAndClickResultsInANewWindow',
    'CoordinatesTest.testShouldGetCoordinatesInViewPortOfAnElementInAFrame',
    'CoordinatesTest.testShouldGetCoordinatesInViewPortOfAnElementInANestedFrame',
    'CoordinatesTest.testShouldGetCoordinatesOfAHiddenElement',
    'CorrectEventFiringTest.testSendingKeysToAFocusedElementShouldNotBlurThatElement',
    'CorrectEventFiringTest.testSendingKeysToAnElementShouldCauseTheFocusEventToFire',
    'CorrectEventFiringTest.testSendingKeysToAnotherElementShouldCauseTheBlurEventToFire',
    'CorrectEventFiringTest.testShouldEmitClickEventWhenClickingOnATextInputElement',
    'ElementAttributeTest.testCanRetrieveTheCurrentValueOfATextFormField_emailInput',
    'ElementAttributeTest.testCanRetrieveTheCurrentValueOfATextFormField_textArea',
    'ElementAttributeTest.testCanRetrieveTheCurrentValueOfATextFormField_textInput',
    'ElementEqualityTest.testAnElementFoundInADifferentFrameViaJsShouldHaveSameId',
    'ElementFindingTest.testAnElementFoundInADifferentFrameIsStale',
    'ElementFindingTest.testAnElementFoundInADifferentFrameViaJsCanBeUsed',
    'ElementSelectingTest.testShouldBeAbleToToggleEnabledMultiSelectOption',
    'ExecutingAsyncJavascriptTest.shouldBeAbleToExecuteAsynchronousScripts',
    'ExecutingAsyncJavascriptTest.shouldNotTimeoutIfScriptCallsbackInsideAZeroTimeout',
    'FormHandlingTest.testSendingKeyboardEventsShouldAppendTextInInputs',
    'FormHandlingTest.testSendingKeyboardEventsShouldAppendTextInInputsWithExistingValue',
    'FormHandlingTest.testSendingKeyboardEventsShouldAppendTextinTextAreas',
    'FormHandlingTest.testSendKeysKeepsCapitalization',
    'FormHandlingTest.testShouldBeAbleToClearTextFromInputElements',
    'FormHandlingTest.testShouldBeAbleToClearTextFromTextAreas',
    'FormHandlingTest.testShouldBeAbleToEnterTextIntoATextAreaBySettingItsValue',
    'FormHandlingTest.testShouldEnterDataIntoFormFields',
    'FrameSwitchingTest.testClosingTheFinalBrowserWindowShouldNotCauseAnExceptionToBeThrown',
    'FrameSwitchingTest.testGetCurrentUrl',
    'FrameSwitchingTest.testShouldNotSwitchMagicallyToTheTopWindow',
    'I18nTest.testEnteringHebrewTextFromLeftToRight',
    'I18nTest.testEnteringHebrewTextFromRightToLeft',
    'I18nTest.testShouldBeAbleToInputJapanese',
    'ImplicitWaitTest.testShouldImplicitlyWaitForAnElementToBeVisibleBeforeInteracting',
    'JavascriptEnabledDriverTest.testChangeEventIsFiredAppropriatelyWhenFocusIsLost',
    'JavascriptEnabledDriverTest.testIssue80ClickShouldGenerateClickEvent',
    'JavascriptEnabledDriverTest.testShouldBeAbleToClickALinkThatClosesAWindow',
    'JavascriptEnabledDriverTest.testShouldFireOnChangeEventWhenSettingAnElementsValue',
    'OpacityTest.testShouldBeAbleToClickOnElementsWithOpacityZero',
    'PageLoadingTest.testShouldBeAbleToAccessPagesWithAnInsecureSslCertificate',
    'SelectElementHandlingTest.testShouldBePossibleToDeselectASingleOptionFromASelectWhichAllowsMultipleChoices',
    'SelectElementTest.shouldAllowOptionsToBeDeselectedByIndex',
    'SelectElementTest.shouldAllowOptionsToBeDeselectedByReturnedValue',
    'SelectElementTest.shouldAllowUserToDeselectAllWhenSelectSupportsMultipleSelections',
    'SelectElementTest.shouldAllowUserToDeselectOptionsByVisibleText',
    'SessionHandlingTest.callingAnyOperationAfterClosingTheLastWindowShouldThrowAnException',
    'TakesScreenshotTest.testCaptureToBase64',
    'TakesScreenshotTest.testSaveScreenshotAsFile',
    'TextHandlingTest.testShouldBeAbleToEnterDatesAfterFillingInOtherValuesFirst',
    'TextHandlingTest.testShouldBeAbleToSetMoreThanOneLineOfTextInATextArea',
    'TextHandlingTest.testTextOfATextAreaShouldBeEqualToItsDefaultTextEvenAfterTyping',
    'TouchFlickTest.*',
    'TouchScrollTest.*',
    'TouchSingleTapTest.*',
    # Passes locally but not on bots??
    'TypingTest.testShouldFireKeyDownEvents',
    # Test is written using local files; doesn't work on Android.
    'UploadTest.testFileUploading',
    # Not applicable on Android.
    'WindowSwitchingTest.*',
]

def GetDisabledTestMatchers(operating_system, chrome_version):
  """Returns the list of disabled test matchers for the specific configuration.

  Args:
    operating_system: The operating system, one of 'linux', 'mac', 'win', or
        'android'.
    chrome_version: Chrome version to test against, e.g., 'HEAD' or '26'.

  Returns:
    List of disabled test matchers, which may contain '*' wildcards.
  """
  return (_OS_NEGATIVE_FILTER[operating_system] +
          _REVISION_NEGATIVE_FILTER[chrome_version])[:]

def ApplyJavaTestFilter(operating_system, chrome_version, tests):
  """Applies the test filter to the given list of tests.

  Args:
    operating_system: The operating system, one of 'linux', 'mac', 'win', or
        'android'.
    chrome_version: Chrome version to test against, e.g., 'HEAD' or '26'.
    test: list of test names to filter.

  Returns:
    Set of passed test names.
  """
  filters = (_OS_NEGATIVE_FILTER[operating_system] +
             _REVISION_NEGATIVE_FILTER[chrome_version])
  passed = set(tests)
  for f in filters:
    passed.difference_update(set(t for t in tests if fnmatch.fnmatch(t, f)))
  return passed
