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

Unified Diff: net/cookies/cookie_monster_unittest.cc

Issue 676073003: Add AddCallbackForCookie method to CookieStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix PrerenderCookieStore build Created 6 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/cookies/cookie_monster.cc ('k') | net/cookies/cookie_store.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/cookies/cookie_monster_unittest.cc
diff --git a/net/cookies/cookie_monster_unittest.cc b/net/cookies/cookie_monster_unittest.cc
index 5c76e8a0796787f9ebc7d2077c7ee3955bc6228d..5c3128153ede05c60ed4d89d17ddb939d5918e2d 100644
--- a/net/cookies/cookie_monster_unittest.cc
+++ b/net/cookies/cookie_monster_unittest.cc
@@ -12,6 +12,7 @@
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
+#include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
@@ -2737,4 +2738,119 @@ TEST_F(CookieMonsterTest, ControlCharacterPurge) {
EXPECT_EQ("foo=bar; hello=world", GetCookies(cm.get(), url));
}
+class CookieMonsterNotificationTest : public CookieMonsterTest {
+ public:
+ CookieMonsterNotificationTest()
+ : test_url_("http://www.google.com/foo"),
+ store_(new MockPersistentCookieStore),
+ monster_(new CookieMonster(store_.get(), NULL)) {}
+
+ virtual ~CookieMonsterNotificationTest() {}
+
+ CookieMonster* monster() { return monster_.get(); }
+
+ protected:
+ const GURL test_url_;
+
+ private:
+ scoped_refptr<MockPersistentCookieStore> store_;
+ scoped_refptr<CookieMonster> monster_;
+};
+
+void CountCalls(int *calls) {
+ (*calls)++;
+}
+
+TEST_F(CookieMonsterNotificationTest, NoNotifyWithNoCookie) {
+ int calls = 0;
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls)));
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(0, calls);
+}
+
+TEST_F(CookieMonsterNotificationTest, NoNotifyWithInitialCookie) {
+ int calls = 0;
+ SetCookie(monster(), test_url_, "abc=def");
+ base::MessageLoop::current()->RunUntilIdle();
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls)));
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(0, calls);
+}
+
+TEST_F(CookieMonsterNotificationTest, NotifyOnSet) {
+ int calls = 0;
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls)));
+ SetCookie(monster(), test_url_, "abc=def");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(1, calls);
+}
+
+TEST_F(CookieMonsterNotificationTest, NotifyOnDelete) {
+ int calls = 0;
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls)));
+ SetCookie(monster(), test_url_, "abc=def");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(1, calls);
+ DeleteCookie(monster(), test_url_, "abc");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(2, calls);
+}
+
+TEST_F(CookieMonsterNotificationTest, NotifyOnUpdate) {
+ int calls = 0;
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls)));
+ SetCookie(monster(), test_url_, "abc=def");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(1, calls);
+ // Replacing an existing cookie is actually a two-phase delete + set
+ // operation, so we get an extra notification. :(
+ SetCookie(monster(), test_url_, "abc=ghi");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(3, calls);
+}
+
+TEST_F(CookieMonsterNotificationTest, MultipleNotifies) {
+ int calls0 = 0;
+ int calls1 = 0;
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub0(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls0)));
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub1(
+ monster()->AddCallbackForCookie(test_url_, "def",
+ base::Bind(&CountCalls, &calls1)));
+ SetCookie(monster(), test_url_, "abc=def");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(1, calls0);
+ EXPECT_EQ(0, calls1);
+ SetCookie(monster(), test_url_, "def=abc");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(1, calls0);
+ EXPECT_EQ(1, calls1);
+}
+
+TEST_F(CookieMonsterNotificationTest, MultipleSameNotifies) {
+ int calls0 = 0;
+ int calls1 = 0;
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub0(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls0)));
+ scoped_ptr<CookieStore::CookieChangedSubscription> sub1(
+ monster()->AddCallbackForCookie(test_url_, "abc",
+ base::Bind(&CountCalls, &calls1)));
+ SetCookie(monster(), test_url_, "abc=def");
+ base::MessageLoop::current()->RunUntilIdle();
+ EXPECT_EQ(1, calls0);
+ EXPECT_EQ(1, calls1);
+}
+
} // namespace net
« no previous file with comments | « net/cookies/cookie_monster.cc ('k') | net/cookies/cookie_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698