2015-11-20 07:29:49 +08:00
|
|
|
import path from 'path'
|
|
|
|
import {Application} from 'spectron';
|
2015-12-10 23:52:20 +08:00
|
|
|
import {clearConfig,
|
|
|
|
setupDefaultConfig,
|
|
|
|
FAKE_DATA_PATH,
|
|
|
|
CONFIG_DIR_PATH} from './config-helper';
|
|
|
|
|
|
|
|
export default class N1Launcher extends Application {
|
|
|
|
constructor(launchArgs = [], configOpts) {
|
|
|
|
|
|
|
|
if (configOpts === N1Launcher.CLEAR_CONFIG) {
|
|
|
|
clearConfig()
|
|
|
|
} else {
|
|
|
|
setupDefaultConfig()
|
|
|
|
}
|
2015-11-20 07:29:49 +08:00
|
|
|
|
|
|
|
super({
|
|
|
|
path: N1Launcher.electronPath(),
|
|
|
|
args: [jasmine.NYLAS_ROOT_PATH].concat(N1Launcher.defaultNylasArgs()).concat(launchArgs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:52:20 +08:00
|
|
|
onboardingWindowReady() {
|
|
|
|
return this.windowReady(N1Launcher.secondaryWindowLoadedMatcher)
|
|
|
|
}
|
|
|
|
|
2015-11-20 07:29:49 +08:00
|
|
|
mainWindowReady() {
|
2015-12-10 23:52:20 +08:00
|
|
|
return this.windowReady(N1Launcher.mainWindowLoadedMatcher).then(() => {
|
|
|
|
return this.client
|
|
|
|
.timeoutsAsyncScript(5000)
|
|
|
|
.executeAsync((FAKE_DATA_PATH, done) => {
|
|
|
|
$n.AccountStore._importFakeData(FAKE_DATA_PATH).then(done);
|
|
|
|
}, FAKE_DATA_PATH)
|
|
|
|
});
|
2015-12-02 10:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
popoutComposerWindowReady() {
|
2015-12-10 23:52:20 +08:00
|
|
|
return this.windowReady(N1Launcher.mainWindowLoadedMatcher).then(() => {
|
2015-12-15 04:05:34 +08:00
|
|
|
return this.client
|
|
|
|
.timeoutsAsyncScript(5000)
|
|
|
|
.executeAsync((fakeDataPath, done) => {
|
|
|
|
return $n.AccountStore._importFakeData(fakeDataPath).then(function(){
|
|
|
|
$n.Actions.composeNewBlankDraft();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, FAKE_DATA_PATH);
|
|
|
|
}).then(()=>{
|
|
|
|
return N1Launcher.waitUntilMatchingWindowLoaded(this.client, N1Launcher.composerWindowMatcher).then((windowId)=>{
|
|
|
|
return new Promise((resolve,reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(this.client.window(windowId));
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-12-02 10:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
windowReady(matcher) {
|
2015-12-10 23:52:20 +08:00
|
|
|
return this.start().then(()=>{
|
2015-12-02 10:49:40 +08:00
|
|
|
return N1Launcher.waitUntilMatchingWindowLoaded(this.client, matcher).then((windowId)=>{
|
|
|
|
return this.client.window(windowId)
|
2015-11-20 07:29:49 +08:00
|
|
|
})
|
2015-12-10 23:52:20 +08:00
|
|
|
});
|
2015-11-20 07:29:49 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:52:20 +08:00
|
|
|
static secondaryWindowLoadedMatcher(client) {
|
|
|
|
// The last thing secondary windows do once they boot is call "show"
|
|
|
|
return client.isWindowVisible()
|
|
|
|
}
|
|
|
|
|
|
|
|
static mainWindowLoadedMatcher(client) {
|
|
|
|
return client.isExisting(".window-loaded").then((exists)=>{
|
2015-12-02 10:49:40 +08:00
|
|
|
if (exists) {return true} else {return false}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static composerWindowMatcher(client) {
|
|
|
|
return client.execute(()=>{
|
|
|
|
return NylasEnv.getLoadSettings().windowType;
|
|
|
|
}).then(({value})=>{
|
|
|
|
if(value === "composer") {
|
2015-12-03 04:43:11 +08:00
|
|
|
return client.isExisting(".contenteditable")
|
2015-12-02 10:49:40 +08:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-20 07:29:49 +08:00
|
|
|
static defaultNylasArgs() {
|
2015-12-10 23:52:20 +08:00
|
|
|
return ["--enable-logging",
|
|
|
|
`--resource-path=${jasmine.NYLAS_ROOT_PATH}`,
|
|
|
|
`--config-dir-path=${CONFIG_DIR_PATH}`]
|
2015-11-20 07:29:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static electronPath() {
|
|
|
|
nylasRoot = jasmine.NYLAS_ROOT_PATH
|
|
|
|
if (process.platform === "darwin") {
|
|
|
|
return path.join(nylasRoot, "electron", "Electron.app", "Contents", "MacOS", "Electron")
|
|
|
|
} else if (process.platform === "win32") {
|
|
|
|
return path.join(nylasRoot, "electron", "electron.exe")
|
|
|
|
}
|
|
|
|
else if (process.platform === "linux") {
|
|
|
|
return path.join(nylasRoot, "electron", "electron")
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`Platform ${process.platform} is not supported`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We unfortunatley can't just Spectron's `waitUntilWindowLoaded` because
|
|
|
|
// the first window that loads isn't necessarily the main render window (it
|
|
|
|
// could be the work window or others), and once the window is "loaded"
|
|
|
|
// it'll take a while for packages to load, etc. As such we periodically
|
|
|
|
// poll the list of windows to find one that looks like the main loaded
|
|
|
|
// window.
|
|
|
|
//
|
|
|
|
// Returns a promise that resolves with the main window's ID once it's
|
|
|
|
// loaded.
|
2015-12-02 10:49:40 +08:00
|
|
|
static waitUntilMatchingWindowLoaded(client, matcher, lastCheck=0) {
|
|
|
|
var CHECK_EVERY = 500
|
2015-11-20 07:29:49 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2015-12-10 23:52:20 +08:00
|
|
|
return client.windowHandles().then(({value}) => {
|
2015-11-20 07:29:49 +08:00
|
|
|
return Promise.mapSeries(value, (windowId)=>{
|
2015-12-02 10:49:40 +08:00
|
|
|
return N1Launcher.switchAndCheckForMatch(client, windowId, matcher)
|
2015-11-20 07:29:49 +08:00
|
|
|
})
|
2015-12-02 10:49:40 +08:00
|
|
|
}).then((windowIdChecks)=>{
|
|
|
|
for (windowId of windowIdChecks) {
|
|
|
|
if (windowId) {return resolve(windowId)}
|
2015-11-20 07:29:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var now = Date.now();
|
|
|
|
var delay = Math.max(CHECK_EVERY - (now - lastCheck), 0)
|
|
|
|
setTimeout(()=>{
|
2015-12-10 23:52:20 +08:00
|
|
|
return N1Launcher.waitUntilMatchingWindowLoaded(client, matcher, now).then(resolve)
|
2015-11-20 07:29:49 +08:00
|
|
|
}, delay)
|
2015-12-10 23:52:20 +08:00
|
|
|
return null
|
2015-11-20 07:29:49 +08:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
2015-12-10 23:52:20 +08:00
|
|
|
return null
|
2015-11-20 07:29:49 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns false or the window ID of the main window
|
2015-12-02 10:49:40 +08:00
|
|
|
// The `matcher` resolves to a boolean.
|
|
|
|
static switchAndCheckForMatch(client, windowId, matcher) {
|
2015-11-20 07:29:49 +08:00
|
|
|
return client.window(windowId).then(()=>{
|
2015-12-02 10:49:40 +08:00
|
|
|
return matcher(client).then((isMatch) => {
|
|
|
|
if (isMatch) {return windowId} else {return false}
|
|
|
|
});
|
2015-11-20 07:29:49 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 23:52:20 +08:00
|
|
|
N1Launcher.CLEAR_CONFIG = "CLEAR_CONFIG"
|