1 // Copyright 2013 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.
5 #include "chrome/browser/extensions/chrome_app_sorting.h"
9 #include "chrome/browser/extensions/./extension_prefs_unittest.h"
10 #include "chrome/common/extensions/extension_constants.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "sync/api/string_ordinal.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
17 namespace keys
= manifest_keys
;
19 class ChromeAppSortingTest
: public ExtensionPrefsTest
{
21 ChromeAppSorting
* app_sorting() {
22 return static_cast<ChromeAppSorting
*>(prefs()->app_sorting());
26 class ChromeAppSortingAppLocation
: public ChromeAppSortingTest
{
28 virtual void Initialize() OVERRIDE
{
29 extension_
= prefs_
.AddExtension("not_an_app");
30 // Non-apps should not have any app launch ordinal or page ordinal.
31 prefs()->OnExtensionInstalled(extension_
.get(),
34 syncer::StringOrdinal(),
38 virtual void Verify() OVERRIDE
{
40 app_sorting()->GetAppLaunchOrdinal(extension_
->id()).IsValid());
42 app_sorting()->GetPageOrdinal(extension_
->id()).IsValid());
46 scoped_refptr
<Extension
> extension_
;
48 TEST_F(ChromeAppSortingAppLocation
, ChromeAppSortingAppLocation
) {}
50 class ChromeAppSortingAppLaunchOrdinal
: public ChromeAppSortingTest
{
52 virtual void Initialize() OVERRIDE
{
54 syncer::StringOrdinal page
= syncer::StringOrdinal::CreateInitialOrdinal();
55 EXPECT_TRUE(syncer::StringOrdinal::CreateInitialOrdinal().Equals(
56 app_sorting()->CreateNextAppLaunchOrdinal(page
)));
58 extension_
= prefs_
.AddApp("on_extension_installed");
59 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_
->id()));
60 prefs()->OnExtensionInstalled(extension_
.get(),
63 syncer::StringOrdinal(),
67 virtual void Verify() OVERRIDE
{
68 syncer::StringOrdinal launch_ordinal
=
69 app_sorting()->GetAppLaunchOrdinal(extension_
->id());
70 syncer::StringOrdinal page_ordinal
=
71 syncer::StringOrdinal::CreateInitialOrdinal();
73 // Extension should have been assigned a valid StringOrdinal.
74 EXPECT_TRUE(launch_ordinal
.IsValid());
75 EXPECT_TRUE(launch_ordinal
.LessThan(
76 app_sorting()->CreateNextAppLaunchOrdinal(page_ordinal
)));
77 // Set a new launch ordinal of and verify it comes after.
78 app_sorting()->SetAppLaunchOrdinal(
80 app_sorting()->CreateNextAppLaunchOrdinal(page_ordinal
));
81 syncer::StringOrdinal new_launch_ordinal
=
82 app_sorting()->GetAppLaunchOrdinal(extension_
->id());
83 EXPECT_TRUE(launch_ordinal
.LessThan(new_launch_ordinal
));
85 // This extension doesn't exist, so it should return an invalid
87 syncer::StringOrdinal invalid_app_launch_ordinal
=
88 app_sorting()->GetAppLaunchOrdinal("foo");
89 EXPECT_FALSE(invalid_app_launch_ordinal
.IsValid());
90 EXPECT_EQ(-1, app_sorting()->PageStringOrdinalAsInteger(
91 invalid_app_launch_ordinal
));
93 // The second page doesn't have any apps so its next launch ordinal should
94 // be the first launch ordinal.
95 syncer::StringOrdinal next_page
= page_ordinal
.CreateAfter();
96 syncer::StringOrdinal next_page_app_launch_ordinal
=
97 app_sorting()->CreateNextAppLaunchOrdinal(next_page
);
98 EXPECT_TRUE(next_page_app_launch_ordinal
.Equals(
99 app_sorting()->CreateFirstAppLaunchOrdinal(next_page
)));
103 scoped_refptr
<Extension
> extension_
;
105 TEST_F(ChromeAppSortingAppLaunchOrdinal
, ChromeAppSortingAppLaunchOrdinal
) {}
107 class ChromeAppSortingPageOrdinal
: public ChromeAppSortingTest
{
109 virtual void Initialize() OVERRIDE
{
110 extension_
= prefs_
.AddApp("page_ordinal");
111 // Install with a page preference.
112 first_page_
= syncer::StringOrdinal::CreateInitialOrdinal();
113 prefs()->OnExtensionInstalled(extension_
.get(),
118 EXPECT_TRUE(first_page_
.Equals(
119 app_sorting()->GetPageOrdinal(extension_
->id())));
120 EXPECT_EQ(0, app_sorting()->PageStringOrdinalAsInteger(first_page_
));
122 scoped_refptr
<Extension
> extension2
= prefs_
.AddApp("page_ordinal_2");
123 // Install without any page preference.
124 prefs()->OnExtensionInstalled(extension2
.get(),
127 syncer::StringOrdinal(),
129 EXPECT_TRUE(first_page_
.Equals(
130 app_sorting()->GetPageOrdinal(extension2
->id())));
132 virtual void Verify() OVERRIDE
{
133 // Set the page ordinal.
134 syncer::StringOrdinal new_page
= first_page_
.CreateAfter();
135 app_sorting()->SetPageOrdinal(extension_
->id(), new_page
);
136 // Verify the page ordinal.
138 new_page
.Equals(app_sorting()->GetPageOrdinal(extension_
->id())));
139 EXPECT_EQ(1, app_sorting()->PageStringOrdinalAsInteger(new_page
));
141 // This extension doesn't exist, so it should return an invalid
143 EXPECT_FALSE(app_sorting()->GetPageOrdinal("foo").IsValid());
147 syncer::StringOrdinal first_page_
;
148 scoped_refptr
<Extension
> extension_
;
150 TEST_F(ChromeAppSortingPageOrdinal
, ChromeAppSortingPageOrdinal
) {}
152 // Ensure that ChromeAppSorting is able to properly initialize off a set
153 // of old page and app launch indices and properly convert them.
154 class ChromeAppSortingInitialize
: public PrefsPrepopulatedTestBase
{
156 ChromeAppSortingInitialize() {}
157 virtual ~ChromeAppSortingInitialize() {}
159 virtual void Initialize() OVERRIDE
{
160 // A preference determining the order of which the apps appear on the NTP.
161 const char kPrefAppLaunchIndexDeprecated
[] = "app_launcher_index";
162 // A preference determining the page on which an app appears in the NTP.
163 const char kPrefPageIndexDeprecated
[] = "page_index";
165 // Setup the deprecated preferences.
166 ExtensionScopedPrefs
* scoped_prefs
=
167 static_cast<ExtensionScopedPrefs
*>(prefs());
168 scoped_prefs
->UpdateExtensionPref(extension1()->id(),
169 kPrefAppLaunchIndexDeprecated
,
170 new base::FundamentalValue(0));
171 scoped_prefs
->UpdateExtensionPref(extension1()->id(),
172 kPrefPageIndexDeprecated
,
173 new base::FundamentalValue(0));
175 scoped_prefs
->UpdateExtensionPref(extension2()->id(),
176 kPrefAppLaunchIndexDeprecated
,
177 new base::FundamentalValue(1));
178 scoped_prefs
->UpdateExtensionPref(extension2()->id(),
179 kPrefPageIndexDeprecated
,
180 new base::FundamentalValue(0));
182 scoped_prefs
->UpdateExtensionPref(extension3()->id(),
183 kPrefAppLaunchIndexDeprecated
,
184 new base::FundamentalValue(0));
185 scoped_prefs
->UpdateExtensionPref(extension3()->id(),
186 kPrefPageIndexDeprecated
,
187 new base::FundamentalValue(1));
189 // We insert the ids in reserve order so that we have to deal with the
190 // element on the 2nd page before the 1st page is seen.
192 ids
.push_back(extension3()->id());
193 ids
.push_back(extension2()->id());
194 ids
.push_back(extension1()->id());
196 prefs()->app_sorting()->Initialize(ids
);
198 virtual void Verify() OVERRIDE
{
199 syncer::StringOrdinal first_ordinal
=
200 syncer::StringOrdinal::CreateInitialOrdinal();
201 AppSorting
* app_sorting
= prefs()->app_sorting();
203 EXPECT_TRUE(first_ordinal
.Equals(
204 app_sorting
->GetAppLaunchOrdinal(extension1()->id())));
205 EXPECT_TRUE(first_ordinal
.LessThan(
206 app_sorting
->GetAppLaunchOrdinal(extension2()->id())));
207 EXPECT_TRUE(first_ordinal
.Equals(
208 app_sorting
->GetAppLaunchOrdinal(extension3()->id())));
210 EXPECT_TRUE(first_ordinal
.Equals(
211 app_sorting
->GetPageOrdinal(extension1()->id())));
212 EXPECT_TRUE(first_ordinal
.Equals(
213 app_sorting
->GetPageOrdinal(extension2()->id())));
214 EXPECT_TRUE(first_ordinal
.LessThan(
215 app_sorting
->GetPageOrdinal(extension3()->id())));
218 TEST_F(ChromeAppSortingInitialize
, ChromeAppSortingInitialize
) {}
220 // Make sure that initialization still works when no extensions are present
221 // (i.e. make sure that the web store icon is still loaded into the map).
222 class ChromeAppSortingInitializeWithNoApps
: public PrefsPrepopulatedTestBase
{
224 ChromeAppSortingInitializeWithNoApps() {}
225 virtual ~ChromeAppSortingInitializeWithNoApps() {}
227 virtual void Initialize() OVERRIDE
{
228 AppSorting
* app_sorting
= prefs()->app_sorting();
230 // Make sure that the web store has valid ordinals.
231 syncer::StringOrdinal initial_ordinal
=
232 syncer::StringOrdinal::CreateInitialOrdinal();
233 app_sorting
->SetPageOrdinal(extension_misc::kWebStoreAppId
,
235 app_sorting
->SetAppLaunchOrdinal(extension_misc::kWebStoreAppId
,
239 app_sorting
->Initialize(ids
);
241 virtual void Verify() OVERRIDE
{
242 ChromeAppSorting
* app_sorting
=
243 static_cast<ChromeAppSorting
*>(prefs()->app_sorting());
245 syncer::StringOrdinal page
=
246 app_sorting
->GetPageOrdinal(extension_misc::kWebStoreAppId
);
247 EXPECT_TRUE(page
.IsValid());
249 ChromeAppSorting::PageOrdinalMap::iterator page_it
=
250 app_sorting
->ntp_ordinal_map_
.find(page
);
251 EXPECT_TRUE(page_it
!= app_sorting
->ntp_ordinal_map_
.end());
253 syncer::StringOrdinal app_launch
=
254 app_sorting
->GetPageOrdinal(extension_misc::kWebStoreAppId
);
255 EXPECT_TRUE(app_launch
.IsValid());
257 ChromeAppSorting::AppLaunchOrdinalMap::iterator app_launch_it
=
258 page_it
->second
.find(app_launch
);
259 EXPECT_TRUE(app_launch_it
!= page_it
->second
.end());
262 TEST_F(ChromeAppSortingInitializeWithNoApps
,
263 ChromeAppSortingInitializeWithNoApps
) {}
265 // Tests the application index to ordinal migration code for values that
266 // shouldn't be converted. This should be removed when the migrate code
268 // http://crbug.com/107376
269 class ChromeAppSortingMigrateAppIndexInvalid
270 : public PrefsPrepopulatedTestBase
{
272 ChromeAppSortingMigrateAppIndexInvalid() {}
273 virtual ~ChromeAppSortingMigrateAppIndexInvalid() {}
275 virtual void Initialize() OVERRIDE
{
276 // A preference determining the order of which the apps appear on the NTP.
277 const char kPrefAppLaunchIndexDeprecated
[] = "app_launcher_index";
278 // A preference determining the page on which an app appears in the NTP.
279 const char kPrefPageIndexDeprecated
[] = "page_index";
281 // Setup the deprecated preference.
282 ExtensionScopedPrefs
* scoped_prefs
=
283 static_cast<ExtensionScopedPrefs
*>(prefs());
284 scoped_prefs
->UpdateExtensionPref(extension1()->id(),
285 kPrefAppLaunchIndexDeprecated
,
286 new base::FundamentalValue(0));
287 scoped_prefs
->UpdateExtensionPref(extension1()->id(),
288 kPrefPageIndexDeprecated
,
289 new base::FundamentalValue(-1));
292 ids
.push_back(extension1()->id());
294 prefs()->app_sorting()->Initialize(ids
);
296 virtual void Verify() OVERRIDE
{
297 // Make sure that the invalid page_index wasn't converted over.
298 EXPECT_FALSE(prefs()->app_sorting()->GetAppLaunchOrdinal(
299 extension1()->id()).IsValid());
302 TEST_F(ChromeAppSortingMigrateAppIndexInvalid
,
303 ChromeAppSortingMigrateAppIndexInvalid
) {}
305 class ChromeAppSortingFixNTPCollisionsAllCollide
306 : public PrefsPrepopulatedTestBase
{
308 ChromeAppSortingFixNTPCollisionsAllCollide() {}
309 virtual ~ChromeAppSortingFixNTPCollisionsAllCollide() {}
311 virtual void Initialize() OVERRIDE
{
312 repeated_ordinal_
= syncer::StringOrdinal::CreateInitialOrdinal();
314 AppSorting
* app_sorting
= prefs()->app_sorting();
316 app_sorting
->SetAppLaunchOrdinal(extension1()->id(),
318 app_sorting
->SetPageOrdinal(extension1()->id(), repeated_ordinal_
);
320 app_sorting
->SetAppLaunchOrdinal(extension2()->id(), repeated_ordinal_
);
321 app_sorting
->SetPageOrdinal(extension2()->id(), repeated_ordinal_
);
323 app_sorting
->SetAppLaunchOrdinal(extension3()->id(), repeated_ordinal_
);
324 app_sorting
->SetPageOrdinal(extension3()->id(), repeated_ordinal_
);
326 app_sorting
->FixNTPOrdinalCollisions();
328 virtual void Verify() OVERRIDE
{
329 AppSorting
* app_sorting
= prefs()->app_sorting();
330 syncer::StringOrdinal extension1_app_launch
=
331 app_sorting
->GetAppLaunchOrdinal(extension1()->id());
332 syncer::StringOrdinal extension2_app_launch
=
333 app_sorting
->GetAppLaunchOrdinal(extension2()->id());
334 syncer::StringOrdinal extension3_app_launch
=
335 app_sorting
->GetAppLaunchOrdinal(extension3()->id());
337 // The overlapping extensions should have be adjusted so that they are
338 // sorted by their id.
339 EXPECT_EQ(extension1()->id() < extension2()->id(),
340 extension1_app_launch
.LessThan(extension2_app_launch
));
341 EXPECT_EQ(extension1()->id() < extension3()->id(),
342 extension1_app_launch
.LessThan(extension3_app_launch
));
343 EXPECT_EQ(extension2()->id() < extension3()->id(),
344 extension2_app_launch
.LessThan(extension3_app_launch
));
346 // The page ordinal should be unchanged.
347 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension1()->id()).Equals(
349 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension2()->id()).Equals(
351 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension3()->id()).Equals(
356 syncer::StringOrdinal repeated_ordinal_
;
358 TEST_F(ChromeAppSortingFixNTPCollisionsAllCollide
,
359 ChromeAppSortingFixNTPCollisionsAllCollide
) {}
361 class ChromeAppSortingFixNTPCollisionsSomeCollideAtStart
362 : public PrefsPrepopulatedTestBase
{
364 ChromeAppSortingFixNTPCollisionsSomeCollideAtStart() {}
365 virtual ~ChromeAppSortingFixNTPCollisionsSomeCollideAtStart() {}
367 virtual void Initialize() OVERRIDE
{
368 first_ordinal_
= syncer::StringOrdinal::CreateInitialOrdinal();
369 syncer::StringOrdinal second_ordinal
= first_ordinal_
.CreateAfter();
371 AppSorting
* app_sorting
= prefs()->app_sorting();
373 // Have the first two extension in the same position, with a third
374 // (non-colliding) extension after.
376 app_sorting
->SetAppLaunchOrdinal(extension1()->id(), first_ordinal_
);
377 app_sorting
->SetPageOrdinal(extension1()->id(), first_ordinal_
);
379 app_sorting
->SetAppLaunchOrdinal(extension2()->id(), first_ordinal_
);
380 app_sorting
->SetPageOrdinal(extension2()->id(), first_ordinal_
);
382 app_sorting
->SetAppLaunchOrdinal(extension3()->id(), second_ordinal
);
383 app_sorting
->SetPageOrdinal(extension3()->id(), first_ordinal_
);
385 app_sorting
->FixNTPOrdinalCollisions();
387 virtual void Verify() OVERRIDE
{
388 AppSorting
* app_sorting
= prefs()->app_sorting();
389 syncer::StringOrdinal extension1_app_launch
=
390 app_sorting
->GetAppLaunchOrdinal(extension1()->id());
391 syncer::StringOrdinal extension2_app_launch
=
392 app_sorting
->GetAppLaunchOrdinal(extension2()->id());
393 syncer::StringOrdinal extension3_app_launch
=
394 app_sorting
->GetAppLaunchOrdinal(extension3()->id());
396 // The overlapping extensions should have be adjusted so that they are
397 // sorted by their id, but they both should be before ext3, which wasn't
399 EXPECT_EQ(extension1()->id() < extension2()->id(),
400 extension1_app_launch
.LessThan(extension2_app_launch
));
401 EXPECT_TRUE(extension1_app_launch
.LessThan(extension3_app_launch
));
402 EXPECT_TRUE(extension2_app_launch
.LessThan(extension3_app_launch
));
404 // The page ordinal should be unchanged.
405 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension1()->id()).Equals(
407 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension2()->id()).Equals(
409 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension3()->id()).Equals(
414 syncer::StringOrdinal first_ordinal_
;
416 TEST_F(ChromeAppSortingFixNTPCollisionsSomeCollideAtStart
,
417 ChromeAppSortingFixNTPCollisionsSomeCollideAtStart
) {}
419 class ChromeAppSortingFixNTPCollisionsSomeCollideAtEnd
420 : public PrefsPrepopulatedTestBase
{
422 ChromeAppSortingFixNTPCollisionsSomeCollideAtEnd() {}
423 virtual ~ChromeAppSortingFixNTPCollisionsSomeCollideAtEnd() {}
425 virtual void Initialize() OVERRIDE
{
426 first_ordinal_
= syncer::StringOrdinal::CreateInitialOrdinal();
427 syncer::StringOrdinal second_ordinal
= first_ordinal_
.CreateAfter();
429 AppSorting
* app_sorting
= prefs()->app_sorting();
431 // Have the first extension in a non-colliding position, followed by two
432 // two extension in the same position.
434 app_sorting
->SetAppLaunchOrdinal(extension1()->id(), first_ordinal_
);
435 app_sorting
->SetPageOrdinal(extension1()->id(), first_ordinal_
);
437 app_sorting
->SetAppLaunchOrdinal(extension2()->id(), second_ordinal
);
438 app_sorting
->SetPageOrdinal(extension2()->id(), first_ordinal_
);
440 app_sorting
->SetAppLaunchOrdinal(extension3()->id(), second_ordinal
);
441 app_sorting
->SetPageOrdinal(extension3()->id(), first_ordinal_
);
443 app_sorting
->FixNTPOrdinalCollisions();
445 virtual void Verify() OVERRIDE
{
446 AppSorting
* app_sorting
= prefs()->app_sorting();
447 syncer::StringOrdinal extension1_app_launch
=
448 app_sorting
->GetAppLaunchOrdinal(extension1()->id());
449 syncer::StringOrdinal extension2_app_launch
=
450 app_sorting
->GetAppLaunchOrdinal(extension2()->id());
451 syncer::StringOrdinal extension3_app_launch
=
452 app_sorting
->GetAppLaunchOrdinal(extension3()->id());
454 // The overlapping extensions should have be adjusted so that they are
455 // sorted by their id, but they both should be after ext1, which wasn't
457 EXPECT_TRUE(extension1_app_launch
.LessThan(extension2_app_launch
));
458 EXPECT_TRUE(extension1_app_launch
.LessThan(extension3_app_launch
));
459 EXPECT_EQ(extension2()->id() < extension3()->id(),
460 extension2_app_launch
.LessThan(extension3_app_launch
));
462 // The page ordinal should be unchanged.
463 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension1()->id()).Equals(
465 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension2()->id()).Equals(
467 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension3()->id()).Equals(
472 syncer::StringOrdinal first_ordinal_
;
474 TEST_F(ChromeAppSortingFixNTPCollisionsSomeCollideAtEnd
,
475 ChromeAppSortingFixNTPCollisionsSomeCollideAtEnd
) {}
477 class ChromeAppSortingFixNTPCollisionsTwoCollisions
478 : public PrefsPrepopulatedTestBase
{
480 ChromeAppSortingFixNTPCollisionsTwoCollisions() {}
481 virtual ~ChromeAppSortingFixNTPCollisionsTwoCollisions() {}
483 virtual void Initialize() OVERRIDE
{
484 first_ordinal_
= syncer::StringOrdinal::CreateInitialOrdinal();
485 syncer::StringOrdinal second_ordinal
= first_ordinal_
.CreateAfter();
487 AppSorting
* app_sorting
= prefs()->app_sorting();
489 // Have two extensions colliding, followed by two more colliding extensions.
490 app_sorting
->SetAppLaunchOrdinal(extension1()->id(), first_ordinal_
);
491 app_sorting
->SetPageOrdinal(extension1()->id(), first_ordinal_
);
493 app_sorting
->SetAppLaunchOrdinal(extension2()->id(), first_ordinal_
);
494 app_sorting
->SetPageOrdinal(extension2()->id(), first_ordinal_
);
496 app_sorting
->SetAppLaunchOrdinal(extension3()->id(), second_ordinal
);
497 app_sorting
->SetPageOrdinal(extension3()->id(), first_ordinal_
);
499 app_sorting
->SetAppLaunchOrdinal(extension4()->id(), second_ordinal
);
500 app_sorting
->SetPageOrdinal(extension4()->id(), first_ordinal_
);
502 app_sorting
->FixNTPOrdinalCollisions();
504 virtual void Verify() OVERRIDE
{
505 AppSorting
* app_sorting
= prefs()->app_sorting();
506 syncer::StringOrdinal extension1_app_launch
=
507 app_sorting
->GetAppLaunchOrdinal(extension1()->id());
508 syncer::StringOrdinal extension2_app_launch
=
509 app_sorting
->GetAppLaunchOrdinal(extension2()->id());
510 syncer::StringOrdinal extension3_app_launch
=
511 app_sorting
->GetAppLaunchOrdinal(extension3()->id());
512 syncer::StringOrdinal extension4_app_launch
=
513 app_sorting
->GetAppLaunchOrdinal(extension4()->id());
515 // The overlapping extensions should have be adjusted so that they are
516 // sorted by their id, with |ext1| and |ext2| appearing before |ext3| and
518 EXPECT_TRUE(extension1_app_launch
.LessThan(extension3_app_launch
));
519 EXPECT_TRUE(extension1_app_launch
.LessThan(extension4_app_launch
));
520 EXPECT_TRUE(extension2_app_launch
.LessThan(extension3_app_launch
));
521 EXPECT_TRUE(extension2_app_launch
.LessThan(extension4_app_launch
));
523 EXPECT_EQ(extension1()->id() < extension2()->id(),
524 extension1_app_launch
.LessThan(extension2_app_launch
));
525 EXPECT_EQ(extension3()->id() < extension4()->id(),
526 extension3_app_launch
.LessThan(extension4_app_launch
));
528 // The page ordinal should be unchanged.
529 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension1()->id()).Equals(
531 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension2()->id()).Equals(
533 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension3()->id()).Equals(
535 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension4()->id()).Equals(
540 syncer::StringOrdinal first_ordinal_
;
542 TEST_F(ChromeAppSortingFixNTPCollisionsTwoCollisions
,
543 ChromeAppSortingFixNTPCollisionsTwoCollisions
) {}
545 class ChromeAppSortingEnsureValidOrdinals
546 : public PrefsPrepopulatedTestBase
{
548 ChromeAppSortingEnsureValidOrdinals() {}
549 virtual ~ChromeAppSortingEnsureValidOrdinals() {}
551 virtual void Initialize() OVERRIDE
{}
552 virtual void Verify() OVERRIDE
{
553 AppSorting
* app_sorting
= prefs()->app_sorting();
555 // Give ext1 invalid ordinals and then check that EnsureValidOrdinals fixes
557 app_sorting
->SetAppLaunchOrdinal(extension1()->id(),
558 syncer::StringOrdinal());
559 app_sorting
->SetPageOrdinal(extension1()->id(), syncer::StringOrdinal());
561 app_sorting
->EnsureValidOrdinals(extension1()->id(),
562 syncer::StringOrdinal());
564 EXPECT_TRUE(app_sorting
->GetAppLaunchOrdinal(extension1()->id()).IsValid());
565 EXPECT_TRUE(app_sorting
->GetPageOrdinal(extension1()->id()).IsValid());
568 TEST_F(ChromeAppSortingEnsureValidOrdinals
,
569 ChromeAppSortingEnsureValidOrdinals
) {}
571 class ChromeAppSortingPageOrdinalMapping
: public PrefsPrepopulatedTestBase
{
573 ChromeAppSortingPageOrdinalMapping() {}
574 virtual ~ChromeAppSortingPageOrdinalMapping() {}
576 virtual void Initialize() OVERRIDE
{}
577 virtual void Verify() OVERRIDE
{
578 std::string ext_1
= "ext_1";
579 std::string ext_2
= "ext_2";
581 ChromeAppSorting
* app_sorting
=
582 static_cast<ChromeAppSorting
*>(prefs()->app_sorting());
583 syncer::StringOrdinal first_ordinal
=
584 syncer::StringOrdinal::CreateInitialOrdinal();
586 // Ensure attempting to removing a mapping with an invalid page doesn't
588 EXPECT_TRUE(app_sorting
->ntp_ordinal_map_
.empty());
589 app_sorting
->RemoveOrdinalMapping(
590 ext_1
, first_ordinal
, first_ordinal
);
591 EXPECT_TRUE(app_sorting
->ntp_ordinal_map_
.empty());
594 app_sorting
->AddOrdinalMapping(ext_1
, first_ordinal
, first_ordinal
);
595 app_sorting
->AddOrdinalMapping(ext_2
, first_ordinal
, first_ordinal
);
597 EXPECT_EQ(1U, app_sorting
->ntp_ordinal_map_
.size());
598 EXPECT_EQ(2U, app_sorting
->ntp_ordinal_map_
[first_ordinal
].size());
600 ChromeAppSorting::AppLaunchOrdinalMap::iterator it
=
601 app_sorting
->ntp_ordinal_map_
[first_ordinal
].find(first_ordinal
);
602 EXPECT_EQ(ext_1
, it
->second
);
604 EXPECT_EQ(ext_2
, it
->second
);
606 app_sorting
->RemoveOrdinalMapping(ext_1
, first_ordinal
, first_ordinal
);
607 EXPECT_EQ(1U, app_sorting
->ntp_ordinal_map_
.size());
608 EXPECT_EQ(1U, app_sorting
->ntp_ordinal_map_
[first_ordinal
].size());
610 it
= app_sorting
->ntp_ordinal_map_
[first_ordinal
].find(first_ordinal
);
611 EXPECT_EQ(ext_2
, it
->second
);
613 // Ensure that attempting to remove an extension with a valid page and app
614 // launch ordinals, but a unused id has no effect.
615 app_sorting
->RemoveOrdinalMapping(
616 "invalid_ext", first_ordinal
, first_ordinal
);
617 EXPECT_EQ(1U, app_sorting
->ntp_ordinal_map_
.size());
618 EXPECT_EQ(1U, app_sorting
->ntp_ordinal_map_
[first_ordinal
].size());
620 it
= app_sorting
->ntp_ordinal_map_
[first_ordinal
].find(first_ordinal
);
621 EXPECT_EQ(ext_2
, it
->second
);
624 TEST_F(ChromeAppSortingPageOrdinalMapping
,
625 ChromeAppSortingPageOrdinalMapping
) {}
627 class ChromeAppSortingPreinstalledAppsBase
: public PrefsPrepopulatedTestBase
{
629 ChromeAppSortingPreinstalledAppsBase() {
630 base::DictionaryValue simple_dict
;
631 simple_dict
.SetString(keys::kVersion
, "1.0.0.0");
632 simple_dict
.SetString(keys::kName
, "unused");
633 simple_dict
.SetString(keys::kApp
, "true");
634 simple_dict
.SetString(keys::kLaunchLocalPath
, "fake.html");
637 app1_scoped_
= Extension::Create(
638 prefs_
.temp_dir().AppendASCII("app1_"), Manifest::EXTERNAL_PREF
,
639 simple_dict
, Extension::NO_FLAGS
, &error
);
640 prefs()->OnExtensionInstalled(app1_scoped_
.get(),
643 syncer::StringOrdinal(),
646 app2_scoped_
= Extension::Create(
647 prefs_
.temp_dir().AppendASCII("app2_"), Manifest::EXTERNAL_PREF
,
648 simple_dict
, Extension::NO_FLAGS
, &error
);
649 prefs()->OnExtensionInstalled(app2_scoped_
.get(),
652 syncer::StringOrdinal(),
655 app1_
= app1_scoped_
.get();
656 app2_
= app2_scoped_
.get();
658 virtual ~ChromeAppSortingPreinstalledAppsBase() {}
661 // Weak references, for convenience.
666 scoped_refptr
<Extension
> app1_scoped_
;
667 scoped_refptr
<Extension
> app2_scoped_
;
670 class ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage
671 : public ChromeAppSortingPreinstalledAppsBase
{
673 ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage() {}
674 virtual ~ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage() {}
676 virtual void Initialize() OVERRIDE
{}
677 virtual void Verify() OVERRIDE
{
678 syncer::StringOrdinal page
= syncer::StringOrdinal::CreateInitialOrdinal();
679 ChromeAppSorting
* app_sorting
=
680 static_cast<ChromeAppSorting
*>(prefs()->app_sorting());
682 syncer::StringOrdinal min
=
683 app_sorting
->GetMinOrMaxAppLaunchOrdinalsOnPage(
685 ChromeAppSorting::MIN_ORDINAL
);
686 syncer::StringOrdinal max
=
687 app_sorting
->GetMinOrMaxAppLaunchOrdinalsOnPage(
689 ChromeAppSorting::MAX_ORDINAL
);
690 EXPECT_TRUE(min
.IsValid());
691 EXPECT_TRUE(max
.IsValid());
692 EXPECT_TRUE(min
.LessThan(max
));
694 // Ensure that the min and max values aren't set for empty pages.
695 min
= syncer::StringOrdinal();
696 max
= syncer::StringOrdinal();
697 syncer::StringOrdinal empty_page
= page
.CreateAfter();
698 EXPECT_FALSE(min
.IsValid());
699 EXPECT_FALSE(max
.IsValid());
700 min
= app_sorting
->GetMinOrMaxAppLaunchOrdinalsOnPage(
702 ChromeAppSorting::MIN_ORDINAL
);
703 max
= app_sorting
->GetMinOrMaxAppLaunchOrdinalsOnPage(
705 ChromeAppSorting::MAX_ORDINAL
);
706 EXPECT_FALSE(min
.IsValid());
707 EXPECT_FALSE(max
.IsValid());
710 TEST_F(ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage
,
711 ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage
) {}
713 // Make sure that empty pages aren't removed from the integer to ordinal
714 // mapping. See http://crbug.com/109802 for details.
715 class ChromeAppSortingKeepEmptyStringOrdinalPages
716 : public ChromeAppSortingPreinstalledAppsBase
{
718 ChromeAppSortingKeepEmptyStringOrdinalPages() {}
719 virtual ~ChromeAppSortingKeepEmptyStringOrdinalPages() {}
721 virtual void Initialize() OVERRIDE
{
722 AppSorting
* app_sorting
= prefs()->app_sorting();
724 syncer::StringOrdinal first_page
=
725 syncer::StringOrdinal::CreateInitialOrdinal();
726 app_sorting
->SetPageOrdinal(app1_
->id(), first_page
);
727 EXPECT_EQ(0, app_sorting
->PageStringOrdinalAsInteger(first_page
));
729 last_page_
= first_page
.CreateAfter();
730 app_sorting
->SetPageOrdinal(app2_
->id(), last_page_
);
731 EXPECT_EQ(1, app_sorting
->PageStringOrdinalAsInteger(last_page_
));
733 // Move the second app to create an empty page.
734 app_sorting
->SetPageOrdinal(app2_
->id(), first_page
);
735 EXPECT_EQ(0, app_sorting
->PageStringOrdinalAsInteger(first_page
));
737 virtual void Verify() OVERRIDE
{
738 AppSorting
* app_sorting
= prefs()->app_sorting();
740 // Move the second app to a new empty page at the end, skipping over
741 // the current empty page.
742 last_page_
= last_page_
.CreateAfter();
743 app_sorting
->SetPageOrdinal(app2_
->id(), last_page_
);
744 EXPECT_EQ(2, app_sorting
->PageStringOrdinalAsInteger(last_page_
));
745 EXPECT_TRUE(last_page_
.Equals(app_sorting
->PageIntegerAsStringOrdinal(2)));
749 syncer::StringOrdinal last_page_
;
751 TEST_F(ChromeAppSortingKeepEmptyStringOrdinalPages
,
752 ChromeAppSortingKeepEmptyStringOrdinalPages
) {}
754 class ChromeAppSortingMakesFillerOrdinals
755 : public ChromeAppSortingPreinstalledAppsBase
{
757 ChromeAppSortingMakesFillerOrdinals() {}
758 virtual ~ChromeAppSortingMakesFillerOrdinals() {}
760 virtual void Initialize() OVERRIDE
{
761 AppSorting
* app_sorting
= prefs()->app_sorting();
763 syncer::StringOrdinal first_page
=
764 syncer::StringOrdinal::CreateInitialOrdinal();
765 app_sorting
->SetPageOrdinal(app1_
->id(), first_page
);
766 EXPECT_EQ(0, app_sorting
->PageStringOrdinalAsInteger(first_page
));
768 virtual void Verify() OVERRIDE
{
769 AppSorting
* app_sorting
= prefs()->app_sorting();
771 // Because the UI can add an unlimited number of empty pages without an app
772 // on them, this test simulates dropping of an app on the 1st and 4th empty
773 // pages (3rd and 6th pages by index) to ensure we don't crash and that
774 // filler ordinals are created as needed. See: http://crbug.com/122214
775 syncer::StringOrdinal page_three
=
776 app_sorting
->PageIntegerAsStringOrdinal(2);
777 app_sorting
->SetPageOrdinal(app1_
->id(), page_three
);
778 EXPECT_EQ(2, app_sorting
->PageStringOrdinalAsInteger(page_three
));
780 syncer::StringOrdinal page_six
= app_sorting
->PageIntegerAsStringOrdinal(5);
781 app_sorting
->SetPageOrdinal(app1_
->id(), page_six
);
782 EXPECT_EQ(5, app_sorting
->PageStringOrdinalAsInteger(page_six
));
785 TEST_F(ChromeAppSortingMakesFillerOrdinals
,
786 ChromeAppSortingMakesFillerOrdinals
) {}
788 class ChromeAppSortingDefaultOrdinalsBase
: public ChromeAppSortingTest
{
790 ChromeAppSortingDefaultOrdinalsBase() {}
791 virtual ~ChromeAppSortingDefaultOrdinalsBase() {}
793 virtual void Initialize() OVERRIDE
{
794 app_
= CreateApp("app");
796 InitDefaultOrdinals();
797 ChromeAppSorting
* app_sorting
=
798 static_cast<ChromeAppSorting
*>(prefs()->app_sorting());
799 ChromeAppSorting::AppOrdinalsMap
& sorting_defaults
=
800 app_sorting
->default_ordinals_
;
801 sorting_defaults
[app_
->id()].page_ordinal
= default_page_ordinal_
;
802 sorting_defaults
[app_
->id()].app_launch_ordinal
=
803 default_app_launch_ordinal_
;
810 scoped_refptr
<Extension
> CreateApp(const std::string
& name
) {
811 base::DictionaryValue simple_dict
;
812 simple_dict
.SetString(keys::kVersion
, "1.0.0.0");
813 simple_dict
.SetString(keys::kName
, name
);
814 simple_dict
.SetString(keys::kApp
, "true");
815 simple_dict
.SetString(keys::kLaunchLocalPath
, "fake.html");
818 scoped_refptr
<Extension
> app
= Extension::Create(
819 prefs_
.temp_dir().AppendASCII(name
), Manifest::EXTERNAL_PREF
,
820 simple_dict
, Extension::NO_FLAGS
, &errors
);
821 EXPECT_TRUE(app
.get()) << errors
;
822 EXPECT_TRUE(Extension::IdIsValid(app
->id()));
826 void InitDefaultOrdinals() {
827 default_page_ordinal_
=
828 syncer::StringOrdinal::CreateInitialOrdinal().CreateAfter();
829 default_app_launch_ordinal_
=
830 syncer::StringOrdinal::CreateInitialOrdinal().CreateBefore();
833 virtual void SetupUserOrdinals() {}
835 virtual void InstallApps() {
836 prefs()->OnExtensionInstalled(app_
.get(),
839 syncer::StringOrdinal(),
843 scoped_refptr
<Extension
> app_
;
844 syncer::StringOrdinal default_page_ordinal_
;
845 syncer::StringOrdinal default_app_launch_ordinal_
;
848 // Tests that the app gets its default ordinals.
849 class ChromeAppSortingDefaultOrdinals
850 : public ChromeAppSortingDefaultOrdinalsBase
{
852 ChromeAppSortingDefaultOrdinals() {}
853 virtual ~ChromeAppSortingDefaultOrdinals() {}
855 virtual void Verify() OVERRIDE
{
856 AppSorting
* app_sorting
= prefs()->app_sorting();
857 EXPECT_TRUE(app_sorting
->GetPageOrdinal(app_
->id()).Equals(
858 default_page_ordinal_
));
859 EXPECT_TRUE(app_sorting
->GetAppLaunchOrdinal(app_
->id()).Equals(
860 default_app_launch_ordinal_
));
863 TEST_F(ChromeAppSortingDefaultOrdinals
,
864 ChromeAppSortingDefaultOrdinals
) {}
866 // Tests that the default page ordinal is overridden by install page ordinal.
867 class ChromeAppSortingDefaultOrdinalOverriddenByInstallPage
868 : public ChromeAppSortingDefaultOrdinalsBase
{
870 ChromeAppSortingDefaultOrdinalOverriddenByInstallPage() {}
871 virtual ~ChromeAppSortingDefaultOrdinalOverriddenByInstallPage() {}
873 virtual void Verify() OVERRIDE
{
874 AppSorting
* app_sorting
= prefs()->app_sorting();
876 EXPECT_FALSE(app_sorting
->GetPageOrdinal(app_
->id()).Equals(
877 default_page_ordinal_
));
878 EXPECT_TRUE(app_sorting
->GetPageOrdinal(app_
->id()).Equals(install_page_
));
882 virtual void InstallApps() OVERRIDE
{
883 install_page_
= default_page_ordinal_
.CreateAfter();
884 prefs()->OnExtensionInstalled(
885 app_
.get(), Extension::ENABLED
, false, install_page_
, std::string());
889 syncer::StringOrdinal install_page_
;
891 TEST_F(ChromeAppSortingDefaultOrdinalOverriddenByInstallPage
,
892 ChromeAppSortingDefaultOrdinalOverriddenByInstallPage
) {}
894 // Tests that the default ordinals are overridden by user values.
895 class ChromeAppSortingDefaultOrdinalOverriddenByUserValue
896 : public ChromeAppSortingDefaultOrdinalsBase
{
898 ChromeAppSortingDefaultOrdinalOverriddenByUserValue() {}
899 virtual ~ChromeAppSortingDefaultOrdinalOverriddenByUserValue() {}
901 virtual void Verify() OVERRIDE
{
902 AppSorting
* app_sorting
= prefs()->app_sorting();
904 EXPECT_TRUE(app_sorting
->GetPageOrdinal(app_
->id()).Equals(
905 user_page_ordinal_
));
906 EXPECT_TRUE(app_sorting
->GetAppLaunchOrdinal(app_
->id()).Equals(
907 user_app_launch_ordinal_
));
911 virtual void SetupUserOrdinals() OVERRIDE
{
912 user_page_ordinal_
= default_page_ordinal_
.CreateAfter();
913 user_app_launch_ordinal_
= default_app_launch_ordinal_
.CreateBefore();
915 AppSorting
* app_sorting
= prefs()->app_sorting();
916 app_sorting
->SetPageOrdinal(app_
->id(), user_page_ordinal_
);
917 app_sorting
->SetAppLaunchOrdinal(app_
->id(), user_app_launch_ordinal_
);
921 syncer::StringOrdinal user_page_ordinal_
;
922 syncer::StringOrdinal user_app_launch_ordinal_
;
924 TEST_F(ChromeAppSortingDefaultOrdinalOverriddenByUserValue
,
925 ChromeAppSortingDefaultOrdinalOverriddenByUserValue
) {}
927 // Tests that the default app launch ordinal is changed to avoid collision.
928 class ChromeAppSortingDefaultOrdinalNoCollision
929 : public ChromeAppSortingDefaultOrdinalsBase
{
931 ChromeAppSortingDefaultOrdinalNoCollision() {}
932 virtual ~ChromeAppSortingDefaultOrdinalNoCollision() {}
934 virtual void Verify() OVERRIDE
{
935 AppSorting
* app_sorting
= prefs()->app_sorting();
937 // Use the default page.
938 EXPECT_TRUE(app_sorting
->GetPageOrdinal(app_
->id()).Equals(
939 default_page_ordinal_
));
940 // Not using the default app launch ordinal because of the collision.
941 EXPECT_FALSE(app_sorting
->GetAppLaunchOrdinal(app_
->id()).Equals(
942 default_app_launch_ordinal_
));
946 virtual void SetupUserOrdinals() OVERRIDE
{
947 other_app_
= prefs_
.AddApp("other_app");
948 // Creates a collision.
949 AppSorting
* app_sorting
= prefs()->app_sorting();
950 app_sorting
->SetPageOrdinal(other_app_
->id(), default_page_ordinal_
);
951 app_sorting
->SetAppLaunchOrdinal(other_app_
->id(),
952 default_app_launch_ordinal_
);
954 yet_another_app_
= prefs_
.AddApp("yet_aother_app");
955 app_sorting
->SetPageOrdinal(yet_another_app_
->id(), default_page_ordinal_
);
956 app_sorting
->SetAppLaunchOrdinal(yet_another_app_
->id(),
957 default_app_launch_ordinal_
);
961 scoped_refptr
<Extension
> other_app_
;
962 scoped_refptr
<Extension
> yet_another_app_
;
964 TEST_F(ChromeAppSortingDefaultOrdinalNoCollision
,
965 ChromeAppSortingDefaultOrdinalNoCollision
) {}
967 } // namespace extensions