Index: chrome/browser/extensions/token_cache/token_cache_service_unittest.cc |
diff --git a/chrome/browser/extensions/token_cache/token_cache_service_unittest.cc b/chrome/browser/extensions/token_cache/token_cache_service_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..507cc7a7049e69da6b13941ae70437697b661c1f |
--- /dev/null |
+++ b/chrome/browser/extensions/token_cache/token_cache_service_unittest.cc |
@@ -0,0 +1,113 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/time.h" |
+#include "chrome/browser/extensions/token_cache/token_cache_service.h" |
+#include "chrome/common/chrome_notification_types.h" |
+#include "content/public/browser/notification_details.h" |
+#include "content/public/browser/notification_source.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using base::Time; |
+using base::TimeDelta; |
+ |
+namespace extensions { |
+ |
+class TokenCacheTest : public testing::Test { |
+ public: |
+ TokenCacheTest() : cache_(NULL) {} |
+ virtual ~TokenCacheTest() {} |
+ |
+ size_t CacheSize() { |
+ return cache_.token_cache_.size(); |
+ } |
+ |
+ bool HasMatch(const std::string& token_name) { |
+ return cache_.RetrieveToken(token_name) != std::string(); |
+ } |
+ |
+ void InsertExpiredToken(const std::string& token_name, |
+ const std::string& token_value) { |
+ EXPECT_TRUE(!HasMatch(token_name)); |
+ |
+ // Compute a time value for yesterday |
+ Time now = Time::Now(); |
+ TimeDelta one_day = one_day.FromDays(1); |
+ Time yesterday = now - one_day; |
+ |
+ TokenCacheData token_data; |
+ token_data.token = token_value; |
+ token_data.expiration_time = yesterday; |
+ |
+ cache_.token_cache_[token_name] = token_data; |
+ } |
+ |
+ protected: |
+ TokenCacheService cache_; |
+}; |
+ |
+TEST_F(TokenCacheTest, SaveTokenTest) { |
+ TimeDelta zero; |
+ cache_.StoreToken("foo", "bar", zero); |
+ |
+ EXPECT_EQ(CacheSize(), 1U); |
dcheng
2013/03/06 19:53:52
Nit: EXPECT_EQ(<actual>, <expected>) is the prefer
Pete Williamson
2013/03/06 21:12:11
Done.
|
+ EXPECT_TRUE(HasMatch("foo")); |
+} |
+ |
+TEST_F(TokenCacheTest, RetrieveTokenTest) { |
+ TimeDelta zero; |
+ cache_.StoreToken("Mozart", "Eine Kleine Nacht Musik", zero); |
+ cache_.StoreToken("Bach", "Brandenburg Concerto #3", zero); |
+ cache_.StoreToken("Beethoven", "Emperor Piano Concerto #5", zero); |
+ cache_.StoreToken("Handel", "Water Music", zero); |
+ cache_.StoreToken("Chopin", "Heroic", zero); |
+ |
+ std::string found_token = cache_.RetrieveToken("Chopin"); |
+ EXPECT_EQ("Heroic", found_token); |
+} |
+ |
+TEST_F(TokenCacheTest, ReplaceTokenTest) { |
+ TimeDelta zero; |
+ cache_.StoreToken("Chopin", "Heroic", zero); |
+ |
+ std::string found_token = cache_.RetrieveToken("Chopin"); |
+ EXPECT_EQ("Heroic", found_token); |
+ |
+ cache_.StoreToken("Chopin", "Military", zero); |
+ |
+ found_token = cache_.RetrieveToken("Chopin"); |
+ EXPECT_EQ("Military", found_token); |
+ EXPECT_EQ(CacheSize(), 1U); |
+} |
+ |
+TEST_F(TokenCacheTest, SignoutTest) { |
+ TimeDelta zero; |
+ cache_.StoreToken("foo", "bar", zero); |
+ content::Source<TokenCacheService> stub_source(&cache_); |
dcheng
2013/03/06 19:53:52
Hmm. This is supposed to be a Profile pointer righ
Pete Williamson
2013/03/06 21:12:11
Done.
|
+ content::NotificationDetails stub_details; |
+ |
+ EXPECT_EQ(CacheSize(), 1U); |
+ EXPECT_TRUE(HasMatch("foo")); |
+ |
+ cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
+ stub_source, stub_details); |
+ |
+ EXPECT_EQ(CacheSize(), 0U); |
+ EXPECT_FALSE(HasMatch("foo")); |
+} |
+ |
+TEST_F(TokenCacheTest, TokenExpireTest) { |
+ // Use the fact that we are friends to insert an expired token. |
+ InsertExpiredToken("foo", "bar"); |
+ |
+ EXPECT_EQ(CacheSize(), 1U); |
+ |
+ // If we attempt to find the token, the attempt should fail. |
+ EXPECT_FALSE(HasMatch("foo")); |
+ EXPECT_EQ(CacheSize(), 0U); |
+} |
+ |
+} // namespace extensions |