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 "chrome/browser/previews/previews_infobar_delegate.h" | |
6 | |
7 #include "base/test/histogram_tester.h" | |
8 #include "chrome/browser/android/android_theme_resources.h" | |
9 #include "chrome/browser/infobars/infobar_service.h" | |
10 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | |
11 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact ory.h" | |
12 #include "chrome/browser/previews/previews_infobar_tab_helper.h" | |
13 #include "chrome/grit/generated_resources.h" | |
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test _utils.h" | |
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_ names.h" | |
17 #include "components/infobars/core/confirm_infobar_delegate.h" | |
18 #include "components/infobars/core/infobar.h" | |
19 #include "components/infobars/core/infobar_delegate.h" | |
20 #include "components/prefs/pref_registry_simple.h" | |
21 #include "components/proxy_config/proxy_config_pref_names.h" | |
22 #include "content/public/browser/web_contents.h" | |
23 #include "content/public/test/web_contents_tester.h" | |
24 #include "ui/base/l10n/l10n_util.h" | |
25 #include "ui/base/window_open_disposition.h" | |
26 | |
27 namespace { | |
28 | |
29 const char kTestUrl[] = "http://www.test.com/"; | |
30 | |
31 // Key of the UMA Previews.InfoBarAction.LoFi histogram. | |
32 const char kUMAPreviewsInfoBarActionLoFi[] = "Previews.InfoBarAction.LoFi"; | |
33 | |
34 // Key of the UMA Previews.InfoBarAction.Offline histogram. | |
35 const char kUMAPreviewsInfoBarActionOffline[] = | |
36 "Previews.InfoBarAction.Offline"; | |
37 | |
38 // Key of the UMA Previews.InfoBarAction.LitePage histogram. | |
39 const char kUMAPreviewsInfoBarActionLitePage[] = | |
40 "Previews.InfoBarAction.LitePage"; | |
41 | |
42 } // namespace | |
43 | |
44 class PreviewsInfoBarDelegateUnitTest : public ChromeRenderViewHostTestHarness { | |
45 protected: | |
46 void SetUp() override { | |
47 ChromeRenderViewHostTestHarness::SetUp(); | |
48 InfoBarService::CreateForWebContents(web_contents()); | |
49 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); | |
50 | |
51 drp_test_context_ = | |
52 data_reduction_proxy::DataReductionProxyTestContext::Builder() | |
53 .WithMockConfig() | |
54 .SkipSettingsInitialization() | |
55 .Build(); | |
56 | |
57 auto* data_reduction_proxy_settings = | |
58 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | |
59 web_contents()->GetBrowserContext()); | |
60 | |
61 PrefRegistrySimple* registry = | |
62 drp_test_context_->pref_service()->registry(); | |
63 registry->RegisterDictionaryPref(proxy_config::prefs::kProxy); | |
64 data_reduction_proxy_settings | |
65 ->set_data_reduction_proxy_enabled_pref_name_for_test( | |
66 drp_test_context_->GetDataReductionProxyEnabledPrefName()); | |
67 data_reduction_proxy_settings->InitDataReductionProxySettings( | |
68 drp_test_context_->io_data(), drp_test_context_->pref_service(), | |
69 drp_test_context_->request_context_getter(), | |
70 base::WrapUnique(new data_reduction_proxy::DataStore()), | |
71 base::ThreadTaskRunnerHandle::Get(), | |
72 base::ThreadTaskRunnerHandle::Get()); | |
73 } | |
74 | |
75 void TearDown() override { | |
76 drp_test_context_->DestroySettings(); | |
77 ChromeRenderViewHostTestHarness::TearDown(); | |
78 } | |
79 | |
80 ConfirmInfoBarDelegate* CreateInfoBar( | |
81 PreviewsInfoBarDelegate::PreviewsInfoBarType type) { | |
82 PreviewsInfoBarDelegate::Create(web_contents(), type); | |
83 | |
84 InfoBarService* infobar_service = | |
85 InfoBarService::FromWebContents(web_contents()); | |
86 EXPECT_EQ(1U, infobar_service->infobar_count()); | |
87 | |
88 return infobar_service->infobar_at(0) | |
89 ->delegate() | |
90 ->AsConfirmInfoBarDelegate(); | |
91 } | |
92 | |
93 InfoBarService* infobar_service() { | |
94 return InfoBarService::FromWebContents(web_contents()); | |
95 } | |
96 | |
97 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> | |
bengr
2016/09/13 23:18:18
#include <memory>
megjablon
2016/09/13 23:27:12
Done.
| |
98 drp_test_context_; | |
99 }; | |
100 | |
101 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestNavigationDismissal) { | |
102 base::HistogramTester tester; | |
103 | |
104 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); | |
105 | |
106 // Try showing a second infobar. Another should not be shown since the page | |
107 // has not navigated. | |
108 PreviewsInfoBarDelegate::Create(web_contents(), | |
109 PreviewsInfoBarDelegate::LOFI); | |
110 EXPECT_EQ(1U, infobar_service()->infobar_count()); | |
111 | |
112 // Navigate and make sure the infobar is dismissed. | |
113 content::WebContentsTester::For(web_contents())-> | |
114 NavigateAndCommit(GURL(kTestUrl)); | |
115 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
116 | |
117 tester.ExpectBucketCount( | |
118 kUMAPreviewsInfoBarActionLoFi, | |
119 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_NAVIGATION, 1); | |
120 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | |
121 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | |
122 } | |
123 | |
124 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { | |
125 base::HistogramTester tester; | |
126 | |
127 ConfirmInfoBarDelegate* infobar = | |
128 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); | |
129 | |
130 // Simulate dismissing the infobar. | |
131 infobar->InfoBarDismissed(); | |
132 infobar_service()->infobar_at(0)->RemoveSelf(); | |
133 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
134 | |
135 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, | |
136 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_USER, | |
137 1); | |
138 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | |
139 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | |
140 } | |
141 | |
142 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLink) { | |
143 base::HistogramTester tester; | |
144 | |
145 ConfirmInfoBarDelegate* infobar = | |
146 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); | |
147 | |
148 // Simulate clicking the infobar link. | |
149 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) | |
150 infobar_service()->infobar_at(0)->RemoveSelf(); | |
151 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
152 | |
153 tester.ExpectBucketCount( | |
154 kUMAPreviewsInfoBarActionLoFi, | |
155 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); | |
156 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( | |
157 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | |
158 } | |
159 | |
160 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { | |
161 ConfirmInfoBarDelegate* infobar = | |
162 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); | |
163 | |
164 // Simulate dismissing the infobar. | |
165 infobar->InfoBarDismissed(); | |
166 infobar_service()->infobar_at(0)->RemoveSelf(); | |
167 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
168 | |
169 PreviewsInfoBarDelegate::Create(web_contents(), | |
170 PreviewsInfoBarDelegate::LOFI); | |
171 | |
172 // Infobar should not be shown again since a navigation hasn't happened. | |
173 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
174 | |
175 // Navigate and show infobar again. | |
176 content::WebContentsTester::For(web_contents())-> | |
177 NavigateAndCommit(GURL(kTestUrl)); | |
178 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); | |
179 } | |
180 | |
181 TEST_F(PreviewsInfoBarDelegateUnitTest, LoFiInfobarTest) { | |
182 base::HistogramTester tester; | |
183 | |
184 ConfirmInfoBarDelegate* infobar = | |
185 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); | |
186 | |
187 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLoFi, | |
188 PreviewsInfoBarDelegate::INFOBAR_SHOWN, 1); | |
189 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( | |
190 data_reduction_proxy::prefs::kLoFiUIShownPerSession)); | |
191 | |
192 ASSERT_TRUE(infobar); | |
193 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE), | |
194 infobar->GetMessageText()); | |
195 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), | |
196 infobar->GetLinkText()); | |
197 #if defined(OS_ANDROID) | |
198 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); | |
199 #else | |
200 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); | |
201 #endif | |
202 } | |
203 | |
204 TEST_F(PreviewsInfoBarDelegateUnitTest, PreviewInfobarTest) { | |
205 base::HistogramTester tester; | |
206 | |
207 ConfirmInfoBarDelegate* infobar = | |
208 CreateInfoBar(PreviewsInfoBarDelegate::LITE_PAGE); | |
209 | |
210 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLitePage, | |
211 PreviewsInfoBarDelegate::INFOBAR_SHOWN, 1); | |
212 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( | |
213 data_reduction_proxy::prefs::kLoFiUIShownPerSession)); | |
214 | |
215 // Check the strings. | |
216 ASSERT_TRUE(infobar); | |
217 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE), | |
218 infobar->GetMessageText()); | |
219 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), | |
220 infobar->GetLinkText()); | |
221 #if defined(OS_ANDROID) | |
222 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); | |
223 #else | |
224 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); | |
225 #endif | |
226 } | |
227 | |
228 TEST_F(PreviewsInfoBarDelegateUnitTest, OfflineInfobarTest) { | |
229 base::HistogramTester tester; | |
230 | |
231 ConfirmInfoBarDelegate* infobar = | |
232 CreateInfoBar(PreviewsInfoBarDelegate::OFFLINE); | |
233 | |
234 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionOffline, | |
235 PreviewsInfoBarDelegate::INFOBAR_SHOWN, 1); | |
236 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | |
237 data_reduction_proxy::prefs::kLoFiUIShownPerSession)); | |
238 | |
239 // Check the strings. | |
240 ASSERT_TRUE(infobar); | |
241 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE), | |
242 infobar->GetMessageText()); | |
243 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), | |
244 infobar->GetLinkText()); | |
245 #if defined(OS_ANDROID) | |
246 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); | |
247 #else | |
248 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); | |
249 #endif | |
250 } | |
OLD | NEW |