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 <abstract/aacore.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 "unstable/nsISHistoryInternal.h"
34 #include "unstable/nsIWebNavigation.h"
35 #include "unstable/nsISHEntry.h"
36 #include "unstable/nsISHContainer.h"
37 #include <abstract/cxxunit/nsTestUtils.h>
38 #include <abstract/cxxunit/nsITest.h>
39 #include <abstract/cxxunit/nsITestRunner.h>
41 /* Project includes */
42 #include <abstract/storage/aaISession.h>
43 #include <abstract/view/aaISHistory.h>
44 #include <abstract/view/aaIPageView.h>
45 #include "aaSessionHistory.h"
46 #include "aaPageView.h"
48 #define AA_VIEW_TEST_CID \
49 {0x39010bed, 0x1087, 0x4363, {0x99, 0x19, 0xf9, 0xe7, 0x1a, 0x74, 0xce, 0x9b}}
50 #define AA_VIEW_TEST_CONTRACT_ID "@aasii.org/view/unit;1"
52 class aaViewTest
: public nsITest
{
55 virtual ~aaViewTest() {;}
59 nsresult
testSH(nsITestRunner
*aTestRunner
);
60 nsresult
testSHE(nsITestRunner
*aTestRunner
);
61 nsresult
testSHChoice(nsITestRunner
*aTestRunner
);
64 /* Module and Factory code */
65 NS_GENERIC_FACTORY_CONSTRUCTOR(aaViewTest
)
67 NS_DECL_CLASSINFO(aaSessionHistory
)
69 static const nsModuleComponentInfo kComponents
[] =
72 "View Module Unit Test",
74 AA_VIEW_TEST_CONTRACT_ID
,
78 NS_IMPL_NSGETMODULE(aaviewt
, kComponents
)
80 NS_IMPL_ISUPPORTS1(aaViewTest
, nsITest
);
84 aaViewTest::Test(nsITestRunner
*aTestRunner
)
86 nsITestRunner
*cxxUnitTestRunner
= aTestRunner
;
87 NS_TEST_ASSERT_OK( testSH( aTestRunner
) );
88 NS_TEST_ASSERT_OK( testSHE( aTestRunner
) );
89 NS_TEST_ASSERT_OK( testSHChoice( aTestRunner
) );
95 aaViewTest::testSH(nsITestRunner
*aTestRunner
)
98 NS_TEST_BEGIN( aTestRunner
);
100 nsCOMPtr
<nsISHistory
> sh( do_CreateInstance(
101 AA_SESSIONHISTORY_CONTRACT_ID
) );
102 NS_TEST_ASSERT_MSG(sh
, "aaSessionHistory intance creation");
104 nsCOMPtr
<nsIWebNavigation
> wn
= do_QueryInterface(sh
);
105 NS_TEST_ASSERT_MSG(wn
, "aaSessionHistory WebNavigation interface");
108 rv
= sh
->GetCount( &count
);
109 NS_TEST_ASSERT_OK(rv
);
110 NS_TEST_ASSERT_MSG(! count
, "aaSessionHistory wrong item count");
112 nsCOMPtr
<nsISHistoryInternal
> shi
= do_QueryInterface(sh
);
113 NS_TEST_ASSERT_MSG(shi
, "aaSessionHistory SHInternal interface");
114 nsCOMPtr
<nsISHEntry
> entry(do_CreateInstance(NS_SHENTRY_CONTRACTID
));
115 NS_ENSURE_TRUE(entry
, NS_ERROR_UNEXPECTED
);
117 rv
= shi
->AddEntry(entry
, PR_TRUE
);
118 NS_TEST_ASSERT_OK(rv
);
120 rv
= sh
->GetCount( &count
);
121 NS_TEST_ASSERT_OK(rv
);
122 NS_TEST_ASSERT_MSG(count
== 1, "aaSessionHistory wrong item count");
125 rv
= sh
->GetIndex( &index
);
126 NS_TEST_ASSERT_OK(rv
);
127 NS_TEST_ASSERT_MSG(count
== 1, "aaSessionHistory wrong index");
129 nsCOMPtr
<aaISHistory
> ash
= do_QueryInterface(sh
);
130 PRBool choosing
= PR_TRUE
;
131 rv
= ash
->GetChoosing( &choosing
);
132 NS_TEST_ASSERT_OK( rv
);
133 NS_TEST_ASSERT_MSG(! choosing
, "ash should not be in choice mode");
139 aaViewTest::testSHE(nsITestRunner
*aTestRunner
)
142 NS_TEST_BEGIN( aTestRunner
);
144 nsCOMPtr
<nsISHEntry
> wrapped( do_CreateInstance(
145 NS_SHENTRY_CONTRACTID
, &rv
) );
146 NS_ENSURE_TRUE(wrapped
, rv
);
148 nsCOMPtr
<nsISHEntry
> she( do_CreateInstance(
149 AA_PAGEVIEW_CONTRACT_ID
, wrapped
));
150 NS_TEST_ASSERT_MSG(she
, "[aaPageView] instance creation");
152 nsCOMPtr
<nsISHContainer
> shc
= do_QueryInterface(she
);
153 NS_TEST_ASSERT_MSG(shc
, "[aaPageView] SHContainer interface");
155 nsCOMPtr
<nsIHistoryEntry
> he
= do_QueryInterface(she
);
156 NS_TEST_ASSERT_MSG(he
, "[aaPageView] HistoryEntry interface");
158 nsCOMPtr
<aaIPageView
> pv
= do_QueryInterface(she
);
159 NS_TEST_ASSERT_MSG(pv
, "[aaPageView] PageView interface");
160 NS_ENSURE_TRUE(pv
, rv
);
162 rv
= pv
->SetChoosing(PR_TRUE
);
163 NS_TEST_ASSERT_OK(rv
);
164 rv
= pv
->SetChoosing(PR_TRUE
);
165 NS_TEST_ASSERT_MSG(NS_FAILED(rv
),
166 "[aaPageView] 'choosing' must be write-once");
172 aaViewTest::testSHChoice(nsITestRunner
*aTestRunner
)
175 NS_TEST_BEGIN( aTestRunner
);
177 /* Prepare test situation */
178 nsCOMPtr
<aaISHistory
> ash
= do_CreateInstance(AA_SESSIONHISTORY_CONTRACT_ID
,
180 NS_ENSURE_TRUE(ash
, rv
);
182 nsCOMPtr
<nsISHistory
> sh
= do_QueryInterface(ash
, &rv
);
183 NS_ENSURE_TRUE(sh
, rv
);
185 nsCOMPtr
<nsISHistoryInternal
> shi
= do_QueryInterface(ash
, &rv
);
186 NS_ENSURE_TRUE(shi
, rv
);
188 nsCOMPtr
<nsISHEntry
> wrapped
= do_CreateInstance(NS_SHENTRY_CONTRACTID
, &rv
);
189 NS_ENSURE_TRUE(wrapped
, rv
);
191 rv
= shi
->AddEntry(wrapped
, PR_TRUE
);
192 NS_ENSURE_SUCCESS(rv
, rv
);
195 rv
= sh
->GetIndex(&index
);
196 NS_ENSURE_SUCCESS(rv
, rv
);
198 nsCOMPtr
<nsIHistoryEntry
> stored
;
199 rv
= sh
->GetEntryAtIndex(index
, PR_FALSE
, getter_AddRefs( stored
));
200 NS_ENSURE_SUCCESS(rv
, rv
);
202 nsCOMPtr
<aaIPageView
> p1
= do_QueryInterface(stored
, &rv
);
203 NS_ENSURE_TRUE(p1
, rv
);
205 wrapped
= do_CreateInstance(NS_SHENTRY_CONTRACTID
, &rv
);
206 NS_ENSURE_TRUE(wrapped
, rv
);
208 rv
= ash
->BeginQuery();
209 NS_ENSURE_SUCCESS(rv
, rv
);
211 rv
= shi
->AddEntry(wrapped
, PR_TRUE
);
212 NS_ENSURE_SUCCESS(rv
, rv
);
214 /* Test PR_TRUE trip to destination */
215 nsCOMPtr
<nsISupportsPRBool
> test1
= do_CreateInstance(
216 NS_SUPPORTS_PRBOOL_CONTRACTID
, &rv
);
217 NS_ENSURE_TRUE(test1
, rv
);
219 rv
= test1
->SetData(PR_TRUE
);
220 NS_ENSURE_SUCCESS(rv
, rv
);
222 rv
= ash
->SetChoice(test1
);
223 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv
), "[session history] setting choice");
224 NS_ENSURE_SUCCESS(rv
, rv
);
226 nsCOMPtr
<nsISupports
> s
;
227 rv
= p1
->GetQueryChoice(getter_AddRefs( s
));
228 NS_TEST_ASSERT_MSG(s
, "[session history] reading choice from entry");
229 NS_ENSURE_TRUE(s
, rv
);
231 nsCOMPtr
<nsISupportsPRBool
> test2
= do_QueryInterface(s
, &rv
);
232 NS_ENSURE_TRUE(test2
, rv
);
233 PRBool b
= PR_FALSE
; test2
->GetData(&b
);
234 NS_TEST_ASSERT_MSG(b
, "[session history] choice value is wrong");
236 /* Test PR_TRUE round-trip */
237 rv
= ash
->GetChoice(getter_AddRefs( s
));
238 NS_TEST_ASSERT_MSG(s
, "[session history] reading choice");
239 NS_ENSURE_TRUE(s
, rv
);
241 test2
= do_QueryInterface(s
, &rv
);
242 NS_ENSURE_TRUE(test2
, rv
);
243 b
= PR_FALSE
; test2
->GetData(&b
);
244 NS_TEST_ASSERT_MSG(b
, "[session history] choice value is wrong");
246 /* Test 'choosing' state at destination */
247 rv
= sh
->GetEntryAtIndex(index
+ 1, PR_FALSE
, getter_AddRefs( stored
));
248 NS_ENSURE_SUCCESS(rv
, rv
);
250 nsCOMPtr
<aaIPageView
> p2
= do_QueryInterface(stored
, &rv
);
251 NS_ENSURE_TRUE(p2
, rv
);
254 rv
= p2
->GetChoosing(&b
);
255 NS_TEST_ASSERT_OK(rv
);
256 NS_TEST_ASSERT_MSG(b
, "[session history] entry not in 'choosing' mode");
258 /* Test multiple 'session.choosing' */
259 rv
= ash
->GetChoosing(&b
);
260 NS_TEST_ASSERT_OK(rv
);
261 NS_TEST_ASSERT_MSG(b
, "[session history] not in 'choosing' mode");
263 rv
= ash
->GetChoosing(&b
);
264 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv
),
265 "[session history] re-reading 'choosing'");