1 # Copyright 2015 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.
8 from telemetry
.core
import util
9 from telemetry
.page
import page
as page_module
10 from telemetry
.page
import shared_page_state
11 from telemetry
import story
13 util
.AddDirToPythonPath(util
.GetChromiumSrcDir(), 'build', 'android')
14 from pylib
.constants
import keyevent
# pylint: disable=import-error
15 from pylib
.device
import intent
# pylint: disable=import-error
34 class ForegroundPage(page_module
.Page
):
35 """Take a measurement after loading a regular webpage."""
37 def __init__(self
, story_set
, name
, url
):
38 super(ForegroundPage
, self
).__init
__(
39 url
=url
, page_set
=story_set
, name
=name
,
40 shared_page_state_class
=shared_page_state
.SharedMobilePageState
)
41 self
.archive_data_file
= story_set
.archive_data_file
43 def _TakeMemoryMeasurement(self
, action_runner
, phase
):
44 android_platform
= action_runner
.tab
.browser
.platform
45 with action_runner
.CreateInteraction(phase
):
46 action_runner
.Wait(DUMP_WAIT_TIME
)
47 action_runner
.ForceGarbageCollection()
48 android_platform
.RelaxMemory()
49 action_runner
.Wait(DUMP_WAIT_TIME
)
50 if not action_runner
.tab
.browser
.DumpMemory():
51 logging
.error('Unable to get a memory dump for %s.', self
.name
)
53 def RunPageInteractions(self
, action_runner
):
54 action_runner
.tab
.WaitForDocumentReadyStateToBeComplete()
55 self
._TakeMemoryMeasurement
(action_runner
, 'foreground')
58 class BackgroundPage(ForegroundPage
):
59 """Take a measurement while Chrome is in the background."""
61 def __init__(self
, story_set
, name
):
62 super(BackgroundPage
, self
).__init
__(story_set
, name
, 'about:blank')
64 def RunPageInteractions(self
, action_runner
):
65 action_runner
.tab
.WaitForDocumentReadyStateToBeComplete()
67 # Launch clock app, pushing Chrome to the background.
68 android_platform
= action_runner
.tab
.browser
.platform
69 android_platform
.LaunchAndroidApplication(
70 intent
.Intent(package
='com.google.android.deskclock',
71 activity
='com.android.deskclock.DeskClock'),
72 app_has_webviews
=False)
75 self
._TakeMemoryMeasurement
(action_runner
, 'background')
78 android_platform
.android_action_runner
.InputKeyEvent(keyevent
.KEYCODE_BACK
)
81 class MemoryHealthStory(story
.StorySet
):
82 """User story for the Memory Health Plan."""
85 super(MemoryHealthStory
, self
).__init
__(
86 archive_data_file
='data/memory_health_plan.json',
87 cloud_storage_bucket
=story
.PARTNER_BUCKET
)
90 # We name pages so their foreground/background counterparts are easy
91 # to identify. For example 'http://google.com' becomes
92 # 'http_google_com' and 'after_http_google_com' respectively.
93 name
= re
.sub('\W+', '_', url
)
94 self
.AddStory(ForegroundPage(self
, name
, url
))
95 self
.AddStory(BackgroundPage(self
, 'after_' + name
))