'use strict';

const assert = require('assert');
const Application = require('spectron').Application;
const fs = require('fs');
const appPath = __dirname + '/../../app/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron';
const app = new Application({
  path: appPath,
  args: ['app/main.js', '--test-mode=true']
});

describe('Prefs', function() {
  this.timeout(60000);
  
	beforeEach(() => {
		return app.start();
	});

	afterEach(() => {
		if (app && app.isRunning()) {
			return app.stop();
		}
	});

  it('opens window', function() {
    assert(app.browserWindow.isVisible());
  });

  it('sets title', function() {
    app.client.waitUntilWindowLoaded().getTitle().then((res) => {
      assert.equal('Before Dawn -- screensaver fun', res);
      done();
    });
  });

  it('lists screensavers', function(done) {
    app.client.waitUntilWindowLoaded().
        getText('body').then(function (text) {
          assert(text.lastIndexOf('Holzer') !== -1)
          done();
        });
  });

  
  /*it('lists screensaver', function(done) {
     app.browserWindow.capturePage().then(function(imageBuffer) {
     console.log("DUMP IT", imageBuffer);
     fs.writeFile('page.png', imageBuffer, function(error) {
     if (error) throw error;
     console.info(`Screenshot saved: ${process.cwd()}/page.png`);
     done();
     });
     }).catch(function (error) {
     // Log any failures
     console.error('Test failed', error.message);
     console.log(error);
     app.stop();
     });
  });*/
});

@@ -117,24 +117,52 @@
 uire('fs');%0A
+const tmp = require('tmp');%0A
 const appPat
@@ -254,16 +254,135 @@
 ectron';
+%0A%0Avar getTempDir = function() %7B%0A  var tmpObj = tmp.dirSync();%0A  return tmpObj.name;%0A%7D;%0Avar workingDir = getTempDir();%0A%0A
 %0Aconst a
@@ -464,16 +464,62 @@
 e=true'%5D
+,%0A  env: %7B%0A    BEFORE_DAWN_DIR: workingDir%0A  %7D
 %0A%7D);%0A%0Ade
@@ -566,17 +566,16 @@
 out(6000
-0
 );%0A  %0A%09b
@@ -1045,71 +1045,510 @@
 ntil
-WindowLoaded().
+TextExists('body', 'Holzer', 10000).getText('body').then((text) =%3E %7B%0A      assert(text.lastIndexOf('Holzer') !== -1);%0A      done();
 %0A    
-    getText('body').then(function (text) %7B%0A
+%7D);%0A  %7D);%0A%0A  it.only('allows picking a screensaver', function(done) %7B%0A    app.client.waitUntilTextExists('body', 'Holzer', 10000)%0A       .getAttribute(%22%5Btype=radio%5D%22,%22data-name%22)%0A       .then(console.log.bind(console))%0A       .then(() =%3E %7B%0A         app.client.click(%22%5Btype=radio%5D%5Bdata-name='Cylon'%5D%22).%0A             getText('body').%0A             then((text) =%3E %7B%0A    
     
@@ -1549,24 +1549,25 @@
             
+ 
 assert(text.
@@ -1571,38 +1571,53 @@
 xt.lastIndexOf('
-Holzer
+lights on your screen
 ') !== -1)%0A     
@@ -1610,17 +1610,20 @@
  !== -1)
-%0A
+;%0A  
         
@@ -1620,24 +1620,27 @@
             
+   
 done();%0A    
@@ -1632,24 +1632,40 @@
    done();%0A 
+            %7D);%0A
        %7D);%0A 
@@ -1669,16 +1669,18 @@
 ;%0A  %7D);%0A
+  
 %0A  %0A  /*

Add test for picking a screensaver