diff --git a/app/index.js b/app/index.js index cdb776870fa..4515ce2aeed 100644 --- a/app/index.js +++ b/app/index.js @@ -28,6 +28,7 @@ var locale = require('./locale') const Immutable = require('immutable') const electron = require('electron') +const path = require('path') const BrowserWindow = electron.BrowserWindow const dialog = electron.dialog const ipcMain = electron.ipcMain @@ -64,6 +65,14 @@ const contentSettings = require('../js/state/contentSettings') const privacy = require('../js/state/privacy') const basicAuth = require('./browser/basicAuth') +if (!process.env.BRAVE_USER_DATA_DIR && ['development', 'test'].includes(process.env.NODE_ENV)) { + process.env.BRAVE_USER_DATA_DIR = path.join(app.getPath('appData'), app.getName() + '-' + process.env.NODE_ENV) +} + +if (process.env.BRAVE_USER_DATA_DIR) { + app.setPath('userData', process.env.BRAVE_USER_DATA_DIR) +} + // Used to collect the per window state when shutting down the application let perWindowState = [] let sessionStateStoreCompleteOnQuit = false diff --git a/app/sessionStore.js b/app/sessionStore.js index 002163dfc3b..89a6ecf9d2c 100644 --- a/app/sessionStore.js +++ b/app/sessionStore.js @@ -23,17 +23,13 @@ const sessionStorageVersion = 1 const filtering = require('./filtering') // const tabState = require('./common/state/tabState') -let suffix = '' -if (process.env.NODE_ENV === 'development') { - suffix = '-dev' -} -const sessionStorageName = `session-store-${sessionStorageVersion}${suffix}` -const storagePath = process.env.NODE_ENV !== 'test' - ? path.join(app.getPath('userData'), sessionStorageName) - : path.join(process.env.HOME, '.brave-test-session-store-1') const getSetting = require('../js/settings').getSetting const promisify = require('../js/lib/promisify') +const sessionStorageName = `session-store-${sessionStorageVersion}` +const getStoragePath = () => { + return path.join(app.getPath('userData'), sessionStorageName) +} /** * Saves the specified immutable browser state to storage. * @@ -78,7 +74,7 @@ module.exports.saveAppState = (payload, isShutdown) => { : path.join(process.env.HOME, '.brave-test-session-store-tmp-' + epochTimestamp) let p = promisify(fs.writeFile, tmpStoragePath, JSON.stringify(payload)) - .then(() => promisify(fs.rename, tmpStoragePath, storagePath)) + .then(() => promisify(fs.rename, tmpStoragePath, getStoragePath())) if (isShutdown) { p = p.then(module.exports.cleanSessionDataOnShutdown()) } @@ -314,7 +310,7 @@ module.exports.loadAppState = () => { return new Promise((resolve, reject) => { let data try { - data = fs.readFileSync(storagePath) + data = fs.readFileSync(getStoragePath()) } catch (e) { } diff --git a/docs/debugging.md b/docs/debugging.md index bfe98c5d37c..5401c252495 100644 --- a/docs/debugging.md +++ b/docs/debugging.md @@ -41,6 +41,20 @@ If you'd like to see code run on each page load, you can edit `app/extensions/br Calls to `console.log` and related functions go into the per page dev tools console mentioned above. +## Debugging Session Data + +The session data is stored in OS specific user data directories. Within those directories there will be a `brave` directory for release builds and a `brave-development` directory for dev (from NODE_ENV). If you want to use a different directory for dev you can set the environment variable `BRAVE_USER_DATA_DIR` to the directory you want to use. Each test run goes in a new tmp directory inside the OS specific tmpdir. Normally these directories are removed when the test is finished, but if you want to keep them you can the enviroment variable `KEEP_BRAVE_USER_DATA_DIR` to true. + +MacOS +~/Library/Application Support/brave + +Linux +linux ~/.config/brave + +Windows +C:\Users\username\AppData\Roaming\brave + + ## Profiling React code The `Debug` menu has a `Toggle React Profiling` option which will start/stop the React addon for profiling. diff --git a/test/app/sessionStoreTest.js b/test/app/sessionStoreTest.js index 068fbbe3e53..e61bd5789d7 100644 --- a/test/app/sessionStoreTest.js +++ b/test/app/sessionStoreTest.js @@ -38,8 +38,8 @@ describe('sessionStore', function () { .waitUntil(function () { return this.getValue(urlInput).then((val) => val === page1Url) }) - yield Brave.stopApp() - yield Brave.startApp(false) + yield Brave.stopApp(false) + yield Brave.startApp() yield setup(Brave.app.client) }) diff --git a/test/components/autofillTest.js b/test/components/autofillTest.js index 58e3a87a959..6855ad27c3f 100644 --- a/test/components/autofillTest.js +++ b/test/components/autofillTest.js @@ -1,11 +1,28 @@ /* global describe, it, before */ const Brave = require('../lib/brave') -const assert = require('assert') const messages = require('../../js/constants/messages') const {urlInput, autofillAddressPanel, autofillCreditCardPanel} = require('../lib/selectors') const {getTargetAboutUrl} = require('../../js/lib/appUrlUtil') +const addAddressButton = '.addAddressButton' +const saveAddressButton = '.saveAddressButton' +const addCreditCardButton = '.addCreditCardButton' +const saveCreditCardButton = '.saveCreditCardButton' +const name = 'Brave Lion' +const organization = 'Brave' +const streetAddress = '1161 Mission Street, #401' +const city = 'San Francisco' +const state = 'CA' +const postalCode = '94103-1550' +const country = 'US' +const phone = '0987654321' +const email = 'press@brave.com' +const cardName = 'Test Card' +const cardNumber = '1234567890' +const expMonth = 9 +const expYear = new Date().getFullYear() + 2 + describe('Autofill', function () { function * setup (client) { yield client @@ -16,28 +33,16 @@ describe('Autofill', function () { .waitForVisible(urlInput) } - describe('Data Management', function () { + describe('address', function () { Brave.beforeAll(this) before(function * () { yield setup(this.app.client) - }) - const page1Url = 'about:autofill' - const addAddressButton = '.addAddressButton' - const saveAddressButton = '.saveAddressButton' - const name = 'Brave Lion' - const organization = 'Brave' - const streetAddress = '1161 Mission Street, #401' - const city = 'San Francisco' - const state = 'CA' - const postalCode = '94103-1550' - const country = 'US' - const phone = '0987654321' - const email = 'press@brave.com' - it('Adding Address', function * () { + this.autofillPreferences = 'about:autofill' + this.formfill = Brave.server.url('formfill.html') + yield this.app.client .tabByIndex(0) - .loadUrl(page1Url) - .url(getTargetAboutUrl(page1Url)) + .loadUrl(this.autofillPreferences) .waitForVisible(addAddressButton) .click(addAddressButton) .windowByUrl(Brave.browserWindowUrl) @@ -66,7 +71,11 @@ describe('Autofill', function () { return val.value.autofill.addresses.guid.length === 1 }) }) - .tabByUrl(this.page1Url) + .tabByIndex(0) + .loadUrl(this.autofillPreferences) + }) + it('adds an autofill address', function * () { + yield this.app.client .waitForVisible('.autofillPage') .getText('.addressName').should.eventually.be.equal(name) .getText('.organization').should.eventually.be.equal(organization) @@ -78,26 +87,44 @@ describe('Autofill', function () { .getText('.phone').should.eventually.be.equal(phone) .getText('.email').should.eventually.be.equal(email) }) - it('Address form test', function * () { - const page1Url = Brave.server.url('formfill.html') + it('autofills the address', function * () { yield this.app.client .tabByIndex(0) - .loadUrl(page1Url) - .url(getTargetAboutUrl(page1Url)) + .loadUrl(this.formfill) .waitForVisible('
diff --git a/test/lib/brave.js b/test/lib/brave.js index 33239aa0a91..8502703a517 100644 --- a/test/lib/brave.js +++ b/test/lib/brave.js @@ -5,6 +5,7 @@ require('./coMocha') const path = require('path') const fs = require('fs') +const os = require('os') const {getTargetAboutUrl, isSourceAboutUrl} = require('../../js/lib/appUrlUtil') var chaiAsPromised = require('chai-as-promised') @@ -13,6 +14,30 @@ chai.use(chaiAsPromised) const Server = require('./server') +const generateUserDataDir = () => { + return path.join(os.tmpdir(), 'brave-test', (new Date().getTime()) + Math.floor(Math.random() * 1000).toString()) +} + +const rmDir = (dirPath) => { + try { + var files = fs.readdirSync(dirPath) + } catch (e) { + console.error(e) + return + } + if (files.length > 0) { + for (var i = 0; i < files.length; i++) { + var filePath = path.join(dirPath, files[i]) + if (fs.statSync(filePath).isFile()) { + fs.unlinkSync(filePath) + } else { + rmDir(filePath) + } + } + } + fs.rmdirSync(dirPath) +} + var promiseMapSeries = function (array, iterator) { var length = array.length var current = Promise.resolve() @@ -26,6 +51,7 @@ var promiseMapSeries = function (array, iterator) { return Promise.all(results) } +let userDataDir = generateUserDataDir() var exports = { keys: { COMMAND: '\ue03d', @@ -411,36 +437,29 @@ var exports = { this.app.client.waitUntilWindowLoaded().windowByUrl(exports.browserWindowUrl) }, - startApp: function (cleanSessionStore = true) { - if (cleanSessionStore) { - let ledgerDir = path.join(process.env.HOME, '.brave-test-ledger') - try { - fs.unlinkSync(path.join(process.env.HOME, '.brave-test-session-store-1')) - } catch (e) { - } - try { - ['publisher', 'state', 'scores', 'synopsis'].forEach((name) => { - try { - fs.unlinkSync(path.join(ledgerDir, `ledger-${name}-test.json`)) - } catch (e) { - } - }) - } catch (e) { - // probably ledgerDir does not exist yet - fs.mkdirSync(ledgerDir) - } + startApp: function () { + if (process.env.KEEP_BRAVE_USER_DATA_DIR) { + console.log('BRAVE_USER_DATA_DIR=' + userDataDir) + } + let env = { + NODE_ENV: 'test', + BRAVE_USER_DATA_DIR: userDataDir } this.app = new Application({ path: './node_modules/.bin/electron', - env: { - NODE_ENV: 'test' - }, - args: ['./', '--debug=5858', '--enable-logging', '--v=0'] + env, + args: ['./', '--debug=5858', '--enable-logging', '--v=1'] }) return this.app.start() }, - stopApp: function () { + stopApp: function (cleanSessionStore = true) { + if (cleanSessionStore) { + if (!process.env.KEEP_BRAVE_USER_DATA_DIR) { + userDataDir && rmDir(userDataDir) + } + userDataDir = generateUserDataDir() + } // this.app.client.getMainProcessLogs().then(function (logs) { // logs.forEach(function (log) { // console.log(log)