| 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 |
| 30 class MockSdchObserver : public SdchObserver { |
| 31 public: |
| 32 MockSdchObserver() : get_dictionary_notifications_(0) {} |
| 33 |
| 34 const GURL& last_dictionary_request_url() { |
| 35 return last_dictionary_request_url_; |
| 36 } |
| 37 const GURL& last_dictionary_url() { return last_dictionary_url_; } |
| 38 int get_dictionary_notifications() { return get_dictionary_notifications_; } |
| 39 |
| 40 // SdchObserver implementation |
| 41 void OnGetDictionary(SdchManager* manager, |
| 42 const GURL& request_url, |
| 43 const GURL& dictionary_url) override { |
| 44 ++get_dictionary_notifications_; |
| 45 last_dictionary_request_url_ = request_url; |
| 46 last_dictionary_url_ = dictionary_url; |
| 47 } |
| 48 void OnClearDictionaries(SdchManager* manager) override {} |
| 49 |
| 50 private: |
| 51 int get_dictionary_notifications_; |
| 52 GURL last_dictionary_request_url_; |
| 53 GURL last_dictionary_url_; |
| 54 }; |
| 55 |
| 28 class SdchManagerTest : public testing::Test { | 56 class SdchManagerTest : public testing::Test { |
| 29 protected: | 57 protected: |
| 30 SdchManagerTest() | 58 SdchManagerTest() |
| 31 : sdch_manager_(new SdchManager), | 59 : sdch_manager_(new SdchManager), |
| 32 default_support_(false), | 60 default_support_(false), |
| 33 default_https_support_(false) { | 61 default_https_support_(false) { |
| 34 default_support_ = sdch_manager_->sdch_enabled(); | 62 default_support_ = sdch_manager_->sdch_enabled(); |
| 35 default_https_support_ = sdch_manager_->secure_scheme_supported(); | 63 default_https_support_ = sdch_manager_->secure_scheme_supported(); |
| 36 } | 64 } |
| 37 | 65 |
| 66 virtual ~SdchManagerTest() {} |
| 67 |
| 38 SdchManager* sdch_manager() { return sdch_manager_.get(); } | 68 SdchManager* sdch_manager() { return sdch_manager_.get(); } |
| 39 | 69 |
| 40 // Reset globals back to default state. | 70 // Reset globals back to default state. |
| 41 void TearDown() override { | 71 void TearDown() override { |
| 42 SdchManager::EnableSdchSupport(default_support_); | 72 SdchManager::EnableSdchSupport(default_support_); |
| 43 SdchManager::EnableSecureSchemeSupport(default_https_support_); | 73 SdchManager::EnableSecureSchemeSupport(default_https_support_); |
| 44 } | 74 } |
| 45 | 75 |
| 46 // Attempt to add a dictionary to the manager and probe for success or | 76 // Attempt to add a dictionary to the manager and probe for success or |
| 47 // failure. | 77 // failure. |
| (...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 | 595 |
| 566 dictionary = NULL; | 596 dictionary = NULL; |
| 567 sdch_manager()->GetVcdiffDictionary( | 597 sdch_manager()->GetVcdiffDictionary( |
| 568 server_hash, | 598 server_hash, |
| 569 GURL("http://" + dictionary_domain + "/random_url"), | 599 GURL("http://" + dictionary_domain + "/random_url"), |
| 570 &dictionary); | 600 &dictionary); |
| 571 EXPECT_FALSE(dictionary.get()); | 601 EXPECT_FALSE(dictionary.get()); |
| 572 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url)); | 602 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url)); |
| 573 } | 603 } |
| 574 | 604 |
| 605 TEST_F(SdchManagerTest, GetDictionaryNotification) { |
| 606 GURL test_request_gurl(GURL("http://www.example.com/data")); |
| 607 GURL test_dictionary_gurl(GURL("http://www.example.com/dict")); |
| 608 MockSdchObserver observer; |
| 609 sdch_manager()->AddObserver(&observer); |
| 610 |
| 611 EXPECT_EQ(0, observer.get_dictionary_notifications()); |
| 612 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl); |
| 613 EXPECT_EQ(1, observer.get_dictionary_notifications()); |
| 614 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url()); |
| 615 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url()); |
| 616 |
| 617 sdch_manager()->RemoveObserver(&observer); |
| 618 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl); |
| 619 EXPECT_EQ(1, observer.get_dictionary_notifications()); |
| 620 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url()); |
| 621 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url()); |
| 622 } |
| 623 |
| 575 #else | 624 #else |
| 576 | 625 |
| 577 TEST(SdchManagerTest, SdchOffByDefault) { | 626 TEST(SdchManagerTest, SdchOffByDefault) { |
| 578 GURL google_url("http://www.google.com"); | 627 GURL google_url("http://www.google.com"); |
| 579 SdchManager* sdch_manager(new SdchManager); | 628 SdchManager* sdch_manager(new SdchManager); |
| 580 | 629 |
| 581 EXPECT_FALSE(sdch_manager->IsInSupportedDomain(google_url)); | 630 EXPECT_FALSE(sdch_manager->IsInSupportedDomain(google_url)); |
| 582 SdchManager::EnableSdchSupport(true); | 631 SdchManager::EnableSdchSupport(true); |
| 583 EXPECT_TRUE(sdch_manager->IsInSupportedDomain(google_url)); | 632 EXPECT_TRUE(sdch_manager->IsInSupportedDomain(google_url)); |
| 584 } | 633 } |
| 585 | 634 |
| 586 #endif // !defined(OS_IOS) | 635 #endif // !defined(OS_IOS) |
| 587 | 636 |
| 588 } // namespace net | 637 } // namespace net |
| OLD | NEW |