OLD | NEW |
---|---|
1 /** | 1 /** |
2 * Mock implementation of the budget service. | 2 * Mock implementation of the budget service. |
3 */ | 3 */ |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 const TEST_BUDGET_COST = 1.2; | 7 const TEST_BUDGET_COST = 1.2; |
8 const TEST_BUDGET_AT = 2.3; | 8 const TEST_BUDGET_AT = 2.3; |
9 const TEST_BUDGET_TIME = new Date().getTime(); | 9 const TEST_BUDGET_TIME = new Date().getTime(); |
10 | 10 |
11 let budgetServiceMock = loadMojoModules( | 11 let budgetServiceMock = loadMojoModules( |
12 'budgetServiceMock', | 12 'budgetServiceMock', |
13 ['third_party/WebKit/public/platform/modules/budget_service/budget_service.m ojom', | 13 ['third_party/WebKit/public/platform/modules/budget_service/budget_service.m ojom', |
14 'mojo/public/js/router' | 14 'mojo/public/js/router' |
15 ]).then(mojo => { | 15 ]).then(mojo => { |
16 const [budgetService, router] = mojo.modules; | 16 const [budgetService, router] = mojo.modules; |
17 | 17 |
18 class BudgetServiceMock { | 18 class BudgetServiceMock { |
19 constructor(interfaceProvider) { | 19 constructor(interfaceProvider) { |
20 interfaceProvider.addInterfaceOverrideForTesting( | 20 interfaceProvider.addInterfaceOverrideForTesting( |
21 budgetService.BudgetService.name, | 21 budgetService.BudgetService.name, |
22 handle => this.connectBudgetService_(handle)); | 22 handle => this.connectBudgetService_(handle)); |
23 | 23 |
24 this.interfaceProvider_ = interfaceProvider; | 24 this.interfaceProvider_ = interfaceProvider; |
25 | |
26 // Values to return for the next getBudget and getCost calls. | |
27 this.cost = new Object(); | |
Peter Beverloo
2016/09/05 17:06:16
this.cost_ = {};
(underscore suffix to indicate t
harkness
2016/09/06 15:45:00
Done.
| |
28 this.budget = new Array(); | |
Peter Beverloo
2016/09/05 17:06:16
this.budget_ = [];
harkness
2016/09/06 15:45:00
Done.
| |
29 this.error = budgetService.BudgetServiceErrorType.NO_ERROR; | |
Peter Beverloo
2016/09/05 17:06:16
this.error_ = budgetService....
harkness
2016/09/06 15:45:00
Done.
| |
25 } | 30 } |
26 | 31 |
27 connectBudgetService_(handle) { | 32 connectBudgetService_(handle) { |
28 this.budgetServiceStub_ = new budgetService.BudgetService.stubClass(this); | 33 this.budgetServiceStub_ = new budgetService.BudgetService.stubClass(this); |
29 this.budgetServiceRouter_ = new router.Router(handle); | 34 this.budgetServiceRouter_ = new router.Router(handle); |
30 this.budgetServiceRouter_.setIncomingReceiver(this.budgetServiceStub_); | 35 this.budgetServiceRouter_.setIncomingReceiver(this.budgetServiceStub_); |
31 } | 36 } |
32 | 37 |
38 // Set the cost of an operation type for future queries. The operationType | |
39 // mapping is needed to mirror the mapping that happens in the mojo layer. | |
Peter Beverloo
2016/09/05 17:06:16
My gut feel is that adding some comments above the
Peter Beverloo
2016/09/05 17:06:16
micro nit: mojo -> Mojo
harkness
2016/09/06 15:45:00
I added in a lot of comments, possibly too many. T
harkness
2016/09/06 15:45:00
Done.
| |
40 setCost(operationType, cost) { | |
41 var mojoOperationType = budgetService.BudgetOperationType.INVALID_OPERATIO N; | |
Peter Beverloo
2016/09/05 17:06:16
nit: var -> let
(Try to avoid "var" everywhere. U
harkness
2016/09/06 15:45:00
Done.
| |
42 if (operationType == "silent-push") | |
43 mojoOperationType = budgetService.BudgetOperationType.SILENT_PUSH; | |
44 | |
45 this.cost[mojoOperationType] = cost; | |
46 } | |
47 | |
33 getCost(operationType) { | 48 getCost(operationType) { |
34 return Promise.resolve({ cost:TEST_BUDGET_COST }); | 49 return Promise.resolve({ cost:this.cost[operationType] }); |
50 } | |
51 | |
52 // Add an entry into the budget array to be returned on the next call. | |
53 addBudget(addTime, addBudget) { | |
54 this.budget.push({time:addTime, budget_at:addBudget}); | |
Peter Beverloo
2016/09/05 17:06:16
nit: this indentation is inconsistent with the val
harkness
2016/09/06 15:45:00
Done.
| |
55 } | |
56 | |
57 // Set the error to be returned. This will be acted upon by the | |
58 // BudgetService in blink. | |
59 setError(errorName) { | |
60 switch (errorName) { | |
61 case "database-error": | |
62 this.error = budgetService.BudgetServiceErrorType.DATABASE_ERROR; | |
63 break; | |
64 case "not-supported": | |
65 this.error = budgetService.BudgetServiceErrorType.NOT_SUPPORTED; | |
66 break; | |
67 case "no-error": | |
68 this.error = budgetService.BudgetServiceErrorType.NO_ERROR; | |
69 break; | |
70 } | |
35 } | 71 } |
36 | 72 |
37 getBudget() { | 73 getBudget() { |
38 return Promise.resolve({ budget: [ { time:TEST_BUDGET_TIME, budget_at:TEST _BUDGET_AT } ] }); | 74 return Promise.resolve({ errorType:this.error, budget:this.budget }); |
75 } | |
76 | |
77 // Set whether the next reserve call should return success or not. | |
78 setReserveSuccess(success) { | |
79 this.success = success; | |
80 } | |
81 | |
82 reserve() { | |
83 return Promise.resolve({ errorType:this.error, success:this.success }); | |
39 } | 84 } |
40 } | 85 } |
41 return new BudgetServiceMock(mojo.interfaces); | 86 return new BudgetServiceMock(mojo.interfaces); |
42 }); | 87 }); |
OLD | NEW |