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
.page
import page
as page_module
9 from telemetry
.page
import shared_page_state
10 from telemetry
import story
12 from pylib
.constants
import keyevent
# pylint: disable=import-error
13 from pylib
.device
import intent
# pylint: disable=import-error
32 class ForegroundPage(page_module
.Page
):
33 """Take a measurement after loading a regular webpage."""
35 def __init__(self
, story_set
, name
, url
):
36 super(ForegroundPage
, self
).__init
__(
37 url
=url
, page_set
=story_set
, name
=name
,
38 shared_page_state_class
=shared_page_state
.SharedMobilePageState
)
39 self
.archive_data_file
= story_set
.archive_data_file
41 def _TakeMemoryMeasurement(self
, action_runner
, phase
):
42 android_platform
= action_runner
.tab
.browser
.platform
43 with action_runner
.CreateInteraction(phase
):
44 action_runner
.Wait(DUMP_WAIT_TIME
)
45 action_runner
.ForceGarbageCollection()
46 android_platform
.RelaxMemory()
47 action_runner
.Wait(DUMP_WAIT_TIME
)
48 if not action_runner
.tab
.browser
.DumpMemory():
49 logging
.error('Unable to get a memory dump for %s.', self
.name
)
51 def RunPageInteractions(self
, action_runner
):
52 action_runner
.tab
.WaitForDocumentReadyStateToBeComplete()
53 self
._TakeMemoryMeasurement
(action_runner
, 'foreground')
56 class BackgroundPage(ForegroundPage
):
57 """Take a measurement while Chrome is in the background."""
59 def __init__(self
, story_set
, name
):
60 super(BackgroundPage
, self
).__init
__(story_set
, name
, 'about:blank')
62 def RunPageInteractions(self
, action_runner
):
63 action_runner
.tab
.WaitForDocumentReadyStateToBeComplete()
65 # Launch clock app, pushing Chrome to the background.
66 android_platform
= action_runner
.tab
.browser
.platform
67 android_platform
.LaunchAndroidApplication(
68 intent
.Intent(package
='com.google.android.deskclock',
69 activity
='com.android.deskclock.DeskClock'),
70 app_has_webviews
=False)
73 self
._TakeMemoryMeasurement
(action_runner
, 'background')
76 android_platform
.android_action_runner
.InputKeyEvent(keyevent
.KEYCODE_BACK
)
79 class MemoryHealthStory(story
.StorySet
):
80 """User story for the Memory Health Plan."""
83 super(MemoryHealthStory
, self
).__init
__(
84 archive_data_file
='data/memory_health_plan.json',
85 cloud_storage_bucket
=story
.PARTNER_BUCKET
)
88 # We name pages so their foreground/background counterparts are easy
89 # to identify. For example 'http://google.com' becomes
90 # 'http_google_com' and 'after_http_google_com' respectively.
91 name
= re
.sub('\W+', '_', url
)
92 self
.AddStory(ForegroundPage(self
, name
, url
))
93 self
.AddStory(BackgroundPage(self
, 'after_' + name
))