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

Side by Side Diff: net/base/sdch_manager_unittest.cc

Issue 664263002: Restructure SDCH layering to allow more separation (observer/1->[0,n] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync'd to p300953. 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 unified diff | Download patch
OLDNEW
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 // Provide sample data and compression results with a sample VCDIFF dictionary. 19 // Provide sample data and compression results with a sample VCDIFF dictionary.
18 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. 20 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary.
19 static const char kTestVcdiffDictionary[] = "DictionaryFor" 21 static const char kTestVcdiffDictionary[] = "DictionaryFor"
20 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; 22 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n";
21 23
22 //------------------------------------------------------------------------------ 24 //------------------------------------------------------------------------------
23 25
24 class SdchManagerTest : public testing::Test { 26 class SdchManagerTest : public testing::Test,
27 public SdchObserver {
25 protected: 28 protected:
26 SdchManagerTest() 29 SdchManagerTest()
27 : sdch_manager_(new SdchManager), 30 : sdch_manager_(new SdchManager),
28 default_support_(false), 31 default_support_(false),
29 default_https_support_(false) { 32 default_https_support_(false),
33 get_dictionary_notifications_(0) {
30 default_support_ = sdch_manager_->sdch_enabled(); 34 default_support_ = sdch_manager_->sdch_enabled();
31 default_https_support_ = sdch_manager_->secure_scheme_supported(); 35 default_https_support_ = sdch_manager_->secure_scheme_supported();
36 sdch_manager_->AddObserver(this);
37 }
38
39 virtual ~SdchManagerTest() {
40 sdch_manager_->RemoveObserver(this);
32 } 41 }
33 42
34 SdchManager* sdch_manager() { return sdch_manager_.get(); } 43 SdchManager* sdch_manager() { return sdch_manager_.get(); }
35 44
36 // Reset globals back to default state. 45 // Reset globals back to default state.
37 virtual void TearDown() { 46 virtual void TearDown() {
38 SdchManager::EnableSdchSupport(default_support_); 47 SdchManager::EnableSdchSupport(default_support_);
39 SdchManager::EnableSecureSchemeSupport(default_https_support_); 48 SdchManager::EnableSecureSchemeSupport(default_https_support_);
40 } 49 }
41 50
42 // Attempt to add a dictionary to the manager and probe for success or 51 // Attempt to add a dictionary to the manager and probe for success or
43 // failure. 52 // failure.
44 bool AddSdchDictionary(const std::string& dictionary_text, 53 bool AddSdchDictionary(const std::string& dictionary_text,
45 const GURL& gurl) { 54 const GURL& gurl) {
46 std::string list; 55 std::string list;
47 sdch_manager_->GetAvailDictionaryList(gurl, &list); 56 sdch_manager_->GetAvailDictionaryList(gurl, &list);
48 sdch_manager_->AddSdchDictionary(dictionary_text, gurl); 57 sdch_manager_->AddSdchDictionary(dictionary_text, gurl);
49 std::string list2; 58 std::string list2;
50 sdch_manager_->GetAvailDictionaryList(gurl, &list2); 59 sdch_manager_->GetAvailDictionaryList(gurl, &list2);
51 60
52 // The list of hashes should change iff the addition succeeds. 61 // The list of hashes should change iff the addition succeeds.
53 return (list != list2); 62 return (list != list2);
54 } 63 }
55 64
65 const GURL& last_dictionary_request_url() {
66 return last_dictionary_request_url_;
67 }
68 const GURL& last_dictionary_url() { return last_dictionary_url_; }
69 int get_dictionary_notifications() { return get_dictionary_notifications_; }
70
71 // SdchObserver implementation
72 void OnGetDictionary(SdchManager* manager,
73 const GURL& request_url,
74 const GURL& dictionary_url) override {
75 ++get_dictionary_notifications_;
76 last_dictionary_request_url_ = request_url;
77 last_dictionary_url_ = dictionary_url;
78 }
79 void OnClearDictionaries(SdchManager* manager) override {}
80
56 private: 81 private:
57 scoped_ptr<SdchManager> sdch_manager_; 82 scoped_ptr<SdchManager> sdch_manager_;
58 bool default_support_; 83 bool default_support_;
59 bool default_https_support_; 84 bool default_https_support_;
85 int get_dictionary_notifications_;
86 GURL last_dictionary_request_url_;
87 GURL last_dictionary_url_;
60 }; 88 };
61 89
62 //------------------------------------------------------------------------------ 90 //------------------------------------------------------------------------------
63 static std::string NewSdchDictionary(const std::string& domain) { 91 static std::string NewSdchDictionary(const std::string& domain) {
64 std::string dictionary; 92 std::string dictionary;
65 if (!domain.empty()) { 93 if (!domain.empty()) {
66 dictionary.append("Domain: "); 94 dictionary.append("Domain: ");
67 dictionary.append(domain); 95 dictionary.append(domain);
68 dictionary.append("\n"); 96 dictionary.append("\n");
69 } 97 }
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 590
563 dictionary = NULL; 591 dictionary = NULL;
564 sdch_manager()->GetVcdiffDictionary( 592 sdch_manager()->GetVcdiffDictionary(
565 server_hash, 593 server_hash,
566 GURL("http://" + dictionary_domain + "/random_url"), 594 GURL("http://" + dictionary_domain + "/random_url"),
567 &dictionary); 595 &dictionary);
568 EXPECT_FALSE(dictionary.get()); 596 EXPECT_FALSE(dictionary.get());
569 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url)); 597 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url));
570 } 598 }
571 599
600 TEST_F(SdchManagerTest, GetDictionaryNotification) {
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
572 } // namespace net 611 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698