1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
4 * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include "xpcom-config.h"
24 #include "nsIGenericFactory.h"
25 #include "nsComponentManagerUtils.h"
26 #include "nsISHistory.h"
28 #include "nsIClassInfoImpl.h"
29 #include "nsXPCOMCID.h"
30 #include "nsISupportsPrimitives.h"
33 #include "nsISHistoryInternal.h"
34 #include "nsIWebNavigation.h"
35 #include "nsISHEntry.h"
36 #include "nsISHContainer.h"
37 #include "nsTestUtils.h"
39 #include "nsITestRunner.h"
41 /* Project includes */
42 #include "aaSessionHistory.h"
43 #include "aaPageView.h"
45 #define AA_VIEW_TEST_CID \
46 {0x39010bed, 0x1087, 0x4363, {0x99, 0x19, 0xf9, 0xe7, 0x1a, 0x74, 0xce, 0x9b}}
47 #define AA_VIEW_TEST_CONTRACT_ID "@aasii.org/view/unit;1"
49 class aaViewTest
: public nsITest
{
52 virtual ~aaViewTest() {;}
56 nsresult
testSH(nsITestRunner
*aTestRunner
);
57 nsresult
testSHE(nsITestRunner
*aTestRunner
);
58 nsresult
testSHChoice(nsITestRunner
*aTestRunner
);
61 /* Module and Factory code */
62 NS_GENERIC_FACTORY_CONSTRUCTOR(aaViewTest
)
64 NS_DECL_CLASSINFO(aaSessionHistory
)
66 static const nsModuleComponentInfo kComponents
[] =
69 "View Module Unit Test",
71 AA_VIEW_TEST_CONTRACT_ID
,
75 NS_IMPL_NSGETMODULE(aaviewt
, kComponents
)
77 NS_IMPL_ISUPPORTS1(aaViewTest
, nsITest
)
81 aaViewTest::Test(nsITestRunner
*aTestRunner
)
83 nsITestRunner
*cxxUnitTestRunner
= aTestRunner
;
84 NS_TEST_ASSERT_OK( testSH( aTestRunner
) );
85 NS_TEST_ASSERT_OK( testSHE( aTestRunner
) );
86 NS_TEST_ASSERT_OK( testSHChoice( aTestRunner
) );
92 aaViewTest::testSH(nsITestRunner
*aTestRunner
)
95 NS_TEST_BEGIN( aTestRunner
);
97 nsCOMPtr
<nsISHistory
> sh( do_CreateInstance(
98 AA_SESSIONHISTORY_CONTRACT_ID
) );
99 NS_TEST_ASSERT_MSG(sh
, "aaSessionHistory intance creation");
101 nsCOMPtr
<nsIWebNavigation
> wn
= do_QueryInterface(sh
);
102 NS_TEST_ASSERT_MSG(wn
, "aaSessionHistory WebNavigation interface");
105 rv
= sh
->GetCount( &count
);
106 NS_TEST_ASSERT_OK(rv
);
107 NS_TEST_ASSERT_MSG(! count
, "aaSessionHistory wrong item count");
109 nsCOMPtr
<nsISHistoryInternal
> shi
= do_QueryInterface(sh
);
110 NS_TEST_ASSERT_MSG(shi
, "aaSessionHistory SHInternal interface");
111 nsCOMPtr
<nsISHEntry
> entry(do_CreateInstance(NS_SHENTRY_CONTRACTID
));
112 NS_ENSURE_TRUE(entry
, NS_ERROR_UNEXPECTED
);
114 rv
= shi
->AddEntry(entry
, PR_TRUE
);
115 NS_TEST_ASSERT_OK(rv
);
117 rv
= sh
->GetCount( &count
);
118 NS_TEST_ASSERT_OK(rv
);
119 NS_TEST_ASSERT_MSG(count
== 1, "aaSessionHistory wrong item count");
122 rv
= sh
->GetIndex( &index
);
123 NS_TEST_ASSERT_OK(rv
);
124 NS_TEST_ASSERT_MSG(count
== 1, "aaSessionHistory wrong index");
126 nsCOMPtr
<aaISHistory
> ash
= do_QueryInterface(sh
);
127 PRBool choosing
= PR_TRUE
;
128 rv
= ash
->GetChoosing( &choosing
);
129 NS_TEST_ASSERT_OK( rv
);
130 NS_TEST_ASSERT_MSG(! choosing
, "ash should not be in choice mode");
136 aaViewTest::testSHE(nsITestRunner
*aTestRunner
)
139 NS_TEST_BEGIN( aTestRunner
);
141 nsCOMPtr
<nsISHEntry
> wrapped( do_CreateInstance(
142 NS_SHENTRY_CONTRACTID
, &rv
) );
143 NS_ENSURE_TRUE(wrapped
, rv
);
145 nsCOMPtr
<nsISHEntry
> she( do_CreateInstance(
146 AA_PAGEVIEW_CONTRACT_ID
, wrapped
));
147 NS_TEST_ASSERT_MSG(she
, "[aaPageView] instance creation");
149 nsCOMPtr
<nsISHContainer
> shc
= do_QueryInterface(she
);
150 NS_TEST_ASSERT_MSG(shc
, "[aaPageView] SHContainer interface");
152 nsCOMPtr
<nsIHistoryEntry
> he
= do_QueryInterface(she
);
153 NS_TEST_ASSERT_MSG(he
, "[aaPageView] HistoryEntry interface");
155 nsCOMPtr
<aaIPageView
> pv
= do_QueryInterface(she
);
156 NS_TEST_ASSERT_MSG(pv
, "[aaPageView] PageView interface");
157 NS_ENSURE_TRUE(pv
, rv
);
159 rv
= pv
->SetChoosing(PR_TRUE
);
160 NS_TEST_ASSERT_OK(rv
);
161 rv
= pv
->SetChoosing(PR_TRUE
);
162 NS_TEST_ASSERT_MSG(NS_FAILED(rv
),
163 "[aaPageView] 'choosing' must be write-once");
169 aaViewTest::testSHChoice(nsITestRunner
*aTestRunner
)
172 NS_TEST_BEGIN( aTestRunner
);
174 /* Prepare test situation */
175 nsCOMPtr
<aaISHistory
> ash
= do_CreateInstance(AA_SESSIONHISTORY_CONTRACT_ID
,
177 NS_ENSURE_TRUE(ash
, rv
);
179 nsCOMPtr
<nsISHistory
> sh
= do_QueryInterface(ash
, &rv
);
180 NS_ENSURE_TRUE(sh
, rv
);
182 nsCOMPtr
<nsISHistoryInternal
> shi
= do_QueryInterface(ash
, &rv
);
183 NS_ENSURE_TRUE(shi
, rv
);
185 nsCOMPtr
<nsISHEntry
> wrapped
= do_CreateInstance(NS_SHENTRY_CONTRACTID
, &rv
);
186 NS_ENSURE_TRUE(wrapped
, rv
);
188 rv
= shi
->AddEntry(wrapped
, PR_TRUE
);
189 NS_ENSURE_SUCCESS(rv
, rv
);
192 rv
= sh
->GetIndex(&index
);
193 NS_ENSURE_SUCCESS(rv
, rv
);
195 nsCOMPtr
<nsIHistoryEntry
> stored
;
196 rv
= sh
->GetEntryAtIndex(index
, PR_FALSE
, getter_AddRefs( stored
));
197 NS_ENSURE_SUCCESS(rv
, rv
);
199 nsCOMPtr
<aaIPageView
> p1
= do_QueryInterface(stored
, &rv
);
200 NS_ENSURE_TRUE(p1
, rv
);
202 wrapped
= do_CreateInstance(NS_SHENTRY_CONTRACTID
, &rv
);
203 NS_ENSURE_TRUE(wrapped
, rv
);
205 rv
= ash
->BeginQuery();
206 NS_ENSURE_SUCCESS(rv
, rv
);
208 rv
= shi
->AddEntry(wrapped
, PR_TRUE
);
209 NS_ENSURE_SUCCESS(rv
, rv
);
211 /* Test PR_TRUE trip to destination */
212 nsCOMPtr
<nsISupportsPRBool
> test1
= do_CreateInstance(
213 NS_SUPPORTS_PRBOOL_CONTRACTID
, &rv
);
214 NS_ENSURE_TRUE(test1
, rv
);
216 rv
= test1
->SetData(PR_TRUE
);
217 NS_ENSURE_SUCCESS(rv
, rv
);
219 rv
= ash
->SetChoice(test1
);
220 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv
), "[session history] setting choice");
221 NS_ENSURE_SUCCESS(rv
, rv
);
223 nsCOMPtr
<nsISupports
> s
;
224 rv
= p1
->GetQueryChoice(getter_AddRefs( s
));
225 NS_TEST_ASSERT_MSG(s
, "[session history] reading choice from entry");
226 NS_ENSURE_TRUE(s
, rv
);
228 nsCOMPtr
<nsISupportsPRBool
> test2
= do_QueryInterface(s
, &rv
);
229 NS_ENSURE_TRUE(test2
, rv
);
230 PRBool b
= PR_FALSE
; test2
->GetData(&b
);
231 NS_TEST_ASSERT_MSG(b
, "[session history] choice value is wrong");
233 /* Test PR_TRUE round-trip */
234 rv
= ash
->GetChoice(getter_AddRefs( s
));
235 NS_TEST_ASSERT_MSG(s
, "[session history] reading choice");
236 NS_ENSURE_TRUE(s
, rv
);
238 test2
= do_QueryInterface(s
, &rv
);
239 NS_ENSURE_TRUE(test2
, rv
);
240 b
= PR_FALSE
; test2
->GetData(&b
);
241 NS_TEST_ASSERT_MSG(b
, "[session history] choice value is wrong");
243 /* Test 'choosing' state at destination */
244 rv
= sh
->GetEntryAtIndex(index
+ 1, PR_FALSE
, getter_AddRefs( stored
));
245 NS_ENSURE_SUCCESS(rv
, rv
);
247 nsCOMPtr
<aaIPageView
> p2
= do_QueryInterface(stored
, &rv
);
248 NS_ENSURE_TRUE(p2
, rv
);
251 rv
= p2
->GetChoosing(&b
);
252 NS_TEST_ASSERT_OK(rv
);
253 NS_TEST_ASSERT_MSG(b
, "[session history] entry not in 'choosing' mode");
255 /* Test multiple 'session.choosing' */
256 rv
= ash
->GetChoosing(&b
);
257 NS_TEST_ASSERT_OK(rv
);
258 NS_TEST_ASSERT_MSG(b
, "[session history] not in 'choosing' mode");
260 rv
= ash
->GetChoosing(&b
);
261 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv
),
262 "[session history] re-reading 'choosing'");