OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cookies/cookie_store_unittest.h" | 5 #include "net/cookies/cookie_store_unittest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/scoped_vector.h" |
15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
17 #include "base/metrics/histogram_samples.h" | 18 #include "base/metrics/histogram_samples.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
20 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
21 #include "base/strings/string_tokenizer.h" | 22 #include "base/strings/string_tokenizer.h" |
22 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
23 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
24 #include "base/time/time.h" | 25 #include "base/time/time.h" |
(...skipping 2705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2730 &initial_cookies); | 2731 &initial_cookies); |
2731 | 2732 |
2732 // Inject our initial cookies into the mock PersistentCookieStore. | 2733 // Inject our initial cookies into the mock PersistentCookieStore. |
2733 store->SetLoadExpectation(true, initial_cookies); | 2734 store->SetLoadExpectation(true, initial_cookies); |
2734 | 2735 |
2735 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL)); | 2736 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL)); |
2736 | 2737 |
2737 EXPECT_EQ("foo=bar; hello=world", GetCookies(cm.get(), url)); | 2738 EXPECT_EQ("foo=bar; hello=world", GetCookies(cm.get(), url)); |
2738 } | 2739 } |
2739 | 2740 |
| 2741 class CookieMonsterNotificationTest : public CookieMonsterTest { |
| 2742 public: |
| 2743 CookieMonsterNotificationTest() |
| 2744 : test_url_("http://www.google.com/foo"), |
| 2745 store_(new MockPersistentCookieStore), |
| 2746 monster_(new CookieMonster(store_.get(), NULL)) {} |
| 2747 |
| 2748 virtual ~CookieMonsterNotificationTest() {} |
| 2749 |
| 2750 CookieMonster* monster() { return monster_.get(); } |
| 2751 |
| 2752 protected: |
| 2753 const GURL test_url_; |
| 2754 |
| 2755 private: |
| 2756 scoped_refptr<MockPersistentCookieStore> store_; |
| 2757 scoped_refptr<CookieMonster> monster_; |
| 2758 }; |
| 2759 |
| 2760 void CountCalls(int *calls) { |
| 2761 (*calls)++; |
| 2762 } |
| 2763 |
| 2764 TEST_F(CookieMonsterNotificationTest, NoNotifyWithNoCookie) { |
| 2765 int calls = 0; |
| 2766 scoped_ptr<CookieStore::CookieChangedSubscription> sub( |
| 2767 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2768 base::Bind(&CountCalls, &calls))); |
| 2769 base::MessageLoop::current()->RunUntilIdle(); |
| 2770 EXPECT_EQ(0, calls); |
| 2771 } |
| 2772 |
| 2773 TEST_F(CookieMonsterNotificationTest, NoNotifyWithInitialCookie) { |
| 2774 int calls = 0; |
| 2775 SetCookie(monster(), test_url_, "abc=def"); |
| 2776 base::MessageLoop::current()->RunUntilIdle(); |
| 2777 scoped_ptr<CookieStore::CookieChangedSubscription> sub( |
| 2778 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2779 base::Bind(&CountCalls, &calls))); |
| 2780 base::MessageLoop::current()->RunUntilIdle(); |
| 2781 EXPECT_EQ(0, calls); |
| 2782 } |
| 2783 |
| 2784 TEST_F(CookieMonsterNotificationTest, NotifyOnSet) { |
| 2785 int calls = 0; |
| 2786 scoped_ptr<CookieStore::CookieChangedSubscription> sub( |
| 2787 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2788 base::Bind(&CountCalls, &calls))); |
| 2789 SetCookie(monster(), test_url_, "abc=def"); |
| 2790 base::MessageLoop::current()->RunUntilIdle(); |
| 2791 EXPECT_EQ(1, calls); |
| 2792 } |
| 2793 |
| 2794 TEST_F(CookieMonsterNotificationTest, NotifyOnDelete) { |
| 2795 int calls = 0; |
| 2796 scoped_ptr<CookieStore::CookieChangedSubscription> sub( |
| 2797 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2798 base::Bind(&CountCalls, &calls))); |
| 2799 SetCookie(monster(), test_url_, "abc=def"); |
| 2800 base::MessageLoop::current()->RunUntilIdle(); |
| 2801 EXPECT_EQ(1, calls); |
| 2802 DeleteCookie(monster(), test_url_, "abc"); |
| 2803 base::MessageLoop::current()->RunUntilIdle(); |
| 2804 EXPECT_EQ(2, calls); |
| 2805 } |
| 2806 |
| 2807 TEST_F(CookieMonsterNotificationTest, NotifyOnUpdate) { |
| 2808 int calls = 0; |
| 2809 scoped_ptr<CookieStore::CookieChangedSubscription> sub( |
| 2810 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2811 base::Bind(&CountCalls, &calls))); |
| 2812 SetCookie(monster(), test_url_, "abc=def"); |
| 2813 base::MessageLoop::current()->RunUntilIdle(); |
| 2814 EXPECT_EQ(1, calls); |
| 2815 // Replacing an existing cookie is actually a two-phase delete + set |
| 2816 // operation, so we get an extra notification. :( |
| 2817 SetCookie(monster(), test_url_, "abc=ghi"); |
| 2818 base::MessageLoop::current()->RunUntilIdle(); |
| 2819 EXPECT_EQ(3, calls); |
| 2820 } |
| 2821 |
| 2822 TEST_F(CookieMonsterNotificationTest, MultipleNotifies) { |
| 2823 int calls0 = 0; |
| 2824 int calls1 = 0; |
| 2825 scoped_ptr<CookieStore::CookieChangedSubscription> sub0( |
| 2826 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2827 base::Bind(&CountCalls, &calls0))); |
| 2828 scoped_ptr<CookieStore::CookieChangedSubscription> sub1( |
| 2829 monster()->AddCallbackForCookie(test_url_, "def", |
| 2830 base::Bind(&CountCalls, &calls1))); |
| 2831 SetCookie(monster(), test_url_, "abc=def"); |
| 2832 base::MessageLoop::current()->RunUntilIdle(); |
| 2833 EXPECT_EQ(1, calls0); |
| 2834 EXPECT_EQ(0, calls1); |
| 2835 SetCookie(monster(), test_url_, "def=abc"); |
| 2836 base::MessageLoop::current()->RunUntilIdle(); |
| 2837 EXPECT_EQ(1, calls0); |
| 2838 EXPECT_EQ(1, calls1); |
| 2839 } |
| 2840 |
| 2841 TEST_F(CookieMonsterNotificationTest, MultipleSameNotifies) { |
| 2842 int calls0 = 0; |
| 2843 int calls1 = 0; |
| 2844 scoped_ptr<CookieStore::CookieChangedSubscription> sub0( |
| 2845 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2846 base::Bind(&CountCalls, &calls0))); |
| 2847 scoped_ptr<CookieStore::CookieChangedSubscription> sub1( |
| 2848 monster()->AddCallbackForCookie(test_url_, "abc", |
| 2849 base::Bind(&CountCalls, &calls1))); |
| 2850 SetCookie(monster(), test_url_, "abc=def"); |
| 2851 base::MessageLoop::current()->RunUntilIdle(); |
| 2852 EXPECT_EQ(1, calls0); |
| 2853 EXPECT_EQ(1, calls1); |
| 2854 } |
| 2855 |
2740 } // namespace net | 2856 } // namespace net |
OLD | NEW |