OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <limits.h> | 5 #include <limits.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 // Provide sample data and compression results with a sample VCDIFF dictionary. | 22 // Provide sample data and compression results with a sample VCDIFF dictionary. |
23 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. | 23 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. |
24 static const char kTestVcdiffDictionary[] = "DictionaryFor" | 24 static const char kTestVcdiffDictionary[] = "DictionaryFor" |
25 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | 25 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
26 | 26 |
27 //------------------------------------------------------------------------------ | 27 //------------------------------------------------------------------------------ |
28 | 28 |
29 class MockSdchObserver : public SdchObserver { | 29 class MockSdchObserver : public SdchObserver { |
30 public: | 30 public: |
31 MockSdchObserver() | 31 MockSdchObserver() |
32 : dictionary_used_notifications_(0), | 32 : dictionary_added_notifications_(0), |
| 33 dictionary_removed_notifications_(0), |
| 34 dictionary_used_notifications_(0), |
33 get_dictionary_notifications_(0), | 35 get_dictionary_notifications_(0), |
34 clear_dictionaries_notifications_(0) {} | 36 clear_dictionaries_notifications_(0) {} |
35 | 37 |
| 38 int dictionary_added_notifications() const { |
| 39 return dictionary_added_notifications_; |
| 40 } |
| 41 int dictionary_removed_notifications() const { |
| 42 return dictionary_removed_notifications_; |
| 43 } |
36 std::string last_server_hash() const { return last_server_hash_; } | 44 std::string last_server_hash() const { return last_server_hash_; } |
37 int dictionary_used_notifications() const { | 45 int dictionary_used_notifications() const { |
38 return dictionary_used_notifications_; | 46 return dictionary_used_notifications_; |
39 } | 47 } |
40 const GURL& last_dictionary_request_url() const { | 48 const GURL& last_dictionary_request_url() const { |
41 return last_dictionary_request_url_; | 49 return last_dictionary_request_url_; |
42 } | 50 } |
43 const GURL& last_dictionary_url() const { return last_dictionary_url_; } | 51 const GURL& last_dictionary_url() const { return last_dictionary_url_; } |
44 int get_dictionary_notifications() const { | 52 int get_dictionary_notifications() const { |
45 return get_dictionary_notifications_; | 53 return get_dictionary_notifications_; |
46 } | 54 } |
47 | 55 |
48 int clear_dictionary_notifications() const { | 56 int clear_dictionary_notifications() const { |
49 return clear_dictionaries_notifications_; | 57 return clear_dictionaries_notifications_; |
50 } | 58 } |
51 | 59 |
52 // SdchObserver implementation | 60 // SdchObserver implementation |
53 void OnDictionaryUsed(SdchManager* manager, | 61 void OnDictionaryAdded(const GURL& dictionary_url, |
54 const std::string& server_hash) override { | 62 const std::string& server_hash) override { |
| 63 last_server_hash_ = server_hash; |
| 64 last_dictionary_url_ = dictionary_url; |
| 65 ++dictionary_added_notifications_; |
| 66 } |
| 67 void OnDictionaryRemoved(const std::string& server_hash) override { |
| 68 last_server_hash_ = server_hash; |
| 69 ++dictionary_removed_notifications_; |
| 70 } |
| 71 void OnDictionaryUsed(const std::string& server_hash) override { |
55 last_server_hash_ = server_hash; | 72 last_server_hash_ = server_hash; |
56 ++dictionary_used_notifications_; | 73 ++dictionary_used_notifications_; |
57 } | 74 } |
58 | 75 |
59 void OnGetDictionary(SdchManager* manager, | 76 void OnGetDictionary(const GURL& request_url, |
60 const GURL& request_url, | |
61 const GURL& dictionary_url) override { | 77 const GURL& dictionary_url) override { |
62 ++get_dictionary_notifications_; | 78 ++get_dictionary_notifications_; |
63 last_dictionary_request_url_ = request_url; | 79 last_dictionary_request_url_ = request_url; |
64 last_dictionary_url_ = dictionary_url; | 80 last_dictionary_url_ = dictionary_url; |
65 } | 81 } |
66 void OnClearDictionaries(SdchManager* manager) override { | 82 void OnClearDictionaries() override { |
67 ++clear_dictionaries_notifications_; | 83 ++clear_dictionaries_notifications_; |
68 } | 84 } |
69 | 85 |
70 private: | 86 private: |
| 87 int dictionary_added_notifications_; |
| 88 int dictionary_removed_notifications_; |
71 int dictionary_used_notifications_; | 89 int dictionary_used_notifications_; |
72 int get_dictionary_notifications_; | 90 int get_dictionary_notifications_; |
73 int clear_dictionaries_notifications_; | 91 int clear_dictionaries_notifications_; |
74 | 92 |
75 std::string last_server_hash_; | 93 std::string last_server_hash_; |
76 GURL last_dictionary_request_url_; | 94 GURL last_dictionary_request_url_; |
77 GURL last_dictionary_url_; | 95 GURL last_dictionary_url_; |
78 | 96 |
79 DISALLOW_COPY_AND_ASSIGN(MockSdchObserver); | 97 DISALLOW_COPY_AND_ASSIGN(MockSdchObserver); |
80 }; | 98 }; |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 EXPECT_EQ(1, observer.dictionary_used_notifications()); | 639 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
622 EXPECT_EQ("xyzzy", observer.last_server_hash()); | 640 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
623 | 641 |
624 std::string dictionary_domain("x.y.z.google.com"); | 642 std::string dictionary_domain("x.y.z.google.com"); |
625 GURL target_gurl("http://" + dictionary_domain); | 643 GURL target_gurl("http://" + dictionary_domain); |
626 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 644 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
627 std::string client_hash; | 645 std::string client_hash; |
628 std::string server_hash; | 646 std::string server_hash; |
629 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); | 647 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); |
630 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl)); | 648 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl)); |
631 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
632 EXPECT_EQ(1, observer.dictionary_used_notifications()); | 649 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
633 | 650 |
634 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_gurl)); | 651 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_gurl)); |
635 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
636 EXPECT_EQ(1, observer.dictionary_used_notifications()); | 652 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
637 | 653 |
638 sdch_manager()->RemoveObserver(&observer); | 654 sdch_manager()->RemoveObserver(&observer); |
639 EXPECT_EQ(1, observer.dictionary_used_notifications()); | 655 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
640 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
641 sdch_manager()->OnDictionaryUsed("plugh"); | 656 sdch_manager()->OnDictionaryUsed("plugh"); |
642 EXPECT_EQ(1, observer.dictionary_used_notifications()); | 657 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
643 EXPECT_EQ("xyzzy", observer.last_server_hash()); | 658 } |
| 659 |
| 660 TEST_F(SdchManagerTest, AddRemoveNotifications) { |
| 661 MockSdchObserver observer; |
| 662 sdch_manager()->AddObserver(&observer); |
| 663 |
| 664 std::string dictionary_domain("x.y.z.google.com"); |
| 665 GURL target_gurl("http://" + dictionary_domain); |
| 666 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 667 std::string client_hash; |
| 668 std::string server_hash; |
| 669 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); |
| 670 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl)); |
| 671 EXPECT_EQ(1, observer.dictionary_added_notifications()); |
| 672 EXPECT_EQ(target_gurl, observer.last_dictionary_url()); |
| 673 EXPECT_EQ(server_hash, observer.last_server_hash()); |
| 674 |
| 675 EXPECT_EQ(SDCH_OK, sdch_manager()->RemoveSdchDictionary(server_hash)); |
| 676 EXPECT_EQ(1, observer.dictionary_removed_notifications()); |
| 677 EXPECT_EQ(server_hash, observer.last_server_hash()); |
| 678 |
| 679 sdch_manager()->RemoveObserver(&observer); |
644 } | 680 } |
645 | 681 |
646 } // namespace net | 682 } // namespace net |
OLD | NEW |