Source code

Revision control

Other Tools

1
# This Source Code Form is subject to the terms of the Mozilla Public
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5
import textwrap
6
7
from telemetry_harness.testcase import TelemetryTestCase
8
from telemetry_harness.ping_filters import (
9
MAIN_ENVIRONMENT_CHANGE_PING,
10
MAIN_SHUTDOWN_PING,
11
)
12
13
14
class TestSearchCounts(TelemetryTestCase):
15
"""Test for SEARCH_COUNTS across sessions."""
16
17
def get_default_search_engine(self):
18
"""Retrieve the identifier of the default search engine."""
19
20
script = """\
21
let [resolve] = arguments;
22
let searchService = Components.classes[
23
"@mozilla.org/browser/search-service;1"]
24
.getService(Components.interfaces.nsISearchService);
25
return searchService.init().then(function () {
26
resolve(searchService.defaultEngine.identifier);
27
});
28
"""
29
30
with self.marionette.using_context(self.marionette.CONTEXT_CHROME):
31
return self.marionette.execute_async_script(textwrap.dedent(script))
32
33
def setUp(self):
34
"""Set up the test case and store the identifier of the default
35
search engine, which is required for reading SEARCH_COUNTS from
36
keyed histograms in pings.
37
"""
38
super(TestSearchCounts, self).setUp()
39
self.search_engine = self.get_default_search_engine()
40
41
def test_search_counts(self):
42
"""Test for SEARCH_COUNTS across sessions."""
43
44
# Session S1, subsession 1:
45
# - Open browser
46
# - Open new tab
47
# - Perform search (awesome bar or search bar)
48
# - Restart browser in new session
49
50
self.search_in_new_tab("mozilla firefox")
51
52
ping1 = self.wait_for_ping(self.restart_browser, MAIN_SHUTDOWN_PING)
53
54
# Session S2, subsession 1:
55
# - Outcome 1
56
# - Received a main ping P1 for previous session
57
# - Ping base contents:
58
# - clientId should be set
59
# - sessionId should be set
60
# - subsessionId should be set
61
# - previousSessionId should not be set
62
# - previousSubsessionId should not be set
63
# - subSessionCounter should be 1
64
# - profileSubSessionCounter should be 1
65
# - reason should be "shutdown"
66
# - Other ping contents:
67
# - SEARCH_COUNTS values should match performed search action
68
69
client_id = ping1["clientId"]
70
self.assertIsValidUUID(client_id)
71
72
ping1_info = ping1["payload"]["info"]
73
self.assertEqual(ping1_info["reason"], "shutdown")
74
75
s1_session_id = ping1_info["sessionId"]
76
self.assertNotEqual(s1_session_id, "")
77
78
s1_s1_subsession_id = ping1_info["subsessionId"]
79
self.assertNotEqual(s1_s1_subsession_id, "")
80
81
self.assertIsNone(ping1_info["previousSessionId"])
82
self.assertIsNone(ping1_info["previousSubsessionId"])
83
self.assertEqual(ping1_info["subsessionCounter"], 1)
84
self.assertEqual(ping1_info["profileSubsessionCounter"], 1)
85
86
scalars1 = ping1["payload"]["processes"]["parent"]["scalars"]
87
self.assertNotIn(
88
"browser.engagement.window_open_event_count", scalars1
89
)
90
self.assertEqual(
91
scalars1["browser.engagement.tab_open_event_count"], 1
92
)
93
94
keyed_histograms1 = ping1["payload"]["keyedHistograms"]
95
search_counts1 = keyed_histograms1["SEARCH_COUNTS"][
96
"{}.urlbar".format(self.search_engine)
97
]
98
self.assertEqual(
99
search_counts1,
100
{
101
u"range": [1, 2],
102
u"bucket_count": 3,
103
u"histogram_type": 4,
104
u"values": {u"1": 0, u"0": 1},
105
u"sum": 1,
106
},
107
)
108
109
# - Install addon
110
# Session S2, subsession 2:
111
# - Outcome 2
112
# - Received a main ping P2 for previous subsession
113
# - Ping base contents:
114
# - clientId should be set to the same value
115
# - sessionId should be set to a new value
116
# - subsessionId should be set to a new value
117
# - previousSessionId should be set to P1s sessionId value
118
# - previousSubsessionId should be set to P1s subsessionId value
119
# - subSessionCounter should be 1
120
# - profileSubSessionCounter should be 2
121
# - reason should be "environment-change"
122
# - Other ping contents:
123
# - SEARCH_COUNTS values should not be in P2
124
# - Verify that there should be no listing for tab scalar as we started a new
125
# session
126
127
ping2 = self.wait_for_ping(
128
self.install_addon, MAIN_ENVIRONMENT_CHANGE_PING
129
)
130
131
self.assertEqual(ping2["clientId"], client_id)
132
133
ping2_info = ping2["payload"]["info"]
134
self.assertEqual(ping2_info["reason"], "environment-change")
135
136
s2_session_id = ping2_info["sessionId"]
137
self.assertNotEqual(s2_session_id, s1_session_id)
138
139
s2_s1_subsession_id = ping2_info["subsessionId"]
140
self.assertNotEqual(s2_s1_subsession_id, s1_s1_subsession_id)
141
142
self.assertEqual(ping2_info["previousSessionId"], s1_session_id)
143
self.assertEqual(
144
ping2_info["previousSubsessionId"], s1_s1_subsession_id
145
)
146
self.assertEqual(ping2_info["subsessionCounter"], 1)
147
self.assertEqual(ping2_info["profileSubsessionCounter"], 2)
148
149
scalars2 = ping2["payload"]["processes"]["parent"]["scalars"]
150
self.assertNotIn(
151
"browser.engagement.window_open_event_count", scalars2
152
)
153
self.assertNotIn("browser.engagement.tab_open_event_count", scalars2)
154
155
keyed_histograms2 = ping2["payload"]["keyedHistograms"]
156
self.assertNotIn("SEARCH_COUNTS", keyed_histograms2)
157
158
# - Perform Search
159
# - Restart Browser
160
161
self.search("mozilla telemetry")
162
self.search("python unittest")
163
self.search("python pytest")
164
165
ping3 = self.wait_for_ping(self.restart_browser, MAIN_SHUTDOWN_PING)
166
167
# Session S3, subsession 1:
168
# - Outcome 3
169
# - Received a main ping P3 for session 2, subsession 1
170
# - Ping base contents:
171
# - clientId should be set to the same value
172
# - sessionId should be set to P2s sessionId value
173
# - subsessionId should be set to a new value
174
# - previousSessionId should be set to P1s sessionId value
175
# - previousSubsessionId should be set to P2s subsessionId value
176
# - subSessionCounter should be 2
177
# - profileSubSessionCounter should be 3
178
# - reason should be "shutdown"
179
# - Other ping contents:
180
# - SEARCH_COUNTS values should be set per above search
181
182
self.assertEqual(ping3["clientId"], client_id)
183
184
ping3_info = ping3["payload"]["info"]
185
186
self.assertEqual(ping3_info["reason"], "shutdown")
187
188
self.assertEqual(ping3_info["sessionId"], s2_session_id)
189
190
s2_s2_subsession_id = ping3_info["subsessionId"]
191
self.assertNotEqual(s2_s2_subsession_id, s1_s1_subsession_id)
192
self.assertNotEqual(s2_s2_subsession_id, s2_s1_subsession_id)
193
194
self.assertEqual(ping3_info["previousSessionId"], s1_session_id)
195
self.assertEqual(
196
ping3_info["previousSubsessionId"], s2_s1_subsession_id
197
)
198
self.assertEqual(ping3_info["subsessionCounter"], 2)
199
self.assertEqual(ping3_info["profileSubsessionCounter"], 3)
200
201
scalars3 = ping3["payload"]["processes"]["parent"]["scalars"]
202
self.assertNotIn(
203
"browser.engagement.window_open_event_count", scalars3
204
)
205
self.assertNotIn("browser.engagement.tab_open_event_count", scalars3)
206
207
keyed_histograms3 = ping3["payload"]["keyedHistograms"]
208
search_counts3 = keyed_histograms3["SEARCH_COUNTS"][
209
"{}.urlbar".format(self.search_engine)
210
]
211
self.assertEqual(
212
search_counts3,
213
{
214
u"range": [1, 2],
215
u"bucket_count": 3,
216
u"histogram_type": 4,
217
u"values": {u"1": 0, u"0": 3},
218
u"sum": 3,
219
},
220
)