Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the GL context to be mocked #2753

Closed
wants to merge 1 commit into from
Closed

Allow the GL context to be mocked #2753

wants to merge 1 commit into from

Conversation

scothis
Copy link
Contributor

@scothis scothis commented Jun 15, 2016

Mocking the GL context is useful in order to integration test
applications that interact with the mapbox-gl-js APIs, but do not have
WebGL available. By mocking the GL context, gl-js will continue to
respond to method invocations and emit events normally. It will fetch
styles, sprites, glyphs and tiles normally.

To mock the GL context, set mapboxgl.config.MOCK_GL = true before
creating the map instance.

Issue: #2750

Mocking the GL context is useful in order to integration test
applications that interact with the mapbox-gl-js APIs, but do not have
WebGL available. By mocking the GL context, gl-js will continue to
respond to method invocations and emit events normally. It will fetch
styles, sprites, glyphs and tiles normally.

To mock the GL context, set `mapboxgl.config.MOCK_GL = true` before
creating the map instance.

Issue: #2750
@scothis
Copy link
Contributor Author

scothis commented Jun 15, 2016

Confirmed to work with a real browser hosted on SauceLabs that does not support WebGL.

@lucaswoj
Copy link
Contributor

As I said in #2750, I am open to this change only if we cannot find a way to mock GL using non-invasive techniques such as proxyquire (even if that requires a small refactor).

@scothis
Copy link
Contributor Author

scothis commented Jun 16, 2016

I was able to get this working directly inside Studio.

  mockGL() {
    const MockWebGLRenderingContext = require('webgl-mock/src/WebGLRenderingContext');
    const MapboxGLCanvas = require('mapbox-gl/js/util/canvas');
    const contexts = new WeakMap();

    MapboxGLCanvas.prototype.getWebGLContext = function() {
      var context = contexts.get(this);
      if (!context) {
        context = new MockWebGLRenderingContext(this.canvas);
        context.getExtension = function(extension) {
          // disable VAO
          if (extension === 'OES_vertex_array_object') {
              return undefined;
          }
          return MockWebGLRenderingContext.prototype.getExtension.apply(this, arguments);
        };
        contexts.set(this, context);
      }
      return context;
    };
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants