OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/components/tether/host_scan_cache.h" | 5 #include "chromeos/components/tether/host_scan_cache.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <unordered_map> | 8 #include <unordered_map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/test/scoped_task_environment.h" | 13 #include "base/test/scoped_task_environment.h" |
14 #include "base/timer/mock_timer.h" | 14 #include "base/timer/mock_timer.h" |
15 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | 15 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
16 #include "chromeos/components/tether/fake_active_host.h" | 16 #include "chromeos/components/tether/fake_active_host.h" |
17 #include "chromeos/components/tether/fake_host_scan_cache.h" | 17 #include "chromeos/components/tether/fake_host_scan_cache.h" |
18 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" | 18 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" |
| 19 #include "chromeos/components/tether/timer_factory.h" |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 20 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "chromeos/network/network_state.h" | 21 #include "chromeos/network/network_state.h" |
21 #include "chromeos/network/network_state_handler.h" | 22 #include "chromeos/network/network_state_handler.h" |
22 #include "chromeos/network/network_state_test.h" | 23 #include "chromeos/network/network_state_test.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 using testing::NiceMock; | 27 using testing::NiceMock; |
27 using testing::Invoke; | 28 using testing::Invoke; |
28 | 29 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 explicit ExtendedMockTimer(const base::Closure& destructor_callback) | 64 explicit ExtendedMockTimer(const base::Closure& destructor_callback) |
64 : base::MockTimer(true /* retain_user_task */, false /* is_repeating */), | 65 : base::MockTimer(true /* retain_user_task */, false /* is_repeating */), |
65 destructor_callback_(destructor_callback) {} | 66 destructor_callback_(destructor_callback) {} |
66 | 67 |
67 ~ExtendedMockTimer() override { destructor_callback_.Run(); } | 68 ~ExtendedMockTimer() override { destructor_callback_.Run(); } |
68 | 69 |
69 private: | 70 private: |
70 base::Closure destructor_callback_; | 71 base::Closure destructor_callback_; |
71 }; | 72 }; |
72 | 73 |
| 74 class TestTimerFactory : public TimerFactory { |
| 75 public: |
| 76 TestTimerFactory() {} |
| 77 ~TestTimerFactory() override {} |
| 78 |
| 79 std::unordered_map<std::string, ExtendedMockTimer*>& |
| 80 tether_network_guid_to_timer_map() { |
| 81 return tether_network_guid_to_timer_map_; |
| 82 } |
| 83 |
| 84 void set_tether_network_guid_for_next_timer( |
| 85 const std::string& tether_network_guid_for_next_timer) { |
| 86 tether_network_guid_for_next_timer_ = tether_network_guid_for_next_timer; |
| 87 } |
| 88 |
| 89 // TimerFactory: |
| 90 std::unique_ptr<base::Timer> CreateOneShotTimer() override { |
| 91 EXPECT_FALSE(tether_network_guid_for_next_timer_.empty()); |
| 92 ExtendedMockTimer* mock_timer = new ExtendedMockTimer(base::Bind( |
| 93 &TestTimerFactory::OnActiveTimerDestructor, base::Unretained(this), |
| 94 tether_network_guid_for_next_timer_)); |
| 95 tether_network_guid_to_timer_map_[tether_network_guid_for_next_timer_] = |
| 96 mock_timer; |
| 97 return base::WrapUnique(mock_timer); |
| 98 } |
| 99 |
| 100 private: |
| 101 void OnActiveTimerDestructor(const std::string& tether_network_guid) { |
| 102 tether_network_guid_to_timer_map_.erase( |
| 103 tether_network_guid_to_timer_map_.find(tether_network_guid)); |
| 104 } |
| 105 |
| 106 std::string tether_network_guid_for_next_timer_; |
| 107 std::unordered_map<std::string, ExtendedMockTimer*> |
| 108 tether_network_guid_to_timer_map_; |
| 109 }; |
| 110 |
73 } // namespace | 111 } // namespace |
74 | 112 |
75 // TODO(khorimoto): The test uses a FakeHostScanCache to keep an in-memory | 113 // TODO(khorimoto): The test uses a FakeHostScanCache to keep an in-memory |
76 // cache of expected values. This has the potential to be confusing, since this | 114 // cache of expected values. This has the potential to be confusing, since this |
77 // is the test for HostScanCache. Clean this up to avoid using FakeHostScanCache | 115 // is the test for HostScanCache. Clean this up to avoid using FakeHostScanCache |
78 // if possible. | 116 // if possible. |
79 class HostScanCacheTest : public NetworkStateTest { | 117 class HostScanCacheTest : public NetworkStateTest { |
80 protected: | 118 protected: |
81 class TestTimerFactory : public HostScanCache::TimerFactory { | |
82 public: | |
83 TestTimerFactory() {} | |
84 ~TestTimerFactory() {} | |
85 | |
86 std::unordered_map<std::string, ExtendedMockTimer*>& | |
87 tether_network_guid_to_timer_map() { | |
88 return tether_network_guid_to_timer_map_; | |
89 } | |
90 | |
91 void set_tether_network_guid_for_next_timer( | |
92 const std::string& tether_network_guid_for_next_timer) { | |
93 tether_network_guid_for_next_timer_ = tether_network_guid_for_next_timer; | |
94 } | |
95 | |
96 // HostScanCache::TimerFactory: | |
97 std::unique_ptr<base::Timer> CreateOneShotTimer() override { | |
98 EXPECT_FALSE(tether_network_guid_for_next_timer_.empty()); | |
99 ExtendedMockTimer* mock_timer = new ExtendedMockTimer(base::Bind( | |
100 &TestTimerFactory::OnActiveTimerDestructor, base::Unretained(this), | |
101 tether_network_guid_for_next_timer_)); | |
102 tether_network_guid_to_timer_map_[tether_network_guid_for_next_timer_] = | |
103 mock_timer; | |
104 return base::WrapUnique(mock_timer); | |
105 } | |
106 | |
107 private: | |
108 void OnActiveTimerDestructor(const std::string& tether_network_guid) { | |
109 tether_network_guid_to_timer_map_.erase( | |
110 tether_network_guid_to_timer_map_.find(tether_network_guid)); | |
111 } | |
112 | |
113 std::string tether_network_guid_for_next_timer_; | |
114 std::unordered_map<std::string, ExtendedMockTimer*> | |
115 tether_network_guid_to_timer_map_; | |
116 }; | |
117 | |
118 HostScanCacheTest() {} | 119 HostScanCacheTest() {} |
119 | 120 |
120 void SetUp() override { | 121 void SetUp() override { |
121 DBusThreadManager::Initialize(); | 122 DBusThreadManager::Initialize(); |
122 NetworkStateTest::SetUp(); | 123 NetworkStateTest::SetUp(); |
123 network_state_handler()->SetTetherTechnologyState( | 124 network_state_handler()->SetTetherTechnologyState( |
124 NetworkStateHandler::TECHNOLOGY_ENABLED); | 125 NetworkStateHandler::TECHNOLOGY_ENABLED); |
125 | 126 |
126 test_timer_factory_ = new TestTimerFactory(); | 127 test_timer_factory_ = new TestTimerFactory(); |
127 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); | 128 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 | 439 |
439 // Simulate a connection to device 1. | 440 // Simulate a connection to device 1. |
440 SetActiveHost(kTetherGuid1); | 441 SetActiveHost(kTetherGuid1); |
441 SetHasConnectedToHost(kTetherGuid1); | 442 SetHasConnectedToHost(kTetherGuid1); |
442 VerifyCacheMatchesNetworkStack(); | 443 VerifyCacheMatchesNetworkStack(); |
443 } | 444 } |
444 | 445 |
445 } // namespace tether | 446 } // namespace tether |
446 | 447 |
447 } // namespace chromeos | 448 } // namespace chromeos |
OLD | NEW |