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_ = {}; |
| 28 this.budget_ = []; |
| 29 this.error_ = budgetService.BudgetServiceErrorType.NONE; |
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 |
33 getCost(operationType) { | 38 // This is called directly from test JavaScript to set up the return value |
34 return Promise.resolve({ cost:TEST_BUDGET_COST }); | 39 // for the getCost Mojo call. The operationType mapping is needed to mirror |
| 40 // the mapping that happens in the Mojo layer. |
| 41 setCost(operationType, cost) { |
| 42 let mojoOperationType = budgetService.BudgetOperationType.INVALID_OPERATIO
N; |
| 43 if (operationType == "silent-push") |
| 44 mojoOperationType = budgetService.BudgetOperationType.SILENT_PUSH; |
| 45 |
| 46 this.cost_[mojoOperationType] = cost; |
35 } | 47 } |
36 | 48 |
| 49 |
| 50 // This is called directly from test JavaScript to set up the budget that is |
| 51 // returned from a later getBudget Mojo call. This adds an entry into the |
| 52 // budget array. |
| 53 addBudget(addTime, addBudget) { |
| 54 this.budget_.push({ time: addTime, budget_at: addBudget }); |
| 55 } |
| 56 |
| 57 // This is called from test JavaScript. It sets whether the next reserve |
| 58 // call should return success or not. |
| 59 setReserveSuccess(success) { |
| 60 this.success_ = success; |
| 61 } |
| 62 |
| 63 // Called from test JavaScript, this sets the error to be returned by the |
| 64 // Mojo service to the BudgetService in Blink. This error is never surfaced |
| 65 // to the test JavaScript, but the test code may get an exception if one of |
| 66 // these is set. |
| 67 setError(errorName) { |
| 68 switch (errorName) { |
| 69 case "database-error": |
| 70 this.error_ = budgetService.BudgetServiceErrorType.DATABASE_ERROR; |
| 71 break; |
| 72 case "not-supported": |
| 73 this.error_ = budgetService.BudgetServiceErrorType.NOT_SUPPORTED; |
| 74 break; |
| 75 case "no-error": |
| 76 this.error_ = budgetService.BudgetServiceErrorType.NONE; |
| 77 break; |
| 78 } |
| 79 } |
| 80 |
| 81 // This provides an implementation for the Mojo serivce getCost call. |
| 82 getCost(operationType) { |
| 83 return Promise.resolve({ cost: this.cost_[operationType] }); |
| 84 } |
| 85 |
| 86 // This provides an implementation for the Mojo serivce getBudget call. |
37 getBudget() { | 87 getBudget() { |
38 return Promise.resolve({ budget: [ { time:TEST_BUDGET_TIME, budget_at:TEST
_BUDGET_AT } ] }); | 88 return Promise.resolve({ error_type: this.error_, budget: this.budget_ }); |
| 89 } |
| 90 |
| 91 // This provides an implementation for the Mojo serivce reserve call. |
| 92 reserve() { |
| 93 return Promise.resolve({ error_type: this.error_, success: this.success_ }
); |
39 } | 94 } |
40 } | 95 } |
41 return new BudgetServiceMock(mojo.interfaces); | 96 return new BudgetServiceMock(mojo.interfaces); |
42 }); | 97 }); |
OLD | NEW |