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

Remember metering entitlements were checked #38522

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -652,12 +652,12 @@ export class GoogleSubscriptionsPlatform {
this.runtime_.clear();

entitlementsParams.metering = {state: meteringState};

// Remember we requested metering entitlements.
// This helps avoid redundant fetches for metering entitlements.
this.serviceAdapter_.rememberMeteringEntitlementsWereFetched();
}

// Remember we checked for metering entitlements.
// This helps avoid redundant fetches for metering entitlements.
this.serviceAdapter_.rememberMeteringEntitlementsWereChecked();

return this.runtime_
.getEntitlements(entitlementsParams)
.then((swgEntitlements) =>
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-subscriptions/0.1/amp-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ export class SubscriptionService {
return;
}

if (this.metering_.entitlementsWereFetchedWithCurrentMeteringState) {
if (this.metering_.checkedForEntitlements) {
// Move along.
// The current metering state isn't granting.
continueAuthorizationFlow();
Expand Down Expand Up @@ -770,7 +770,7 @@ export class SubscriptionService {
this.renderer_.toggleLoading(true);

if (this.metering_) {
this.metering_.entitlementsWereFetchedWithCurrentMeteringState = false;
this.metering_.checkedForEntitlements = false;
}

this.platformStore_
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-subscriptions/0.1/metering.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Metering {
* This helps avoid redundant fetches.
* @type {boolean}
*/
this.entitlementsWereFetchedWithCurrentMeteringState = false;
this.checkedForEntitlements = false;

/**
* Key for the subscription platform with metering enabled.
Expand Down Expand Up @@ -80,7 +80,7 @@ export class Metering {
}

this.meteringStateString_ = meteringStateString;
this.entitlementsWereFetchedWithCurrentMeteringState = false;
this.checkedForEntitlements = false;
});
}

Expand Down
6 changes: 3 additions & 3 deletions extensions/amp-subscriptions/0.1/service-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ export class ServiceAdapter {
}

/**
* Remembers metering entitlements were fetched
* Remembers metering entitlements were checked
* with the current metering state.
*
* This helps avoid redundant fetches.
*/
rememberMeteringEntitlementsWereFetched() {
rememberMeteringEntitlementsWereChecked() {
if (!this.subscriptionService_.metering_) {
return;
}

this.subscriptionService_.metering_.entitlementsWereFetchedWithCurrentMeteringState = true;
this.subscriptionService_.metering_.checkedForEntitlements = true;
}
}
11 changes: 4 additions & 7 deletions extensions/amp-subscriptions/0.1/test/test-amp-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ describes.fakeWin('AmpSubscriptions', {amp: true}, (env) => {

env.sandbox.stub(platform, 'activate');
env.sandbox.stub(platform, 'getEntitlements').callsFake(() => {
subscriptionService.metering_.entitlementsWereFetchedWithCurrentMeteringState = true;
subscriptionService.metering_.checkedForEntitlements = true;
return Promise.resolve();
});
env.sandbox.stub(renderer, 'setGrantState');
Expand Down Expand Up @@ -790,7 +790,7 @@ describes.fakeWin('AmpSubscriptions', {amp: true}, (env) => {
platformStore.getGrantStatus.resolves(false);

// We already fetched entitlements with the current metering state.
subscriptionService.metering_.entitlementsWereFetchedWithCurrentMeteringState = true;
subscriptionService.metering_.checkedForEntitlements = true;

subscriptionService.startAuthorizationFlow_();
await flush();
Expand Down Expand Up @@ -881,14 +881,11 @@ describes.fakeWin('AmpSubscriptions', {amp: true}, (env) => {
});

it('should clear metering flag(s), if metering is enabled', async () => {
subscriptionService.metering_.entitlementsWereFetchedWithCurrentMeteringState = true;
subscriptionService.metering_.checkedForEntitlements = true;

await subscriptionService.resetPlatforms();

expect(
subscriptionService.metering_
.entitlementsWereFetchedWithCurrentMeteringState
).to.be.false;
expect(subscriptionService.metering_.checkedForEntitlements).to.be.false;
});
});

Expand Down
5 changes: 2 additions & 3 deletions extensions/amp-subscriptions/0.1/test/test-metering.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ describes.fakeWin('Metering', {amp: true}, () => {
});

it('resets flag', async () => {
metering.entitlementsWereFetchedWithCurrentMeteringState = true;
metering.checkedForEntitlements = true;

await metering.saveMeteringState({new: true});

expect(metering.entitlementsWereFetchedWithCurrentMeteringState).to.be
.false;
expect(metering.checkedForEntitlements).to.be.false;
});
});

Expand Down
11 changes: 3 additions & 8 deletions extensions/amp-subscriptions/0.1/test/test-service-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,12 @@ describes.realWin(

describe('rememberMeteringEntitlementsWereFetched', () => {
it('should update flag', async () => {
expect(
subscriptionService.metering_
.entitlementsWereFetchedWithCurrentMeteringState
).to.be.false;
expect(subscriptionService.metering_.checkedForEntitlements).to.be
.false;

serviceAdapter.rememberMeteringEntitlementsWereFetched();

expect(
subscriptionService.metering_
.entitlementsWereFetchedWithCurrentMeteringState
).to.be.true;
expect(subscriptionService.metering_.checkedForEntitlements).to.be.true;
});

it('should handle metering being disabled', async () => {
Expand Down