mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 4 | |
| 5 | #include "shill/manager.h" |
| 6 | |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 7 | #include <map> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 8 | #include <set> |
| 9 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 12 | #include <base/file_util.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 13 | #include <base/scoped_temp_dir.h> |
| 14 | #include <base/stl_util.h> |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 15 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 19 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 20 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 21 | #include "shill/ephemeral_profile.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 22 | #include "shill/error.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 23 | #include "shill/glib.h" |
| 24 | #include "shill/key_file_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 25 | #include "shill/key_value_store.h" |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 26 | #include "shill/logging.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 27 | #include "shill/mock_adaptors.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 28 | #include "shill/mock_connection.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 29 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 30 | #include "shill/mock_device.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 31 | #include "shill/mock_device_info.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | #include "shill/mock_glib.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 33 | #include "shill/mock_metrics.h" |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 34 | #include "shill/mock_power_manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 35 | #include "shill/mock_profile.h" |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 36 | #include "shill/mock_resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 37 | #include "shill/mock_service.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 38 | #include "shill/mock_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 39 | #include "shill/mock_wifi.h" |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 40 | #include "shill/mock_wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 41 | #include "shill/property_store_unittest.h" |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 42 | #include "shill/proxy_factory.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 43 | #include "shill/service_under_test.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 44 | #include "shill/wifi_service.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 45 | #include "shill/wimax_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 46 | |
| 47 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 48 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 49 | using std::string; |
| 50 | using std::vector; |
| 51 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 52 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 53 | using ::testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 54 | using ::testing::AnyNumber; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 55 | using ::testing::ContainerEq; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 56 | using ::testing::DoAll; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 57 | using ::testing::InSequence; |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 58 | using ::testing::Mock; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 59 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 60 | using ::testing::NiceMock; |
| 61 | using ::testing::Return; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 62 | using ::testing::ReturnRef; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 63 | using ::testing::SaveArg; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 64 | using ::testing::StrEq; |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 65 | using ::testing::StrictMock; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 66 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 67 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 68 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 69 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 70 | ManagerTest() |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame^] | 71 | : power_manager_(new MockPowerManager(NULL, &proxy_factory_)), |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 72 | mock_wifi_(new NiceMock<MockWiFi>(control_interface(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 73 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | metrics(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 75 | manager(), |
| 76 | "wifi0", |
| 77 | "addr4", |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 78 | 4)), |
| 79 | device_info_(new NiceMock<MockDeviceInfo>( |
| 80 | control_interface(), |
| 81 | reinterpret_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 82 | reinterpret_cast<Metrics*>(NULL), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 83 | reinterpret_cast<Manager*>(NULL))), |
| 84 | manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 85 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 86 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 87 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 88 | manager(), |
| 89 | "null0", |
| 90 | "addr0", |
| 91 | 0)); |
| 92 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 93 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 94 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 95 | manager(), |
| 96 | "null1", |
| 97 | "addr1", |
| 98 | 1)); |
| 99 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 100 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 101 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 102 | manager(), |
| 103 | "null2", |
| 104 | "addr2", |
| 105 | 2)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 106 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 107 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 108 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 109 | manager(), |
| 110 | "null3", |
| 111 | "addr3", |
| 112 | 3)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 113 | manager()->connect_profiles_to_rpc_ = false; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 114 | |
| 115 | // Replace the manager's adaptor with a quieter one, and one |
| 116 | // we can do EXPECT*() against. Passes ownership. |
| 117 | manager()->adaptor_.reset(manager_adaptor_); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 118 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 119 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 120 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 121 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 122 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 123 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 124 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 125 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 126 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 127 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 128 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 129 | void AdoptProfile(Manager *manager, ProfileRefPtr profile) { |
| 130 | manager->profiles_.push_back(profile); |
| 131 | } |
| 132 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 133 | ProfileRefPtr GetEphemeralProfile(Manager *manager) { |
| 134 | return manager->ephemeral_profile_; |
| 135 | } |
| 136 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 137 | Profile *CreateProfileForManager(Manager *manager, GLib *glib) { |
| 138 | Profile::Identifier id("rather", "irrelevant"); |
| 139 | scoped_ptr<Profile> profile(new Profile(control_interface(), |
| 140 | manager, |
| 141 | id, |
| 142 | "", |
| 143 | false)); |
| 144 | FilePath final_path(storage_path()); |
| 145 | final_path = final_path.Append("test.profile"); |
| 146 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 147 | storage->set_path(final_path); |
| 148 | if (!storage->Open()) |
| 149 | return NULL; |
| 150 | profile->set_storage(storage.release()); // Passes ownership. |
| 151 | return profile.release(); |
| 152 | } |
| 153 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 154 | bool CreateBackingStoreForService(ScopedTempDir *temp_dir, |
| 155 | const string &profile_identifier, |
| 156 | const string &service_name) { |
| 157 | GLib glib; |
| 158 | KeyFileStore store(&glib); |
| 159 | store.set_path(temp_dir->path().Append(profile_identifier + ".profile")); |
| 160 | return store.Open() && |
| 161 | store.SetString(service_name, "rather", "irrelevant") && |
| 162 | store.Close(); |
| 163 | } |
| 164 | |
| 165 | Error::Type TestCreateProfile(Manager *manager, const string &name) { |
| 166 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 167 | string path; |
| 168 | manager->CreateProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 169 | return error.type(); |
| 170 | } |
| 171 | |
| 172 | Error::Type TestPopAnyProfile(Manager *manager) { |
| 173 | Error error; |
| 174 | manager->PopAnyProfile(&error); |
| 175 | return error.type(); |
| 176 | } |
| 177 | |
| 178 | Error::Type TestPopProfile(Manager *manager, const string &name) { |
| 179 | Error error; |
| 180 | manager->PopProfile(name, &error); |
| 181 | return error.type(); |
| 182 | } |
| 183 | |
| 184 | Error::Type TestPushProfile(Manager *manager, const string &name) { |
| 185 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 186 | string path; |
| 187 | manager->PushProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 188 | return error.type(); |
| 189 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 190 | |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 191 | void AddMockProfileToManager(Manager *manager) { |
| 192 | scoped_refptr<MockProfile> profile( |
| 193 | new MockProfile(control_interface(), manager, "")); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 194 | EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return("/")); |
| 195 | EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false)); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 196 | AdoptProfile(manager, profile); |
| 197 | } |
| 198 | |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 199 | void CompleteServiceSort() { |
| 200 | EXPECT_FALSE(manager()->sort_services_task_.IsCancelled()); |
| 201 | dispatcher()->DispatchPendingEvents(); |
| 202 | EXPECT_TRUE(manager()->sort_services_task_.IsCancelled()); |
| 203 | } |
| 204 | |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 205 | RpcIdentifier GetDefaultServiceRpcIdentifier() { |
| 206 | return manager()->GetDefaultServiceRpcIdentifier(NULL); |
| 207 | } |
| 208 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 209 | void SetResolver(Resolver *resolver) { |
| 210 | manager()->resolver_ = resolver; |
| 211 | } |
| 212 | |
| 213 | void SetIgnoredDNSSearchPaths(const string &search_paths) { |
| 214 | manager()->SetIgnoredDNSSearchPaths(search_paths, NULL); |
| 215 | } |
| 216 | |
| 217 | const string &GetIgnoredDNSSearchPaths() { |
| 218 | return manager()->props_.ignored_dns_search_paths; |
| 219 | } |
| 220 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 221 | protected: |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 222 | typedef scoped_refptr<MockService> MockServiceRefPtr; |
| 223 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 224 | class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> { |
| 225 | public: |
| 226 | ServiceWatcher() {} |
| 227 | virtual ~ServiceWatcher() {} |
| 228 | |
| 229 | MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service)); |
| 230 | |
| 231 | private: |
| 232 | DISALLOW_COPY_AND_ASSIGN(ServiceWatcher); |
| 233 | }; |
| 234 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 235 | class TestProxyFactory : public ProxyFactory { |
| 236 | public: |
| 237 | TestProxyFactory() {} |
| 238 | |
| 239 | virtual PowerManagerProxyInterface *CreatePowerManagerProxy( |
| 240 | PowerManagerProxyDelegate */*delegate*/) { |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | DISALLOW_COPY_AND_ASSIGN(TestProxyFactory); |
| 246 | }; |
| 247 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame^] | 248 | class TerminationActionTest : |
| 249 | public base::SupportsWeakPtr<TerminationActionTest> { |
| 250 | public: |
| 251 | static const char kActionName[]; |
| 252 | |
| 253 | TerminationActionTest() : manager_(NULL) {} |
| 254 | virtual ~TerminationActionTest() {} |
| 255 | |
| 256 | MOCK_METHOD1(Done, void(const Error &error)); |
| 257 | |
| 258 | void Action() { |
| 259 | manager_->TerminationActionComplete("action"); |
| 260 | } |
| 261 | |
| 262 | void set_manager(Manager *manager) { manager_ = manager; } |
| 263 | |
| 264 | private: |
| 265 | Manager *manager_; |
| 266 | DISALLOW_COPY_AND_ASSIGN(TerminationActionTest); |
| 267 | }; |
| 268 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 269 | void SetPowerState(PowerManagerProxyDelegate::SuspendState state) { |
| 270 | power_manager_->power_state_ = state; |
| 271 | } |
| 272 | |
| 273 | void SetPowerManager() { |
| 274 | manager()->set_power_manager(power_manager_.release()); |
| 275 | } |
| 276 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame^] | 277 | HookTable *GetTerminationActions() { |
| 278 | return &manager()->termination_actions_; |
| 279 | } |
| 280 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 281 | void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) { |
| 282 | manager()->OnPowerStateChanged(state); |
| 283 | } |
| 284 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame^] | 285 | void OnSuspendDelay(uint32 sequence_number) { |
| 286 | manager()->OnSuspendDelay(sequence_number); |
| 287 | } |
| 288 | |
| 289 | void OnSuspendActionsComplete(uint32 sequence_number, const Error &error) { |
| 290 | manager()->OnSuspendActionsComplete(sequence_number, error); |
| 291 | } |
| 292 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 293 | MockServiceRefPtr MakeAutoConnectableService() { |
| 294 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 295 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 296 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 297 | manager()); |
| 298 | service->MakeFavorite(); |
| 299 | service->set_connectable(true); |
| 300 | return service; |
| 301 | } |
| 302 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 303 | TestProxyFactory proxy_factory_; |
| 304 | scoped_ptr<MockPowerManager> power_manager_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 305 | scoped_refptr<MockWiFi> mock_wifi_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 306 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 307 | scoped_ptr<MockDeviceInfo> device_info_; |
| 308 | |
| 309 | // This pointer is owned by the manager, and only tracked here for EXPECT*() |
| 310 | ManagerMockAdaptor *manager_adaptor_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame^] | 313 | const char ManagerTest::TerminationActionTest::kActionName[] = "action"; |
| 314 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 315 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 316 | if (!manager()->sort_services_task_.IsCancelled()) { |
| 317 | manager()->SortServicesTask(); |
| 318 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 319 | return (svc0.get() == manager()->services_[0].get() && |
| 320 | svc1.get() == manager()->services_[1].get()); |
| 321 | } |
| 322 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 323 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 324 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 325 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 328 | TEST_F(ManagerTest, DeviceRegistration) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 329 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 330 | .WillByDefault(Return(Technology::kEthernet)); |
| 331 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 332 | .WillByDefault(Return(Technology::kWifi)); |
| 333 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 334 | .WillByDefault(Return(Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 335 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 336 | manager()->RegisterDevice(mock_devices_[0]); |
| 337 | manager()->RegisterDevice(mock_devices_[1]); |
| 338 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 339 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 340 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 341 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 342 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 345 | TEST_F(ManagerTest, DeviceRegistrationAndStart) { |
| 346 | manager()->running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 347 | mock_devices_[0]->enabled_persistent_ = true; |
| 348 | mock_devices_[1]->enabled_persistent_ = false; |
| 349 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 350 | .Times(1); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 351 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 352 | .Times(0); |
| 353 | manager()->RegisterDevice(mock_devices_[0]); |
| 354 | manager()->RegisterDevice(mock_devices_[1]); |
| 355 | } |
| 356 | |
| 357 | TEST_F(ManagerTest, DeviceRegistrationWithProfile) { |
| 358 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 359 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 360 | AdoptProfile(manager(), profile); // Passes ownership. |
| 361 | EXPECT_CALL(*profile, ConfigureDevice(device_ref)); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 362 | EXPECT_CALL(*profile, UpdateDevice(device_ref)); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 363 | manager()->RegisterDevice(mock_devices_[0]); |
| 364 | } |
| 365 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 366 | TEST_F(ManagerTest, DeviceDeregistration) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 367 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 368 | .WillByDefault(Return(Technology::kEthernet)); |
| 369 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 370 | .WillByDefault(Return(Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 371 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 372 | manager()->RegisterDevice(mock_devices_[0]); |
| 373 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 374 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 375 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 376 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 377 | |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 378 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 379 | AdoptProfile(manager(), profile); // Passes ownership. |
| 380 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 381 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false)); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 382 | EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0]))); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 383 | manager()->DeregisterDevice(mock_devices_[0]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 384 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 385 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 386 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false)); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 387 | EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1]))); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 388 | manager()->DeregisterDevice(mock_devices_[1]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 389 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 393 | // It's much easier and safer to use a real GLib for this test. |
| 394 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 395 | Manager manager(control_interface(), |
| 396 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 397 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 398 | &glib, |
| 399 | run_path(), |
| 400 | storage_path(), |
| 401 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 402 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 403 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 404 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 405 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 406 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 407 | new NiceMock<MockService>(control_interface(), |
| 408 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 409 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 410 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 411 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 412 | new NiceMock<MockService>(control_interface(), |
| 413 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 414 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 415 | &manager)); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 416 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 417 | string service1_name(mock_service->UniqueName()); |
| 418 | string service2_name(mock_service2->UniqueName()); |
| 419 | |
| 420 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 421 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 422 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 423 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 424 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 425 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 426 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 427 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 428 | manager.RegisterService(mock_service); |
| 429 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 430 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 431 | Error error; |
| 432 | vector<string> rpc_ids = manager.EnumerateAvailableServices(&error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 433 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 434 | EXPECT_EQ(2, ids.size()); |
| 435 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 436 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 437 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 438 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 439 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 440 | |
| 441 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 444 | TEST_F(ManagerTest, RegisterKnownService) { |
| 445 | // It's much easier and safer to use a real GLib for this test. |
| 446 | GLib glib; |
| 447 | Manager manager(control_interface(), |
| 448 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 449 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 450 | &glib, |
| 451 | run_path(), |
| 452 | storage_path(), |
| 453 | string()); |
| 454 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 455 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 456 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 457 | { |
| 458 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 459 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 460 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 461 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 462 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 463 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 464 | } // Force destruction of service1. |
| 465 | |
| 466 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 467 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 468 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 469 | &manager)); |
| 470 | manager.RegisterService(service2); |
| 471 | EXPECT_EQ(service2->profile().get(), profile.get()); |
| 472 | manager.Stop(); |
| 473 | } |
| 474 | |
| 475 | TEST_F(ManagerTest, RegisterUnknownService) { |
| 476 | // It's much easier and safer to use a real GLib for this test. |
| 477 | GLib glib; |
| 478 | Manager manager(control_interface(), |
| 479 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 480 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 481 | &glib, |
| 482 | run_path(), |
| 483 | storage_path(), |
| 484 | string()); |
| 485 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 486 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 487 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 488 | { |
| 489 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 490 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 491 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 492 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 493 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 494 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 495 | } // Force destruction of service1. |
| 496 | scoped_refptr<MockService> mock_service2( |
| 497 | new NiceMock<MockService>(control_interface(), |
| 498 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 499 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 500 | &manager)); |
| 501 | EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier()) |
| 502 | .WillRepeatedly(Return(mock_service2->UniqueName())); |
| 503 | manager.RegisterService(mock_service2); |
| 504 | EXPECT_NE(mock_service2->profile().get(), profile.get()); |
| 505 | manager.Stop(); |
| 506 | } |
| 507 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 508 | TEST_F(ManagerTest, DeregisterUnregisteredService) { |
| 509 | // WiFi assumes that it can deregister a service that is not |
| 510 | // registered. (E.g. a hidden service can be deregistered when it |
| 511 | // loses its last endpoint, and again when WiFi is Stop()-ed.) |
| 512 | // |
| 513 | // So test that doing so doesn't cause a crash. |
| 514 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 515 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 516 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 517 | manager()); |
| 518 | manager()->DeregisterService(service); |
| 519 | } |
| 520 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 521 | TEST_F(ManagerTest, GetProperties) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 522 | AddMockProfileToManager(manager()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 523 | map<string, ::DBus::Variant> props; |
| 524 | Error error(Error::kInvalidProperty, ""); |
| 525 | { |
| 526 | ::DBus::Error dbus_error; |
| 527 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 528 | manager()->mutable_store()->SetStringProperty( |
| 529 | flimflam::kCheckPortalListProperty, |
| 530 | expected, |
| 531 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 532 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 533 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 534 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 535 | expected); |
| 536 | } |
| 537 | { |
| 538 | ::DBus::Error dbus_error; |
| 539 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 540 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 541 | expected, |
| 542 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 543 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 544 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 545 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 546 | expected); |
| 547 | } |
| 548 | } |
| 549 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 550 | TEST_F(ManagerTest, GetDevicesProperty) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 551 | AddMockProfileToManager(manager()); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 552 | manager()->RegisterDevice(mock_devices_[0]); |
| 553 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 554 | { |
| 555 | map<string, ::DBus::Variant> props; |
| 556 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 557 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 558 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 559 | vector < ::DBus::Path> devices = |
| 560 | props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>(); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 561 | EXPECT_EQ(2, devices.size()); |
| 562 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 563 | } |
| 564 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 565 | TEST_F(ManagerTest, GetServicesProperty) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 566 | AddMockProfileToManager(manager()); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 567 | map<string, ::DBus::Variant> props; |
| 568 | ::DBus::Error dbus_error; |
| 569 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
| 570 | map<string, ::DBus::Variant>::const_iterator prop = |
| 571 | props.find(flimflam::kServicesProperty); |
| 572 | ASSERT_FALSE(prop == props.end()); |
| 573 | const ::DBus::Variant &variant = prop->second; |
| 574 | ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature())); |
| 575 | } |
| 576 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 577 | TEST_F(ManagerTest, MoveService) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 578 | Manager manager(control_interface(), |
| 579 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 580 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 581 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 582 | run_path(), |
| 583 | storage_path(), |
| 584 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 585 | scoped_refptr<MockService> s2(new MockService(control_interface(), |
| 586 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 587 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 588 | &manager)); |
| 589 | // Inject an actual profile, backed by a fake StoreInterface |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 590 | { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 591 | Profile::Identifier id("irrelevant"); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 592 | ProfileRefPtr profile( |
| 593 | new Profile(control_interface(), &manager, id, "", false)); |
| 594 | MockStore *storage = new MockStore; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 595 | EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier())) |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 596 | .WillRepeatedly(Return(true)); |
| 597 | EXPECT_CALL(*storage, Flush()) |
| 598 | .Times(AnyNumber()) |
| 599 | .WillRepeatedly(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 600 | profile->set_storage(storage); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 601 | AdoptProfile(&manager, profile); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 602 | } |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 603 | // Create a profile that already has |s2| in it. |
| 604 | ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager)); |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 605 | EXPECT_TRUE(profile->AdoptService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 606 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 607 | // Now, move the Service |s2| to another profile. |
| 608 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 609 | ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 610 | |
| 611 | // Force destruction of the original Profile, to ensure that the Service |
| 612 | // is kept alive and populated with data. |
| 613 | profile = NULL; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 614 | ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 615 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 618 | TEST_F(ManagerTest, LookupProfileByRpcIdentifier) { |
| 619 | scoped_refptr<MockProfile> mock_profile( |
| 620 | new MockProfile(control_interface(), manager(), "")); |
| 621 | const string kProfileName("profile0"); |
| 622 | EXPECT_CALL(*mock_profile, GetRpcIdentifier()) |
| 623 | .WillRepeatedly(Return(kProfileName)); |
| 624 | AdoptProfile(manager(), mock_profile); |
| 625 | |
| 626 | EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo")); |
| 627 | ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName); |
| 628 | EXPECT_EQ(mock_profile.get(), profile.get()); |
| 629 | } |
| 630 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 631 | TEST_F(ManagerTest, SetProfileForService) { |
| 632 | scoped_refptr<MockProfile> profile0( |
| 633 | new MockProfile(control_interface(), manager(), "")); |
| 634 | string profile_name0("profile0"); |
| 635 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 636 | .WillRepeatedly(Return(profile_name0)); |
| 637 | AdoptProfile(manager(), profile0); |
| 638 | scoped_refptr<MockService> service(new MockService(control_interface(), |
| 639 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 640 | metrics(), |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 641 | manager())); |
Paul Stewart | 649f3a4 | 2012-04-24 23:22:16 -0700 | [diff] [blame] | 642 | EXPECT_FALSE(manager()->HasService(service)); |
| 643 | { |
| 644 | Error error; |
| 645 | EXPECT_CALL(*profile0, AdoptService(_)) |
| 646 | .WillOnce(Return(true)); |
| 647 | // Expect that setting the profile of a service that does not already |
| 648 | // have one assigned does not cause a crash. |
| 649 | manager()->SetProfileForService(service, "profile0", &error); |
| 650 | EXPECT_TRUE(error.IsSuccess()); |
| 651 | } |
| 652 | |
| 653 | // The service should be registered as a side-effect of the profile being |
| 654 | // set for this service. |
| 655 | EXPECT_TRUE(manager()->HasService(service)); |
| 656 | |
| 657 | // Since we have mocked Profile::AdoptServie() above, the service's |
| 658 | // profile was not actually changed. Do so explicitly now. |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 659 | service->set_profile(profile0); |
| 660 | |
| 661 | { |
| 662 | Error error; |
| 663 | manager()->SetProfileForService(service, "foo", &error); |
| 664 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 665 | EXPECT_EQ("Unknown Profile foo requested for Service", error.message()); |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | { |
| 669 | Error error; |
| 670 | manager()->SetProfileForService(service, profile_name0, &error); |
| 671 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 672 | EXPECT_EQ("Service is already connected to this profile", error.message()); |
| 673 | } |
| 674 | |
| 675 | scoped_refptr<MockProfile> profile1( |
| 676 | new MockProfile(control_interface(), manager(), "")); |
| 677 | string profile_name1("profile1"); |
| 678 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 679 | .WillRepeatedly(Return(profile_name1)); |
| 680 | AdoptProfile(manager(), profile1); |
| 681 | |
| 682 | { |
| 683 | Error error; |
| 684 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 685 | .WillOnce(Return(true)); |
| 686 | EXPECT_CALL(*profile0, AbandonService(_)) |
| 687 | .WillOnce(Return(true)); |
| 688 | manager()->SetProfileForService(service, profile_name1, &error); |
| 689 | EXPECT_TRUE(error.IsSuccess()); |
| 690 | } |
| 691 | } |
| 692 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 693 | TEST_F(ManagerTest, CreateProfile) { |
| 694 | // It's much easier to use real Glib here since we want the storage |
| 695 | // side-effects. |
| 696 | GLib glib; |
| 697 | ScopedTempDir temp_dir; |
| 698 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 699 | |
| 700 | Manager manager(control_interface(), |
| 701 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 702 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 703 | &glib, |
| 704 | run_path(), |
| 705 | storage_path(), |
| 706 | temp_dir.path().value()); |
| 707 | |
| 708 | // Invalid name should be rejected. |
| 709 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "")); |
| 710 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 711 | // A profile with invalid characters in it should similarly be rejected. |
| 712 | EXPECT_EQ(Error::kInvalidArguments, |
| 713 | TestCreateProfile(&manager, "valid_profile")); |
| 714 | |
| 715 | // We should be able to create a machine profile. |
| 716 | EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid")); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 717 | |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 718 | // We should succeed in creating a valid user profile. Verify the returned |
| 719 | // path. |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 720 | const char kProfile[] = "~user/profile"; |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 721 | { |
| 722 | Error error; |
| 723 | string path; |
| 724 | manager.CreateProfile(kProfile, &path, &error); |
| 725 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 726 | EXPECT_EQ("/profile_rpc", path); |
| 727 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 728 | |
| 729 | // We should fail in creating it a second time (already exists). |
| 730 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile)); |
| 731 | } |
| 732 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 733 | // We receive PopProfile when a user logs out, and it should always trigger a |
| 734 | // MemoryLog Clear() call. |
| 735 | TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) { |
| 736 | GLib glib; |
| 737 | ScopedTempDir temp_dir; |
| 738 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 739 | Manager manager(control_interface(), |
| 740 | dispatcher(), |
| 741 | metrics(), |
| 742 | &glib, |
| 743 | run_path(), |
| 744 | storage_path(), |
| 745 | temp_dir.path().value()); |
| 746 | const char kProfile0[] = "~user/profile0"; |
| 747 | const char kPurgedMessage[] = "This message should be purged"; |
| 748 | // Create a profile and push it on the stack, leave one uncreated |
| 749 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 750 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 751 | |
| 752 | // Popping a profile which isn't on top should still clear the log. |
| 753 | LOG(INFO) << kPurgedMessage; |
| 754 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 755 | kPurgedMessage)); |
| 756 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1")); |
| 757 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 758 | kPurgedMessage)); |
| 759 | |
| 760 | // Popping an invalid profile name should do the same thing. |
| 761 | LOG(INFO) << kPurgedMessage; |
| 762 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 763 | kPurgedMessage)); |
| 764 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 765 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 766 | kPurgedMessage)); |
| 767 | |
| 768 | // Successful pops also purge the message log. |
| 769 | LOG(INFO) << kPurgedMessage; |
| 770 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 771 | kPurgedMessage)); |
| 772 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0)); |
| 773 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 774 | kPurgedMessage)); |
| 775 | } |
| 776 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 777 | TEST_F(ManagerTest, PushPopProfile) { |
| 778 | // It's much easier to use real Glib in creating a Manager for this |
| 779 | // test here since we want the storage side-effects. |
| 780 | GLib glib; |
| 781 | ScopedTempDir temp_dir; |
| 782 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 783 | Manager manager(control_interface(), |
| 784 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 785 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 786 | &glib, |
| 787 | run_path(), |
| 788 | storage_path(), |
| 789 | temp_dir.path().value()); |
| 790 | |
| 791 | // Pushing an invalid profile should fail. |
| 792 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "")); |
| 793 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 794 | // Pushing a default profile that does not exist should fail. |
| 795 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default")); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 796 | |
| 797 | const char kProfile0[] = "~user/profile0"; |
| 798 | const char kProfile1[] = "~user/profile1"; |
| 799 | |
| 800 | // Create a couple of profiles. |
| 801 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 802 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1)); |
| 803 | |
| 804 | // Push these profiles on the stack. |
| 805 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 806 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 807 | |
| 808 | // Pushing a profile a second time should fail. |
| 809 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0)); |
| 810 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1)); |
| 811 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 812 | Error error; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 813 | // Active profile should be the last one we pushed. |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 814 | EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 815 | |
| 816 | // Make sure a profile name that doesn't exist fails. |
| 817 | const char kProfile2Id[] = "profile2"; |
| 818 | const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id); |
| 819 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2)); |
| 820 | |
| 821 | // Create a new service, with a specific storage name. |
| 822 | scoped_refptr<MockService> service( |
| 823 | new NiceMock<MockService>(control_interface(), |
| 824 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 825 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 826 | &manager)); |
| 827 | const char kServiceName[] = "service_storage_name"; |
| 828 | EXPECT_CALL(*service.get(), GetStorageIdentifier()) |
| 829 | .WillRepeatedly(Return(kServiceName)); |
| 830 | EXPECT_CALL(*service.get(), Load(_)) |
| 831 | .WillRepeatedly(Return(true)); |
| 832 | |
| 833 | // Add this service to the manager -- it should end up in the ephemeral |
| 834 | // profile. |
| 835 | manager.RegisterService(service); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 836 | ASSERT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 837 | |
| 838 | // Create storage for a profile that contains the service storage name. |
| 839 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id, |
| 840 | kServiceName)); |
| 841 | |
| 842 | // When we push the profile, the service should move away from the |
| 843 | // ephemeral profile to this new profile since it has an entry for |
| 844 | // this service. |
| 845 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 846 | EXPECT_NE(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 847 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 848 | |
| 849 | // Insert another profile that should supersede ownership of the service. |
| 850 | const char kProfile3Id[] = "profile3"; |
| 851 | const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id); |
| 852 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id, |
| 853 | kServiceName)); |
| 854 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3)); |
| 855 | EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName()); |
| 856 | |
| 857 | // Popping an invalid profile name should fail. |
| 858 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 859 | |
| 860 | // Popping an profile that is not at the top of the stack should fail. |
| 861 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0)); |
| 862 | |
| 863 | // Popping the top profile should succeed. |
| 864 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3)); |
| 865 | |
| 866 | // Moreover the service should have switched profiles to profile 2. |
| 867 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 868 | |
| 869 | // Popping the top profile should succeed. |
| 870 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 871 | |
| 872 | // The service should now revert to the ephemeral profile. |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 873 | EXPECT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 874 | |
| 875 | // Pop the remaining two services off the stack. |
| 876 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 877 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 878 | |
| 879 | // Next pop should fail with "stack is empty". |
| 880 | EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager)); |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 881 | |
| 882 | const char kMachineProfile0[] = "machineprofile0"; |
| 883 | const char kMachineProfile1[] = "machineprofile1"; |
| 884 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0)); |
| 885 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1)); |
| 886 | |
| 887 | // Should be able to push a machine profile. |
| 888 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0)); |
| 889 | |
| 890 | // Should be able to push a user profile atop a machine profile. |
| 891 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 892 | |
| 893 | // Pushing a system-wide profile on top of a user profile should fail. |
| 894 | EXPECT_EQ(Error::kInvalidArguments, |
| 895 | TestPushProfile(&manager, kMachineProfile1)); |
| 896 | |
| 897 | // However if we pop the user profile, we should be able stack another |
| 898 | // machine profile on. |
| 899 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 900 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 903 | TEST_F(ManagerTest, RemoveProfile) { |
| 904 | // It's much easier to use real Glib in creating a Manager for this |
| 905 | // test here since we want the storage side-effects. |
| 906 | GLib glib; |
| 907 | ScopedTempDir temp_dir; |
| 908 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 909 | Manager manager(control_interface(), |
| 910 | dispatcher(), |
| 911 | metrics(), |
| 912 | &glib, |
| 913 | run_path(), |
| 914 | storage_path(), |
| 915 | temp_dir.path().value()); |
| 916 | |
| 917 | const char kProfile0[] = "profile0"; |
| 918 | FilePath profile_path( |
| 919 | FilePath(storage_path()).Append(string(kProfile0) + ".profile")); |
| 920 | |
| 921 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 922 | ASSERT_TRUE(file_util::PathExists(profile_path)); |
| 923 | |
| 924 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 925 | |
| 926 | // Remove should fail since the profile is still on the stack. |
| 927 | { |
| 928 | Error error; |
| 929 | manager.RemoveProfile(kProfile0, &error); |
| 930 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 931 | } |
| 932 | |
| 933 | // Profile path should still exist. |
| 934 | EXPECT_TRUE(file_util::PathExists(profile_path)); |
| 935 | |
| 936 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 937 | |
| 938 | // This should succeed now that the profile is off the stack. |
| 939 | { |
| 940 | Error error; |
| 941 | manager.RemoveProfile(kProfile0, &error); |
| 942 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 943 | } |
| 944 | |
| 945 | // Profile path should no longer exist. |
| 946 | EXPECT_FALSE(file_util::PathExists(profile_path)); |
| 947 | |
| 948 | // Another remove succeeds, due to a foible in file_util::Delete -- |
| 949 | // it is not an error to delete a file that does not exist. |
| 950 | { |
| 951 | Error error; |
| 952 | manager.RemoveProfile(kProfile0, &error); |
| 953 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 954 | } |
| 955 | |
| 956 | // Let's create an error case that will "work". Create a non-empty |
| 957 | // directory in the place of the profile pathname. |
| 958 | ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo"))); |
| 959 | { |
| 960 | Error error; |
| 961 | manager.RemoveProfile(kProfile0, &error); |
| 962 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 963 | } |
| 964 | } |
| 965 | |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 966 | TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) { |
| 967 | // It's much easier to use real Glib in creating a Manager for this |
| 968 | // test here since we want the storage side-effects. |
| 969 | GLib glib; |
| 970 | ScopedTempDir temp_dir; |
| 971 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 972 | Manager manager(control_interface(), |
| 973 | dispatcher(), |
| 974 | metrics(), |
| 975 | &glib, |
| 976 | run_path(), |
| 977 | storage_path(), |
| 978 | temp_dir.path().value()); |
| 979 | |
| 980 | const char kProfile0[] = "profile0"; |
| 981 | FilePath profile_path( |
| 982 | FilePath(storage_path()).Append(string(kProfile0) + ".profile")); |
| 983 | |
| 984 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 985 | ASSERT_TRUE(file_util::PathExists(profile_path)); |
| 986 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 987 | |
| 988 | // Ensure that even if the backing filestore is removed, we still can't |
| 989 | // create a profile twice. |
| 990 | ASSERT_TRUE(file_util::Delete(profile_path, false)); |
| 991 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0)); |
| 992 | } |
| 993 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 994 | // Use this matcher instead of passing RefPtrs directly into the arguments |
| 995 | // of EXPECT_CALL() because otherwise we may create un-cleaned-up references at |
| 996 | // system teardown. |
| 997 | MATCHER_P(IsRefPtrTo, ref_address, "") { |
| 998 | return arg.get() == ref_address; |
| 999 | } |
| 1000 | |
| 1001 | TEST_F(ManagerTest, HandleProfileEntryDeletion) { |
| 1002 | MockServiceRefPtr s_not_in_profile( |
| 1003 | new NiceMock<MockService>(control_interface(), |
| 1004 | dispatcher(), |
| 1005 | metrics(), |
| 1006 | manager())); |
| 1007 | MockServiceRefPtr s_not_in_group( |
| 1008 | new NiceMock<MockService>(control_interface(), |
| 1009 | dispatcher(), |
| 1010 | metrics(), |
| 1011 | manager())); |
| 1012 | MockServiceRefPtr s_configure_fail( |
| 1013 | new NiceMock<MockService>(control_interface(), |
| 1014 | dispatcher(), |
| 1015 | metrics(), |
| 1016 | manager())); |
| 1017 | MockServiceRefPtr s_configure_succeed( |
| 1018 | new NiceMock<MockService>(control_interface(), |
| 1019 | dispatcher(), |
| 1020 | metrics(), |
| 1021 | manager())); |
| 1022 | |
| 1023 | string entry_name("entry_name"); |
| 1024 | EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0); |
| 1025 | EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier()) |
| 1026 | .WillRepeatedly(Return("not_entry_name")); |
| 1027 | EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier()) |
| 1028 | .WillRepeatedly(Return(entry_name)); |
| 1029 | EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier()) |
| 1030 | .WillRepeatedly(Return(entry_name)); |
| 1031 | |
| 1032 | manager()->RegisterService(s_not_in_profile); |
| 1033 | manager()->RegisterService(s_not_in_group); |
| 1034 | manager()->RegisterService(s_configure_fail); |
| 1035 | manager()->RegisterService(s_configure_succeed); |
| 1036 | |
| 1037 | scoped_refptr<MockProfile> profile0( |
| 1038 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1039 | scoped_refptr<MockProfile> profile1( |
| 1040 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1041 | |
| 1042 | s_not_in_group->set_profile(profile1); |
| 1043 | s_configure_fail->set_profile(profile1); |
| 1044 | s_configure_succeed->set_profile(profile1); |
| 1045 | |
| 1046 | AdoptProfile(manager(), profile0); |
| 1047 | AdoptProfile(manager(), profile1); |
| 1048 | |
| 1049 | // No services are a member of this profile. |
| 1050 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name)); |
| 1051 | |
| 1052 | // No services that are members of this profile have this entry name. |
| 1053 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, "")); |
| 1054 | |
| 1055 | // Only services that are members of the profile and group will be abandoned. |
| 1056 | EXPECT_CALL(*profile1.get(), |
| 1057 | AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 1058 | EXPECT_CALL(*profile1.get(), |
| 1059 | AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 1060 | EXPECT_CALL(*profile1.get(), |
| 1061 | AbandonService(IsRefPtrTo(s_configure_fail.get()))) |
| 1062 | .WillOnce(Return(true)); |
| 1063 | EXPECT_CALL(*profile1.get(), |
| 1064 | AbandonService(IsRefPtrTo(s_configure_succeed.get()))) |
| 1065 | .WillOnce(Return(true)); |
| 1066 | |
| 1067 | // Never allow services to re-join profile1. |
| 1068 | EXPECT_CALL(*profile1.get(), ConfigureService(_)) |
| 1069 | .WillRepeatedly(Return(false)); |
| 1070 | |
| 1071 | // Only allow one of the members of the profile and group to successfully |
| 1072 | // join profile0. |
| 1073 | EXPECT_CALL(*profile0.get(), |
| 1074 | ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 1075 | EXPECT_CALL(*profile0.get(), |
| 1076 | ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 1077 | EXPECT_CALL(*profile0.get(), |
| 1078 | ConfigureService(IsRefPtrTo(s_configure_fail.get()))) |
| 1079 | .WillOnce(Return(false)); |
| 1080 | EXPECT_CALL(*profile0.get(), |
| 1081 | ConfigureService(IsRefPtrTo(s_configure_succeed.get()))) |
| 1082 | .WillOnce(Return(true)); |
| 1083 | |
| 1084 | // Expect the failed-to-configure service to have Unload() called on it. |
| 1085 | EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0); |
| 1086 | EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0); |
| 1087 | EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1); |
| 1088 | EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0); |
| 1089 | |
| 1090 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name)); |
| 1091 | |
| 1092 | EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get()); |
| 1093 | EXPECT_EQ(profile1, s_not_in_group->profile()); |
| 1094 | EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile()); |
| 1095 | |
| 1096 | // Since we are using a MockProfile, the profile does not actually change, |
| 1097 | // since ConfigureService was not actually called on the service. |
| 1098 | EXPECT_EQ(profile1, s_configure_succeed->profile()); |
| 1099 | } |
| 1100 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1101 | TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) { |
| 1102 | MockServiceRefPtr s_will_remove0( |
| 1103 | new NiceMock<MockService>(control_interface(), |
| 1104 | dispatcher(), |
| 1105 | metrics(), |
| 1106 | manager())); |
| 1107 | MockServiceRefPtr s_will_remove1( |
| 1108 | new NiceMock<MockService>(control_interface(), |
| 1109 | dispatcher(), |
| 1110 | metrics(), |
| 1111 | manager())); |
| 1112 | MockServiceRefPtr s_will_not_remove0( |
| 1113 | new NiceMock<MockService>(control_interface(), |
| 1114 | dispatcher(), |
| 1115 | metrics(), |
| 1116 | manager())); |
| 1117 | MockServiceRefPtr s_will_not_remove1( |
| 1118 | new NiceMock<MockService>(control_interface(), |
| 1119 | dispatcher(), |
| 1120 | metrics(), |
| 1121 | manager())); |
| 1122 | |
| 1123 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 1124 | .Times(4); // Once for each registration. |
| 1125 | |
| 1126 | string entry_name("entry_name"); |
| 1127 | EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier()) |
| 1128 | .WillRepeatedly(Return(entry_name)); |
| 1129 | EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier()) |
| 1130 | .WillRepeatedly(Return(entry_name)); |
| 1131 | EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier()) |
| 1132 | .WillRepeatedly(Return(entry_name)); |
| 1133 | EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier()) |
| 1134 | .WillRepeatedly(Return(entry_name)); |
| 1135 | |
| 1136 | manager()->RegisterService(s_will_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1137 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1138 | manager()->RegisterService(s_will_not_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1139 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1140 | manager()->RegisterService(s_will_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1141 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1142 | manager()->RegisterService(s_will_not_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1143 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1144 | |
| 1145 | // One for each service added above. |
| 1146 | ASSERT_EQ(4, manager()->services_.size()); |
| 1147 | |
| 1148 | scoped_refptr<MockProfile> profile( |
| 1149 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1150 | |
| 1151 | s_will_remove0->set_profile(profile); |
| 1152 | s_will_remove1->set_profile(profile); |
| 1153 | s_will_not_remove0->set_profile(profile); |
| 1154 | s_will_not_remove1->set_profile(profile); |
| 1155 | |
| 1156 | AdoptProfile(manager(), profile); |
| 1157 | |
| 1158 | // Deny any of the services re-entry to the profile. |
| 1159 | EXPECT_CALL(*profile, ConfigureService(_)) |
| 1160 | .WillRepeatedly(Return(false)); |
| 1161 | |
| 1162 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0))) |
| 1163 | .WillOnce(Return(true)); |
| 1164 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1))) |
| 1165 | .WillOnce(Return(true)); |
| 1166 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0))) |
| 1167 | .WillOnce(Return(true)); |
| 1168 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1))) |
| 1169 | .WillOnce(Return(true)); |
| 1170 | |
| 1171 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 1172 | .WillOnce(Return(true)); |
| 1173 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 1174 | .WillOnce(Return(true)); |
| 1175 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
| 1176 | .WillOnce(Return(false)); |
| 1177 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 1178 | .WillOnce(Return(false)); |
| 1179 | |
| 1180 | |
| 1181 | // This will cause all the profiles to be unloaded. |
| 1182 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name)); |
| 1183 | |
| 1184 | // 2 of the 4 services added above should have been unregistered and |
| 1185 | // removed, leaving 2. |
| 1186 | EXPECT_EQ(2, manager()->services_.size()); |
| 1187 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 1188 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
| 1189 | } |
| 1190 | |
| 1191 | TEST_F(ManagerTest, PopProfileWithUnload) { |
| 1192 | MockServiceRefPtr s_will_remove0( |
| 1193 | new NiceMock<MockService>(control_interface(), |
| 1194 | dispatcher(), |
| 1195 | metrics(), |
| 1196 | manager())); |
| 1197 | MockServiceRefPtr s_will_remove1( |
| 1198 | new NiceMock<MockService>(control_interface(), |
| 1199 | dispatcher(), |
| 1200 | metrics(), |
| 1201 | manager())); |
| 1202 | MockServiceRefPtr s_will_not_remove0( |
| 1203 | new NiceMock<MockService>(control_interface(), |
| 1204 | dispatcher(), |
| 1205 | metrics(), |
| 1206 | manager())); |
| 1207 | MockServiceRefPtr s_will_not_remove1( |
| 1208 | new NiceMock<MockService>(control_interface(), |
| 1209 | dispatcher(), |
| 1210 | metrics(), |
| 1211 | manager())); |
| 1212 | |
| 1213 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 1214 | .Times(5); // Once for each registration, and one after profile pop. |
| 1215 | |
| 1216 | manager()->RegisterService(s_will_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1217 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1218 | manager()->RegisterService(s_will_not_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1219 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1220 | manager()->RegisterService(s_will_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1221 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1222 | manager()->RegisterService(s_will_not_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1223 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1224 | |
| 1225 | // One for each service added above. |
| 1226 | ASSERT_EQ(4, manager()->services_.size()); |
| 1227 | |
| 1228 | scoped_refptr<MockProfile> profile0( |
| 1229 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1230 | scoped_refptr<MockProfile> profile1( |
| 1231 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1232 | |
| 1233 | s_will_remove0->set_profile(profile1); |
| 1234 | s_will_remove1->set_profile(profile1); |
| 1235 | s_will_not_remove0->set_profile(profile1); |
| 1236 | s_will_not_remove1->set_profile(profile1); |
| 1237 | |
| 1238 | AdoptProfile(manager(), profile0); |
| 1239 | AdoptProfile(manager(), profile1); |
| 1240 | |
| 1241 | // Deny any of the services entry to profile0, so they will all be unloaded. |
| 1242 | EXPECT_CALL(*profile0, ConfigureService(_)) |
| 1243 | .WillRepeatedly(Return(false)); |
| 1244 | |
| 1245 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 1246 | .WillOnce(Return(true)); |
| 1247 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 1248 | .WillOnce(Return(true)); |
| 1249 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 1250 | .WillRepeatedly(Return(false)); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1251 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 1252 | .WillOnce(Return(false)); |
| 1253 | |
| 1254 | // This will pop profile1, which should cause all our profiles to unload. |
| 1255 | manager()->PopProfileInternal(); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1256 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1257 | |
| 1258 | // 2 of the 4 services added above should have been unregistered and |
| 1259 | // removed, leaving 2. |
| 1260 | EXPECT_EQ(2, manager()->services_.size()); |
| 1261 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 1262 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 1263 | |
| 1264 | // Expect the unloaded services to lose their profile reference. |
| 1265 | EXPECT_FALSE(s_will_remove0->profile()); |
| 1266 | EXPECT_FALSE(s_will_remove1->profile()); |
| 1267 | |
| 1268 | // If we explicitly deregister a service, the effect should be the same |
| 1269 | // with respect to the profile reference. |
| 1270 | ASSERT_TRUE(s_will_not_remove0->profile()); |
| 1271 | manager()->DeregisterService(s_will_not_remove0); |
| 1272 | EXPECT_FALSE(s_will_not_remove0->profile()); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1275 | TEST_F(ManagerTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1276 | { |
| 1277 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1278 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1279 | flimflam::kOfflineModeProperty, |
| 1280 | PropertyStoreTest::kBoolV, |
| 1281 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1282 | } |
| 1283 | { |
| 1284 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1285 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1286 | flimflam::kCountryProperty, |
| 1287 | PropertyStoreTest::kStringV, |
| 1288 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1289 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1290 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1291 | { |
| 1292 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1293 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1294 | flimflam::kCountryProperty, |
| 1295 | PropertyStoreTest::kBoolV, |
| 1296 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1297 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1298 | } |
| 1299 | { |
| 1300 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1301 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1302 | flimflam::kOfflineModeProperty, |
| 1303 | PropertyStoreTest::kStringV, |
| 1304 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1305 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1306 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1307 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1308 | { |
| 1309 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1310 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 1311 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1312 | flimflam::kEnabledTechnologiesProperty, |
| 1313 | PropertyStoreTest::kStringsV, |
| 1314 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1315 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1316 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1319 | TEST_F(ManagerTest, RequestScan) { |
| 1320 | { |
| 1321 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1322 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 1323 | manager()->RegisterDevice(mock_devices_[1].get()); |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1324 | EXPECT_CALL(*mock_devices_[0], technology()) |
| 1325 | .WillRepeatedly(Return(Technology::kWifi)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1326 | EXPECT_CALL(*mock_devices_[0], Scan(_)); |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1327 | EXPECT_CALL(*mock_devices_[1], technology()) |
| 1328 | .WillRepeatedly(Return(Technology::kUnknown)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1329 | EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1330 | manager()->RequestScan(flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | { |
| 1334 | Error error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1335 | manager()->RequestScan("bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1336 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1337 | } |
| 1338 | } |
| 1339 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1340 | TEST_F(ManagerTest, GetServiceNoType) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1341 | KeyValueStore args; |
| 1342 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1343 | manager()->GetService(args, &e); |
| 1344 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1345 | EXPECT_EQ("must specify service type", e.message()); |
| 1346 | } |
| 1347 | |
| 1348 | TEST_F(ManagerTest, GetServiceUnknownType) { |
| 1349 | KeyValueStore args; |
| 1350 | Error e; |
| 1351 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet); |
| 1352 | manager()->GetService(args, &e); |
| 1353 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1354 | EXPECT_EQ("service type is unsupported", e.message()); |
| 1355 | } |
| 1356 | |
| 1357 | TEST_F(ManagerTest, GetServiceNoWifiDevice) { |
| 1358 | KeyValueStore args; |
| 1359 | Error e; |
| 1360 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1361 | manager()->GetService(args, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1362 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1363 | EXPECT_EQ("no wifi devices available", e.message()); |
| 1364 | } |
| 1365 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1366 | TEST_F(ManagerTest, GetServiceWifi) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1367 | KeyValueStore args; |
| 1368 | Error e; |
| 1369 | WiFiServiceRefPtr wifi_service; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1370 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1371 | manager()->RegisterDevice(mock_wifi_); |
| 1372 | EXPECT_CALL(*mock_wifi_, GetService(_, _)) |
| 1373 | .WillRepeatedly(Return(wifi_service)); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1374 | manager()->GetService(args, &e); |
| 1375 | EXPECT_TRUE(e.IsSuccess()); |
| 1376 | } |
| 1377 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1378 | TEST_F(ManagerTest, GetServiceVPNUnknownType) { |
| 1379 | KeyValueStore args; |
| 1380 | Error e; |
| 1381 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1382 | scoped_refptr<MockProfile> profile( |
| 1383 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1384 | AdoptProfile(manager(), profile); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1385 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1386 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1387 | EXPECT_FALSE(service); |
| 1388 | } |
| 1389 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1390 | TEST_F(ManagerTest, GetServiceVPN) { |
| 1391 | KeyValueStore args; |
| 1392 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1393 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1394 | args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 1395 | args.SetString(flimflam::kProviderHostProperty, "10.8.0.1"); |
| 1396 | args.SetString(flimflam::kProviderNameProperty, "vpn-name"); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1397 | scoped_refptr<MockProfile> profile( |
| 1398 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1399 | AdoptProfile(manager(), profile); |
| 1400 | ServiceRefPtr updated_service; |
| 1401 | EXPECT_CALL(*profile, UpdateService(_)) |
| 1402 | .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true))); |
| 1403 | ServiceRefPtr configured_service; |
| 1404 | EXPECT_CALL(*profile, ConfigureService(_)) |
| 1405 | .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true))); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1406 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1407 | EXPECT_TRUE(e.IsSuccess()); |
| 1408 | EXPECT_TRUE(service); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1409 | EXPECT_EQ(service, updated_service); |
| 1410 | EXPECT_EQ(service, configured_service); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 1413 | TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) { |
| 1414 | KeyValueStore args; |
| 1415 | Error e; |
| 1416 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax); |
| 1417 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1418 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1419 | EXPECT_EQ("Missing WiMAX network id.", e.message()); |
| 1420 | EXPECT_FALSE(service); |
| 1421 | } |
| 1422 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1423 | TEST_F(ManagerTest, GetServiceWiMax) { |
| 1424 | KeyValueStore args; |
| 1425 | Error e; |
| 1426 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 1427 | args.SetString(WiMaxService::kNetworkIdProperty, "01234567"); |
| 1428 | args.SetString(flimflam::kNameProperty, "WiMAX Network"); |
| 1429 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1430 | EXPECT_TRUE(e.IsSuccess()); |
| 1431 | EXPECT_TRUE(service); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1434 | TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) { |
| 1435 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1436 | scoped_refptr<MockProfile> profile( |
| 1437 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1438 | AdoptProfile(manager(), profile); |
| 1439 | |
| 1440 | KeyValueStore args; |
| 1441 | args.SetString(flimflam::kProfileProperty, "xxx"); |
| 1442 | Error error; |
| 1443 | manager()->ConfigureService(args, &error); |
| 1444 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1445 | EXPECT_EQ("Invalid profile name xxx", error.message()); |
| 1446 | } |
| 1447 | |
| 1448 | TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) { |
| 1449 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1450 | scoped_refptr<MockProfile> profile( |
| 1451 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1452 | AdoptProfile(manager(), profile); |
| 1453 | |
| 1454 | KeyValueStore args; |
| 1455 | Error error; |
| 1456 | manager()->ConfigureService(args, &error); |
| 1457 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1458 | EXPECT_EQ("must specify service type", error.message()); |
| 1459 | } |
| 1460 | |
| 1461 | // A registered service in the ephemeral profile should be moved to the |
| 1462 | // active profile as a part of configuration if no profile was explicitly |
| 1463 | // specified. |
| 1464 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) { |
| 1465 | scoped_refptr<MockProfile> profile( |
| 1466 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1467 | |
| 1468 | AdoptProfile(manager(), profile); // This is now the active profile. |
| 1469 | |
| 1470 | const std::vector<uint8_t> ssid; |
| 1471 | scoped_refptr<MockWiFiService> service( |
| 1472 | new NiceMock<MockWiFiService>(control_interface(), |
| 1473 | dispatcher(), |
| 1474 | metrics(), |
| 1475 | manager(), |
| 1476 | mock_wifi_, |
| 1477 | ssid, |
| 1478 | "", |
| 1479 | "", |
| 1480 | false)); |
| 1481 | |
| 1482 | manager()->RegisterService(service); |
| 1483 | service->set_profile(GetEphemeralProfile(manager())); |
| 1484 | |
| 1485 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1486 | // the same device as that used above causes a refcounting loop. |
| 1487 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1488 | dispatcher(), |
| 1489 | metrics(), |
| 1490 | manager(), |
| 1491 | "wifi1", |
| 1492 | "addr5", |
| 1493 | 5)); |
| 1494 | manager()->RegisterDevice(wifi); |
| 1495 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1496 | .WillOnce(Return(service)); |
| 1497 | EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get()))) |
| 1498 | .WillOnce(Return(true)); |
| 1499 | EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get()))) |
| 1500 | .WillOnce(Return(true)); |
| 1501 | |
| 1502 | KeyValueStore args; |
| 1503 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1504 | Error error; |
| 1505 | manager()->ConfigureService(args, &error); |
| 1506 | EXPECT_TRUE(error.IsSuccess()); |
| 1507 | } |
| 1508 | |
| 1509 | // If were configure a service that was already registered and explicitly |
| 1510 | // specify a profile, it should be moved from the profile it was previously |
| 1511 | // in to the specified profile if one was requested. |
| 1512 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) { |
| 1513 | scoped_refptr<MockProfile> profile0( |
| 1514 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1515 | scoped_refptr<MockProfile> profile1( |
| 1516 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1517 | |
| 1518 | const string kProfileName0 = "profile0"; |
| 1519 | const string kProfileName1 = "profile1"; |
| 1520 | |
| 1521 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1522 | .WillRepeatedly(Return(kProfileName0)); |
| 1523 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1524 | .WillRepeatedly(Return(kProfileName1)); |
| 1525 | |
| 1526 | AdoptProfile(manager(), profile0); |
| 1527 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1528 | |
| 1529 | const std::vector<uint8_t> ssid; |
| 1530 | scoped_refptr<MockWiFiService> service( |
| 1531 | new NiceMock<MockWiFiService>(control_interface(), |
| 1532 | dispatcher(), |
| 1533 | metrics(), |
| 1534 | manager(), |
| 1535 | mock_wifi_, |
| 1536 | ssid, |
| 1537 | "", |
| 1538 | "", |
| 1539 | false)); |
| 1540 | |
| 1541 | manager()->RegisterService(service); |
| 1542 | service->set_profile(profile1); |
| 1543 | |
| 1544 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1545 | // the same device as that used above causes a refcounting loop. |
| 1546 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1547 | dispatcher(), |
| 1548 | metrics(), |
| 1549 | manager(), |
| 1550 | "wifi1", |
| 1551 | "addr5", |
| 1552 | 5)); |
| 1553 | manager()->RegisterDevice(wifi); |
| 1554 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1555 | .WillOnce(Return(service)); |
| 1556 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1557 | .WillOnce(Return(true)); |
| 1558 | EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get()))) |
| 1559 | .WillOnce(Return(true)); |
| 1560 | EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get()))) |
| 1561 | .WillOnce(Return(true)); |
| 1562 | |
| 1563 | KeyValueStore args; |
| 1564 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1565 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1566 | Error error; |
| 1567 | manager()->ConfigureService(args, &error); |
| 1568 | EXPECT_TRUE(error.IsSuccess()); |
| 1569 | service->set_profile(NULL); // Breaks refcounting loop. |
| 1570 | } |
| 1571 | |
| 1572 | // An unregistered service should remain unregistered, but its contents should |
| 1573 | // be saved to the specified profile nonetheless. |
| 1574 | TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) { |
| 1575 | scoped_refptr<MockProfile> profile0( |
| 1576 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1577 | scoped_refptr<MockProfile> profile1( |
| 1578 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1579 | |
| 1580 | const string kProfileName0 = "profile0"; |
| 1581 | const string kProfileName1 = "profile1"; |
| 1582 | |
| 1583 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1584 | .WillRepeatedly(Return(kProfileName0)); |
| 1585 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1586 | .WillRepeatedly(Return(kProfileName1)); |
| 1587 | |
| 1588 | AdoptProfile(manager(), profile0); |
| 1589 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1590 | |
| 1591 | const std::vector<uint8_t> ssid; |
| 1592 | scoped_refptr<MockWiFiService> service( |
| 1593 | new NiceMock<MockWiFiService>(control_interface(), |
| 1594 | dispatcher(), |
| 1595 | metrics(), |
| 1596 | manager(), |
| 1597 | mock_wifi_, |
| 1598 | ssid, |
| 1599 | "", |
| 1600 | "", |
| 1601 | false)); |
| 1602 | |
| 1603 | service->set_profile(profile1); |
| 1604 | |
| 1605 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1606 | // the same device as that used above causes a refcounting loop. |
| 1607 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1608 | dispatcher(), |
| 1609 | metrics(), |
| 1610 | manager(), |
| 1611 | "wifi1", |
| 1612 | "addr5", |
| 1613 | 5)); |
| 1614 | manager()->RegisterDevice(wifi); |
| 1615 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1616 | .WillOnce(Return(service)); |
| 1617 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1618 | .WillOnce(Return(true)); |
| 1619 | EXPECT_CALL(*profile0, AdoptService(_)) |
| 1620 | .Times(0); |
| 1621 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 1622 | .Times(0); |
| 1623 | |
| 1624 | KeyValueStore args; |
| 1625 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1626 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1627 | Error error; |
| 1628 | manager()->ConfigureService(args, &error); |
| 1629 | EXPECT_TRUE(error.IsSuccess()); |
| 1630 | } |
| 1631 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1632 | TEST_F(ManagerTest, TechnologyOrder) { |
| 1633 | Error error; |
| 1634 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 1635 | string(flimflam::kTypeWifi), &error); |
| 1636 | ASSERT_TRUE(error.IsSuccess()); |
| 1637 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 1638 | string(flimflam::kTypeEthernet) + "," + |
| 1639 | string(flimflam::kTypeWifi)); |
| 1640 | |
| 1641 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 1642 | string(flimflam::kTypeWifi), &error); |
| 1643 | ASSERT_FALSE(error.IsSuccess()); |
| 1644 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1645 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 1646 | string(flimflam::kTypeWifi), |
| 1647 | manager()->GetTechnologyOrder()); |
| 1648 | } |
| 1649 | |
| 1650 | TEST_F(ManagerTest, SortServices) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1651 | // TODO(quiche): Some of these tests would probably fit better in |
| 1652 | // service_unittest, since the actual comparison of Services is |
| 1653 | // implemented in Service. (crosbug.com/23370) |
| 1654 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1655 | scoped_refptr<MockService> mock_service0( |
| 1656 | new NiceMock<MockService>(control_interface(), |
| 1657 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1658 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1659 | manager())); |
| 1660 | scoped_refptr<MockService> mock_service1( |
| 1661 | new NiceMock<MockService>(control_interface(), |
| 1662 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1663 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1664 | manager())); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1665 | |
| 1666 | manager()->RegisterService(mock_service0); |
| 1667 | manager()->RegisterService(mock_service1); |
| 1668 | |
| 1669 | // Services should already be sorted by UniqueName |
| 1670 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1671 | |
| 1672 | // Asking explictly to sort services should not change anything |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1673 | manager()->SortServicesTask(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1674 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1675 | |
| 1676 | // Two otherwise equal services should be reordered by strength |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 1677 | mock_service1->SetStrength(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1678 | manager()->UpdateService(mock_service1); |
| 1679 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1680 | |
| 1681 | // Security |
Paul Stewart | 1ca3e85 | 2011-11-04 07:50:49 -0700 | [diff] [blame] | 1682 | mock_service0->set_security_level(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1683 | manager()->UpdateService(mock_service0); |
| 1684 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1685 | |
| 1686 | // Technology |
Joshua Kroll | 053fa82 | 2012-06-05 09:50:43 -0700 | [diff] [blame] | 1687 | EXPECT_CALL(*mock_service0.get(), technology()) |
| 1688 | .WillRepeatedly(Return((Technology::kWifi))); |
| 1689 | EXPECT_CALL(*mock_service1.get(), technology()) |
| 1690 | .WillRepeatedly(Return(Technology::kEthernet)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1691 | |
| 1692 | Error error; |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 1693 | // Default technology ordering should favor Ethernet over WiFi. |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1694 | manager()->SortServicesTask(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1695 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1696 | |
| 1697 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 1698 | string(flimflam::kTypeEthernet), &error); |
| 1699 | EXPECT_TRUE(error.IsSuccess()); |
| 1700 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1701 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1702 | // Priority. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1703 | mock_service0->set_priority(1); |
| 1704 | manager()->UpdateService(mock_service0); |
| 1705 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1706 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1707 | // Favorite. |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1708 | mock_service1->MakeFavorite(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1709 | manager()->UpdateService(mock_service1); |
| 1710 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1711 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1712 | // Auto-connect. |
| 1713 | mock_service0->set_auto_connect(true); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1714 | manager()->UpdateService(mock_service0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1715 | mock_service1->set_auto_connect(false); |
| 1716 | manager()->UpdateService(mock_service1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1717 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1718 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1719 | // Connectable. |
| 1720 | mock_service1->set_connectable(true); |
| 1721 | manager()->UpdateService(mock_service1); |
| 1722 | mock_service0->set_connectable(false); |
| 1723 | manager()->UpdateService(mock_service0); |
| 1724 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1725 | |
| 1726 | // IsFailed. |
| 1727 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1728 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 1729 | EXPECT_CALL(*mock_service0.get(), IsFailed()) |
| 1730 | .WillRepeatedly(Return(false)); |
| 1731 | manager()->UpdateService(mock_service0); |
| 1732 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1733 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1734 | EXPECT_CALL(*mock_service1.get(), IsFailed()) |
| 1735 | .WillRepeatedly(Return(true)); |
| 1736 | manager()->UpdateService(mock_service1); |
| 1737 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1738 | |
| 1739 | // Connecting. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1740 | EXPECT_CALL(*mock_service1.get(), state()) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1741 | .WillRepeatedly(Return(Service::kStateAssociating)); |
| 1742 | EXPECT_CALL(*mock_service1.get(), IsConnecting()) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1743 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1744 | manager()->UpdateService(mock_service1); |
| 1745 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1746 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1747 | // Connected. |
| 1748 | EXPECT_CALL(*mock_service0.get(), state()) |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 1749 | .WillRepeatedly(Return(Service::kStatePortal)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1750 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 1751 | .WillRepeatedly(Return(true)); |
| 1752 | manager()->UpdateService(mock_service0); |
| 1753 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1754 | |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 1755 | // Portal. |
| 1756 | EXPECT_CALL(*mock_service1.get(), state()) |
| 1757 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1758 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 1759 | .WillRepeatedly(Return(true)); |
| 1760 | manager()->UpdateService(mock_service1); |
| 1761 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1762 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1763 | manager()->DeregisterService(mock_service0); |
| 1764 | manager()->DeregisterService(mock_service1); |
| 1765 | } |
| 1766 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1767 | TEST_F(ManagerTest, SortServicesWithConnection) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1768 | MockMetrics mock_metrics; |
| 1769 | manager()->set_metrics(&mock_metrics); |
| 1770 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1771 | scoped_refptr<MockService> mock_service0( |
| 1772 | new NiceMock<MockService>(control_interface(), |
| 1773 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1774 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1775 | manager())); |
| 1776 | scoped_refptr<MockService> mock_service1( |
| 1777 | new NiceMock<MockService>(control_interface(), |
| 1778 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1779 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1780 | manager())); |
| 1781 | |
| 1782 | scoped_refptr<MockConnection> mock_connection0( |
| 1783 | new NiceMock<MockConnection>(device_info_.get())); |
| 1784 | scoped_refptr<MockConnection> mock_connection1( |
| 1785 | new NiceMock<MockConnection>(device_info_.get())); |
| 1786 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1787 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1788 | manager()->RegisterService(mock_service0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1789 | CompleteServiceSort(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1790 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1791 | manager()->RegisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1792 | CompleteServiceSort(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1793 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1794 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1795 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1796 | |
| 1797 | mock_service1->set_priority(1); |
| 1798 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1799 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1800 | |
| 1801 | mock_service1->set_priority(0); |
| 1802 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1803 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1804 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1805 | mock_service0->set_mock_connection(mock_connection0); |
| 1806 | mock_service1->set_mock_connection(mock_connection1); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1807 | |
| 1808 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1809 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1810 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1811 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1812 | ServiceWatcher service_watcher; |
| 1813 | int tag = |
| 1814 | manager()->RegisterDefaultServiceCallback( |
| 1815 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 1816 | service_watcher.AsWeakPtr())); |
| 1817 | EXPECT_EQ(1, tag); |
| 1818 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1819 | mock_service1->set_priority(1); |
| 1820 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false)); |
| 1821 | EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1822 | EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1823 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get())); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1824 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1825 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1826 | manager()->DeregisterDefaultServiceCallback(tag); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1827 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1828 | EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1829 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1830 | mock_service1->set_mock_connection(NULL); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1831 | manager()->DeregisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1832 | CompleteServiceSort(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1833 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1834 | mock_service0->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1835 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1836 | manager()->DeregisterService(mock_service0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1837 | CompleteServiceSort(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1838 | |
| 1839 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1840 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1841 | } |
| 1842 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1843 | TEST_F(ManagerTest, NotifyDefaultServiceChanged) { |
| 1844 | EXPECT_EQ(0, manager()->default_service_callback_tag_); |
| 1845 | EXPECT_TRUE(manager()->default_service_callbacks_.empty()); |
| 1846 | |
| 1847 | MockMetrics mock_metrics; |
| 1848 | manager()->set_metrics(&mock_metrics); |
| 1849 | |
| 1850 | scoped_refptr<MockService> mock_service( |
| 1851 | new NiceMock<MockService>( |
| 1852 | control_interface(), dispatcher(), metrics(), manager())); |
| 1853 | ServiceRefPtr service = mock_service; |
| 1854 | ServiceRefPtr null_service; |
| 1855 | |
| 1856 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1857 | manager()->NotifyDefaultServiceChanged(null_service); |
| 1858 | |
| 1859 | ServiceWatcher service_watcher1; |
| 1860 | ServiceWatcher service_watcher2; |
| 1861 | int tag1 = |
| 1862 | manager()->RegisterDefaultServiceCallback( |
| 1863 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 1864 | service_watcher1.AsWeakPtr())); |
| 1865 | EXPECT_EQ(1, tag1); |
| 1866 | int tag2 = |
| 1867 | manager()->RegisterDefaultServiceCallback( |
| 1868 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 1869 | service_watcher2.AsWeakPtr())); |
| 1870 | EXPECT_EQ(2, tag2); |
| 1871 | |
| 1872 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service)); |
| 1873 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service)); |
| 1874 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1875 | manager()->NotifyDefaultServiceChanged(null_service); |
| 1876 | |
| 1877 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service)); |
| 1878 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service)); |
| 1879 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 1880 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 1881 | |
| 1882 | manager()->DeregisterDefaultServiceCallback(tag1); |
| 1883 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0); |
| 1884 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service)); |
| 1885 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 1886 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 1887 | EXPECT_EQ(1, manager()->default_service_callbacks_.size()); |
| 1888 | |
| 1889 | manager()->DeregisterDefaultServiceCallback(tag2); |
| 1890 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0); |
| 1891 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 1892 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 1893 | |
| 1894 | EXPECT_EQ(2, manager()->default_service_callback_tag_); |
| 1895 | EXPECT_TRUE(manager()->default_service_callbacks_.empty()); |
| 1896 | } |
| 1897 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1898 | TEST_F(ManagerTest, AvailableTechnologies) { |
| 1899 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 1900 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1901 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1902 | manager(), |
| 1903 | "null4", |
| 1904 | "addr4", |
| 1905 | 0)); |
| 1906 | manager()->RegisterDevice(mock_devices_[0]); |
| 1907 | manager()->RegisterDevice(mock_devices_[1]); |
| 1908 | manager()->RegisterDevice(mock_devices_[2]); |
| 1909 | manager()->RegisterDevice(mock_devices_[3]); |
| 1910 | |
| 1911 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1912 | .WillByDefault(Return(Technology::kEthernet)); |
| 1913 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1914 | .WillByDefault(Return(Technology::kWifi)); |
| 1915 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1916 | .WillByDefault(Return(Technology::kCellular)); |
| 1917 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1918 | .WillByDefault(Return(Technology::kWifi)); |
| 1919 | |
| 1920 | set<string> expected_technologies; |
| 1921 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1922 | Technology::kEthernet)); |
| 1923 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1924 | Technology::kWifi)); |
| 1925 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1926 | Technology::kCellular)); |
| 1927 | Error error; |
| 1928 | vector<string> technologies = manager()->AvailableTechnologies(&error); |
| 1929 | |
| 1930 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1931 | ContainerEq(expected_technologies)); |
| 1932 | } |
| 1933 | |
| 1934 | TEST_F(ManagerTest, ConnectedTechnologies) { |
| 1935 | scoped_refptr<MockService> connected_service1( |
| 1936 | new NiceMock<MockService>(control_interface(), |
| 1937 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1938 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1939 | manager())); |
| 1940 | scoped_refptr<MockService> connected_service2( |
| 1941 | new NiceMock<MockService>(control_interface(), |
| 1942 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1943 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1944 | manager())); |
| 1945 | scoped_refptr<MockService> disconnected_service1( |
| 1946 | new NiceMock<MockService>(control_interface(), |
| 1947 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1948 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1949 | manager())); |
| 1950 | scoped_refptr<MockService> disconnected_service2( |
| 1951 | new NiceMock<MockService>(control_interface(), |
| 1952 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1953 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1954 | manager())); |
| 1955 | |
| 1956 | ON_CALL(*connected_service1.get(), IsConnected()) |
| 1957 | .WillByDefault(Return(true)); |
| 1958 | ON_CALL(*connected_service2.get(), IsConnected()) |
| 1959 | .WillByDefault(Return(true)); |
| 1960 | |
| 1961 | manager()->RegisterService(connected_service1); |
| 1962 | manager()->RegisterService(connected_service2); |
| 1963 | manager()->RegisterService(disconnected_service1); |
| 1964 | manager()->RegisterService(disconnected_service2); |
| 1965 | |
| 1966 | manager()->RegisterDevice(mock_devices_[0]); |
| 1967 | manager()->RegisterDevice(mock_devices_[1]); |
| 1968 | manager()->RegisterDevice(mock_devices_[2]); |
| 1969 | manager()->RegisterDevice(mock_devices_[3]); |
| 1970 | |
| 1971 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1972 | .WillByDefault(Return(Technology::kEthernet)); |
| 1973 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1974 | .WillByDefault(Return(Technology::kWifi)); |
| 1975 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1976 | .WillByDefault(Return(Technology::kCellular)); |
| 1977 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1978 | .WillByDefault(Return(Technology::kWifi)); |
| 1979 | |
| 1980 | mock_devices_[0]->SelectService(connected_service1); |
| 1981 | mock_devices_[1]->SelectService(disconnected_service1); |
| 1982 | mock_devices_[2]->SelectService(disconnected_service2); |
| 1983 | mock_devices_[3]->SelectService(connected_service2); |
| 1984 | |
| 1985 | set<string> expected_technologies; |
| 1986 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1987 | Technology::kEthernet)); |
| 1988 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1989 | Technology::kWifi)); |
| 1990 | Error error; |
| 1991 | |
| 1992 | vector<string> technologies = manager()->ConnectedTechnologies(&error); |
| 1993 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1994 | ContainerEq(expected_technologies)); |
| 1995 | } |
| 1996 | |
| 1997 | TEST_F(ManagerTest, DefaultTechnology) { |
| 1998 | scoped_refptr<MockService> connected_service( |
| 1999 | new NiceMock<MockService>(control_interface(), |
| 2000 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2001 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2002 | manager())); |
| 2003 | scoped_refptr<MockService> disconnected_service( |
| 2004 | new NiceMock<MockService>(control_interface(), |
| 2005 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2006 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2007 | manager())); |
| 2008 | |
| 2009 | // Connected. WiFi. |
| 2010 | ON_CALL(*connected_service.get(), IsConnected()) |
| 2011 | .WillByDefault(Return(true)); |
| 2012 | ON_CALL(*connected_service.get(), state()) |
| 2013 | .WillByDefault(Return(Service::kStateConnected)); |
| 2014 | ON_CALL(*connected_service.get(), technology()) |
| 2015 | .WillByDefault(Return(Technology::kWifi)); |
| 2016 | |
| 2017 | // Disconnected. Ethernet. |
| 2018 | ON_CALL(*disconnected_service.get(), technology()) |
| 2019 | .WillByDefault(Return(Technology::kEthernet)); |
| 2020 | |
| 2021 | manager()->RegisterService(disconnected_service); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2022 | CompleteServiceSort(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2023 | Error error; |
| 2024 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq("")); |
| 2025 | |
| 2026 | |
| 2027 | manager()->RegisterService(connected_service); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2028 | CompleteServiceSort(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2029 | // Connected service should be brought to the front now. |
| 2030 | string expected_technology = |
| 2031 | Technology::NameFromIdentifier(Technology::kWifi); |
| 2032 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology)); |
| 2033 | } |
| 2034 | |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2035 | TEST_F(ManagerTest, Stop) { |
| 2036 | scoped_refptr<MockProfile> profile( |
| 2037 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 2038 | AdoptProfile(manager(), profile); |
| 2039 | scoped_refptr<MockService> service( |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2040 | new NiceMock<MockService>(control_interface(), |
| 2041 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2042 | metrics(), |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2043 | manager())); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2044 | manager()->RegisterService(service); |
| 2045 | manager()->RegisterDevice(mock_devices_[0]); |
| 2046 | EXPECT_CALL(*profile.get(), |
| 2047 | UpdateDevice(DeviceRefPtr(mock_devices_[0].get()))) |
| 2048 | .WillOnce(Return(true)); |
| 2049 | EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true)); |
| 2050 | EXPECT_CALL(*service.get(), Disconnect(_)).Times(1); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2051 | manager()->Stop(); |
| 2052 | } |
| 2053 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2054 | TEST_F(ManagerTest, UpdateServiceConnected) { |
| 2055 | scoped_refptr<MockService> mock_service( |
| 2056 | new NiceMock<MockService>(control_interface(), |
| 2057 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2058 | metrics(), |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2059 | manager())); |
| 2060 | manager()->RegisterService(mock_service); |
| 2061 | EXPECT_FALSE(mock_service->favorite()); |
| 2062 | EXPECT_FALSE(mock_service->auto_connect()); |
| 2063 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2064 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 2065 | .WillRepeatedly(Return(true)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2066 | manager()->UpdateService(mock_service); |
| 2067 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 2068 | // to mock out MakeFavorite. And mocking that out would break the |
| 2069 | // SortServices test. (crosbug.com/23370) |
| 2070 | EXPECT_TRUE(mock_service->favorite()); |
| 2071 | EXPECT_TRUE(mock_service->auto_connect()); |
| 2072 | } |
| 2073 | |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2074 | TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) { |
| 2075 | // This tests the case where the user connects to a service that is |
| 2076 | // currently associated with a profile. We want to make sure that the |
| 2077 | // favorite flag is set and that the flag is saved to the current |
| 2078 | // profile. |
| 2079 | scoped_refptr<MockService> mock_service( |
| 2080 | new NiceMock<MockService>(control_interface(), |
| 2081 | dispatcher(), |
| 2082 | metrics(), |
| 2083 | manager())); |
| 2084 | manager()->RegisterService(mock_service); |
| 2085 | EXPECT_FALSE(mock_service->favorite()); |
| 2086 | EXPECT_FALSE(mock_service->auto_connect()); |
| 2087 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2088 | scoped_refptr<MockProfile> profile( |
| 2089 | new MockProfile(control_interface(), manager(), "")); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2090 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2091 | mock_service->set_profile(profile); |
| 2092 | EXPECT_CALL(*mock_service, IsConnected()) |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2093 | .WillRepeatedly(Return(true)); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2094 | EXPECT_CALL(*profile, |
| 2095 | UpdateService(static_cast<ServiceRefPtr>(mock_service))); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2096 | manager()->UpdateService(mock_service); |
| 2097 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 2098 | // to mock out MakeFavorite. And mocking that out would break the |
| 2099 | // SortServices test. (crosbug.com/23370) |
| 2100 | EXPECT_TRUE(mock_service->favorite()); |
| 2101 | EXPECT_TRUE(mock_service->auto_connect()); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2102 | // This releases the ref on the mock profile. |
| 2103 | mock_service->set_profile(NULL); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2104 | } |
| 2105 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 2106 | TEST_F(ManagerTest, SaveSuccessfulService) { |
| 2107 | scoped_refptr<MockProfile> profile( |
| 2108 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 2109 | AdoptProfile(manager(), profile); |
| 2110 | scoped_refptr<MockService> service( |
| 2111 | new NiceMock<MockService>(control_interface(), |
| 2112 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2113 | metrics(), |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 2114 | manager())); |
| 2115 | |
| 2116 | // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly. |
| 2117 | ServiceRefPtr expect_service(service.get()); |
| 2118 | |
| 2119 | EXPECT_CALL(*profile.get(), ConfigureService(expect_service)) |
| 2120 | .WillOnce(Return(false)); |
| 2121 | manager()->RegisterService(service); |
| 2122 | |
| 2123 | EXPECT_CALL(*service.get(), state()) |
| 2124 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 2125 | EXPECT_CALL(*service.get(), IsConnected()) |
| 2126 | .WillRepeatedly(Return(true)); |
| 2127 | EXPECT_CALL(*profile.get(), AdoptService(expect_service)) |
| 2128 | .WillOnce(Return(true)); |
| 2129 | manager()->UpdateService(service); |
| 2130 | } |
| 2131 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 2132 | TEST_F(ManagerTest, UpdateDevice) { |
| 2133 | MockProfile *profile0 = new MockProfile(control_interface(), manager(), ""); |
| 2134 | MockProfile *profile1 = new MockProfile(control_interface(), manager(), ""); |
| 2135 | MockProfile *profile2 = new MockProfile(control_interface(), manager(), ""); |
| 2136 | AdoptProfile(manager(), profile0); // Passes ownership. |
| 2137 | AdoptProfile(manager(), profile1); // Passes ownership. |
| 2138 | AdoptProfile(manager(), profile2); // Passes ownership. |
| 2139 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 2140 | EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0); |
| 2141 | EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true)); |
| 2142 | EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false)); |
| 2143 | manager()->UpdateDevice(mock_devices_[0]); |
| 2144 | } |
| 2145 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 2146 | TEST_F(ManagerTest, EnumerateProfiles) { |
| 2147 | vector<string> profile_paths; |
| 2148 | for (size_t i = 0; i < 10; i++) { |
| 2149 | scoped_refptr<MockProfile> profile( |
| 2150 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
Jason Glasgow | 5d8197b | 2012-01-27 08:37:32 -0500 | [diff] [blame] | 2151 | profile_paths.push_back(base::StringPrintf("/profile/%zd", i)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 2152 | EXPECT_CALL(*profile.get(), GetRpcIdentifier()) |
| 2153 | .WillOnce(Return(profile_paths.back())); |
| 2154 | AdoptProfile(manager(), profile); |
| 2155 | } |
| 2156 | |
| 2157 | Error error; |
| 2158 | vector<string> returned_paths = manager()->EnumerateProfiles(&error); |
| 2159 | EXPECT_TRUE(error.IsSuccess()); |
| 2160 | EXPECT_EQ(profile_paths.size(), returned_paths.size()); |
| 2161 | for (size_t i = 0; i < profile_paths.size(); i++) { |
| 2162 | EXPECT_EQ(profile_paths[i], returned_paths[i]); |
| 2163 | } |
| 2164 | } |
| 2165 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2166 | TEST_F(ManagerTest, AutoConnectOnRegister) { |
| 2167 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2168 | EXPECT_CALL(*service.get(), AutoConnect()); |
| 2169 | manager()->RegisterService(service); |
| 2170 | dispatcher()->DispatchPendingEvents(); |
| 2171 | } |
| 2172 | |
| 2173 | TEST_F(ManagerTest, AutoConnectOnUpdate) { |
| 2174 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 2175 | service1->set_priority(1); |
| 2176 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 2177 | service2->set_priority(2); |
| 2178 | manager()->RegisterService(service1); |
| 2179 | manager()->RegisterService(service2); |
| 2180 | dispatcher()->DispatchPendingEvents(); |
| 2181 | |
| 2182 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 2183 | EXPECT_CALL(*service2.get(), state()) |
| 2184 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 2185 | EXPECT_CALL(*service2.get(), IsFailed()) |
| 2186 | .WillRepeatedly(Return(true)); |
| 2187 | EXPECT_CALL(*service2.get(), IsConnected()) |
| 2188 | .WillRepeatedly(Return(false)); |
| 2189 | manager()->UpdateService(service2); |
| 2190 | dispatcher()->DispatchPendingEvents(); |
| 2191 | } |
| 2192 | |
| 2193 | TEST_F(ManagerTest, AutoConnectOnDeregister) { |
| 2194 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 2195 | service1->set_priority(1); |
| 2196 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 2197 | service2->set_priority(2); |
| 2198 | manager()->RegisterService(service1); |
| 2199 | manager()->RegisterService(service2); |
| 2200 | dispatcher()->DispatchPendingEvents(); |
| 2201 | |
| 2202 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 2203 | manager()->DeregisterService(service2); |
| 2204 | dispatcher()->DispatchPendingEvents(); |
| 2205 | } |
| 2206 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame^] | 2207 | TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) { |
| 2208 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2209 | SetPowerState(PowerManagerProxyDelegate::kSuspending); |
| 2210 | SetPowerManager(); |
| 2211 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2212 | manager()->RegisterService(service); |
| 2213 | dispatcher()->DispatchPendingEvents(); |
| 2214 | } |
| 2215 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2216 | TEST_F(ManagerTest, AutoConnectOnPowerStateMem) { |
| 2217 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2218 | SetPowerState(PowerManagerProxyDelegate::kMem); |
| 2219 | SetPowerManager(); |
| 2220 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2221 | manager()->RegisterService(service); |
| 2222 | dispatcher()->DispatchPendingEvents(); |
| 2223 | } |
| 2224 | |
| 2225 | TEST_F(ManagerTest, AutoConnectOnPowerStateOn) { |
| 2226 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2227 | SetPowerState(PowerManagerProxyDelegate::kOn); |
| 2228 | SetPowerManager(); |
| 2229 | EXPECT_CALL(*service, AutoConnect()); |
| 2230 | manager()->RegisterService(service); |
| 2231 | dispatcher()->DispatchPendingEvents(); |
| 2232 | } |
| 2233 | |
| 2234 | TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) { |
| 2235 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2236 | SetPowerState(PowerManagerProxyDelegate::kUnknown); |
| 2237 | SetPowerManager(); |
| 2238 | EXPECT_CALL(*service, AutoConnect()); |
| 2239 | manager()->RegisterService(service); |
| 2240 | dispatcher()->DispatchPendingEvents(); |
| 2241 | } |
| 2242 | |
| 2243 | TEST_F(ManagerTest, OnPowerStateChanged) { |
| 2244 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2245 | SetPowerState(PowerManagerProxyDelegate::kOn); |
| 2246 | SetPowerManager(); |
| 2247 | EXPECT_CALL(*service, AutoConnect()); |
| 2248 | manager()->RegisterService(service); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2249 | manager()->RegisterDevice(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2250 | dispatcher()->DispatchPendingEvents(); |
| 2251 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2252 | EXPECT_CALL(*mock_devices_[0], OnAfterResume()); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2253 | OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 2254 | EXPECT_CALL(*service, AutoConnect()); |
| 2255 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2256 | Mock::VerifyAndClearExpectations(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2257 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2258 | EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend()); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2259 | OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 2260 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2261 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2262 | Mock::VerifyAndClearExpectations(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2263 | } |
| 2264 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame^] | 2265 | TEST_F(ManagerTest, AddTerminationAction) { |
| 2266 | EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _)); |
| 2267 | EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_)); |
| 2268 | SetPowerManager(); |
| 2269 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2270 | manager()->AddTerminationAction("action1", base::Closure()); |
| 2271 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2272 | manager()->AddTerminationAction("action2", base::Closure()); |
| 2273 | } |
| 2274 | |
| 2275 | TEST_F(ManagerTest, RemoveTerminationAction) { |
| 2276 | static const char kKey1[] = "action1"; |
| 2277 | static const char kKey2[] = "action2"; |
| 2278 | |
| 2279 | MockPowerManager &power_manager = *power_manager_; |
| 2280 | SetPowerManager(); |
| 2281 | |
| 2282 | // Removing an action when the hook table is empty should not result in any |
| 2283 | // calls to the power manager. |
| 2284 | EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0); |
| 2285 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0); |
| 2286 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2287 | manager()->RemoveTerminationAction("unknown"); |
| 2288 | Mock::VerifyAndClearExpectations(&power_manager); |
| 2289 | |
| 2290 | manager()->AddTerminationAction(kKey1, base::Closure()); |
| 2291 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2292 | manager()->AddTerminationAction(kKey2, base::Closure()); |
| 2293 | |
| 2294 | // Removing an action that ends up with a non-empty hook table should not |
| 2295 | // result in any calls to the power manager. |
| 2296 | EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0); |
| 2297 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0); |
| 2298 | manager()->RemoveTerminationAction(kKey1); |
| 2299 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2300 | Mock::VerifyAndClearExpectations(&power_manager); |
| 2301 | |
| 2302 | // Removing the last action should trigger unregistering from the power |
| 2303 | // manager. |
| 2304 | EXPECT_CALL(power_manager, UnregisterSuspendDelay()); |
| 2305 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)); |
| 2306 | manager()->RemoveTerminationAction(kKey2); |
| 2307 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2308 | } |
| 2309 | |
| 2310 | TEST_F(ManagerTest, RunTerminationActions) { |
| 2311 | TerminationActionTest test_action; |
| 2312 | const string kActionName = "action"; |
| 2313 | |
| 2314 | EXPECT_CALL(test_action, Done(_)); |
| 2315 | manager()->RunTerminationActions(Bind(&TerminationActionTest::Done, |
| 2316 | test_action.AsWeakPtr())); |
| 2317 | |
| 2318 | manager()->AddTerminationAction(TerminationActionTest::kActionName, |
| 2319 | Bind(&TerminationActionTest::Action, |
| 2320 | test_action.AsWeakPtr())); |
| 2321 | test_action.set_manager(manager()); |
| 2322 | EXPECT_CALL(test_action, Done(_)); |
| 2323 | manager()->RunTerminationActions(Bind(&TerminationActionTest::Done, |
| 2324 | test_action.AsWeakPtr())); |
| 2325 | } |
| 2326 | |
| 2327 | TEST_F(ManagerTest, OnSuspendDelay) { |
| 2328 | const uint32 kSeqNumber = 123; |
| 2329 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2330 | EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber)); |
| 2331 | SetPowerManager(); |
| 2332 | OnSuspendDelay(kSeqNumber); |
| 2333 | } |
| 2334 | |
| 2335 | TEST_F(ManagerTest, OnSuspendActionsComplete) { |
| 2336 | const uint32 kSeqNumber = 54321; |
| 2337 | Error error; |
| 2338 | EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber)); |
| 2339 | SetPowerManager(); |
| 2340 | OnSuspendActionsComplete(kSeqNumber, error); |
| 2341 | } |
| 2342 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 2343 | TEST_F(ManagerTest, RecheckPortal) { |
| 2344 | EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection()) |
| 2345 | .WillOnce(Return(false)); |
| 2346 | EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection()) |
| 2347 | .WillOnce(Return(true)); |
| 2348 | EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection()) |
| 2349 | .Times(0); |
| 2350 | |
| 2351 | manager()->RegisterDevice(mock_devices_[0]); |
| 2352 | manager()->RegisterDevice(mock_devices_[1]); |
| 2353 | manager()->RegisterDevice(mock_devices_[2]); |
| 2354 | |
| 2355 | manager()->RecheckPortal(NULL); |
| 2356 | } |
| 2357 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 2358 | TEST_F(ManagerTest, RecheckPortalOnService) { |
| 2359 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 2360 | dispatcher(), |
| 2361 | metrics(), |
| 2362 | manager()); |
| 2363 | EXPECT_CALL(*mock_devices_[0].get(), |
| 2364 | IsConnectedToService(IsRefPtrTo(service))) |
| 2365 | .WillOnce(Return(false)); |
| 2366 | EXPECT_CALL(*mock_devices_[1].get(), |
| 2367 | IsConnectedToService(IsRefPtrTo(service))) |
| 2368 | .WillOnce(Return(true)); |
| 2369 | EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection()) |
| 2370 | .WillOnce(Return(true)); |
| 2371 | EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_)) |
| 2372 | .Times(0); |
| 2373 | |
| 2374 | manager()->RegisterDevice(mock_devices_[0]); |
| 2375 | manager()->RegisterDevice(mock_devices_[1]); |
| 2376 | manager()->RegisterDevice(mock_devices_[2]); |
| 2377 | |
| 2378 | manager()->RecheckPortalOnService(service); |
| 2379 | } |
| 2380 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2381 | TEST_F(ManagerTest, GetDefaultService) { |
| 2382 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2383 | EXPECT_EQ("/", GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2384 | |
| 2385 | scoped_refptr<MockService> mock_service( |
| 2386 | new NiceMock<MockService>(control_interface(), |
| 2387 | dispatcher(), |
| 2388 | metrics(), |
| 2389 | manager())); |
| 2390 | |
| 2391 | manager()->RegisterService(mock_service); |
| 2392 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2393 | EXPECT_EQ("/", GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2394 | |
| 2395 | scoped_refptr<MockConnection> mock_connection( |
| 2396 | new NiceMock<MockConnection>(device_info_.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2397 | mock_service->set_mock_connection(mock_connection); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2398 | EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2399 | EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2400 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2401 | mock_service->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2402 | manager()->DeregisterService(mock_service); |
| 2403 | } |
| 2404 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 2405 | TEST_F(ManagerTest, GetServiceWithGUID) { |
| 2406 | scoped_refptr<MockService> mock_service0( |
| 2407 | new NiceMock<MockService>(control_interface(), |
| 2408 | dispatcher(), |
| 2409 | metrics(), |
| 2410 | manager())); |
| 2411 | |
| 2412 | scoped_refptr<MockService> mock_service1( |
| 2413 | new NiceMock<MockService>(control_interface(), |
| 2414 | dispatcher(), |
| 2415 | metrics(), |
| 2416 | manager())); |
| 2417 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 2418 | EXPECT_CALL(*mock_service0.get(), Configure(_, _)) |
| 2419 | .Times(0); |
| 2420 | EXPECT_CALL(*mock_service1.get(), Configure(_, _)) |
| 2421 | .Times(0); |
| 2422 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 2423 | manager()->RegisterService(mock_service0); |
| 2424 | manager()->RegisterService(mock_service1); |
| 2425 | |
| 2426 | const string kGUID0 = "GUID0"; |
| 2427 | const string kGUID1 = "GUID1"; |
| 2428 | |
| 2429 | { |
| 2430 | Error error; |
| 2431 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 2432 | EXPECT_FALSE(error.IsSuccess()); |
| 2433 | EXPECT_FALSE(service); |
| 2434 | } |
| 2435 | |
| 2436 | KeyValueStore args; |
| 2437 | args.SetString(flimflam::kGuidProperty, kGUID1); |
| 2438 | |
| 2439 | { |
| 2440 | Error error; |
| 2441 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 2442 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 2443 | EXPECT_FALSE(service); |
| 2444 | } |
| 2445 | |
| 2446 | mock_service0->set_guid(kGUID0); |
| 2447 | mock_service1->set_guid(kGUID1); |
| 2448 | |
| 2449 | { |
| 2450 | Error error; |
| 2451 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 2452 | EXPECT_TRUE(error.IsSuccess()); |
| 2453 | EXPECT_EQ(mock_service0.get(), service.get()); |
| 2454 | } |
| 2455 | |
| 2456 | { |
| 2457 | Error error; |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 2458 | EXPECT_CALL(*mock_service1.get(), Configure(_, &error)) |
| 2459 | .Times(1); |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 2460 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 2461 | EXPECT_TRUE(error.IsSuccess()); |
| 2462 | EXPECT_EQ(mock_service1.get(), service.get()); |
| 2463 | } |
| 2464 | |
| 2465 | manager()->DeregisterService(mock_service0); |
| 2466 | manager()->DeregisterService(mock_service1); |
| 2467 | } |
| 2468 | |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 2469 | |
| 2470 | TEST_F(ManagerTest, CalculateStateOffline) { |
| 2471 | MockMetrics mock_metrics; |
| 2472 | manager()->set_metrics(&mock_metrics); |
| 2473 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_)) |
| 2474 | .Times(AnyNumber()); |
| 2475 | scoped_refptr<MockService> mock_service0( |
| 2476 | new NiceMock<MockService>(control_interface(), |
| 2477 | dispatcher(), |
| 2478 | metrics(), |
| 2479 | manager())); |
| 2480 | |
| 2481 | scoped_refptr<MockService> mock_service1( |
| 2482 | new NiceMock<MockService>(control_interface(), |
| 2483 | dispatcher(), |
| 2484 | metrics(), |
| 2485 | manager())); |
| 2486 | |
| 2487 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 2488 | .WillRepeatedly(Return(false)); |
| 2489 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 2490 | .WillRepeatedly(Return(false)); |
| 2491 | |
| 2492 | manager()->RegisterService(mock_service0); |
| 2493 | manager()->RegisterService(mock_service1); |
| 2494 | |
| 2495 | EXPECT_EQ("offline", manager()->CalculateState(NULL)); |
| 2496 | |
| 2497 | manager()->DeregisterService(mock_service0); |
| 2498 | manager()->DeregisterService(mock_service1); |
| 2499 | } |
| 2500 | |
| 2501 | TEST_F(ManagerTest, CalculateStateOnline) { |
| 2502 | MockMetrics mock_metrics; |
| 2503 | manager()->set_metrics(&mock_metrics); |
| 2504 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_)) |
| 2505 | .Times(AnyNumber()); |
| 2506 | scoped_refptr<MockService> mock_service0( |
| 2507 | new NiceMock<MockService>(control_interface(), |
| 2508 | dispatcher(), |
| 2509 | metrics(), |
| 2510 | manager())); |
| 2511 | |
| 2512 | scoped_refptr<MockService> mock_service1( |
| 2513 | new NiceMock<MockService>(control_interface(), |
| 2514 | dispatcher(), |
| 2515 | metrics(), |
| 2516 | manager())); |
| 2517 | |
| 2518 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 2519 | .WillRepeatedly(Return(false)); |
| 2520 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 2521 | .WillRepeatedly(Return(true)); |
| 2522 | EXPECT_CALL(*mock_service0.get(), state()) |
| 2523 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 2524 | EXPECT_CALL(*mock_service1.get(), state()) |
| 2525 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 2526 | |
| 2527 | manager()->RegisterService(mock_service0); |
| 2528 | manager()->RegisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2529 | CompleteServiceSort(); |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 2530 | |
| 2531 | EXPECT_EQ("online", manager()->CalculateState(NULL)); |
| 2532 | |
| 2533 | manager()->DeregisterService(mock_service0); |
| 2534 | manager()->DeregisterService(mock_service1); |
| 2535 | } |
| 2536 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 2537 | TEST_F(ManagerTest, StartupPortalList) { |
| 2538 | // Simulate loading value from the default profile. |
| 2539 | const string kProfileValue("wifi,vpn"); |
| 2540 | manager()->props_.check_portal_list = kProfileValue; |
| 2541 | |
| 2542 | EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL)); |
| 2543 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi)); |
| 2544 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 2545 | |
| 2546 | const string kStartupValue("cellular,ethernet"); |
| 2547 | manager()->SetStartupPortalList(kStartupValue); |
| 2548 | // Ensure profile value is not overwritten, so when we save the default |
| 2549 | // profile, the correct value will still be written. |
| 2550 | EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list); |
| 2551 | |
| 2552 | // However we should read back a different list. |
| 2553 | EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL)); |
| 2554 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi)); |
| 2555 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 2556 | |
| 2557 | const string kRuntimeValue("ppp"); |
| 2558 | // Setting a runtime value over the control API should overwrite both |
| 2559 | // the profile value and what we read back. |
| 2560 | Error error; |
| 2561 | manager()->mutable_store()->SetStringProperty( |
| 2562 | flimflam::kCheckPortalListProperty, |
| 2563 | kRuntimeValue, |
| 2564 | &error); |
| 2565 | ASSERT_TRUE(error.IsSuccess()); |
| 2566 | EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL)); |
| 2567 | EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list); |
| 2568 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 2569 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP)); |
| 2570 | } |
| 2571 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 2572 | TEST_F(ManagerTest, LinkMonitorEnabled) { |
| 2573 | const string kEnabledTechnologies("wifi,vpn"); |
| 2574 | manager()->props_.link_monitor_technologies = kEnabledTechnologies; |
| 2575 | EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi)); |
| 2576 | EXPECT_FALSE( |
| 2577 | manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular)); |
| 2578 | } |
| 2579 | |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 2580 | TEST_F(ManagerTest, EnableTechnology) { |
| 2581 | Error error(Error::kOperationInitiated); |
| 2582 | ResultCallback callback; |
| 2583 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2584 | EXPECT_TRUE(error.IsSuccess()); |
| 2585 | |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 2586 | ON_CALL(*mock_devices_[0], technology()) |
| 2587 | .WillByDefault(Return(Technology::kEthernet)); |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 2588 | |
| 2589 | manager()->RegisterDevice(mock_devices_[0]); |
| 2590 | |
| 2591 | // Device is enabled, so expect operation is successful. |
| 2592 | mock_devices_[0]->enabled_ = true; |
| 2593 | error.Populate(Error::kOperationInitiated); |
| 2594 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2595 | EXPECT_TRUE(error.IsSuccess()); |
| 2596 | |
| 2597 | // Device is disabled, so expect operation in progress. |
| 2598 | mock_devices_[0]->enabled_ = false; |
| 2599 | EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _)); |
| 2600 | error.Populate(Error::kOperationInitiated); |
| 2601 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2602 | EXPECT_TRUE(error.IsOngoing()); |
| 2603 | } |
| 2604 | |
| 2605 | TEST_F(ManagerTest, DisableTechnology) { |
| 2606 | Error error(Error::kOperationInitiated); |
| 2607 | ResultCallback callback; |
| 2608 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2609 | EXPECT_TRUE(error.IsSuccess()); |
| 2610 | |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 2611 | ON_CALL(*mock_devices_[0], technology()) |
| 2612 | .WillByDefault(Return(Technology::kEthernet)); |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 2613 | |
| 2614 | manager()->RegisterDevice(mock_devices_[0]); |
| 2615 | |
| 2616 | // Device is disabled, so expect operation is successful. |
| 2617 | error.Populate(Error::kOperationInitiated); |
| 2618 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2619 | EXPECT_TRUE(error.IsSuccess()); |
| 2620 | |
| 2621 | // Device is enabled, so expect operation in progress. |
| 2622 | EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _)); |
| 2623 | mock_devices_[0]->enabled_ = true; |
| 2624 | error.Populate(Error::kOperationInitiated); |
| 2625 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2626 | EXPECT_TRUE(error.IsOngoing()); |
| 2627 | } |
| 2628 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 2629 | TEST_F(ManagerTest, IgnoredSearchList) { |
| 2630 | scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>()); |
| 2631 | SetResolver(resolver.get()); |
| 2632 | vector<string> ignored_paths; |
| 2633 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 2634 | SetIgnoredDNSSearchPaths(""); |
| 2635 | EXPECT_EQ("", GetIgnoredDNSSearchPaths()); |
| 2636 | |
| 2637 | const string kIgnored0 = "chromium.org"; |
| 2638 | ignored_paths.push_back(kIgnored0); |
| 2639 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 2640 | SetIgnoredDNSSearchPaths(kIgnored0); |
| 2641 | EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths()); |
| 2642 | |
| 2643 | const string kIgnored1 = "google.com"; |
| 2644 | const string kIgnoredSum = kIgnored0 + "," + kIgnored1; |
| 2645 | ignored_paths.push_back(kIgnored1); |
| 2646 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 2647 | SetIgnoredDNSSearchPaths(kIgnoredSum); |
| 2648 | EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths()); |
| 2649 | |
| 2650 | SetResolver(Resolver::GetInstance()); |
| 2651 | } |
| 2652 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 2653 | } // namespace shill |