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

Side by Side Diff: components/web_resource/web_resource_service_unittest.cc

Issue 2217683002: Allow embedder to use custom ResourceRequestAllowedNotifier (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Allow embedder to use custom ResourceRequestAllowedNotifier Created 3 years, 9 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
« no previous file with comments | « components/web_resource/web_resource_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
40
41 void SetState(ResourceRequestAllowedNotifier::State state) { state_ = state; }
42
43 void NotifyState(ResourceRequestAllowedNotifier::State state) {
44 SetState(state);
45 SetObserverRequestedForTesting(true);
46 MaybeNotifyObserver();
47 }
48
49 private:
50 ResourceRequestAllowedNotifier::State state_;
51 };
52
53 class TestWebResourceService : public WebResourceService {
54 public:
55 TestWebResourceService(PrefService* prefs,
56 const GURL& web_resource_server,
57 const std::string& application_locale,
58 const char* last_update_time_pref_name,
59 int start_fetch_delay_ms,
60 int cache_update_delay_ms,
61 net::URLRequestContextGetter* request_context,
62 const char* disable_network_switch,
63 const ParseJSONCallback& parse_json_callback)
64 : WebResourceService(prefs,
65 web_resource_server,
66 application_locale,
67 last_update_time_pref_name,
68 start_fetch_delay_ms,
69 cache_update_delay_ms,
70 request_context,
71 disable_network_switch,
72 parse_json_callback){};
73
74 void Unpack(const base::DictionaryValue& parsed_json) override{};
75 };
76
77 class WebResourceServiceTest : public testing::Test {
78 public:
79 WebResourceServiceTest() {}
80
81 void SetUp() override {
82 request_context_getter_ = new net::TestURLRequestContextGetter(
83 base::ThreadTaskRunnerHandle::Get());
84 local_state_.reset(new TestingPrefServiceSimple());
85 local_state_->registry()->RegisterStringPref(kCacheUpdatePath, "0");
86 test_web_resource_service_.reset(new TestWebResourceService(
87 local_state_.get(), GURL(kTestUrl), "", kCacheUpdatePath.c_str(), 100,
88 5000, request_context_getter_.get(), nullptr,
89 base::Bind(web_resource::WebResourceServiceTest::Parse)));
90 error_message_ = "";
91 TestResourceRequestAllowedNotifier* notifier =
92 new TestResourceRequestAllowedNotifier(local_state_.get(), nullptr);
93 notifier->SetState(ResourceRequestAllowedNotifier::ALLOWED);
94 test_web_resource_service_->SetResourceRequestAllowedNotifier(
95 std::unique_ptr<ResourceRequestAllowedNotifier>(notifier));
96 }
97
98 TestResourceRequestAllowedNotifier* resource_notifier() {
99 return static_cast<TestResourceRequestAllowedNotifier*>(
100 test_web_resource_service_->resource_request_allowed_notifier_.get());
101 }
102
103 bool GetFetchScheduled() {
104 return test_web_resource_service_->GetFetchScheduled();
105 }
106
107 void CallScheduleFetch(int64_t delay_ms) {
108 return test_web_resource_service_->ScheduleFetch(delay_ms);
109 }
110
111 static void Parse(const std::string& unsafe_json,
112 const WebResourceService::SuccessCallback& success_callback,
113 const WebResourceService::ErrorCallback& error_callback) {
114 std::unique_ptr<base::Value> value;
115 if (!error_message_.empty())
116 error_callback.Run(error_message_);
117 else
118 success_callback.Run(std::move(value));
119 }
120
121 WebResourceService* web_resource_service() {
122 return test_web_resource_service_.get();
123 }
124
125 void CallStartFetch() { test_web_resource_service_->StartFetch(); }
126
127 private:
128 base::MessageLoop message_loop_; // needed for TestURLFetcherFactory
129 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
130 std::unique_ptr<TestingPrefServiceSimple> local_state_;
131 std::unique_ptr<TestWebResourceService> test_web_resource_service_;
132 };
133
134 TEST_F(WebResourceServiceTest, FetchScheduledAfterStartDelayTest) {
135 web_resource_service()->StartAfterDelay();
136 EXPECT_TRUE(GetFetchScheduled());
137 }
138
139 TEST_F(WebResourceServiceTest, FetchScheduledOnScheduleFetchTest) {
140 web_resource_service()->StartAfterDelay();
141 resource_notifier()->NotifyState(ResourceRequestAllowedNotifier::ALLOWED);
142 EXPECT_TRUE(GetFetchScheduled());
143 }
144
145 TEST_F(WebResourceServiceTest, FetchScheduledOnStartFetchTest) {
146 resource_notifier()->NotifyState(
147 ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN);
148 CallStartFetch();
149 EXPECT_FALSE(GetFetchScheduled());
150 resource_notifier()->NotifyState(ResourceRequestAllowedNotifier::ALLOWED);
151 EXPECT_TRUE(GetFetchScheduled());
152 }
153
154 } // namespace web_resource
OLDNEW
« no previous file with comments | « components/web_resource/web_resource_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698