<commit_before>import { Log, Pipeline, UiFinder, Step, Assertions, Keys } from '@ephox/agar';
import { UnitTest } from '@ephox/bedrock';
import { TinyActions, TinyApis, TinyLoader } from '@ephox/mcagar';
import { Body, Css, Element, Scroll } from '@ephox/sugar';
import Editor from 'tinymce/core/api/Editor';
import SilverTheme from 'tinymce/themes/silver/Theme';

UnitTest.asynctest('IFrame editor ContextToolbar Position test', (success, failure) => {
  SilverTheme();

  interface Scenario {
    label: string;
    content: string;
    contentStyles?: string;
    cursor: {
      elementPath: number[];
      offset: number;
    };
    classes: string;
  }

  const imageSrc = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFElEQVR42mNk+A+ERADGUYX0VQgAXAYT9xTSUocAAAAASUVORK5CYII=';

  TinyLoader.setup(
    (editor, onSuccess, onFailure) => {
      const tinyApis = TinyApis(editor);
      const tinyActions = TinyActions(editor);

      const sScrollTo = (x: number, y: number) => Step.sync(() => Scroll.to(x, y, Element.fromDom(editor.getDoc())));

      const sAssertPosition = (position: string, value: number, diff = 5) => Step.sync(() => {
        UiFinder.findIn(Body.body(), '.tox-pop').each((ele) => {
          const styles = parseInt(Css.getRaw(ele, position).getOr('0').replace('px', ''), 10);
          Assertions.assertEq(`Assert toolbar position - ${position} ${styles}px ~= ${value}px`, true, Math.abs(styles - value) <= diff);
        });
      });

      const sTestPositionWhileScrolling = (scenario: Scenario) => {
        return Log.stepsAsStep('TBA', scenario.label, [
          tinyApis.sSetContent(
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>' +
            `<p style="height: 25px;${scenario.contentStyles || ''}">${scenario.content}</p>` +
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>'
          ),
          sScrollTo(0, 200),
          tinyApis.sSetCursor(scenario.cursor.elementPath, scenario.cursor.offset),
          UiFinder.sWaitForVisible('Waiting for toolbar to appear above content', Body.body(), '.tox-pop.tox-pop--bottom' + scenario.classes),
          sAssertPosition('bottom', 232),

          // Position the link at the top of the viewport, just below the toolbar
          sScrollTo(0, 300),<commit_msg>Fixed the `ContextToolbarIframePositionTest` sometimes failing on Internet Explorer<commit_after>import { Log, Pipeline, UiFinder, Step, Assertions, Keys } from '@ephox/agar';
import { UnitTest } from '@ephox/bedrock';
import { TinyActions, TinyApis, TinyLoader } from '@ephox/mcagar';
import { Body, Css, Element, Scroll } from '@ephox/sugar';
import Editor from 'tinymce/core/api/Editor';
import SilverTheme from 'tinymce/themes/silver/Theme';

UnitTest.asynctest('IFrame editor ContextToolbar Position test', (success, failure) => {
  SilverTheme();

  interface Scenario {
    label: string;
    content: string;
    contentStyles?: string;
    cursor: {
      elementPath: number[];
      offset: number;
    };
    classes: string;
  }

  const imageSrc = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFElEQVR42mNk+A+ERADGUYX0VQgAXAYT9xTSUocAAAAASUVORK5CYII=';

  TinyLoader.setup(
    (editor, onSuccess, onFailure) => {
      const tinyApis = TinyApis(editor);
      const tinyActions = TinyActions(editor);

      const sScrollTo = (x: number, y: number) => Step.sync(() => Scroll.to(x, y, Element.fromDom(editor.getDoc())));

      const sAssertPosition = (position: string, value: number, diff = 5) => Step.sync(() => {
        UiFinder.findIn(Body.body(), '.tox-pop').each((ele) => {
          const styles = parseInt(Css.getRaw(ele, position).getOr('0').replace('px', ''), 10);
          Assertions.assertEq(`Assert toolbar position - ${position} ${styles}px ~= ${value}px`, true, Math.abs(styles - value) <= diff);
        });
      });

      const sTestPositionWhileScrolling = (scenario: Scenario) => {
        return Log.stepsAsStep('TBA', scenario.label, [
          tinyApis.sSetContent(
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>' +
            `<p style="height: 25px;${scenario.contentStyles || ''}">${scenario.content}</p>` +
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>' +
            '<p style="height: 100px"></p>'
          ),
          tinyApis.sFocus,
          sScrollTo(0, 200),
          tinyApis.sSetCursor(scenario.cursor.elementPath, scenario.cursor.offset),
          UiFinder.sWaitForVisible('Waiting for toolbar to appear above content', Body.body(), '.tox-pop.tox-pop--bottom' + scenario.classes),
          sAssertPosition('bottom', 232),

          // Position the link at the top of the viewport, just below the toolbar
          sScrollTo(0, 300),
{'fraction_non_alphanumeric': 0.158577990812862, 'fraction_numerical': 0.01757539444777312, 'mean_word_length': 3.265758091993186, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3756974', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1598, 'n_words': 341}