Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: chrome/browser/sync/credential_cache_service_win_unittest.cc

Issue 10656033: [sync] Automatic bootstrapping of Sync on Win 8 from cached credentials (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit tests Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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.
4
5 #include <string>
6
7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop.h"
9 #include "base/scoped_temp_dir.h"
10 #include "base/values.h"
11 #include "chrome/browser/sync/credential_cache_service_win.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/common/pref_names.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace syncer {
17
18 class CredentialCacheServiceTest : public CredentialCacheService {
19 public:
20 CredentialCacheServiceTest()
21 : CredentialCacheService(NULL),
22 file_message_loop_(MessageLoop::TYPE_IO) {
23 SetUp();
24 }
25
26 virtual ~CredentialCacheServiceTest() {}
27
28 void SetUp() {
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30 local_store_ = new JsonPrefStore(
31 temp_dir_.path().Append(chrome::kSyncCredentialsFilename),
32 file_message_loop_.message_loop_proxy());
33 }
34
35 // ProfileKeyedService implementation.
36 virtual void Shutdown() OVERRIDE {}
37
38 // PrefStore::Observer implementation.
39 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {}
40 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE {}
41
42 scoped_refptr<JsonPrefStore> local_store() { return local_store_; }
43
44 private:
45 ScopedTempDir temp_dir_;
46 MessageLoop file_message_loop_;
47 DISALLOW_COPY_AND_ASSIGN(CredentialCacheServiceTest);
48 };
49
50 } // namespace syncer
51
52 namespace {
53
54 TEST(CredentialCacheServiceTest, TestPackAndUnpack) {
55 // Pack a sample credential string.
56 std::string original = "sample_credential";
57 base::StringValue* packed =
58 syncer::CredentialCacheService::PackCredential(original);
59
60 // Unpack the result and make sure it matches the original.
61 std::string unpacked =
62 syncer::CredentialCacheService::UnpackCredential(*packed);
63 ASSERT_EQ(original, unpacked);
64 }
65
66 TEST(CredentialCacheServiceTest, TestPackAndUnpackEmpty) {
67 // Pack an empty credential string.
68 std::string original = "";
69 base::StringValue* packed =
70 syncer::CredentialCacheService::PackCredential(original);
71
72 // Make sure the packed value is an empty string.
73 std::string packed_string;
74 packed->GetAsString(&packed_string);
75 ASSERT_EQ(original, packed_string);
76
77 // Unpack it and make sure it matches the original.
78 std::string unpacked =
79 syncer::CredentialCacheService::UnpackCredential(*packed);
80 ASSERT_EQ(original, unpacked);
81 }
82
83 TEST(CredentialCacheServiceTest, TestWriteAndReadCredentials) {
84 syncer::CredentialCacheServiceTest ccs;
85 std::string username = "user@gmail.com";
86 std::string sync_token = "sync_token";
87 bool sync_everything = true;
88
89 // Write a string pref, a token and a boolean pref.
90 ccs.UpdateStringPref(prefs::kGoogleServicesUsername, username);
91 ccs.UpdateStringPref(syncer::kSyncServiceToken, sync_token);
92 ccs.UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything);
93
94 // Verify that they can be read, and that they contain the original values.
95 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kGoogleServicesUsername));
96 ASSERT_EQ(username, ccs.GetStringPref(ccs.local_store(),
97 prefs::kGoogleServicesUsername));
98 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), syncer::kSyncServiceToken));
99 ASSERT_EQ(sync_token, ccs.GetStringPref(ccs.local_store(),
100 syncer::kSyncServiceToken));
101 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kSyncKeepEverythingSynced));
102 ASSERT_TRUE(sync_everything ==
103 ccs.GetBooleanPref(ccs.local_store(),
104 prefs::kSyncKeepEverythingSynced));
105 }
106
107 TEST(CredentialCacheServiceTest, TestWriteAndReadCredentialsAfterSignOut) {
108 syncer::CredentialCacheServiceTest ccs;
109 std::string username = "user@gmail.com";
110 std::string sync_token = "sync_token";
111 bool sync_everything = true;
112
113 // Write a non-empty username, indicating that the user is signed in.
114 ccs.UpdateStringPref(prefs::kGoogleServicesUsername, username);
115 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kGoogleServicesUsername));
116 ASSERT_EQ(username, ccs.GetStringPref(ccs.local_store(),
117 prefs::kGoogleServicesUsername));
118
119 // Write an empty username, indicating that the user is signed out.
120 ccs.UpdateStringPref(prefs::kGoogleServicesUsername, "");
121 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kGoogleServicesUsername));
122 ASSERT_EQ("", ccs.GetStringPref(ccs.local_store(),
123 prefs::kGoogleServicesUsername));
124
125 // Try to write a non-empty string and make sure an empty string is written in
126 // its place.
127 ccs.UpdateStringPref(syncer::kSyncServiceToken, sync_token);
128 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), syncer::kSyncServiceToken));
129 ASSERT_EQ("", ccs.GetStringPref(ccs.local_store(),
130 syncer::kSyncServiceToken));
131
132 // Try to write a boolean pref with value true, and make sure an the default
133 // value of false is written in its place.
134 ccs.UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything);
135 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kSyncKeepEverythingSynced));
136 ASSERT_TRUE(false == ccs.GetBooleanPref(ccs.local_store(),
137 prefs::kSyncKeepEverythingSynced));
138 }
139
140 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698