Home | History | Annotate | Download | only in pairing
      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 CHROMEOS_PAIRING_FAKE_CONTROLLER_PAIRING_FLOW_H_
      6 #define CHROMEOS_PAIRING_FAKE_CONTROLLER_PAIRING_FLOW_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 "chromeos/chromeos_export.h"
     15 #include "chromeos/pairing/controller_pairing_flow.h"
     16 
     17 namespace chromeos {
     18 
     19 class CHROMEOS_EXPORT FakeControllerPairingFlow
     20     : public ControllerPairingFlow,
     21       public ControllerPairingFlow::Observer {
     22  public:
     23   typedef ControllerPairingFlow::Observer Observer;
     24 
     25   enum DiscoveryEventType { DEVICE_FOUND, DEVICE_LOST, NOTHING_FOUND };
     26 
     27   typedef std::pair<DiscoveryEventType, std::string> DiscoveryEvent;
     28   typedef std::vector<DiscoveryEvent> DiscoveryScenario;
     29 
     30   // Config is a coma separated list of key-value pairs separated by colon.
     31   // Supported options:
     32   // * async_duration - integer. Default: 3000.
     33   // * fail_connecting - {0,1}. Default: 0.
     34   // * connection_lost - integer-integer. Default: 0-0.
     35   // * fail_enrollment - {0,1}. Default: 0.
     36   // * discovery - {F,L,N}-string(~{F,L,N}-string)*. Default:
     37   //   F-Device_1~F-Device_5~F-Device_3~L-Device_3~L-Device_1~F-Device_1
     38   // * code - 6 digits or empty string. Default: empty string. If strings is
     39   // empty, random code is generated.
     40   FakeControllerPairingFlow(const std::string& config);
     41   virtual ~FakeControllerPairingFlow();
     42 
     43   // Applies given |config| to flow.
     44   void ApplyConfig(const std::string& config);
     45 
     46   // Sets delay for asynchronous operations. like device searching or host
     47   // enrollment. Default value is 3 seconds.
     48   void set_async_duration(base::TimeDelta async_duration) {
     49     async_duration_ = async_duration;
     50   }
     51 
     52   // Causing an error on |STAGE_ESTABLISHING_CONNECTION| stage once.
     53   void SetShouldFailOnConnecting();
     54 
     55   // Causing a connection loss on |stage_begin| and a connection recovery
     56   // on |stage_end| once.
     57   void SetShouldLoseConnection(Stage stage_begin, Stage stage_end);
     58 
     59   // Makes host enrollment to fail once.
     60   void SetEnrollmentShouldFail();
     61 
     62   // Sets devices discovery scenario for |STAGE_DEVICES_DESCOVERY| stage.
     63   // Events are executed with interval of |async_duration_|.
     64   // For default scenario refer to implementation.
     65   void SetDiscoveryScenario(const DiscoveryScenario& discovery_scenario);
     66 
     67   // Overridden from ControllerPairingFlow:
     68   virtual void AddObserver(Observer* observer) OVERRIDE;
     69   virtual void RemoveObserver(Observer* observer) OVERRIDE;
     70   virtual Stage GetCurrentStage() OVERRIDE;
     71   virtual void StartFlow() OVERRIDE;
     72   virtual DeviceIdList GetDiscoveredDevices() OVERRIDE;
     73   virtual void ChooseDeviceForPairing(const std::string& device_id) OVERRIDE;
     74   virtual void RepeatDiscovery() OVERRIDE;
     75   virtual std::string GetConfirmationCode() OVERRIDE;
     76   virtual void SetConfirmationCodeIsCorrect(bool correct) OVERRIDE;
     77   virtual void OnAuthenticationDone(
     78       const chromeos::UserContext& user_context,
     79       content::BrowserContext* browser_context) OVERRIDE;
     80   virtual void StartSession() OVERRIDE;
     81 
     82  private:
     83   void ChangeStage(Stage new_stage);
     84   void ChangeStageLater(Stage new_stage);
     85   void ExecuteDiscoveryEvent(size_t event_position);
     86   void DeviceFound(const std::string& device_id);
     87   void DeviceLost(const std::string& device_id);
     88 
     89   // Overridden from ui::ControllerPairingFlow::Observer:
     90   virtual void PairingStageChanged(Stage new_stage) OVERRIDE;
     91   virtual void DiscoveredDevicesListChanged() OVERRIDE;
     92 
     93   ObserverList<ControllerPairingFlow::Observer> observers_;
     94   Stage current_stage_;
     95   std::string confirmation_code_;
     96   std::string preset_confirmation_code_;
     97   base::TimeDelta async_duration_;
     98   DiscoveryScenario discovery_scenario_;
     99   std::set<std::string> discovered_devices_;
    100   std::string choosen_device_;
    101   bool should_fail_on_connecting_;
    102   Stage connection_lost_begin_;
    103   Stage connection_lost_end_;
    104   bool enrollment_should_fail_;
    105 
    106   DISALLOW_COPY_AND_ASSIGN(FakeControllerPairingFlow);
    107 };
    108 
    109 }  // namespace chromeos
    110 
    111 #endif  // CHROMEOS_PAIRING_FAKE_CONTROLLER_PAIRING_FLOW_H_
    112