1//
2// Copyright (C) 2013 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "shill/service_property_change_test.h"
18
19#include <string>
20
21#if defined(__ANDROID__)
22#include <dbus/service_constants.h>
23#else
24#include <chromeos/dbus/service_constants.h>
25#endif  // __ANDROID__
26#include <gtest/gtest.h>
27#include <gmock/gmock.h>
28
29#include "shill/error.h"
30#include "shill/mock_adaptors.h"
31#include "shill/mock_manager.h"
32#include "shill/mock_profile.h"
33#include "shill/refptr_types.h"
34#include "shill/service.h"
35
36using std::string;
37using testing::_;
38using testing::AnyNumber;
39using testing::Mock;
40using testing::NiceMock;
41
42namespace shill {
43
44// Some of these tests are duplicative, as we also have broader tests
45// for specific setters. However, it's convenient to have all the property
46// change notifications documented (and tested) in one place.
47
48void TestCommonPropertyChanges(ServiceRefPtr service,
49                               ServiceMockAdaptor* adaptor) {
50  Error error;
51
52  EXPECT_EQ(Service::kStateIdle, service->state());
53  EXPECT_CALL(*adaptor, EmitStringChanged(kStateProperty, _));
54  service->SetState(Service::kStateConnected);
55  Mock::VerifyAndClearExpectations(adaptor);
56
57  // TODO(quiche): Once crbug.com/216664 is resolved, add a test
58  // that service->SetConnection emits kIPConfigProperty changed.
59
60  bool connectable = service->connectable();
61  EXPECT_CALL(*adaptor, EmitBoolChanged(kConnectableProperty, _));
62  service->SetConnectable(!connectable);
63  Mock::VerifyAndClearExpectations(adaptor);
64
65  EXPECT_EQ(string(), service->guid());
66  EXPECT_CALL(*adaptor, EmitStringChanged(kGuidProperty, _));
67  service->SetGuid("some garbage", &error);
68  Mock::VerifyAndClearExpectations(adaptor);
69
70  // Depending on our caller, AutoConnect may be true.
71  service->ClearAutoConnect(nullptr);
72  EXPECT_FALSE(service->auto_connect());
73  EXPECT_CALL(*adaptor, EmitBoolChanged(kAutoConnectProperty, _));
74  service->SetAutoConnect(true);
75  Mock::VerifyAndClearExpectations(adaptor);
76
77  EXPECT_EQ(0, service->priority());
78  EXPECT_CALL(*adaptor, EmitIntChanged(kPriorityProperty, _));
79  service->SetPriority(1, &error);
80  Mock::VerifyAndClearExpectations(adaptor);
81
82  EXPECT_EQ(string(), service->GetProxyConfig(&error));
83  EXPECT_CALL(*adaptor, EmitStringChanged(kProxyConfigProperty, _));
84  service->SetProxyConfig("some garbage", &error);
85  Mock::VerifyAndClearExpectations(adaptor);
86
87  uint8_t strength = service->strength();
88  EXPECT_CALL(*adaptor,
89              EmitUint8Changed(kSignalStrengthProperty, _));
90  service->SetStrength(strength+1);
91  Mock::VerifyAndClearExpectations(adaptor);
92
93  EXPECT_EQ(string(), service->error_details());
94  EXPECT_CALL(*adaptor, EmitStringChanged(kErrorDetailsProperty, _));
95  service->SetErrorDetails("some garbage");
96  Mock::VerifyAndClearExpectations(adaptor);
97
98  EXPECT_EQ(Service::kFailureUnknown, service->failure());
99  EXPECT_EQ(Service::ConnectFailureToString(Service::kFailureUnknown),
100            service->error());
101  EXPECT_CALL(*adaptor, EmitStringChanged(kStateProperty, _));
102  EXPECT_CALL(*adaptor, EmitStringChanged(kErrorProperty, _));
103  service->SetFailure(Service::kFailureAAA);
104  Mock::VerifyAndClearExpectations(adaptor);
105
106  EXPECT_NE(Service::ConnectFailureToString(Service::kFailureUnknown),
107            service->error());
108  EXPECT_CALL(*adaptor, EmitStringChanged(kStateProperty, _));
109  EXPECT_CALL(*adaptor, EmitStringChanged(kErrorDetailsProperty, _));
110  EXPECT_CALL(*adaptor, EmitStringChanged(kErrorProperty, _));
111  service->SetState(Service::kStateConnected);
112  Mock::VerifyAndClearExpectations(adaptor);
113
114  EXPECT_EQ(Service::ConnectFailureToString(Service::kFailureUnknown),
115            service->error());
116  EXPECT_CALL(*adaptor, EmitStringChanged(kStateProperty, _));
117  EXPECT_CALL(*adaptor, EmitStringChanged(kErrorProperty, _));
118  service->SetFailureSilent(Service::kFailureAAA);
119  Mock::VerifyAndClearExpectations(adaptor);
120}
121
122void TestAutoConnectPropertyChange(ServiceRefPtr service,
123                                   ServiceMockAdaptor* adaptor) {
124  bool auto_connect = service->auto_connect();
125  EXPECT_CALL(*adaptor, EmitBoolChanged(kAutoConnectProperty, _));
126  service->SetAutoConnect(!auto_connect);
127  Mock::VerifyAndClearExpectations(adaptor);
128}
129
130void TestNamePropertyChange(ServiceRefPtr service,
131                            ServiceMockAdaptor* adaptor) {
132  Error error;
133  string name = service->GetNameProperty(&error);
134  EXPECT_CALL(*adaptor, EmitStringChanged(kNameProperty, _));
135  service->SetNameProperty(name + " and some new stuff", &error);
136  Mock::VerifyAndClearExpectations(adaptor);
137}
138
139void TestCustomSetterNoopChange(ServiceRefPtr service,
140                                MockManager* mock_manager) {
141  // SetAutoConnectFull
142  {
143    Error error;
144    EXPECT_CALL(*mock_manager, UpdateService(_)).Times(0);
145    EXPECT_FALSE(service->SetAutoConnectFull(service->auto_connect(), &error));
146    EXPECT_TRUE(error.IsSuccess());
147    Mock::VerifyAndClearExpectations(mock_manager);
148  }
149
150  // SetCheckPortal
151  {
152    Error error;
153    EXPECT_FALSE(service->SetCheckPortal(service->check_portal_, &error));
154    EXPECT_TRUE(error.IsSuccess());
155  }
156
157  // SetNameProperty
158  {
159    Error error;
160    EXPECT_FALSE(service->SetNameProperty(service->friendly_name_, &error));
161    EXPECT_TRUE(error.IsSuccess());
162  }
163
164  // SetProfileRpcId
165  {
166    Error error;
167    scoped_refptr<MockProfile> profile(
168        new NiceMock<MockProfile>(nullptr, nullptr, nullptr));
169    service->set_profile(profile);
170    EXPECT_FALSE(service->SetProfileRpcId(profile->GetRpcIdentifier(),
171                                           &error));
172    EXPECT_TRUE(error.IsSuccess());
173  }
174
175  // SetProxyConfig
176  {
177    Error error;
178    static const string kProxyConfig = "some opaque blob";
179    // Set to known value.
180    EXPECT_TRUE(service->SetProxyConfig(kProxyConfig, &error));
181    EXPECT_TRUE(error.IsSuccess());
182    // Set to same value.
183    EXPECT_FALSE(service->SetProxyConfig(kProxyConfig, &error));
184    EXPECT_TRUE(error.IsSuccess());
185  }
186}
187
188}  // namespace shill
189