| 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 void SetPreference(NetworkPredictionOptions value) { | 50 void SetPreference(NetworkPredictionOptions value) { |
| 51 profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value); | 51 profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void SetCommandLineValue(const std::string& value) { | 54 void SetCommandLineValue(const std::string& value) { |
| 55 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 55 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 56 switches::kSpeculativeResourcePrefetching, value); | 56 switches::kSpeculativeResourcePrefetching, value); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void TestIsPrefetchEnabled(const ResourcePrefetchPredictorConfig& config) { | 59 void TestIsPrefetchEnabledForOrigin( |
| 60 const ResourcePrefetchPredictorConfig& config, |
| 61 PrefetchOrigin origin) { |
| 60 EXPECT_TRUE(config.IsLearningEnabled()); | 62 EXPECT_TRUE(config.IsLearningEnabled()); |
| 61 EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get())); | 63 EXPECT_TRUE(config.IsPrefetchingEnabledForOrigin(profile_.get(), origin)); |
| 62 EXPECT_TRUE(config.IsURLLearningEnabled()); | |
| 63 EXPECT_TRUE(config.IsHostLearningEnabled()); | |
| 64 EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get())); | |
| 65 EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get())); | |
| 66 } | 64 } |
| 67 | 65 |
| 68 void TestIsPrefetchLearning(const ResourcePrefetchPredictorConfig& config) { | 66 void TestIsPrefetchLearning(const ResourcePrefetchPredictorConfig& config) { |
| 69 EXPECT_TRUE(config.IsLearningEnabled()); | 67 EXPECT_TRUE(config.IsLearningEnabled()); |
| 70 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get())); | 68 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin( |
| 71 EXPECT_TRUE(config.IsURLLearningEnabled()); | 69 profile_.get(), PrefetchOrigin::EXTERNAL)); |
| 72 EXPECT_TRUE(config.IsHostLearningEnabled()); | 70 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin( |
| 73 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get())); | 71 profile_.get(), PrefetchOrigin::NAVIGATION)); |
| 74 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get())); | |
| 75 } | 72 } |
| 76 | 73 |
| 77 void TestIsDefaultExtraConfig(const ResourcePrefetchPredictorConfig& config) { | 74 void TestIsDefaultExtraConfig(const ResourcePrefetchPredictorConfig& config) { |
| 78 EXPECT_FALSE(config.IsLowConfidenceForTest()); | 75 EXPECT_FALSE(config.IsLowConfidenceForTest()); |
| 79 EXPECT_FALSE(config.IsHighConfidenceForTest()); | 76 EXPECT_FALSE(config.IsHighConfidenceForTest()); |
| 80 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); | 77 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| 81 EXPECT_FALSE(config.IsSmallDBEnabledForTest()); | 78 EXPECT_FALSE(config.IsSmallDBEnabledForTest()); |
| 82 } | 79 } |
| 83 | 80 |
| 84 protected: | 81 protected: |
| 85 base::MessageLoop loop_; | 82 base::MessageLoop loop_; |
| 86 content::TestBrowserThread ui_thread_; | 83 content::TestBrowserThread ui_thread_; |
| 87 std::unique_ptr<TestingProfile> profile_; | 84 std::unique_ptr<TestingProfile> profile_; |
| 88 }; | 85 }; |
| 89 | 86 |
| 90 ResourcePrefetchCommonTest::ResourcePrefetchCommonTest() | 87 ResourcePrefetchCommonTest::ResourcePrefetchCommonTest() |
| 91 : loop_(base::MessageLoop::TYPE_DEFAULT), | 88 : loop_(base::MessageLoop::TYPE_DEFAULT), |
| 92 ui_thread_(content::BrowserThread::UI, &loop_), | 89 ui_thread_(content::BrowserThread::UI, &loop_), |
| 93 profile_(new TestingProfile()) { } | 90 profile_(new TestingProfile()) { } |
| 94 | 91 |
| 95 TEST_F(ResourcePrefetchCommonTest, IsDisabledByDefault) { | 92 TEST_F(ResourcePrefetchCommonTest, IsDisabledByDefault) { |
| 96 ResourcePrefetchPredictorConfig config; | 93 ResourcePrefetchPredictorConfig config; |
| 97 EXPECT_FALSE( | 94 EXPECT_FALSE( |
| 98 IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); | 95 IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| 99 | 96 |
| 100 EXPECT_FALSE(config.IsLearningEnabled()); | 97 EXPECT_FALSE(config.IsLearningEnabled()); |
| 101 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get())); | 98 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin(profile_.get(), |
| 102 EXPECT_FALSE(config.IsURLLearningEnabled()); | 99 PrefetchOrigin::EXTERNAL)); |
| 103 EXPECT_FALSE(config.IsHostLearningEnabled()); | 100 EXPECT_FALSE(config.IsLearningEnabled()); |
| 104 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get())); | |
| 105 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get())); | |
| 106 | 101 |
| 107 TestIsDefaultExtraConfig(config); | 102 TestIsDefaultExtraConfig(config); |
| 108 } | 103 } |
| 109 | 104 |
| 110 TEST_F(ResourcePrefetchCommonTest, EnableLearning) { | 105 TEST_F(ResourcePrefetchCommonTest, EnableLearning) { |
| 111 SetCommandLineValue("learning"); | 106 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingLearning); |
| 112 ResourcePrefetchPredictorConfig config; | 107 ResourcePrefetchPredictorConfig config; |
| 113 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); | 108 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| 114 TestIsPrefetchLearning(config); | 109 TestIsPrefetchLearning(config); |
| 115 TestIsDefaultExtraConfig(config); | 110 TestIsDefaultExtraConfig(config); |
| 116 } | 111 } |
| 117 | 112 |
| 118 TEST_F(ResourcePrefetchCommonTest, EnablePrefetch) { | 113 TEST_F(ResourcePrefetchCommonTest, EnablePrefetch) { |
| 119 SetCommandLineValue("enabled"); | 114 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingEnabled); |
| 120 ResourcePrefetchPredictorConfig config; | 115 ResourcePrefetchPredictorConfig config; |
| 121 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); | 116 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| 122 TestIsPrefetchEnabled(config); | 117 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL); |
| 118 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::NAVIGATION); |
| 119 TestIsDefaultExtraConfig(config); |
| 120 } |
| 121 |
| 122 TEST_F(ResourcePrefetchCommonTest, EnablePrefetchExternalOnly) { |
| 123 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingEnabledExternal); |
| 124 ResourcePrefetchPredictorConfig config; |
| 125 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| 126 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL); |
| 127 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin( |
| 128 profile_.get(), PrefetchOrigin::NAVIGATION)); |
| 123 TestIsDefaultExtraConfig(config); | 129 TestIsDefaultExtraConfig(config); |
| 124 } | 130 } |
| 125 | 131 |
| 126 // Verifies whether prefetching is disabled according to the network type. But | 132 // Verifies whether prefetching is disabled according to the network type. But |
| 127 // learning should not be disabled by network. | 133 // learning should not be disabled by network. |
| 128 TEST_F(ResourcePrefetchCommonTest, RespectsNetworkSettings) { | 134 TEST_F(ResourcePrefetchCommonTest, RespectsNetworkSettings) { |
| 129 SetCommandLineValue("enabled"); | 135 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingEnabled); |
| 130 ResourcePrefetchPredictorConfig config; | 136 ResourcePrefetchPredictorConfig config; |
| 131 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); | 137 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| 132 TestIsPrefetchEnabled(config); | 138 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL); |
| 133 TestIsDefaultExtraConfig(config); | 139 TestIsDefaultExtraConfig(config); |
| 134 | 140 |
| 135 // Set preference to WIFI_ONLY: prefetch when not on cellular. | 141 // Set preference to WIFI_ONLY: prefetch when not on cellular. |
| 136 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY); | 142 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY); |
| 137 { | 143 { |
| 138 std::unique_ptr<NetworkChangeNotifier> mock( | 144 std::unique_ptr<NetworkChangeNotifier> mock( |
| 139 new MockNetworkChangeNotifierWIFI); | 145 new MockNetworkChangeNotifierWIFI); |
| 140 TestIsPrefetchEnabled(config); | 146 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL); |
| 141 } | 147 } |
| 142 { | 148 { |
| 143 std::unique_ptr<NetworkChangeNotifier> mock( | 149 std::unique_ptr<NetworkChangeNotifier> mock( |
| 144 new MockNetworkChangeNotifier4G); | 150 new MockNetworkChangeNotifier4G); |
| 145 TestIsPrefetchLearning(config); | 151 TestIsPrefetchLearning(config); |
| 146 } | 152 } |
| 147 | 153 |
| 148 // Set preference to NEVER: never prefetch. | 154 // Set preference to NEVER: never prefetch. |
| 149 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER); | 155 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER); |
| 150 { | 156 { |
| 151 std::unique_ptr<NetworkChangeNotifier> mock( | 157 std::unique_ptr<NetworkChangeNotifier> mock( |
| 152 new MockNetworkChangeNotifierWIFI); | 158 new MockNetworkChangeNotifierWIFI); |
| 153 TestIsPrefetchLearning(config); | 159 TestIsPrefetchLearning(config); |
| 154 } | 160 } |
| 155 { | 161 { |
| 156 std::unique_ptr<NetworkChangeNotifier> mock( | 162 std::unique_ptr<NetworkChangeNotifier> mock( |
| 157 new MockNetworkChangeNotifier4G); | 163 new MockNetworkChangeNotifier4G); |
| 158 TestIsPrefetchLearning(config); | 164 TestIsPrefetchLearning(config); |
| 159 } | 165 } |
| 160 } | 166 } |
| 161 | 167 |
| 162 } // namespace predictors | 168 } // namespace predictors |
| OLD | NEW |