function setUp()

in javascript/widgets/handler/testhelper.js [189:403]


function setUp() {
  // Used to initialize internal auth instance.
  firebase = {};
  firebase.initializeApp = function(options, name) {
    return new firebaseui.auth.testing.FakeAppClient(options, name);
  };
  // Developer provided auth instance.
  externalAuthApp = new firebaseui.auth.testing.FakeAppClient();
  // Install developer provided auth instance.
  externalAuth = externalAuthApp.auth().install();
  app = new firebaseui.auth.AuthUI(externalAuth, appId);
  // Install internal temporary auth instance.
  testAuth = app.getAuth().install();

  mockClock.install();

  // For browsers that do not support CORS which rely on gapi for XHR, simulate
  // this capability so as to test XHR requests and responses properly.
  testStubs.set(firebaseui.auth.util, 'supportsCors', function() {
    return true;
  });

  testStubs.replace(firebaseui.auth.idp, 'getAuthCredential',
                    createMockCredential);
  // Build mock auth providers.
  firebase['auth'] = {};
  // Mock reCAPTCHA verifier.
  firebase.auth.RecaptchaVerifier = function(container, params, app) {
    // Install on initialization.
    recaptchaVerifierInstance =
        new firebaseui.auth.testing.RecaptchaVerifier(container, params, app);
    recaptchaVerifierInstance.install();
    return recaptchaVerifierInstance;
  };
  for (var key in firebaseui.auth.idp.AuthProviders) {
    firebase['auth'][firebaseui.auth.idp.AuthProviders[key]] = function() {
      this.scopes = [];
      this.customParameters = {};
    };
    firebase['auth'][firebaseui.auth.idp.AuthProviders[key]].PROVIDER_ID = key;
    for (var method in firebaseui.auth.idp.SignInMethods[key]) {
      firebase['auth'][firebaseui.auth.idp.AuthProviders[key]][method] =
          firebaseui.auth.idp.SignInMethods[key][method];
    }
    if (key != 'twitter.com' && key != 'password') {
      firebase['auth'][firebaseui.auth.idp.AuthProviders[key]]
          .prototype.addScope = function(scope) {
        this.scopes.push(scope);
      };
    }
    if (key != 'password') {
      // Record setCustomParameters for all OAuth providers.
      firebase['auth'][firebaseui.auth.idp.AuthProviders[key]]
          .prototype.setCustomParameters = function(customParameters) {
        this.customParameters = customParameters;
      };
    }
  }
  firebase['auth']['SAMLAuthProvider'] = function(providerId) {
    this.providerId = providerId;
    this.customParameters = {};
  };
  firebase['auth']['SAMLAuthProvider'].prototype.setCustomParameters =
      function(customParameters) {
    this.customParameters = customParameters;
    return this;
  };
  firebase['auth']['OAuthProvider'] = function(providerId) {
    this.providerId = providerId;
    this.scopes = [];
    this.customParameters = {};
  };
  firebase['auth']['OAuthProvider'].prototype.setCustomParameters =
      function(customParameters) {
    this.customParameters = customParameters;
    return this;
  };
  firebase['auth']['OAuthProvider'].prototype.addScope =
      function(scope) {
    this.scopes.push(scope);
    return this;
  };
  // Initialize mock credentials.
  authCredential = createMockCredential(
      {'accessToken': 'facebookAccessToken', 'providerId': 'facebook.com'});
  federatedCredential = createMockCredential(
      {'accessToken': 'googleAccessToken', 'providerId': 'google.com'});
  // Simulate email auth provider credential.
  firebase['auth']['EmailAuthProvider'] =
      firebase['auth']['EmailAuthProvider'] || {};
  firebase['auth']['EmailAuthProvider']['credential'] = function(
      email, password) {
    return {
      'email': email, 'password': password, 'providerId': 'password',
      'signInMethod': 'password'
    };
  };
  firebase['auth']['EmailAuthProvider']['credentialWithLink'] = function(
      email, link) {
    return {
      'email': email, 'link': link, 'providerId': 'password',
      'signInMethod': 'emailLink'
    };
  };
  // Simulate Google Auth Provider credential.
  firebase['auth']['GoogleAuthProvider'] =
      firebase['auth']['GoogleAuthProvider'] || {};
  firebase['auth']['GoogleAuthProvider']['credential'] = function(
      idToken, accessToken) {
    return createMockCredential({
      'idToken': idToken,
      'accessToken': accessToken,
      'providerId': 'google.com'
    });
  };
  firebase['auth']['AuthCredential'] = {
    'fromJSON': function(json) {
      return createMockCredential(json);
    }
  };
  getApp = function() {
    return app;
  };
  // Assume widget already rendered and AuthUI global reference set.
  testStubs.replace(firebaseui.auth.AuthUI, 'getAuthUi', function() {
    return app;
  });

  // Mock dialog polyfill.
  window['dialogPolyfill'] = {
    'registerDialog': function(dialog) {
      dialog.open = false;
      dialog.showModal = function() {
        dialog.open = true;
      };
      dialog.close = function() {
        dialog.open = false;
      };
    }
  };
  container = goog.dom.createDom(goog.dom.TagName.DIV);
  document.body.appendChild(container);
  // Test component used for setLoggedIn tests which requires a component.
  container2 = goog.dom.createDom(goog.dom.TagName.DIV);
  document.body.appendChild(container2);
  testComponent = new firebaseui.auth.ui.page.Base(function() {
    return '<div></div>';
  });
  // Render test component in container2.
  testComponent.render(container2);
  testUtil = new firebaseui.auth.testing.FakeUtil().install();
  signInCallbackUser = undefined;
  signInCallbackRedirectUrl = undefined;
  signInCallbackCredential = undefined;
  signInCallbackAdditionalUserInfo = undefined;
  signInCallbackOperationType = undefined;
  uiShownCallbackCount = 0;
  // Define recorded signInFailure callback.
  signInFailureCallback = goog.testing.recordFunction(function() {
    return goog.Promise.resolve();
  });
  tosCallback = goog.testing.recordFunction();
  app.setConfig({
    'signInSuccessUrl': 'http://localhost/home',
    'widgetUrl': 'http://localhost/firebaseui-widget',
    'signInOptions': ['google.com', 'facebook.com', 'password', 'github.com',
                      {
                        'provider': 'microsoft.com',
                        'loginHintKey': 'login_hint',
                        'buttonColor': '#2F2F2F',
                        'iconUrl': '<icon-url>'
                      },
                      {
                        'provider': 'saml.provider',
                        'providerName': 'SAML Provider',
                        'buttonColor': '#2F2F2F',
                        'iconUrl': '<icon-url>'
                      }],
    'siteName': 'Test Site',
    'popupMode': false,
    'tosUrl': tosCallback,
    'privacyPolicyUrl': 'http://localhost/privacy_policy',
    'credentialHelper':
        firebaseui.auth.widget.Config.CredentialHelper.NONE,
    'callbacks': {
      'signInFailure': signInFailureCallback
    },

  });
  // Install mock cookie storage.
  testCookieStorage = new firebaseui.auth.testing.FakeCookieStorage().install();
  window.localStorage.clear();
  window.sessionStorage.clear();
  // Remove any grecaptcha mocks.
  delete goog.global['grecaptcha'];
  // Record all calls to One-Tap show and cancel APIs.
  testStubs.replace(
      firebaseui.auth.AuthUI.prototype,
      'showOneTapSignIn',
      goog.testing.recordFunction());
  testStubs.replace(
      firebaseui.auth.AuthUI.prototype,
      'cancelOneTapSignIn',
      goog.testing.recordFunction());
  // Record calls to revertLanguageCode.
  lastRevertLanguageCodeCall = null;
  testStubs.replace(
      firebaseui.auth.AuthUI.prototype,
      'revertLanguageCode',
      function() {
        lastRevertLanguageCodeCall = this;
      });
  pageEventDispatcher = new firebaseui.auth.EventDispatcher(container);
  pageEventDispatcher.register();
}