1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_PAIRING_FAKE_CONTROLLER_PAIRING_CONTROLLER_H_ 6 #define COMPONENTS_PAIRING_FAKE_CONTROLLER_PAIRING_CONTROLLER_H_ 7 8 #include <set> 9 #include <utility> 10 11 #include "base/macros.h" 12 #include "base/observer_list.h" 13 #include "base/time/time.h" 14 #include "components/pairing/controller_pairing_controller.h" 15 16 namespace pairing_chromeos { 17 18 class FakeControllerPairingController 19 : public ControllerPairingController, 20 public ControllerPairingController::Observer { 21 public: 22 typedef ControllerPairingController::Observer Observer; 23 24 enum DiscoveryEventType { DEVICE_FOUND, DEVICE_LOST, NOTHING_FOUND }; 25 26 typedef std::pair<DiscoveryEventType, std::string> DiscoveryEvent; 27 typedef std::vector<DiscoveryEvent> DiscoveryScenario; 28 29 // Config is a coma separated list of key-value pairs separated by colon. 30 // Supported options: 31 // * async_duration - integer. Default: 3000. 32 // * fail_connecting - {0,1}. Default: 0. 33 // * connection_lost - integer-integer. Default: 0-0. 34 // * fail_enrollment - {0,1}. Default: 0. 35 // * discovery - {F,L,N}-string(~{F,L,N}-string)*. Default: 36 // F-Device_1~F-Device_5~F-Device_3~L-Device_3~L-Device_1~F-Device_1 37 // * code - 6 digits or empty string. Default: empty string. If strings is 38 // empty, random code is generated. 39 explicit FakeControllerPairingController(const std::string& config); 40 virtual ~FakeControllerPairingController(); 41 42 // Applies given |config| to controller. 43 void ApplyConfig(const std::string& config); 44 45 // Sets delay for asynchronous operations. like device searching or host 46 // enrollment. Default value is 3 seconds. set_async_duration(base::TimeDelta async_duration)47 void set_async_duration(base::TimeDelta async_duration) { 48 async_duration_ = async_duration; 49 } 50 51 // Causing an error on |STAGE_ESTABLISHING_CONNECTION| stage once. 52 void SetShouldFailOnConnecting(); 53 54 // Causing a connection loss on |stage_begin| and a connection recovery 55 // on |stage_end| once. 56 void SetShouldLoseConnection(Stage stage_begin, Stage stage_end); 57 58 // Makes host enrollment to fail once. 59 void SetEnrollmentShouldFail(); 60 61 // Sets devices discovery scenario for |STAGE_DEVICES_DESCOVERY| stage. 62 // Events are executed with interval of |async_duration_|. 63 // For default scenario refer to implementation. 64 void SetDiscoveryScenario(const DiscoveryScenario& discovery_scenario); 65 66 // Overridden from ControllerPairingController: 67 virtual void AddObserver(Observer* observer) OVERRIDE; 68 virtual void RemoveObserver(Observer* observer) OVERRIDE; 69 virtual Stage GetCurrentStage() OVERRIDE; 70 virtual void StartPairing() OVERRIDE; 71 virtual DeviceIdList GetDiscoveredDevices() OVERRIDE; 72 virtual void ChooseDeviceForPairing(const std::string& device_id) OVERRIDE; 73 virtual void RepeatDiscovery() OVERRIDE; 74 virtual std::string GetConfirmationCode() OVERRIDE; 75 virtual void SetConfirmationCodeIsCorrect(bool correct) OVERRIDE; 76 virtual void SetHostConfiguration( 77 bool accepted_eula, 78 const std::string& lang, 79 const std::string& timezone, 80 bool send_reports, 81 const std::string& keyboard_layout) OVERRIDE; 82 virtual void OnAuthenticationDone(const std::string& domain, 83 const std::string& auth_token) OVERRIDE; 84 virtual void StartSession() OVERRIDE; 85 86 private: 87 void ChangeStage(Stage new_stage); 88 void ChangeStageLater(Stage new_stage); 89 void ExecuteDiscoveryEvent(size_t event_position); 90 void DeviceFound(const std::string& device_id); 91 void DeviceLost(const std::string& device_id); 92 93 // Overridden from ui::ControllerPairingController::Observer: 94 virtual void PairingStageChanged(Stage new_stage) OVERRIDE; 95 virtual void DiscoveredDevicesListChanged() OVERRIDE; 96 97 ObserverList<ControllerPairingController::Observer> observers_; 98 Stage current_stage_; 99 std::string confirmation_code_; 100 std::string preset_confirmation_code_; 101 base::TimeDelta async_duration_; 102 DiscoveryScenario discovery_scenario_; 103 std::set<std::string> discovered_devices_; 104 std::string choosen_device_; 105 bool should_fail_on_connecting_; 106 Stage connection_lost_begin_; 107 Stage connection_lost_end_; 108 bool enrollment_should_fail_; 109 110 DISALLOW_COPY_AND_ASSIGN(FakeControllerPairingController); 111 }; 112 113 } // namespace pairing_chromeos 114 115 #endif // COMPONENTS_PAIRING_FAKE_CONTROLLER_PAIRING_CONTROLLER_H_ 116