# 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.testIncludesAlertTextInUnhandledAlertException',
    'AlertsTest.testSettingTheValueOfAnAlertThrows',
    'AlertsTest.testShouldGetTextOfAlertOpenedInSetTimeout',
    'AlertsTest.testShouldHandleAlertOnPageUnload',
    'AlertsTest.testShouldHandleAlertOnWindowClose',
    'AlertsTest.testShouldNotHandleAlertInAnotherWindow',
    'AlertsTest.testSwitchingToMissingAlertInAClosedWindowThrows',
    'AuthenticatedPageLoadingTest.*',
    'AvailableLogsTest.driverLogShouldBeEnabledByDefault',
    'BasicMouseInterfaceTest.testContextClick',
    'BasicMouseInterfaceTest.testMousePositionIsNotPreservedInActionsChain',
    'BasicMouseInterfaceTest.testMovingIntoAnImageEnclosedInALink',
    'BasicMouseInterfaceTest.testMovingMouseBackAndForthPastViewPort',
    'BasicMouseInterfaceTest.testMovingMousePastViewPort',
    'ChromeOptionsFunctionalTest.canStartChromeWithCustomOptions',
    'ClickScrollingTest.testShouldBeAbleToClickOnAnElementHiddenByDoubleOverflow',
    'ClickScrollingTest.testShouldNotBeAbleToClickElementThatIsOutOfViewInANonScrollableFrame',
    'ClickScrollingTest.testShouldNotScrollIfAlreadyScrolledAndElementIsInView',
    'ClickTest.testCanClickAnImageMapArea',
    'ClickTest.testShouldBeAbleToClickOnAnElementInFrameGreaterThanTwoViewports',
    'CombinedInputActionsTest.testClickingOnFormElements',
    'CoordinatesTest.testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort',
    'CorrectEventFiringTest.testShouldFireEventsInTheRightOrder',
    'CorrectEventFiringTest.testShouldFireFocusEventWhenClicking',
    'DragAndDropTest.testDragTooFar',
    'ExecutingAsyncJavascriptTest.includesAlertTextInUnhandledAlertException',
    'ExecutingAsyncJavascriptTest.shouldCatchErrorsWithMessageAndStacktraceWhenExecutingInitialScript',
    'ExecutingAsyncJavascriptTest.throwsIfAlertHappensDuringScript',
    'ExecutingAsyncJavascriptTest.throwsIfAlertHappensDuringScriptWhichTimesOut',
    'ExecutingAsyncJavascriptTest.throwsIfScriptTriggersAlert',
    'ExecutingAsyncJavascriptTest.throwsIfScriptTriggersAlertWhichTimesOut',
    'ExecutingJavascriptTest.testShouldThrowExceptionIfExecutingOnNoPage',
    'ExecutingJavascriptTest.testShouldThrowAnExceptionWithMessageAndStacktraceWhenTheJavascriptIsBad',
    'FormHandlingTest.testShouldNotBeAbleToSubmitAFormThatDoesNotExist',
    'FrameSwitchingTest.testShouldNotBeAbleToDoAnythingTheFrameIsDeletedFromUnderUs',
    # Disabled until https://code.google.com/p/chromedriver/issues/detail?id=345 is fixed.
    'I18nTest.*',
    '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.*',
    'SvgDocumentTest.testClickOnSvgElement',
    'TakesScreenshotTest.testShouldCaptureScreenshot',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithLongX',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithLongY',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithTooLong',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithTooLongX',
    'TakesScreenshotTest.testShouldCaptureScreenshotWithTooLongY',
    'TextHandlingTest.testShouldNotReturnLtrMarks',
    'TextPagesTest.testShouldBeAbleToLoadASimplePageOfText',
    'TextPagesTest.testShouldThrowExceptionWhenAddingCookieToAPageThatIsNotHtml',
    'TypingTest.testGenerateKeyPressEventEvenWhenElementPreventsDefault',
    'TypingTest.testNonPrintableCharactersShouldWorkWithContentEditableOrDesignModeSet',
    'TypingTest.testShouldBeAbleToTypeIntoContentEditableElementWithExistingValue',
    'TypingTest.testShouldNotTypeIntoElementsThatPreventKeyDownEvents',
    'TypingTest.testTypingIntoAnIFrameWithContentEditableOrDesignModeSet',
    'UnexpectedAlertBehaviorTest.*',
    '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'] = [
    'AlertsTest.testHandlesTwoAlertsFromOneInteraction',
    'AlertsTest.testShouldAllowAUserToSetTheValueOfAPrompt',
    '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'] = [
    'AlertsTest.*',
    '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.handleFormWithJavascriptAction',
    '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.testAllPrintableKeys',
    'TypingTest.testFunctionKeys',
    'TypingTest.testLowerCaseAlphaKeys',
    'TypingTest.testNumericNonShiftKeys',
    'TypingTest.testNumericShiftKeys',
    'TypingTest.testShouldBeAbleToMixUpperAndLowerCaseLetters',
    'TypingTest.testShouldBeAbleToTypeCapitalLetters',
    'TypingTest.testShouldBeAbleToTypeIntoEmptyContentEditableElement',
    'TypingTest.testShouldBeAbleToTypeOnAnEmailInputField',
    'TypingTest.testShouldBeAbleToTypeQuoteMarks',
    'TypingTest.testShouldBeAbleToTypeTheAtCharacter',
    'TypingTest.testShouldBeAbleToUseArrowKeys',
    'TypingTest.testShouldFireFocusKeyEventsInTheRightOrder',
    'TypingTest.testShouldFireKeyDownEvents',
    'TypingTest.testShouldFireKeyPressEvents',
    'TypingTest.testShouldFireKeyUpEvents',
    'TypingTest.testShouldTypeIntoInputElementsThatHaveNoTypeAttribute',
    'TypingTest.testShouldTypeLowerCaseLetters',
    'TypingTest.testSpecialSpaceKeys',
    'TypingTest.testUppercaseAlphaKeys',
    'TypingTest.testWillSimulateAKeyDownWhenEnteringTextIntoInputElements',
    'TypingTest.testWillSimulateAKeyDownWhenEnteringTextIntoTextAreas',
    'TypingTest.testWillSimulateAKeyPressWhenEnteringTextIntoInputElements',
    'TypingTest.testWillSimulateAKeyPressWhenEnteringTextIntoTextAreas',
    'TypingTest.testWillSimulateAKeyUpWhenEnteringTextIntoInputElements',
    'TypingTest.testWillSimulateAKeyUpWhenEnteringTextIntoTextAreas',

    # 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
