summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/emulation/browser_setUserAgentOverride.js
blob: a0f292435d52c7c2b211d449cbce0ec149e30a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const DOC = toDataURL(`<script>document.write(navigator.userAgent);</script>`);

add_task(async function invalidPlatform({ client }) {
  const { Emulation } = client;
  const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";

  for (const platform of [null, true, 1, [], {}]) {
    await Assert.rejects(
      Emulation.setUserAgentOverride({ userAgent, platform }),
      /platform: string value expected/,
      `Fails with invalid type: ${platform}`
    );
  }
});

add_task(async function setAndResetUserAgent({ client }) {
  const { Emulation } = client;
  const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";

  await loadURL(DOC);
  const originalUserAgent = await getNavigatorProperty("userAgent");

  isnot(originalUserAgent, userAgent, "Custom user agent hasn't been set");

  await Emulation.setUserAgentOverride({ userAgent });
  await loadURL(DOC);
  is(
    await getNavigatorProperty("userAgent"),
    userAgent,
    "Custom user agent has been set"
  );

  await Emulation.setUserAgentOverride({ userAgent: "" });
  await loadURL(DOC);
  is(
    await getNavigatorProperty("userAgent"),
    originalUserAgent,
    "Custom user agent has been reset"
  );
});

add_task(async function invalidUserAgent({ client }) {
  const { Emulation } = client;
  const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";

  await loadURL(DOC);
  isnot(
    await getNavigatorProperty("userAgent"),
    userAgent,
    "Custom user agent hasn't been set"
  );

  await Assert.rejects(
    Emulation.setUserAgentOverride({ userAgent }),
    err => err.message.includes("Invalid characters found in userAgent"),
    "Invalid user agent format raised error"
  );
});

add_task(async function setAndResetPlatform({ client }) {
  const { Emulation } = client;
  const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
  const platform = "foobar";

  await loadURL(DOC);
  const originalUserAgent = await getNavigatorProperty("userAgent");
  const originalPlatform = await getNavigatorProperty("platform");

  isnot(userAgent, originalUserAgent, "Custom user agent hasn't been set");
  isnot(platform, originalPlatform, "Custom platform hasn't been set");

  await Emulation.setUserAgentOverride({ userAgent, platform });
  await loadURL(DOC);
  is(
    await getNavigatorProperty("userAgent"),
    userAgent,
    "Custom user agent has been set"
  );
  is(
    await getNavigatorProperty("platform"),
    platform,
    "Custom platform has been set"
  );

  await Emulation.setUserAgentOverride({ userAgent: "", platform: "" });
  await loadURL(DOC);
  is(
    await getNavigatorProperty("userAgent"),
    originalUserAgent,
    "Custom user agent has been reset"
  );
  is(
    await getNavigatorProperty("platform"),
    originalPlatform,
    "Custom platform has been reset"
  );
});

add_task(async function notSetForNewContext({ client }) {
  const { Emulation, Target } = client;
  const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
  const platform = "foobar";

  await Emulation.setUserAgentOverride({ userAgent, platform });
  await loadURL(DOC);
  is(
    await getNavigatorProperty("userAgent"),
    userAgent,
    "Custom user agent has been set"
  );
  is(
    await getNavigatorProperty("platform"),
    platform,
    "Custom platform has been set"
  );

  await openTab(Target, { activate: true });

  isnot(
    await getNavigatorProperty("userAgent"),
    userAgent,
    "Custom user agent has not been set"
  );
  isnot(
    await getNavigatorProperty("platform"),
    platform,
    "Custom platform has not been set"
  );
});

async function getNavigatorProperty(prop) {
  return SpecialPowers.spawn(gBrowser.selectedBrowser, [prop], _prop => {
    return content.navigator[_prop];
  });
}