# 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.
import os
import unittest

from telemetry.core import browser_finder
from telemetry.test import simple_mock
from telemetry.test import options_for_unittests

_ = simple_mock.DONT_CARE

class FormBasedCredentialsBackendUnitTestBase(unittest.TestCase):
  def setUp(self):
    self._credentials_type = None

  def testRealLoginIfPossible(self):
    credentials_path = os.path.join(
      os.path.dirname(__file__),
      '..', '..', 'perf', 'data', 'credentials.json')
    if not os.path.exists(credentials_path):
      return

    options = options_for_unittests.GetCopy()
    with browser_finder.FindBrowser(options).Create() as b:
      b.credentials.credentials_path = credentials_path
      if not b.credentials.CanLogin(self._credentials_type):
        return
      ret = b.credentials.LoginNeeded(b.tabs[0], self._credentials_type)
      self.assertTrue(ret)

  def testRealLoginWithDontOverrideProfileIfPossible(self):
    credentials_path = os.path.join(
      os.path.dirname(__file__),
      '..', '..', 'perf', 'data', 'credentials.json')
    if not os.path.exists(credentials_path):
      return

    options = options_for_unittests.GetCopy()

    # Login once to make sure our default profile is logged in.
    with browser_finder.FindBrowser(options).Create() as b:
      b.credentials.credentials_path = credentials_path

      if not b.credentials.CanLogin(self._credentials_type):
        return

      tab = b.tabs[0]

      # Should not be logged in, since this is a fresh credentials
      # instance.
      self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))

      # Log in.
      ret = b.credentials.LoginNeeded(tab, self._credentials_type)

      # Make sure login was successful.
      self.assertTrue(ret)
      self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))

      # Reset state. Now the backend thinks we're logged out, even
      # though we are logged in in our current browser session. This
      # simulates the effects of running with --dont-override-profile.
      b.credentials._ResetLoggedInState() # pylint: disable=W0212

      # Make sure the backend thinks we're logged out.
      self.assertFalse(b.credentials.IsLoggedIn(self._credentials_type))
      self.assertTrue(b.credentials.CanLogin(self._credentials_type))

      # Attempt to login again. This should detect that we've hit
      # the 'logged in' page instead of the login form, and succeed
      # instead of timing out.
      ret = b.credentials.LoginNeeded(tab, self._credentials_type)

      # Make sure our login attempt did in fact succeed and set the
      # backend's internal state to 'logged in'.
      self.assertTrue(ret)
      self.assertTrue(b.credentials.IsLoggedIn(self._credentials_type))

  def testLoginUsingMock(self):
    raise NotImplementedError()

  def _LoginUsingMock(self, backend, login_page_url, email_element_id,
                      password_element_id): # pylint: disable=R0201
    tab = simple_mock.MockObject()

    config = {'username': 'blah',
              'password': 'blargh'}

    tab.ExpectCall('Navigate', login_page_url)
    tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
    tab.ExpectCall('EvaluateJavaScript', _).WillReturn(True)
    tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)
    tab.ExpectCall('WaitForDocumentReadyStateToBeInteractiveOrBetter')

    def VerifyEmail(js):
      assert email_element_id in js
      assert 'blah' in js
    tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail)

    def VerifyPw(js):
      assert password_element_id in js
      assert 'largh' in js
    tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw)

    def VerifySubmit(js):
      assert '.submit' in js
    tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit)

    # Checking for form still up.
    tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)

    backend.LoginNeeded(tab, config)

@@ -159,16 +159,31 @@
 E file.%0A
+import logging%0A
 import o
@@ -359,16 +359,312 @@
 T_CARE%0A%0A
+%0Adef _GetCredentialsPath():%0A  # TODO: This shouldn't depend on tools/perf.%0A  credentials_path = os.path.join(%0A      os.path.dirname(__file__),%0A      '..', '..', '..', '..', 'perf', 'data', 'credentials.json')%0A  if not os.path.exists(credentials_path):%0A    return None%0A  return credentials_path%0A%0A%0A
 class Fo
@@ -839,153 +839,126 @@
 h = 
-os.path.join(%0A      os.path.dirname(__file__),%0A      '..', '..', 'perf', 'data', 'credentials.json')%0A    if not os.path.exists(credentials_path):
+_GetCredentialsPath()%0A    if not credentials_path:%0A      logging.warning('Credentials file not found, skipping test.')
 %0A   
@@ -1394,153 +1394,126 @@
 h = 
-os.path.join(%0A      os.path.dirname(__file__),%0A      '..', '..', 'perf', 'data', 'credentials.json')%0A    if not os.path.exists(credentials_path):
+_GetCredentialsPath()%0A    if not credentials_path:%0A      logging.warning('Credentials file not found, skipping test.')
 %0A   

Fix real login unit tests.