OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 <memory> | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "chrome/browser/chromeos/net/network_throttling_observer.h" | |
9 #include "chrome/common/pref_names.h" | |
10 #include "chromeos/dbus/dbus_thread_manager.h" | |
11 #include "chromeos/dbus/mock_shill_manager_client.h" | |
12 #include "chromeos/network/network_state_handler.h" | |
13 #include "components/prefs/pref_registry_simple.h" | |
14 #include "components/prefs/testing_pref_service.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using testing::_; | |
19 using testing::Mock; | |
20 using testing::NiceMock; | |
21 | |
22 namespace chromeos { | |
23 | |
24 namespace test { | |
25 | |
26 class NetworkThrottlingObserverTest : public ::testing::Test { | |
27 public: | |
28 NetworkThrottlingObserverTest() : message_loop_(new base::MessageLoop()) { | |
29 std::unique_ptr<DBusThreadManagerSetter> dbus_setter = | |
30 DBusThreadManager::GetSetterForTesting(); | |
31 // Owned by DBusThreadManager, which takes care of cleaning it | |
32 mock_manager_client_ = new NiceMock<MockShillManagerClient>(); | |
33 dbus_setter->SetShillManagerClient( | |
34 std::unique_ptr<ShillManagerClient>(mock_manager_client_)); | |
35 network_state_handler_.reset(NetworkStateHandler::InitializeForTest()); | |
36 NetworkHandler::Initialize(); | |
37 local_state_.reset(new TestingPrefServiceSimple()); | |
Lei Zhang
2016/11/04 00:44:19
Prefer: local_state_ = base::MakeUnique<TestingPre
| |
38 local_state_.get()->registry()->RegisterDictionaryPref( | |
Lei Zhang
2016/11/04 00:44:19
You don't need the ".get()" here. std::unique_ptr
| |
39 prefs::kNetworkThrottlingEnabled); | |
40 observer_ = base::MakeUnique<NetworkThrottlingObserver>(local_state_.get()); | |
41 } | |
42 | |
43 ~NetworkThrottlingObserverTest() override { | |
44 observer_.reset(); | |
Lei Zhang
2016/11/04 00:44:19
These 2 are probably not necessary, but probably c
| |
45 local_state_.reset(); | |
46 network_state_handler_.reset(); | |
47 NetworkHandler::Shutdown(); | |
48 DBusThreadManager::Shutdown(); | |
49 delete message_loop_; | |
50 } | |
51 | |
52 base::MessageLoop* message_loop_; | |
Lei Zhang
2016/11/04 00:44:19
This is not what I mean, just declare this as: bas
| |
53 std::unique_ptr<NetworkStateHandler> network_state_handler_; | |
54 std::unique_ptr<TestingPrefServiceSimple> local_state_; | |
55 std::unique_ptr<NetworkThrottlingObserver> observer_; | |
56 MockShillManagerClient* mock_manager_client_; | |
57 | |
58 private: | |
59 DISALLOW_COPY_AND_ASSIGN(NetworkThrottlingObserverTest); | |
60 }; | |
61 | |
62 TEST_F(NetworkThrottlingObserverTest, ThrottlingChangeCallsShill) { | |
63 // Test that a change in the throttling policy value leads to | |
64 // shill_manager_client being called. | |
65 base::DictionaryValue updated_throttling_policy; | |
66 bool enabled = true; | |
67 uint32_t upload_rate = 1200; | |
68 uint32_t download_rate = 2000; | |
69 updated_throttling_policy.SetBoolean("enabled", enabled); | |
70 updated_throttling_policy.SetInteger("upload_rate_kbits", upload_rate); | |
71 updated_throttling_policy.SetInteger("download_rate_kbits", download_rate); | |
72 EXPECT_CALL( | |
73 *mock_manager_client_, | |
74 SetNetworkThrottlingStatus(enabled, upload_rate, download_rate, _, _)) | |
75 .Times(1); | |
76 local_state_->Set(prefs::kNetworkThrottlingEnabled, | |
77 updated_throttling_policy); | |
78 Mock::VerifyAndClearExpectations(mock_manager_client_); | |
79 | |
80 // Clearing the preference should disable throttling | |
81 EXPECT_CALL(*mock_manager_client_, | |
82 SetNetworkThrottlingStatus(false, 0, 0, _, _)) | |
83 .Times(1); | |
84 local_state_->ClearPref(prefs::kNetworkThrottlingEnabled); | |
85 } | |
86 | |
87 } // namespace test | |
88 } // namespace chromeos | |
OLD | NEW |