1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import re
6
7from telemetry.page import page as page_module
8from telemetry.page import page_set as page_set_module
9
10
11def _CreateXpathFunction(xpath):
12  return ('document.evaluate("%s",'
13                             'document,'
14                             'null,'
15                             'XPathResult.FIRST_ORDERED_NODE_TYPE,'
16                             'null)'
17          '.singleNodeValue' % re.escape(xpath))
18
19
20class GmailComposeDiscardPage(page_module.Page):
21
22  """ Why: Compose and discard a new email """
23
24  def __init__(self, page_set):
25    super(GmailComposeDiscardPage, self).__init__(
26      url='https://mail.google.com/mail/',
27      page_set=page_set)
28    self.credentials_path = 'data/credentials.json'
29    self.credentials = 'google'
30    self.user_agent_type = 'desktop'
31
32  def RunNavigateSteps(self, action_runner):
33    action_runner.NavigateToPage(self)
34    action_runner.WaitForJavaScriptCondition(
35        'window.gmonkey !== undefined &&'
36        'document.getElementById("gb") !== null')
37
38  def ComposeClick(self, action_runner):
39    action_runner.ExecuteJavaScript('''
40      var button=document.evaluate('//div[text()="COMPOSE"]',
41          document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null)
42          .singleNodeValue;
43      var mousedownevent=new MouseEvent('mousedown',true,true,window,0,0,0,0,0,
44        false,false,false,false,0,null);
45      var mouseupevent=new MouseEvent('mouseup',true,true,window,0,0,0,0,0,
46        false,false,false,false,0,null);
47      button.dispatchEvent(mousedownevent);
48      button.dispatchEvent(mouseupevent);''')
49
50  def RunEndure(self, action_runner):
51    action_runner.WaitForElement(
52        element_function=_CreateXpathFunction('//div[text()="COMPOSE"]'))
53    self.ComposeClick(action_runner)
54    action_runner.Wait(1)
55    action_runner.WaitForElement(
56        'div[class~="oh"][data-tooltip="Discard draft"]')
57    action_runner.ClickElement('div[class~="oh"][data-tooltip="Discard draft"]')
58    action_runner.Wait(1)
59
60
61class GmailComposeDiscardPageSet(page_set_module.PageSet):
62
63  """
64  Description: Gmail endure test: compose and discard an email.
65  """
66
67  def __init__(self):
68    super(GmailComposeDiscardPageSet, self).__init__(
69      credentials_path='data/credentials.json',
70      user_agent_type='desktop')
71
72    self.AddPage(GmailComposeDiscardPage(self))
73