function setUpMonitor(initialRatio, {
  hasMatchMedia = false,
  deprecatedAddListener = false,
  mockAddEventListener = false
} = {}) {
  fakeWindow = {devicePixelRatio: initialRatio};

  if (hasMatchMedia) {
    // Every call to matchMedia should return a new media query list with its
    // own set of listeners.
    fakeWindow.matchMedia = (query) => {
      const listeners = [];
      const newList = {
        addListener: function(listener) {
          listeners.push(listener);
        },
        removeListener: function(listener) {
          googArray.remove(listeners, listener);
        },
        callListeners: function() {
          for (let i = 0; i < listeners.length; i++) {
            listeners[i]();
          }
        },
        getListenerCount: function() {
          return listeners.length;
        },
      };
      if (mockAddEventListener) {
        newList.addEventListener = function(target, listener) {
          if (target === 'change') {
            listeners.push(listener);
          }
        };
        newList.removeEventListener = function(target, listener) {
          if (target === 'change') {
            googArray.remove(listeners, listener);
          }
        };
      }
      // Regression testing Cobalt browser compatibility (https://cobalt.dev/).
      // Cobalt browser has `matchMedia` but doesn't implement the deprecated
      // addListener method.
      if (deprecatedAddListener) {
        newList.addListener = undefined;
      }
      mediaQueryLists.push(newList);
      return newList;
    };
  }

  const domHelper = mockControl.createStrictMock(DomHelper);
  domHelper.getWindow().$returns(fakeWindow);
  mockControl.$replayAll();

  monitor = new PixelDensityMonitor(domHelper);
  events.listen(monitor, PixelDensityMonitor.EventType.CHANGE, recordFunction);
}
Helper to create `matchMedia` mock and assign to `fakeWindow`. Used to mock
different browser compatibilities.

@param {number|undefined} initialRatio
@param {!MonitorOpts=} options