Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <memory> | |
| 6 #include <utility> | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/values.h" | |
| 11 #include "components/prefs/pref_registry_simple.h" | |
| 12 #include "components/prefs/testing_pref_service.h" | |
| 13 #include "components/web_resource/resource_request_allowed_notifier.h" | |
| 14 #include "components/web_resource/web_resource_service.h" | |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | |
| 16 #include "net/url_request/url_request_context_getter.h" | |
| 17 #include "net/url_request/url_request_status.h" | |
| 18 #include "net/url_request/url_request_test_util.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace { | |
| 22 const std::string kTestUrl = "http://www.test.com"; | |
| 23 const std::string kCacheUpdatePath = "cache_update_path"; | |
| 24 std::string error_message_; | |
| 25 } // namespace | |
| 26 | |
| 27 namespace web_resource { | |
| 28 | |
| 29 class TestResourceRequestAllowedNotifier | |
| 30 : public ResourceRequestAllowedNotifier { | |
| 31 public: | |
| 32 TestResourceRequestAllowedNotifier( | |
| 33 PrefService* prefs, | |
| 34 const char* disable_network_switch) | |
| 35 : ResourceRequestAllowedNotifier( | |
| 36 prefs, | |
| 37 disable_network_switch) { | |
| 38 }; | |
|
Dan Beam
2016/08/08 18:15:34
nit: please omit these optional semi-colons (;)
| |
| 39 | |
| 40 ResourceRequestAllowedNotifier::State | |
| 41 GetResourceRequestsAllowedState() override { | |
| 42 return state_; | |
| 43 }; | |
| 44 | |
| 45 void SetState(ResourceRequestAllowedNotifier::State state) { | |
| 46 state_ = state; | |
| 47 }; | |
| 48 | |
| 49 void NotifyState(ResourceRequestAllowedNotifier::State state) { | |
| 50 state_ = state; | |
|
Dan Beam
2016/08/08 18:15:33
nit: call SetState()?
| |
| 51 SetObserverRequestedForTesting(true); | |
| 52 MaybeNotifyObserver(); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 ResourceRequestAllowedNotifier::State state_; | |
| 57 }; | |
| 58 | |
| 59 class TestWebResourceService : public WebResourceService { | |
| 60 public: | |
| 61 TestWebResourceService( | |
| 62 PrefService* prefs, | |
| 63 const GURL& web_resource_server, | |
| 64 const std::string& application_locale, | |
| 65 const char* last_update_time_pref_name, | |
| 66 int start_fetch_delay_ms, | |
| 67 int cache_update_delay_ms, | |
| 68 net::URLRequestContextGetter* request_context, | |
| 69 const char* disable_network_switch, | |
| 70 const ParseJSONCallback& parse_json_callback) | |
| 71 : WebResourceService( | |
| 72 prefs, | |
| 73 web_resource_server, | |
| 74 application_locale, | |
| 75 last_update_time_pref_name, | |
| 76 start_fetch_delay_ms, | |
| 77 cache_update_delay_ms, | |
| 78 request_context, | |
| 79 disable_network_switch, | |
| 80 parse_json_callback) { | |
| 81 }; | |
| 82 | |
| 83 void Unpack(const base::DictionaryValue& parsed_json) override { | |
| 84 }; | |
| 85 | |
| 86 }; | |
| 87 | |
| 88 class WebResourceServiceTest : public testing::Test { | |
| 89 public: | |
| 90 WebResourceServiceTest() { | |
| 91 } | |
| 92 | |
| 93 void SetUp() override { | |
| 94 request_context_getter_ = new net::TestURLRequestContextGetter( | |
| 95 base::ThreadTaskRunnerHandle::Get()); | |
| 96 local_state_ = new TestingPrefServiceSimple(); | |
| 97 local_state_->registry()-> | |
| 98 RegisterStringPref( | |
| 99 kCacheUpdatePath, | |
| 100 "0"); | |
|
Dan Beam
2016/08/08 18:15:33
can you run `git cl format` on this changelist? i
| |
| 101 test_web_resource_service_ = new TestWebResourceService( | |
| 102 local_state_, | |
| 103 GURL(kTestUrl), | |
| 104 "", | |
| 105 kCacheUpdatePath.c_str(), | |
| 106 100, | |
| 107 5000, | |
| 108 request_context_getter_.get(), | |
| 109 NULL, | |
| 110 base::Bind(web_resource::WebResourceServiceTest::Parse)); | |
| 111 error_message_ = ""; | |
| 112 TestResourceRequestAllowedNotifier* notifier = | |
| 113 new TestResourceRequestAllowedNotifier(local_state_, NULL); | |
|
Dan Beam
2016/08/08 18:15:34
please use nullptr (instead of NULL) in new code
Dan Beam
2016/08/08 18:15:34
indent off
| |
| 114 notifier->SetState(ResourceRequestAllowedNotifier::ALLOWED); | |
| 115 test_web_resource_service_->SetResourceRequestAllowedNotifier( | |
| 116 std::unique_ptr<ResourceRequestAllowedNotifier>(notifier)); | |
| 117 } | |
| 118 | |
| 119 TestResourceRequestAllowedNotifier* resource_notifier() { | |
| 120 return static_cast<TestResourceRequestAllowedNotifier*> | |
| 121 (test_web_resource_service_->resource_request_allowed_notifier_.get()); | |
| 122 } | |
| 123 | |
| 124 bool GetFetchScheduled() { | |
| 125 return test_web_resource_service_->fetch_scheduled_; | |
|
Dan Beam
2016/08/08 18:15:34
can we just make a const accessor for this instead
| |
| 126 } | |
| 127 | |
| 128 void CallScheduleFetch(int64_t delay_ms) { | |
| 129 return test_web_resource_service_->ScheduleFetch(delay_ms); | |
| 130 } | |
| 131 | |
| 132 static void Parse(const std::string& unsafe_json, | |
| 133 const WebResourceService::SuccessCallback& success_callback, | |
| 134 const WebResourceService::ErrorCallback& error_callback) { | |
|
Dan Beam
2016/08/08 18:15:33
indent off
| |
| 135 std::unique_ptr<base::Value> value; | |
| 136 if (!error_message_.empty()) { | |
| 137 error_callback.Run(error_message_); | |
| 138 } else { | |
| 139 success_callback.Run(std::move(value)); | |
| 140 } | |
|
Dan Beam
2016/08/08 18:15:34
no curlies
| |
| 141 } | |
| 142 | |
| 143 WebResourceService* web_resource_service() { | |
| 144 return test_web_resource_service_; | |
| 145 } | |
| 146 | |
| 147 void CallStartFetch() { | |
| 148 test_web_resource_service_->StartFetch(); | |
| 149 } | |
| 150 | |
| 151 private: | |
| 152 base::MessageLoop message_loop_; // needed for TestURLFetcherFactory | |
| 153 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 154 TestingPrefServiceSimple* local_state_; | |
|
Dan Beam
2016/08/08 18:15:34
can you initialize |local_state_| and |test_web_re
| |
| 155 TestWebResourceService* test_web_resource_service_; | |
| 156 }; | |
| 157 | |
| 158 TEST_F(WebResourceServiceTest, FetchScheduledAfterStartDelayTest) { | |
| 159 web_resource_service()->StartAfterDelay(); | |
| 160 EXPECT_TRUE(GetFetchScheduled()); | |
| 161 } | |
| 162 | |
| 163 TEST_F(WebResourceServiceTest, FetchScheduledOnScheduleFetchTest) { | |
| 164 web_resource_service()->StartAfterDelay(); | |
| 165 resource_notifier()->NotifyState( | |
| 166 ResourceRequestAllowedNotifier::ALLOWED); | |
| 167 EXPECT_TRUE(GetFetchScheduled()); | |
| 168 } | |
| 169 | |
| 170 TEST_F(WebResourceServiceTest, FetchScheduledOnStartFetchTest) { | |
| 171 resource_notifier()->NotifyState( | |
| 172 ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN); | |
| 173 CallStartFetch(); | |
| 174 EXPECT_FALSE(GetFetchScheduled()); | |
| 175 resource_notifier()->NotifyState( | |
| 176 ResourceRequestAllowedNotifier::ALLOWED); | |
| 177 EXPECT_TRUE(GetFetchScheduled()); | |
| 178 } | |
|
Dan Beam
2016/08/08 18:15:33
nit: \n here
| |
| 179 } // namespace web_resource | |
| OLD | NEW |