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