1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/logging.h"
#include "chrome/browser/extensions/apps_promo.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/ui/webui/ntp/shown_sections_handler.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/testing_browser_process_test.h"
#include "chrome/test/testing_pref_service.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kPromoId[] = "23123123";
const char kPromoHeader[] = "Get great apps!";
const char kPromoButton[] = "Click for apps!";
const char kPromoLink[] = "http://apps.com";
const char kPromoLogo[] = "chrome://theme/IDR_WEBSTORE_ICON";
const char kPromoExpire[] = "No thanks.";
const int kPromoUserGroup =
AppsPromo::USERS_NEW | AppsPromo::USERS_EXISTING;
void ExpectAppsSectionMaximized(PrefService* prefs, bool maximized) {
EXPECT_EQ(maximized,
(ShownSectionsHandler::GetShownSections(prefs) & APPS) != 0);
EXPECT_EQ(!maximized,
(ShownSectionsHandler::GetShownSections(prefs) & THUMB) != 0);
}
void ExpectAppsPromoHidden(PrefService* prefs) {
// Hiding the promo places the apps section in menu mode and maximizes the
// most visited section.
EXPECT_TRUE((ShownSectionsHandler::GetShownSections(prefs) &
(MENU_APPS | THUMB)) != 0);
}
} // namespace
class ExtensionAppsPromo : public TestingBrowserProcessTest {
public:
TestingPrefService* prefs() { return &prefs_; }
AppsPromo* apps_promo() { return &apps_promo_; }
protected:
explicit ExtensionAppsPromo();
// testing::Test
virtual void SetUp();
private:
TestingPrefService prefs_;
ScopedTestingLocalState local_state_;
AppsPromo apps_promo_;
};
ExtensionAppsPromo::ExtensionAppsPromo()
: local_state_(testing_browser_process_.get()),
apps_promo_(&prefs_) {
}
void ExtensionAppsPromo::SetUp() {
browser::RegisterUserPrefs(&prefs_);
}
// TODO(dpolukhin): On Chrome OS all apps are installed via external extensions,
// and the web store promo is never shown.
#if !defined(OS_CHROMEOS)
TEST_F(ExtensionAppsPromo, HappyPath) {
const ExtensionIdSet& default_app_ids = apps_promo()->old_default_apps();
EXPECT_GT(default_app_ids.size(), 0u);
// The promo counter should default to the max, since we only use the counter
// if they were installed from older versions of Chrome.
EXPECT_EQ(AppsPromo::kDefaultAppsCounterMax + 1,
apps_promo()->GetPromoCounter());
// The app launcher and promo should not be shown if there are no extensions
// installed and no promo is set.
ExtensionIdSet installed_ids;
bool promo_just_expired = false;
EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
EXPECT_FALSE(apps_promo()->ShouldShowAppLauncher(installed_ids));
EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
&promo_just_expired));
EXPECT_FALSE(promo_just_expired);
// Make sure the web store can be supported even when the promo is not active.
AppsPromo::SetWebStoreSupportedForLocale(true);
EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
&promo_just_expired));
// We should be able to disable the web store as well.
AppsPromo::SetWebStoreSupportedForLocale(false);
EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
EXPECT_FALSE(apps_promo()->ShouldShowAppLauncher(installed_ids));
EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
&promo_just_expired));
// Once the promo is set, we show both the promo and app launcher.
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
kPromoUserGroup);
AppsPromo::SetWebStoreSupportedForLocale(true);
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
EXPECT_TRUE(apps_promo()->ShouldShowPromo(installed_ids,
&promo_just_expired));
EXPECT_FALSE(promo_just_expired);
// Now install an app and the promo should not be shown.
installed_ids.insert(*(default_app_ids.begin()));
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
&promo_just_expired));
EXPECT_FALSE(promo_just_expired);
// Even if the user installs the exact set of default apps, we still don't
// show the promo.
installed_ids = default_app_ids;
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
EXPECT_FALSE(apps_promo()->ShouldShowPromo(installed_ids,
&promo_just_expired));
EXPECT_FALSE(promo_just_expired);
// If the user then uninstalls the apps, the app launcher should remain
// and the promo should return.
installed_ids.clear();
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
EXPECT_TRUE(apps_promo()->ShouldShowAppLauncher(installed_ids));
EXPECT_TRUE(apps_promo()->ShouldShowPromo(installed_ids,
&promo_just_expired));
EXPECT_FALSE(promo_just_expired);
}
// Tests get and set of promo content.
TEST_F(ExtensionAppsPromo, PromoPrefs) {
// Store a promo....
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
kPromoUserGroup);
// ... then make sure AppsPromo can access it.
EXPECT_EQ(kPromoId, AppsPromo::GetPromoId());
EXPECT_EQ(kPromoHeader, AppsPromo::GetPromoHeaderText());
EXPECT_EQ(kPromoButton, AppsPromo::GetPromoButtonText());
EXPECT_EQ(GURL(kPromoLink), AppsPromo::GetPromoLink());
EXPECT_EQ(kPromoExpire, AppsPromo::GetPromoExpireText());
EXPECT_EQ(kPromoUserGroup, AppsPromo::GetPromoUserGroup());
// The promo logo should be the default value.
EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromoLogo());
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
AppsPromo::ClearPromo();
EXPECT_EQ("", AppsPromo::GetPromoId());
EXPECT_EQ("", AppsPromo::GetPromoHeaderText());
EXPECT_EQ("", AppsPromo::GetPromoButtonText());
EXPECT_EQ(GURL(""), AppsPromo::GetPromoLink());
EXPECT_EQ("", AppsPromo::GetPromoExpireText());
EXPECT_EQ(AppsPromo::USERS_NONE, AppsPromo::GetPromoUserGroup());
EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromoLogo());
EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale());
// Make sure we can set the logo to something other than the default.
std::string promo_logo = "data:image/png;base64,iVBORw0kGgoAAAN";
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(promo_logo),
kPromoUserGroup);
EXPECT_EQ(GURL(promo_logo), AppsPromo::GetPromoLogo());
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
// Verify that the default is returned instead of http or https URLs.
promo_logo = "http://google.com/logo.png";
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(promo_logo),
kPromoUserGroup);
EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromoLogo());
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
promo_logo = "https://google.com/logo.png";
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(promo_logo),
kPromoUserGroup);
EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromoLogo());
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
// Try an invalid URL.
promo_logo = "sldkfjlsdn";
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(promo_logo),
kPromoUserGroup);
EXPECT_EQ(GURL(kPromoLogo), AppsPromo::GetPromoLogo());
EXPECT_TRUE(AppsPromo::IsPromoSupportedForLocale());
// Try the web store supported flag.
EXPECT_FALSE(AppsPromo::IsWebStoreSupportedForLocale());
AppsPromo::SetWebStoreSupportedForLocale(true);
EXPECT_TRUE(AppsPromo::IsWebStoreSupportedForLocale());
AppsPromo::SetWebStoreSupportedForLocale(false);
EXPECT_FALSE(AppsPromo::IsWebStoreSupportedForLocale());
}
// Tests maximizing the promo for USERS_NONE.
TEST_F(ExtensionAppsPromo, UpdatePromoFocus_UsersNone) {
// Verify that the apps section is not already maximized.
ExpectAppsSectionMaximized(prefs(), false);
// The promo shouldn't maximize for anyone.
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_NONE);
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), false);
// The promo still shouldn't maximize if we change it's ID.
AppsPromo::SetPromo("lkksdf", kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_NONE);
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), false);
}
// Tests maximizing the promo for USERS_EXISTING.
TEST_F(ExtensionAppsPromo, UpdatePromoFocus_UsersExisting) {
// Verify that the apps section is not already maximized.
ExpectAppsSectionMaximized(prefs(), false);
// Set the promo content.
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_EXISTING);
// This is a new user so the apps section shouldn't maximize.
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), false);
// Set a new promo and now it should maximize.
AppsPromo::SetPromo("lksdf", kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_EXISTING);
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), true);
apps_promo()->HidePromo();
ExpectAppsPromoHidden(prefs());
}
// Tests maximizing the promo for USERS_NEW.
TEST_F(ExtensionAppsPromo, UpdatePromoFocus_UsersNew) {
// Verify that the apps section is not already maximized.
ExpectAppsSectionMaximized(prefs(), false);
// The promo should maximize for new users.
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_NEW);
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), true);
// Try hiding the promo.
apps_promo()->HidePromo();
ExpectAppsPromoHidden(prefs());
// The same promo should not maximize twice.
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), false);
// Another promo targetting new users should not maximize.
AppsPromo::SetPromo("lksdf", kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_NEW);
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), false);
}
// Tests maximizing the promo for USERS_NEW | USERS_EXISTING.
TEST_F(ExtensionAppsPromo, UpdatePromoFocus_UsersAll) {
// Verify that the apps section is not already maximized.
ExpectAppsSectionMaximized(prefs(), false);
// The apps section should maximize for all users.
AppsPromo::SetPromo(kPromoId, kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_NEW | AppsPromo::USERS_EXISTING);
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), true);
apps_promo()->HidePromo();
ExpectAppsPromoHidden(prefs());
// The same promo should not maximize twice.
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), false);
// A promo with a new ID should maximize though.
AppsPromo::SetPromo("lkksdf", kPromoHeader, kPromoButton,
GURL(kPromoLink), kPromoExpire, GURL(""),
AppsPromo::USERS_NEW | AppsPromo::USERS_EXISTING);
apps_promo()->MaximizeAppsIfNecessary();
ExpectAppsSectionMaximized(prefs(), true);
}
#endif // OS_CHROMEOS
|