Skip to content

Commit

Permalink
Merge pull request #1450 from dvoytenko/devchannel1
Browse files Browse the repository at this point in the history
isDevChannel method and whitelist for access
  • Loading branch information
dvoytenko committed Jan 15, 2016
2 parents 4d1ba58 + 4c02e24 commit bf248dc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
17 changes: 17 additions & 0 deletions build-system/tasks/presubmit-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var privateServiceFactory = 'This service should only be installed in ' +
'the whitelisted files. Other modules should use a public function ' +
'typically called serviceNameFor.';

var shouldNeverBeUsed =
'Usage of this API is not allowed - only for internal purposes.';

// Terms that must not appear in our source files.
var forbiddenTerms = {
'DO NOT SUBMIT': '',
Expand Down Expand Up @@ -151,6 +154,20 @@ var forbiddenTerms = {
'tools/experiments/experiments.js',
]
},
'isDevChannel\\W': {
message: requiresReviewPrivacy,
whitelist: [
'extensions/amp-access/0.1/amp-access.js',
'src/experiments.js',
'tools/experiments/experiments.js',
]
},
'isDevChannelVersionDoNotUse_\\W': {
message: shouldNeverBeUsed,
whitelist: [
'src/experiments.js',
]
},
'eval\\(': '',
'localStorage': {
message: requiresReviewPrivacy,
Expand Down
5 changes: 3 additions & 2 deletions extensions/amp-access/0.1/amp-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {documentStateFor} from '../../../src/document-state';
import {evaluateAccessExpr} from './access-expr';
import {getService} from '../../../src/service';
import {installStyles} from '../../../src/styles';
import {isExperimentOn} from '../../../src/experiments';
import {isDevChannel, isExperimentOn} from '../../../src/experiments';
import {listenOnce} from '../../../src/event-helper';
import {log} from '../../../src/log';
import {onDocumentReady} from '../../../src/document-state';
Expand Down Expand Up @@ -91,7 +91,8 @@ export class AccessService {
installStyles(this.win.document, $CSS$, () => {});

/** @const @private {boolean} */
this.isExperimentOn_ = isExperimentOn(this.win, EXPERIMENT);
this.isExperimentOn_ = (isExperimentOn(this.win, EXPERIMENT) ||
isDevChannel(this.win));

const accessElement = document.getElementById('amp-access');

Expand Down
30 changes: 30 additions & 0 deletions src/experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ const COOKIE_MAX_AGE_DAYS = 180; // 6 month
/** @const {time} */
const COOKIE_EXPIRATION_INTERVAL = COOKIE_MAX_AGE_DAYS * 24 * 60 * 60 * 1000;

/** @const {string} */
const CANARY_EXPERIMENT_ID = 'dev-channel';


/**
* Whether the scripts come from a dev channel.
* @param {!Window} win
* @return {boolean}
*/
export function isDevChannel(win) {
if (isExperimentOn(win, CANARY_EXPERIMENT_ID)) {
return true;
}
if (isDevChannelVersionDoNotUse_('$internalRuntimeVersion$')) {
return true;
}
return false;
}


/**
* Whether the version corresponds to the dev-channel binary.
* @param {string} version
* @return {boolean}
* @private Visible for testing only!
*/
export function isDevChannelVersionDoNotUse_(version) {
return !!version.match(/\-canary$/);
}


/**
* Whether the specified experiment is on or off.
Expand Down
25 changes: 24 additions & 1 deletion test/functional/test-experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import {isExperimentOn, toggleExperiment} from '../../src/experiments';
import {isDevChannel, isDevChannelVersionDoNotUse_,
isExperimentOn, toggleExperiment} from '../../src/experiments';
import * as sinon from 'sinon';


Expand Down Expand Up @@ -110,3 +111,25 @@ describe('toggleExperiment', () => {
expectToggle('AMP_EXP=e1', 'e1', false).to.equal('false; AMP_EXP=');
});
});


describe('isDevChannel', () => {

function expectDevChannel(cookiesString) {
return expect(isDevChannel({
document: {
cookie: cookiesString
}
}));
}

it('should return value based on cookie', () => {
expectDevChannel('AMP_EXP=other').to.be.false;
expectDevChannel('AMP_EXP=dev-channel').to.be.true;
});

it('should return value based on binary version', () => {
expect(isDevChannelVersionDoNotUse_('123456789')).to.be.false;
expect(isDevChannelVersionDoNotUse_('123456789-canary')).to.be.true;
});
});

0 comments on commit bf248dc

Please sign in to comment.