From c9edf77e8ea8f48fc92206c3de5296115a424cd1 Mon Sep 17 00:00:00 2001 From: Alexey Kuzmin Date: Tue, 2 Oct 2018 03:34:52 +0200 Subject: [PATCH] test: slightly rewrite getGPUInfo() tests (#14863) Rewrite GPUInfo tests for linux and update `getGPUInfo` functionality for linux. `basic` and `complete` GPUInfo is same for linux. --- atom/browser/api/atom_api_app.cc | 16 ++++-- spec/api-app-spec.js | 90 +++++++++++++++++++++----------- spec/fixtures/api/gpu-info.js | 17 ++++++ 3 files changed, 89 insertions(+), 34 deletions(-) create mode 100644 spec/fixtures/api/gpu-info.js diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 61e82925da23..220b6a14ea24 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -1154,15 +1154,25 @@ v8::Local App::GetGPUInfo(v8::Isolate* isolate, const std::string& info_type) { auto* const gpu_data_manager = content::GpuDataManagerImpl::GetInstance(); scoped_refptr promise = new util::Promise(isolate); - if ((info_type != "basic" && info_type != "complete") || - !gpu_data_manager->GpuAccessAllowed(nullptr)) { - promise->Reject("Error fetching GPU Info"); + if (info_type != "basic" && info_type != "complete") { + promise->RejectWithErrorMessage( + "Invalid info type. Use 'basic' or 'complete'"); + return promise->GetHandle(); + } + std::string reason; + if (!gpu_data_manager->GpuAccessAllowed(&reason)) { + promise->RejectWithErrorMessage("GPU access not allowed. Reason: " + + reason); return promise->GetHandle(); } auto* const info_mgr = GPUInfoManager::GetInstance(); if (info_type == "complete") { +#if defined(OS_MACOSX) || defined(OS_WIN) info_mgr->FetchCompleteInfo(promise); +#else + info_mgr->FetchBasicInfo(promise); +#endif } else /* (info_type == "basic") */ { info_mgr->FetchBasicInfo(promise); } diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index 6ab6bd093852..0e592ac84fe5 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -805,41 +805,69 @@ describe('app module', () => { }) describe('getGPUInfo() API', () => { - before(function () { - // TODO(alexeykuzmoin): Fails on linux. Enable them back. - // https://github.com/electron/electron/pull/14863 + const appPath = path.join(__dirname, 'fixtures', 'api', 'gpu-info.js') + + const getGPUInfo = async (type) => { + const appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, type]) + let gpuInfoData = '' + let errorData = '' + appProcess.stdout.on('data', (data) => { + gpuInfoData += data + }) + appProcess.stderr.on('data', (data) => { + errorData += data + }) + const [exitCode] = await emittedOnce(appProcess, 'exit') + if (exitCode === 0) { + // return info data on successful exit + return JSON.parse(gpuInfoData) + } else { + // return error if not clean exit + return Promise.reject(new Error(errorData)) + } + } + const verifyBasicGPUInfo = async (gpuInfo) => { + // Devices information is always present in the available info. + expect(gpuInfo).to.have.own.property('gpuDevice') + .that.is.an('array') + .and.is.not.empty() + + const device = gpuInfo.gpuDevice[0] + expect(device).to.be.an('object') + .and.to.have.property('deviceId') + .that.is.a('number') + .not.lessThan(0) + } + + it('succeeds with basic GPUInfo', async () => { + const gpuInfo = await getGPUInfo('basic') + await verifyBasicGPUInfo(gpuInfo) + }) + + it('succeeds with complete GPUInfo', async () => { + const completeInfo = await getGPUInfo('complete') if (process.platform === 'linux') { - this.skip() + // For linux complete info is same as basic info + await verifyBasicGPUInfo(completeInfo) + const basicInfo = await getGPUInfo('basic') + expect(completeInfo).to.deep.equal(basicInfo) + } else { + // Gl version is present in the complete info. + expect(completeInfo).to.have.own.property('auxAttributes') + .that.is.an('object') + expect(completeInfo.auxAttributes).to.have.own.property('glVersion') + .that.is.a('string') + .and.not.empty() } }) - it('succeeds with basic GPUInfo', (done) => { - app.getGPUInfo('basic').then((gpuInfo) => { - // Devices information is always present in the available info - expect(gpuInfo.gpuDevice).to.be.an('array') - expect(gpuInfo.gpuDevice.length).to.be.greaterThan(0) - const device = gpuInfo.gpuDevice[0] - expect(device).to.be.an('object') - expect(device) - .to.have.property('deviceId') - .that.is.a('number') - .not.lessThan(0) - done() - }) - }) - - it('succeeds with complete GPUInfo', (done) => { - app.getGPUInfo('complete').then((completeInfo) => { - // Driver version is present in the complete info - expect(completeInfo.auxAttributes.glVersion).to.be.a('string').that.has.length.greaterThan(0) - done() - }) - }) - - it('fails for invalid info_type', (done) => { - app.getGPUInfo('invalid').catch(() => { - done() - }) + it('fails for invalid info_type', () => { + const invalidType = 'invalid' + const errorMessage = + `app.getGPUInfo() didn't fail for the "${invalidType}" info type` + return app.getGPUInfo(invalidType).then( + () => Promise.reject(new Error(errorMessage)), + () => Promise.resolve()) }) }) diff --git a/spec/fixtures/api/gpu-info.js b/spec/fixtures/api/gpu-info.js new file mode 100644 index 000000000000..1954f9b4b8a8 --- /dev/null +++ b/spec/fixtures/api/gpu-info.js @@ -0,0 +1,17 @@ +const { app } = require('electron') + +app.commandLine.appendSwitch('--disable-software-rasterizer') + +app.on('ready', () => { + const infoType = process.argv.pop() + app.getGPUInfo(infoType).then( + (gpuInfo) => { + console.log(JSON.stringify(gpuInfo)) + app.exit(0) + }, + (error) => { + console.error(error) + app.exit(1) + } + ) +})