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" |
11 #include "net/base/sdch_manager.h" | 11 #include "net/base/sdch_manager.h" |
12 #include "net/base/sdch_observer.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "url/gurl.h" | |
13 | 15 |
14 namespace net { | 16 namespace net { |
15 | 17 |
16 //------------------------------------------------------------------------------ | 18 //------------------------------------------------------------------------------ |
17 // Workaround for http://crbug.com/418975; remove when fixed. | 19 // Workaround for http://crbug.com/418975; remove when fixed. |
18 #if !defined(OS_IOS) | 20 #if !defined(OS_IOS) |
19 | 21 |
20 //------------------------------------------------------------------------------ | 22 //------------------------------------------------------------------------------ |
21 // Provide sample data and compression results with a sample VCDIFF dictionary. | 23 // Provide sample data and compression results with a sample VCDIFF dictionary. |
22 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. | 24 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. |
23 static const char kTestVcdiffDictionary[] = "DictionaryFor" | 25 static const char kTestVcdiffDictionary[] = "DictionaryFor" |
24 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | 26 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
25 | 27 |
26 //------------------------------------------------------------------------------ | 28 //------------------------------------------------------------------------------ |
27 | 29 |
28 class SdchManagerTest : public testing::Test { | 30 class SdchManagerTest : public testing::Test, public SdchObserver { |
29 protected: | 31 protected: |
30 SdchManagerTest() | 32 SdchManagerTest() |
31 : sdch_manager_(new SdchManager), | 33 : sdch_manager_(new SdchManager), |
32 default_support_(false), | 34 default_support_(false), |
33 default_https_support_(false) { | 35 default_https_support_(false), |
36 get_dictionary_notifications_(0) { | |
34 default_support_ = sdch_manager_->sdch_enabled(); | 37 default_support_ = sdch_manager_->sdch_enabled(); |
35 default_https_support_ = sdch_manager_->secure_scheme_supported(); | 38 default_https_support_ = sdch_manager_->secure_scheme_supported(); |
39 sdch_manager_->AddObserver(this); | |
36 } | 40 } |
37 | 41 |
42 virtual ~SdchManagerTest() { sdch_manager_->RemoveObserver(this); } | |
43 | |
38 SdchManager* sdch_manager() { return sdch_manager_.get(); } | 44 SdchManager* sdch_manager() { return sdch_manager_.get(); } |
39 | 45 |
40 // Reset globals back to default state. | 46 // Reset globals back to default state. |
41 void TearDown() override { | 47 void TearDown() override { |
42 SdchManager::EnableSdchSupport(default_support_); | 48 SdchManager::EnableSdchSupport(default_support_); |
43 SdchManager::EnableSecureSchemeSupport(default_https_support_); | 49 SdchManager::EnableSecureSchemeSupport(default_https_support_); |
44 } | 50 } |
45 | 51 |
46 // Attempt to add a dictionary to the manager and probe for success or | 52 // Attempt to add a dictionary to the manager and probe for success or |
47 // failure. | 53 // failure. |
48 bool AddSdchDictionary(const std::string& dictionary_text, | 54 bool AddSdchDictionary(const std::string& dictionary_text, |
49 const GURL& gurl) { | 55 const GURL& gurl) { |
50 std::string list; | 56 std::string list; |
51 sdch_manager_->GetAvailDictionaryList(gurl, &list); | 57 sdch_manager_->GetAvailDictionaryList(gurl, &list); |
52 sdch_manager_->AddSdchDictionary(dictionary_text, gurl); | 58 sdch_manager_->AddSdchDictionary(dictionary_text, gurl); |
53 std::string list2; | 59 std::string list2; |
54 sdch_manager_->GetAvailDictionaryList(gurl, &list2); | 60 sdch_manager_->GetAvailDictionaryList(gurl, &list2); |
55 | 61 |
56 // The list of hashes should change iff the addition succeeds. | 62 // The list of hashes should change iff the addition succeeds. |
57 return (list != list2); | 63 return (list != list2); |
58 } | 64 } |
59 | 65 |
66 const GURL& last_dictionary_request_url() { | |
67 return last_dictionary_request_url_; | |
68 } | |
69 const GURL& last_dictionary_url() { return last_dictionary_url_; } | |
70 int get_dictionary_notifications() { return get_dictionary_notifications_; } | |
71 | |
72 // SdchObserver implementation | |
73 void OnGetDictionary(SdchManager* manager, | |
74 const GURL& request_url, | |
75 const GURL& dictionary_url) override { | |
76 ++get_dictionary_notifications_; | |
77 last_dictionary_request_url_ = request_url; | |
78 last_dictionary_url_ = dictionary_url; | |
79 } | |
80 void OnClearDictionaries(SdchManager* manager) override {} | |
81 | |
60 private: | 82 private: |
61 scoped_ptr<SdchManager> sdch_manager_; | 83 scoped_ptr<SdchManager> sdch_manager_; |
62 bool default_support_; | 84 bool default_support_; |
63 bool default_https_support_; | 85 bool default_https_support_; |
86 int get_dictionary_notifications_; | |
87 GURL last_dictionary_request_url_; | |
88 GURL last_dictionary_url_; | |
64 }; | 89 }; |
65 | 90 |
66 static std::string NewSdchDictionary(const std::string& domain) { | 91 static std::string NewSdchDictionary(const std::string& domain) { |
67 std::string dictionary; | 92 std::string dictionary; |
68 if (!domain.empty()) { | 93 if (!domain.empty()) { |
69 dictionary.append("Domain: "); | 94 dictionary.append("Domain: "); |
70 dictionary.append(domain); | 95 dictionary.append(domain); |
71 dictionary.append("\n"); | 96 dictionary.append("\n"); |
72 } | 97 } |
73 dictionary.append("\n"); | 98 dictionary.append("\n"); |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
565 | 590 |
566 dictionary = NULL; | 591 dictionary = NULL; |
567 sdch_manager()->GetVcdiffDictionary( | 592 sdch_manager()->GetVcdiffDictionary( |
568 server_hash, | 593 server_hash, |
569 GURL("http://" + dictionary_domain + "/random_url"), | 594 GURL("http://" + dictionary_domain + "/random_url"), |
570 &dictionary); | 595 &dictionary); |
571 EXPECT_FALSE(dictionary.get()); | 596 EXPECT_FALSE(dictionary.get()); |
572 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url)); | 597 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url)); |
573 } | 598 } |
574 | 599 |
600 TEST_F(SdchManagerTest, GetDictionaryNotification) { | |
Ryan Sleevi
2014/11/05 22:21:39
Pedantry: You don't test that _removing_ the obser
Randy Smith (Not in Mondays)
2014/11/05 23:49:59
Good point. Done.
| |
601 GURL test_request_gurl(GURL("http://www.example.com/data")); | |
602 GURL test_dictionary_gurl(GURL("http://www.example.com/dict")); | |
603 | |
604 EXPECT_EQ(0, get_dictionary_notifications()); | |
605 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl); | |
606 EXPECT_EQ(1, get_dictionary_notifications()); | |
607 EXPECT_EQ(test_request_gurl, last_dictionary_request_url()); | |
608 EXPECT_EQ(test_dictionary_gurl, last_dictionary_url()); | |
609 } | |
610 | |
575 #else | 611 #else |
576 | 612 |
577 TEST(SdchManagerTest, SdchOffByDefault) { | 613 TEST(SdchManagerTest, SdchOffByDefault) { |
578 GURL google_url("http://www.google.com"); | 614 GURL google_url("http://www.google.com"); |
579 SdchManager* sdch_manager(new SdchManager); | 615 SdchManager* sdch_manager(new SdchManager); |
580 | 616 |
581 EXPECT_FALSE(sdch_manager->IsInSupportedDomain(google_url)); | 617 EXPECT_FALSE(sdch_manager->IsInSupportedDomain(google_url)); |
582 SdchManager::EnableSdchSupport(true); | 618 SdchManager::EnableSdchSupport(true); |
583 EXPECT_TRUE(sdch_manager->IsInSupportedDomain(google_url)); | 619 EXPECT_TRUE(sdch_manager->IsInSupportedDomain(google_url)); |
584 } | 620 } |
585 | 621 |
586 #endif // !defined(OS_IOS) | 622 #endif // !defined(OS_IOS) |
587 | 623 |
588 } // namespace net | 624 } // namespace net |
OLD | NEW |