OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <time.h> | 5 #include <time.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <sstream> | 8 #include <sstream> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 predictor.SerializeReferrers(&recovered_referral_list); | 369 predictor.SerializeReferrers(&recovered_referral_list); |
370 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 370 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
371 double rate; | 371 double rate; |
372 EXPECT_TRUE(GetDataFromSerialization( | 372 EXPECT_TRUE(GetDataFromSerialization( |
373 motivation_url, subresource_url, recovered_referral_list, &rate)); | 373 motivation_url, subresource_url, recovered_referral_list, &rate)); |
374 EXPECT_EQ(rate, kUseRate); | 374 EXPECT_EQ(rate, kUseRate); |
375 | 375 |
376 predictor.Shutdown(); | 376 predictor.Shutdown(); |
377 } | 377 } |
378 | 378 |
| 379 // Check that GetHtmlReferrerLists() doesn't crash when given duplicated |
| 380 // domains for referring URL, and that it sorts the results in the |
| 381 // correct order. |
| 382 TEST_F(PredictorTest, GetHtmlReferrerLists) { |
| 383 Predictor predictor(true); |
| 384 predictor.SetHostResolver(host_resolver_.get()); |
| 385 const double kUseRate = 23.4; |
| 386 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); |
| 387 |
| 388 AddToSerializedList( |
| 389 GURL("http://d.google.com/x1"), |
| 390 GURL("http://foo.com/"), |
| 391 kUseRate, referral_list.get()); |
| 392 |
| 393 // Duplicated hostname (d.google.com). This should not cause any crashes |
| 394 // (i.e. crbug.com/116345) |
| 395 AddToSerializedList( |
| 396 GURL("http://d.google.com/x2"), |
| 397 GURL("http://foo.com/"), |
| 398 kUseRate, referral_list.get()); |
| 399 |
| 400 AddToSerializedList( |
| 401 GURL("http://a.yahoo.com/y"), |
| 402 GURL("http://foo1.com/"), |
| 403 kUseRate, referral_list.get()); |
| 404 |
| 405 AddToSerializedList( |
| 406 GURL("http://b.google.com/x3"), |
| 407 GURL("http://foo2.com/"), |
| 408 kUseRate, referral_list.get()); |
| 409 |
| 410 AddToSerializedList( |
| 411 GURL("http://d.yahoo.com/x5"), |
| 412 GURL("http://i.like.turtles/"), |
| 413 kUseRate, referral_list.get()); |
| 414 |
| 415 AddToSerializedList( |
| 416 GURL("http://c.yahoo.com/x4"), |
| 417 GURL("http://foo3.com/"), |
| 418 kUseRate, referral_list.get()); |
| 419 |
| 420 predictor.DeserializeReferrers(*referral_list.get()); |
| 421 |
| 422 std::string html; |
| 423 predictor.GetHtmlReferrerLists(&html); |
| 424 |
| 425 // The lexicographic sorting of hostnames would be: |
| 426 // a.yahoo.com |
| 427 // b.google.com |
| 428 // c.yahoo.com |
| 429 // d.google.com |
| 430 // d.yahoo.com |
| 431 // |
| 432 // However we expect to sort them by domain in the output: |
| 433 // b.google.com |
| 434 // d.google.com |
| 435 // a.yahoo.com |
| 436 // c.yahoo.com |
| 437 // d.yahoo.com |
| 438 |
| 439 size_t pos[] = { |
| 440 html.find("<td rowspan=1>http://b.google.com/x3"), |
| 441 html.find("<td rowspan=1>http://d.google.com/x1"), |
| 442 html.find("<td rowspan=1>http://d.google.com/x2"), |
| 443 html.find("<td rowspan=1>http://a.yahoo.com/y"), |
| 444 html.find("<td rowspan=1>http://c.yahoo.com/x4"), |
| 445 html.find("<td rowspan=1>http://d.yahoo.com/x5"), |
| 446 }; |
| 447 |
| 448 // Make sure things appeared in the expected order. |
| 449 for (size_t i = 1; i < arraysize(pos); ++i) { |
| 450 EXPECT_LT(pos[i - 1], pos[i]) << "Mismatch for pos[" << i << "]"; |
| 451 } |
| 452 |
| 453 predictor.Shutdown(); |
| 454 } |
| 455 |
379 // Verify that two floats are within 1% of each other in value. | 456 // Verify that two floats are within 1% of each other in value. |
380 #define EXPECT_SIMILAR(a, b) do { \ | 457 #define EXPECT_SIMILAR(a, b) do { \ |
381 double espilon_ratio = 1.01; \ | 458 double espilon_ratio = 1.01; \ |
382 if ((a) < 0.) \ | 459 if ((a) < 0.) \ |
383 espilon_ratio = 1 / espilon_ratio; \ | 460 espilon_ratio = 1 / espilon_ratio; \ |
384 EXPECT_LT(a, espilon_ratio * (b)); \ | 461 EXPECT_LT(a, espilon_ratio * (b)); \ |
385 EXPECT_GT((a) * espilon_ratio, b); \ | 462 EXPECT_GT((a) * espilon_ratio, b); \ |
386 } while (0) | 463 } while (0) |
387 | 464 |
388 | 465 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 EXPECT_EQ(2U, referral_list.GetSize()); | 668 EXPECT_EQ(2U, referral_list.GetSize()); |
592 | 669 |
593 predictor.DiscardAllResults(); | 670 predictor.DiscardAllResults(); |
594 predictor.SerializeReferrers(&referral_list); | 671 predictor.SerializeReferrers(&referral_list); |
595 EXPECT_EQ(1U, referral_list.GetSize()); | 672 EXPECT_EQ(1U, referral_list.GetSize()); |
596 | 673 |
597 predictor.Shutdown(); | 674 predictor.Shutdown(); |
598 } | 675 } |
599 | 676 |
600 } // namespace chrome_browser_net | 677 } // namespace chrome_browser_net |
OLD | NEW |