1 // Copyright (c) 2011 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 "base/values.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "chrome/browser/prerender/prerender_history.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 bool ListEntryMatches(ListValue
* list
,
16 const char* expected_url
,
17 FinalStatus expected_final_status
,
18 Origin expected_origin
,
19 const std::string
& expected_end_time
) {
20 if (index
>= list
->GetSize())
22 DictionaryValue
* dict
= NULL
;
23 if (!list
->GetDictionary(index
, &dict
))
25 if (dict
->size() != 4u)
28 if (!dict
->GetString("url", &url
))
30 if (url
!= expected_url
)
32 std::string final_status
;
33 if (!dict
->GetString("final_status", &final_status
))
35 if (final_status
!= NameFromFinalStatus(expected_final_status
))
38 if (!dict
->GetString("origin", &origin
))
40 if (origin
!= NameFromOrigin(expected_origin
))
43 if (!dict
->GetString("end_time", &end_time
))
45 if (end_time
!= expected_end_time
)
50 TEST(PrerenderHistoryTest
, GetAsValue
) {
51 scoped_ptr
<Value
> entry_value
;
52 ListValue
* entry_list
= NULL
;
54 // Create a history with only 2 values.
55 PrerenderHistory
history(2);
57 // Make sure an empty list exists when retrieving as value.
58 entry_value
.reset(history
.GetEntriesAsValue());
59 ASSERT_TRUE(entry_value
.get() != NULL
);
60 ASSERT_TRUE(entry_value
->GetAsList(&entry_list
));
61 EXPECT_TRUE(entry_list
->empty());
63 // Base time used for all events. Each event is given a time 1 millisecond
64 // after that of the previous one.
65 base::Time epoch_start
= base::Time::UnixEpoch();
67 // Add a single entry and make sure that it matches up.
68 const char* const kFirstUrl
= "http://www.alpha.com/";
69 const FinalStatus kFirstFinalStatus
= FINAL_STATUS_USED
;
70 const Origin kFirstOrigin
= ORIGIN_LINK_REL_PRERENDER
;
71 PrerenderHistory::Entry
entry_first(
72 GURL(kFirstUrl
), kFirstFinalStatus
, kFirstOrigin
, epoch_start
);
73 history
.AddEntry(entry_first
);
74 entry_value
.reset(history
.GetEntriesAsValue());
75 ASSERT_TRUE(entry_value
.get() != NULL
);
76 ASSERT_TRUE(entry_value
->GetAsList(&entry_list
));
77 EXPECT_EQ(1u, entry_list
->GetSize());
78 EXPECT_TRUE(ListEntryMatches(entry_list
, 0u, kFirstUrl
, kFirstFinalStatus
,
81 // Add a second entry and make sure both first and second appear.
82 const char* const kSecondUrl
= "http://www.beta.com/";
83 const FinalStatus kSecondFinalStatus
= FINAL_STATUS_INVALID_HTTP_METHOD
;
84 const Origin kSecondOrigin
= ORIGIN_OMNIBOX_EXACT
;
85 PrerenderHistory::Entry
entry_second(
86 GURL(kSecondUrl
), kSecondFinalStatus
, kSecondOrigin
,
87 epoch_start
+ base::TimeDelta::FromMilliseconds(1));
88 history
.AddEntry(entry_second
);
89 entry_value
.reset(history
.GetEntriesAsValue());
90 ASSERT_TRUE(entry_value
.get() != NULL
);
91 ASSERT_TRUE(entry_value
->GetAsList(&entry_list
));
92 EXPECT_EQ(2u, entry_list
->GetSize());
93 EXPECT_TRUE(ListEntryMatches(entry_list
, 0u, kSecondUrl
, kSecondFinalStatus
,
95 EXPECT_TRUE(ListEntryMatches(entry_list
, 1u, kFirstUrl
, kFirstFinalStatus
,
98 // Add a third entry and make sure that the first one drops off.
99 const char* const kThirdUrl
= "http://www.gamma.com/";
100 const FinalStatus kThirdFinalStatus
= FINAL_STATUS_AUTH_NEEDED
;
101 const Origin kThirdOrigin
= ORIGIN_LINK_REL_PRERENDER
;
102 PrerenderHistory::Entry
entry_third(
103 GURL(kThirdUrl
), kThirdFinalStatus
, kThirdOrigin
,
104 epoch_start
+ base::TimeDelta::FromMilliseconds(2));
105 history
.AddEntry(entry_third
);
106 entry_value
.reset(history
.GetEntriesAsValue());
107 ASSERT_TRUE(entry_value
.get() != NULL
);
108 ASSERT_TRUE(entry_value
->GetAsList(&entry_list
));
109 EXPECT_EQ(2u, entry_list
->GetSize());
110 EXPECT_TRUE(ListEntryMatches(entry_list
, 0u, kThirdUrl
, kThirdFinalStatus
,
112 EXPECT_TRUE(ListEntryMatches(entry_list
, 1u, kSecondUrl
, kSecondFinalStatus
,
113 kSecondOrigin
, "1"));
115 // Make sure clearing history acts as expected.
117 entry_value
.reset(history
.GetEntriesAsValue());
118 ASSERT_TRUE(entry_value
.get() != NULL
);
119 ASSERT_TRUE(entry_value
->GetAsList(&entry_list
));
120 EXPECT_TRUE(entry_list
->empty());
125 } // namespace prerender