1 // Copyright 2019 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 #include "chromeos/components/sync_wifi/fake_timer_factory.h"
6 
7 #include "base/unguessable_token.h"
8 #include "chromeos/components/sync_wifi/fake_one_shot_timer.h"
9 
10 namespace chromeos {
11 
12 namespace sync_wifi {
13 
14 FakeTimerFactory::FakeTimerFactory() = default;
15 
16 FakeTimerFactory::~FakeTimerFactory() = default;
17 
CreateOneShotTimer()18 std::unique_ptr<base::OneShotTimer> FakeTimerFactory::CreateOneShotTimer() {
19   auto mock_timer = std::make_unique<FakeOneShotTimer>(
20       base::BindOnce(&FakeTimerFactory::OnOneShotTimerDeleted,
21                      weak_ptr_factory_.GetWeakPtr()));
22   id_to_timer_map_[mock_timer->id()] = mock_timer.get();
23   return mock_timer;
24 }
25 
FireAll()26 void FakeTimerFactory::FireAll() {
27   // Make a copy because firing a timer will usually destroy it.  This calls
28   // OnOneShotTimerDeleted and removes it from |id_to_timer_map_|.
29   auto id_to_timer_map_copy = id_to_timer_map_;
30   for (auto it : id_to_timer_map_copy)
31     it.second->Fire();
32 }
33 
OnOneShotTimerDeleted(const base::UnguessableToken & deleted_timer_id)34 void FakeTimerFactory::OnOneShotTimerDeleted(
35     const base::UnguessableToken& deleted_timer_id) {
36   id_to_timer_map_.erase(deleted_timer_id);
37 }
38 
39 }  // namespace sync_wifi
40 
41 }  // namespace chromeos
42