profile_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. var assert = require('assert'),
  19. fs = require('fs'),
  20. path = require('path');
  21. var promise = require('../..').promise,
  22. Profile = require('../../firefox/profile').Profile,
  23. decode = require('../../firefox/profile').decode,
  24. loadUserPrefs = require('../../firefox/profile').loadUserPrefs,
  25. io = require('../../io');
  26. var JETPACK_EXTENSION = path.join(__dirname,
  27. '../../lib/test/data/firefox/jetpack-sample.xpi');
  28. var NORMAL_EXTENSION = path.join(__dirname,
  29. '../../lib/test/data/firefox/sample.xpi');
  30. var WEBEXTENSION_EXTENSION = path.join(__dirname,
  31. '../../lib/test/data/firefox/webextension.xpi');
  32. var JETPACK_EXTENSION_ID = 'jid1-EaXX7k0wwiZR7w@jetpack.xpi';
  33. var NORMAL_EXTENSION_ID = 'sample@seleniumhq.org';
  34. var WEBEXTENSION_EXTENSION_ID = 'webextensions-selenium-example@example.com';
  35. describe('Profile', function() {
  36. describe('setPreference', function() {
  37. it('allows setting custom properties', function() {
  38. var profile = new Profile();
  39. assert.equal(undefined, profile.getPreference('foo'));
  40. profile.setPreference('foo', 'bar');
  41. assert.equal('bar', profile.getPreference('foo'));
  42. });
  43. it('allows overriding mutable properties', function() {
  44. var profile = new Profile();
  45. profile.setPreference('browser.newtab.url', 'http://www.example.com');
  46. assert.equal('http://www.example.com',
  47. profile.getPreference('browser.newtab.url'));
  48. });
  49. });
  50. describe('writeToDisk', function() {
  51. it('copies template directory recursively', function() {
  52. var templateDir;
  53. return io.tmpDir().then(function(td) {
  54. templateDir = td;
  55. var foo = path.join(templateDir, 'foo');
  56. fs.writeFileSync(foo, 'Hello, world');
  57. var bar = path.join(templateDir, 'subfolder/bar');
  58. fs.mkdirSync(path.dirname(bar));
  59. fs.writeFileSync(bar, 'Goodbye, world!');
  60. return new Profile(templateDir).writeToDisk();
  61. }).then(function(profileDir) {
  62. assert.notEqual(profileDir, templateDir);
  63. assert.equal('Hello, world',
  64. fs.readFileSync(path.join(profileDir, 'foo')));
  65. assert.equal('Goodbye, world!',
  66. fs.readFileSync(path.join(profileDir, 'subfolder/bar')));
  67. });
  68. });
  69. it('does not copy lock files', function() {
  70. return io.tmpDir().then(function(dir) {
  71. fs.writeFileSync(path.join(dir, 'parent.lock'), 'lock');
  72. fs.writeFileSync(path.join(dir, 'lock'), 'lock');
  73. fs.writeFileSync(path.join(dir, '.parentlock'), 'lock');
  74. return new Profile(dir).writeToDisk();
  75. }).then(function(dir) {
  76. assert.ok(fs.existsSync(dir));
  77. assert.ok(!fs.existsSync(path.join(dir, 'parent.lock')));
  78. assert.ok(!fs.existsSync(path.join(dir, 'lock')));
  79. assert.ok(!fs.existsSync(path.join(dir, '.parentlock')));
  80. });
  81. });
  82. describe('user.js', function() {
  83. it('merges template user.js into preferences', function() {
  84. return io.tmpDir().then(function(dir) {
  85. fs.writeFileSync(path.join(dir, 'user.js'), [
  86. 'user_pref("browser.newtab.url", "http://www.example.com")',
  87. 'user_pref("dom.max_script_run_time", 1234)'
  88. ].join('\n'));
  89. return new Profile(dir).writeToDisk();
  90. }).then(function(profile) {
  91. return loadUserPrefs(path.join(profile, 'user.js'));
  92. }).then(function(prefs) {
  93. assert.equal('http://www.example.com', prefs['browser.newtab.url']);
  94. assert.equal(1234, prefs['dom.max_script_run_time']);
  95. });
  96. });
  97. });
  98. describe('extensions', function() {
  99. it('are copied into new profile directory', function() {
  100. var profile = new Profile();
  101. profile.addExtension(JETPACK_EXTENSION);
  102. profile.addExtension(NORMAL_EXTENSION);
  103. profile.addExtension(WEBEXTENSION_EXTENSION);
  104. return profile.writeToDisk().then(function(dir) {
  105. dir = path.join(dir, 'extensions');
  106. assertExists(JETPACK_EXTENSION_ID);
  107. assertExists(NORMAL_EXTENSION_ID);
  108. assertExists(WEBEXTENSION_EXTENSION_ID + '.xpi');
  109. function assertExists(file) {
  110. assert.ok(
  111. fs.existsSync(path.join(dir, file)),
  112. `expected ${file} to exist`);
  113. }
  114. });
  115. });
  116. });
  117. });
  118. });