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