OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "base/basictypes.h" | |
6 #include "base/compiler_specific.h" | |
7 #include "base/time.h" | |
8 #include "chrome/browser/extensions/api/token_cache/token_cache.h" | |
9 #include "chrome/common/chrome_notification_types.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 using base::Time; | |
13 using base::TimeDelta; | |
14 | |
15 namespace { | |
16 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.
| |
17 } // namespace | |
18 | |
19 class TokenCacheTest : public testing::Test { | |
20 public: | |
21 TokenCacheTest() : cache_(NULL) {} | |
22 virtual ~TokenCacheTest() {} | |
23 | |
24 // Methods from testing::Test. | |
25 virtual void SetUp() OVERRIDE {} | |
26 virtual void TearDown() OVERRIDE { | |
27 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
| |
28 } | |
29 | |
30 size_t CacheSize() { | |
31 return cache_.token_cache_.size(); | |
32 } | |
33 | |
34 bool HasMatch(std::string token_name) { | |
35 return cache_.FindMatch(token_name) != cache_.token_cache_.end(); | |
36 } | |
37 | |
38 void InsertExpiredToken(std::string token_name, std::string token_value) { | |
39 | |
dcheng
2013/03/04 22:40:59
Extra newline...
Pete Williamson
2013/03/05 19:42:25
Done.
| |
40 EXPECT_TRUE(!HasMatch(token_name)); | |
41 | |
42 // Compute a time value for yesterday | |
43 Time now = Time::Now(); | |
44 TimeDelta one_day = one_day.FromDays(1); | |
45 Time yesterday = now - one_day; | |
46 | |
47 TokenCacheData token_data; | |
48 token_data.token_name = token_name; | |
49 token_data.token = token_value; | |
50 token_data.expiration_time = yesterday; | |
51 | |
52 cache_.token_cache_.push_back(token_data); | |
53 } | |
54 | |
55 protected: | |
56 TokenCacheService cache_; | |
57 | |
58 }; | |
59 | |
60 TEST_F(TokenCacheTest, SaveTokenTest) { | |
61 | |
dcheng
2013/03/04 22:40:59
Extra newline...
Pete Williamson
2013/03/05 19:42:25
Done.
| |
62 cache_.StoreToken("foo", "bar", zero); | |
63 | |
64 EXPECT_EQ(CacheSize(), (size_t) 1); | |
65 EXPECT_TRUE(HasMatch("foo")); | |
66 } | |
67 | |
68 TEST_F(TokenCacheTest, RetrieveTokenTest) { | |
69 // TODO: does it make the test nicer to make these std::string and not rely on | |
70 // 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.
| |
71 cache_.StoreToken("Mozart", "Eine Kleine Nacht Musik", zero); | |
72 cache_.StoreToken("Bach", "Brandenburg Concerto #3", zero); | |
73 cache_.StoreToken("Beethoven", "Emperor Piano Concerto #5", zero); | |
74 cache_.StoreToken("Handel", "Water Music", zero); | |
75 cache_.StoreToken("Chopin", "Heroic", zero); | |
76 | |
77 std::string found_token = cache_.RetrieveToken("Chopin"); | |
78 EXPECT_EQ("Heroic", found_token); | |
79 } | |
80 | |
81 TEST_F(TokenCacheTest, ReplaceTokenTest) { | |
82 cache_.StoreToken("Chopin", "Heroic", zero); | |
83 | |
84 std::string found_token = cache_.RetrieveToken("Chopin"); | |
85 EXPECT_EQ("Heroic", found_token); | |
86 | |
87 cache_.StoreToken("Chopin", "Military", zero); | |
88 | |
89 found_token = cache_.RetrieveToken("Chopin"); | |
90 EXPECT_EQ("Military", found_token); | |
91 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.
| |
92 } | |
93 | |
94 TEST_F(TokenCacheTest, SignoutTest) { | |
95 cache_.StoreToken("foo", "bar", zero); | |
96 content::Source<TokenCacheService> stub_source(&cache_); | |
97 content::NotificationDetails stub_details; | |
98 | |
99 EXPECT_EQ(CacheSize(), (size_t) 1); | |
100 EXPECT_TRUE(HasMatch("foo")); | |
101 | |
102 cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
103 stub_source, stub_details); | |
104 | |
105 EXPECT_EQ(CacheSize(), (size_t) 0); | |
106 EXPECT_FALSE(HasMatch("foo")); | |
107 } | |
108 | |
109 TEST_F(TokenCacheTest, ShutdownTest) { | |
110 cache_.StoreToken("foo", "bar", zero); | |
111 | |
112 EXPECT_EQ(CacheSize(), (size_t) 1); | |
113 EXPECT_TRUE(HasMatch("foo")); | |
114 | |
115 cache_.Shutdown(); | |
116 | |
117 EXPECT_EQ(CacheSize(), (size_t) 0); | |
118 EXPECT_FALSE(HasMatch("foo")); | |
119 } | |
120 | |
121 TEST_F(TokenCacheTest, TokenExpireTest) { | |
122 // Use the fact that we are friends to insert an expired token. | |
123 InsertExpiredToken("foo", "bar"); | |
124 | |
125 EXPECT_EQ(CacheSize(), (size_t) 1); | |
126 | |
127 // If we attempt to find the token, the attempt should fail. | |
128 EXPECT_FALSE(HasMatch("foo")); | |
129 EXPECT_EQ(CacheSize(), (size_t) 0); | |
130 | |
131 } | |
OLD | NEW |