Chromium Code Reviews| Index: chrome/browser/extensions/api/token_cache/token_cache_unittest.cc |
| diff --git a/chrome/browser/extensions/api/token_cache/token_cache_unittest.cc b/chrome/browser/extensions/api/token_cache/token_cache_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b8379b752fd81419910a84bdc00fe3358b85dfe6 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/token_cache/token_cache_unittest.cc |
| @@ -0,0 +1,131 @@ |
| +// 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/api/token_cache/token_cache.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::Time; |
| +using base::TimeDelta; |
| + |
| +namespace { |
| + base::TimeDelta zero; |
|
dcheng
2013/03/04 22:40:59
Indent is incorrect. Also, I think this will creat
Pete Williamson
2013/03/05 19:42:25
Done.
|
| +} // namespace |
| + |
| +class TokenCacheTest : public testing::Test { |
| + public: |
| + TokenCacheTest() : cache_(NULL) {} |
| + virtual ~TokenCacheTest() {} |
| + |
| + // Methods from testing::Test. |
| + virtual void SetUp() OVERRIDE {} |
| + virtual void TearDown() OVERRIDE { |
| + cache_.token_cache_.clear(); |
|
dcheng
2013/03/04 22:40:59
Why are we doing this?
Pete Williamson
2013/03/05 19:42:25
To make sure the cache is cleared out for the next
|
| + } |
| + |
| + size_t CacheSize() { |
| + return cache_.token_cache_.size(); |
| + } |
| + |
| + bool HasMatch(std::string token_name) { |
| + return cache_.FindMatch(token_name) != cache_.token_cache_.end(); |
| + } |
| + |
| + void InsertExpiredToken(std::string token_name, std::string token_value) { |
| + |
|
dcheng
2013/03/04 22:40:59
Extra newline...
Pete Williamson
2013/03/05 19:42:25
Done.
|
| + 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_name = token_name; |
| + token_data.token = token_value; |
| + token_data.expiration_time = yesterday; |
| + |
| + cache_.token_cache_.push_back(token_data); |
| + } |
| + |
| + protected: |
| + TokenCacheService cache_; |
| + |
| +}; |
| + |
| +TEST_F(TokenCacheTest, SaveTokenTest) { |
| + |
|
dcheng
2013/03/04 22:40:59
Extra newline...
Pete Williamson
2013/03/05 19:42:25
Done.
|
| + cache_.StoreToken("foo", "bar", zero); |
| + |
| + EXPECT_EQ(CacheSize(), (size_t) 1); |
| + EXPECT_TRUE(HasMatch("foo")); |
| +} |
| + |
| +TEST_F(TokenCacheTest, RetrieveTokenTest) { |
| + // TODO: does it make the test nicer to make these std::string and not rely on |
| + // autoconverting? |
|
dcheng
2013/03/04 22:40:59
Remove this TODO (I would argue the answer is no).
Pete Williamson
2013/03/05 19:42:25
Done.
|
| + 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) { |
| + 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(), (size_t) 1); |
|
dcheng
2013/03/04 22:40:59
Don't use C-style casts. In this case, you can jus
Pete Williamson
2013/03/05 19:42:25
Done.
|
| +} |
| + |
| +TEST_F(TokenCacheTest, SignoutTest) { |
| + cache_.StoreToken("foo", "bar", zero); |
| + content::Source<TokenCacheService> stub_source(&cache_); |
| + content::NotificationDetails stub_details; |
| + |
| + EXPECT_EQ(CacheSize(), (size_t) 1); |
| + EXPECT_TRUE(HasMatch("foo")); |
| + |
| + cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
| + stub_source, stub_details); |
| + |
| + EXPECT_EQ(CacheSize(), (size_t) 0); |
| + EXPECT_FALSE(HasMatch("foo")); |
| +} |
| + |
| +TEST_F(TokenCacheTest, ShutdownTest) { |
| + cache_.StoreToken("foo", "bar", zero); |
| + |
| + EXPECT_EQ(CacheSize(), (size_t) 1); |
| + EXPECT_TRUE(HasMatch("foo")); |
| + |
| + cache_.Shutdown(); |
| + |
| + EXPECT_EQ(CacheSize(), (size_t) 0); |
| + 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(), (size_t) 1); |
| + |
| + // If we attempt to find the token, the attempt should fail. |
| + EXPECT_FALSE(HasMatch("foo")); |
| + EXPECT_EQ(CacheSize(), (size_t) 0); |
| + |
| +} |