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(PrefService* prefs, | |
| 33 const char* disable_network_switch) | |
| 34 : ResourceRequestAllowedNotifier(prefs, disable_network_switch) {} | |
| 35 | |
| 36 ResourceRequestAllowedNotifier::State GetResourceRequestsAllowedState() | |
| 37 override { | |
| 38 return state_; | |
| 39 }; | |
|
Dan Beam
2016/08/19 02:14:32
nit: no need for these ; after methods
guptaag
2017/03/01 21:52:24
On 2016/08/19 02:14:32, Dan Beam wrote:
> nit: no
guptaag
2017/03/01 21:52:24
Done.
| |
| 40 | |
| 41 void SetState(ResourceRequestAllowedNotifier::State state) { | |
| 42 state_ = state; | |
| 43 }; | |
| 44 | |
| 45 void NotifyState(ResourceRequestAllowedNotifier::State state) { | |
| 46 SetState(state); | |
| 47 SetObserverRequestedForTesting(true); | |
| 48 MaybeNotifyObserver(); | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 ResourceRequestAllowedNotifier::State state_; | |
| 53 }; | |
| 54 | |
| 55 class TestWebResourceService : public WebResourceService { | |
| 56 public: | |
| 57 TestWebResourceService(PrefService* prefs, | |
| 58 const GURL& web_resource_server, | |
| 59 const std::string& application_locale, | |
| 60 const char* last_update_time_pref_name, | |
| 61 int start_fetch_delay_ms, | |
| 62 int cache_update_delay_ms, | |
| 63 net::URLRequestContextGetter* request_context, | |
| 64 const char* disable_network_switch, | |
| 65 const ParseJSONCallback& parse_json_callback) | |
| 66 : WebResourceService(prefs, | |
| 67 web_resource_server, | |
| 68 application_locale, | |
| 69 last_update_time_pref_name, | |
| 70 start_fetch_delay_ms, | |
| 71 cache_update_delay_ms, | |
| 72 request_context, | |
| 73 disable_network_switch, | |
| 74 parse_json_callback){}; | |
| 75 | |
| 76 void Unpack(const base::DictionaryValue& parsed_json) override{}; | |
| 77 }; | |
| 78 | |
| 79 class WebResourceServiceTest : public testing::Test { | |
| 80 public: | |
| 81 WebResourceServiceTest() {} | |
| 82 | |
| 83 void SetUp() override { | |
| 84 request_context_getter_ = new net::TestURLRequestContextGetter( | |
| 85 base::ThreadTaskRunnerHandle::Get()); | |
| 86 local_state_ = new TestingPrefServiceSimple(); | |
| 87 local_state_->registry()->RegisterStringPref(kCacheUpdatePath, "0"); | |
| 88 test_web_resource_service_ = new TestWebResourceService( | |
| 89 local_state_, GURL(kTestUrl), "", kCacheUpdatePath.c_str(), 100, 5000, | |
| 90 request_context_getter_.get(), nullptr, | |
| 91 base::Bind(web_resource::WebResourceServiceTest::Parse)); | |
| 92 error_message_ = ""; | |
| 93 TestResourceRequestAllowedNotifier* notifier = | |
| 94 new TestResourceRequestAllowedNotifier(local_state_, nullptr); | |
| 95 notifier->SetState(ResourceRequestAllowedNotifier::ALLOWED); | |
| 96 test_web_resource_service_->SetResourceRequestAllowedNotifier( | |
| 97 std::unique_ptr<ResourceRequestAllowedNotifier>(notifier)); | |
| 98 } | |
| 99 | |
| 100 TestResourceRequestAllowedNotifier* resource_notifier() { | |
| 101 return static_cast<TestResourceRequestAllowedNotifier*>( | |
| 102 test_web_resource_service_->resource_request_allowed_notifier_.get()); | |
| 103 } | |
| 104 | |
| 105 bool GetFetchScheduled() { | |
| 106 return test_web_resource_service_->GetFetchScheduled(); | |
| 107 } | |
| 108 | |
| 109 void CallScheduleFetch(int64_t delay_ms) { | |
| 110 return test_web_resource_service_->ScheduleFetch(delay_ms); | |
| 111 } | |
| 112 | |
| 113 static void Parse(const std::string& unsafe_json, | |
| 114 const WebResourceService::SuccessCallback& success_callback, | |
| 115 const WebResourceService::ErrorCallback& error_callback) { | |
| 116 std::unique_ptr<base::Value> value; | |
| 117 if (!error_message_.empty()) | |
| 118 error_callback.Run(error_message_); | |
| 119 else | |
| 120 success_callback.Run(std::move(value)); | |
| 121 } | |
| 122 | |
| 123 WebResourceService* web_resource_service() { | |
| 124 return test_web_resource_service_; | |
| 125 } | |
| 126 | |
| 127 void CallStartFetch() { test_web_resource_service_->StartFetch(); } | |
| 128 | |
| 129 private: | |
| 130 base::MessageLoop message_loop_; // needed for TestURLFetcherFactory | |
| 131 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 132 TestingPrefServiceSimple* local_state_ = nullptr; | |
| 133 TestWebResourceService* test_web_resource_service_ = nullptr; | |
| 134 }; | |
| 135 | |
| 136 TEST_F(WebResourceServiceTest, FetchScheduledAfterStartDelayTest) { | |
| 137 web_resource_service()->StartAfterDelay(); | |
| 138 EXPECT_TRUE(GetFetchScheduled()); | |
| 139 } | |
| 140 | |
| 141 TEST_F(WebResourceServiceTest, FetchScheduledOnScheduleFetchTest) { | |
| 142 web_resource_service()->StartAfterDelay(); | |
| 143 resource_notifier()->NotifyState(ResourceRequestAllowedNotifier::ALLOWED); | |
| 144 EXPECT_TRUE(GetFetchScheduled()); | |
| 145 } | |
| 146 | |
| 147 TEST_F(WebResourceServiceTest, FetchScheduledOnStartFetchTest) { | |
| 148 resource_notifier()->NotifyState( | |
| 149 ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN); | |
| 150 CallStartFetch(); | |
| 151 EXPECT_FALSE(GetFetchScheduled()); | |
| 152 resource_notifier()->NotifyState(ResourceRequestAllowedNotifier::ALLOWED); | |
| 153 EXPECT_TRUE(GetFetchScheduled()); | |
| 154 } | |
| 155 | |
| 156 } // namespace web_resource | |
| OLD | NEW |