BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / wifi_credentials_helper.cc
blob7472aa1e228041fb3d8d9a03310c6c54e99e1e4e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/sync/test/integration/wifi_credentials_helper.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_test.h"
12 #include "components/wifi_sync/wifi_credential_syncable_service.h"
13 #include "components/wifi_sync/wifi_credential_syncable_service_factory.h"
15 #if defined(OS_CHROMEOS)
16 #include "chrome/browser/sync/test/integration/wifi_credentials_helper_chromeos.h"
17 #endif
19 using wifi_sync::WifiCredential;
20 using wifi_sync::WifiCredentialSyncableService;
21 using wifi_sync::WifiCredentialSyncableServiceFactory;
22 using wifi_sync::WifiSecurityClass;
23 using sync_datatype_helper::test;
25 using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet;
27 namespace wifi_credentials_helper {
29 namespace {
31 void SetupClientForProfile(Profile* profile) {
32 #if defined(OS_CHROMEOS)
33 wifi_credentials_helper::chromeos::SetupClientForProfileChromeOs(profile);
34 #else
35 NOTREACHED();
36 #endif
39 WifiCredentialSyncableService* GetServiceForBrowserContext(
40 content::BrowserContext* context) {
41 return WifiCredentialSyncableServiceFactory::GetForBrowserContext(
42 context);
45 WifiCredentialSyncableService* GetServiceForProfile(int profile_index) {
46 return GetServiceForBrowserContext(test()->GetProfile(profile_index));
49 void AddWifiCredentialToProfile(
50 Profile* profile, const WifiCredential& credential) {
51 #if defined(OS_CHROMEOS)
52 wifi_credentials_helper::chromeos::AddWifiCredentialToProfileChromeOs(
53 profile, credential);
54 #else
55 NOTREACHED();
56 #endif
59 bool CredentialsMatch(const WifiCredentialSet& a_credentials,
60 const WifiCredentialSet& b_credentials) {
61 if (a_credentials.size() != b_credentials.size()) {
62 LOG(ERROR) << "CredentialSets a and b do not match in size: "
63 << a_credentials.size()
64 << " vs " << b_credentials.size() << " respectively.";
65 return false;
68 for (const WifiCredential& credential : a_credentials) {
69 if (b_credentials.find(credential) == b_credentials.end()) {
70 LOG(ERROR)
71 << "Network from a not found in b. "
72 << "SSID (hex): "
73 << base::HexEncode(credential.ssid().data(),
74 credential.ssid().size()).c_str()
75 << " SecurityClass: " << credential.security_class()
76 << " Passphrase: " << credential.passphrase();
77 return false;
81 return true;
84 } // namespace
86 void SetUp() {
87 #if defined(OS_CHROMEOS)
88 wifi_credentials_helper::chromeos::SetUpChromeOs();
89 #else
90 NOTREACHED();
91 #endif
94 void SetupClients() {
95 SetupClientForProfile(test()->verifier());
96 for (int i = 0; i < test()->num_clients(); ++i)
97 SetupClientForProfile(test()->GetProfile(i));
100 bool VerifierIsEmpty() {
101 return GetWifiCredentialsForProfile(test()->verifier()).empty();
104 bool ProfileMatchesVerifier(int profile_index) {
105 WifiCredentialSet verifier_credentials =
106 GetWifiCredentialsForProfile(test()->verifier());
107 WifiCredentialSet other_credentials =
108 GetWifiCredentialsForProfile(test()->GetProfile(profile_index));
109 return CredentialsMatch(verifier_credentials, other_credentials);
112 bool AllProfilesMatch() {
113 if (test()->use_verifier() && !ProfileMatchesVerifier(0)) {
114 LOG(ERROR) << "Profile 0 does not match verifier.";
115 return false;
118 WifiCredentialSet profile0_credentials =
119 GetWifiCredentialsForProfile(test()->GetProfile(0));
120 for (int i = 1; i < test()->num_clients(); ++i) {
121 WifiCredentialSet other_profile_credentials =
122 GetWifiCredentialsForProfile(test()->GetProfile(i));
123 if (!CredentialsMatch(profile0_credentials, other_profile_credentials)) {
124 LOG(ERROR) << "Profile " << i << " " << "does not match with profile 0.";
125 return false;
128 return true;
131 scoped_ptr<WifiCredential> MakeWifiCredential(const std::string& ssid,
132 WifiSecurityClass security_class,
133 const std::string& passphrase) {
134 return WifiCredential::Create(WifiCredential::MakeSsidBytesForTest(ssid),
135 security_class,
136 passphrase);
139 void AddWifiCredential(int profile_index,
140 const std::string& sync_id,
141 const WifiCredential& credential) {
142 AddWifiCredentialToProfile(test()->GetProfile(profile_index), credential);
143 if (test()->use_verifier())
144 AddWifiCredentialToProfile(test()->verifier(), credential);
146 // TODO(quiche): Remove this, once we have plumbing to route
147 // NetworkConfigurationObserver events to
148 // WifiCredentialSyncableService instances.
149 GetServiceForProfile(profile_index)
150 ->AddToSyncedNetworks(sync_id, credential);
153 WifiCredentialSet GetWifiCredentialsForProfile(const Profile* profile) {
154 #if defined(OS_CHROMEOS)
155 return wifi_credentials_helper::chromeos::
156 GetWifiCredentialsForProfileChromeOs(profile);
157 #else
158 NOTREACHED();
159 return WifiCredential::MakeSet();
160 #endif
163 } // namespace wifi_credentials_helper