frames_test.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. // $Id: frames_test.php 1787 2008-04-26 20:35:39Z pp11 $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../tag.php');
  5. require_once(dirname(__FILE__) . '/../page.php');
  6. require_once(dirname(__FILE__) . '/../frames.php');
  7. Mock::generate('SimplePage');
  8. Mock::generate('SimpleForm');
  9. class TestOfFrameset extends UnitTestCase {
  10. function testTitleReadFromFramesetPage() {
  11. $page = new MockSimplePage();
  12. $page->setReturnValue('getTitle', 'This page');
  13. $frameset = new SimpleFrameset($page);
  14. $this->assertEqual($frameset->getTitle(), 'This page');
  15. }
  16. function TestHeadersReadFromFramesetByDefault() {
  17. $page = new MockSimplePage();
  18. $page->setReturnValue('getHeaders', 'Header: content');
  19. $page->setReturnValue('getMimeType', 'text/xml');
  20. $page->setReturnValue('getResponseCode', 401);
  21. $page->setReturnValue('getTransportError', 'Could not parse headers');
  22. $page->setReturnValue('getAuthentication', 'Basic');
  23. $page->setReturnValue('getRealm', 'Safe place');
  24. $frameset = new SimpleFrameset($page);
  25. $this->assertIdentical($frameset->getHeaders(), 'Header: content');
  26. $this->assertIdentical($frameset->getMimeType(), 'text/xml');
  27. $this->assertIdentical($frameset->getResponseCode(), 401);
  28. $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
  29. $this->assertIdentical($frameset->getAuthentication(), 'Basic');
  30. $this->assertIdentical($frameset->getRealm(), 'Safe place');
  31. }
  32. function testEmptyFramesetHasNoContent() {
  33. $page = new MockSimplePage();
  34. $page->setReturnValue('getRaw', 'This content');
  35. $frameset = new SimpleFrameset($page);
  36. $this->assertEqual($frameset->getRaw(), '');
  37. }
  38. function testRawContentIsFromOnlyFrame() {
  39. $page = new MockSimplePage();
  40. $page->expectNever('getRaw');
  41. $frame = new MockSimplePage();
  42. $frame->setReturnValue('getRaw', 'Stuff');
  43. $frameset = new SimpleFrameset($page);
  44. $frameset->addFrame($frame);
  45. $this->assertEqual($frameset->getRaw(), 'Stuff');
  46. }
  47. function testRawContentIsFromAllFrames() {
  48. $page = new MockSimplePage();
  49. $page->expectNever('getRaw');
  50. $frame1 = new MockSimplePage();
  51. $frame1->setReturnValue('getRaw', 'Stuff1');
  52. $frame2 = new MockSimplePage();
  53. $frame2->setReturnValue('getRaw', 'Stuff2');
  54. $frameset = new SimpleFrameset($page);
  55. $frameset->addFrame($frame1);
  56. $frameset->addFrame($frame2);
  57. $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
  58. }
  59. function testTextContentIsFromOnlyFrame() {
  60. $page = new MockSimplePage();
  61. $page->expectNever('getText');
  62. $frame = new MockSimplePage();
  63. $frame->setReturnValue('getText', 'Stuff');
  64. $frameset = new SimpleFrameset($page);
  65. $frameset->addFrame($frame);
  66. $this->assertEqual($frameset->getText(), 'Stuff');
  67. }
  68. function testTextContentIsFromAllFrames() {
  69. $page = new MockSimplePage();
  70. $page->expectNever('getText');
  71. $frame1 = new MockSimplePage();
  72. $frame1->setReturnValue('getText', 'Stuff1');
  73. $frame2 = new MockSimplePage();
  74. $frame2->setReturnValue('getText', 'Stuff2');
  75. $frameset = new SimpleFrameset($page);
  76. $frameset->addFrame($frame1);
  77. $frameset->addFrame($frame2);
  78. $this->assertEqual($frameset->getText(), 'Stuff1 Stuff2');
  79. }
  80. function testFieldFoundIsFirstInFramelist() {
  81. $frame1 = new MockSimplePage();
  82. $frame1->setReturnValue('getField', null);
  83. $frame1->expectOnce('getField', array(new SimpleByName('a')));
  84. $frame2 = new MockSimplePage();
  85. $frame2->setReturnValue('getField', 'A');
  86. $frame2->expectOnce('getField', array(new SimpleByName('a')));
  87. $frame3 = new MockSimplePage();
  88. $frame3->expectNever('getField');
  89. $page = new MockSimplePage();
  90. $frameset = new SimpleFrameset($page);
  91. $frameset->addFrame($frame1);
  92. $frameset->addFrame($frame2);
  93. $frameset->addFrame($frame3);
  94. $this->assertIdentical($frameset->getField(new SimpleByName('a')), 'A');
  95. }
  96. function testFrameReplacementByIndex() {
  97. $page = new MockSimplePage();
  98. $page->expectNever('getRaw');
  99. $frame1 = new MockSimplePage();
  100. $frame1->setReturnValue('getRaw', 'Stuff1');
  101. $frame2 = new MockSimplePage();
  102. $frame2->setReturnValue('getRaw', 'Stuff2');
  103. $frameset = new SimpleFrameset($page);
  104. $frameset->addFrame($frame1);
  105. $frameset->setFrame(array(1), $frame2);
  106. $this->assertEqual($frameset->getRaw(), 'Stuff2');
  107. }
  108. function testFrameReplacementByName() {
  109. $page = new MockSimplePage();
  110. $page->expectNever('getRaw');
  111. $frame1 = new MockSimplePage();
  112. $frame1->setReturnValue('getRaw', 'Stuff1');
  113. $frame2 = new MockSimplePage();
  114. $frame2->setReturnValue('getRaw', 'Stuff2');
  115. $frameset = new SimpleFrameset($page);
  116. $frameset->addFrame($frame1, 'a');
  117. $frameset->setFrame(array('a'), $frame2);
  118. $this->assertEqual($frameset->getRaw(), 'Stuff2');
  119. }
  120. }
  121. class TestOfFrameNavigation extends UnitTestCase {
  122. function testStartsWithoutFrameFocus() {
  123. $page = new MockSimplePage();
  124. $frameset = new SimpleFrameset($page);
  125. $frameset->addFrame(new MockSimplePage());
  126. $this->assertFalse($frameset->getFrameFocus());
  127. }
  128. function testCanFocusOnSingleFrame() {
  129. $page = new MockSimplePage();
  130. $page->expectNever('getRaw');
  131. $frame = new MockSimplePage();
  132. $frame->setReturnValue('getFrameFocus', array());
  133. $frame->setReturnValue('getRaw', 'Stuff');
  134. $frameset = new SimpleFrameset($page);
  135. $frameset->addFrame($frame);
  136. $this->assertFalse($frameset->setFrameFocusByIndex(0));
  137. $this->assertTrue($frameset->setFrameFocusByIndex(1));
  138. $this->assertEqual($frameset->getRaw(), 'Stuff');
  139. $this->assertFalse($frameset->setFrameFocusByIndex(2));
  140. $this->assertIdentical($frameset->getFrameFocus(), array(1));
  141. }
  142. function testContentComesFromFrameInFocus() {
  143. $page = new MockSimplePage();
  144. $frame1 = new MockSimplePage();
  145. $frame1->setReturnValue('getRaw', 'Stuff1');
  146. $frame1->setReturnValue('getFrameFocus', array());
  147. $frame2 = new MockSimplePage();
  148. $frame2->setReturnValue('getRaw', 'Stuff2');
  149. $frame2->setReturnValue('getFrameFocus', array());
  150. $frameset = new SimpleFrameset($page);
  151. $frameset->addFrame($frame1);
  152. $frameset->addFrame($frame2);
  153. $this->assertTrue($frameset->setFrameFocusByIndex(1));
  154. $this->assertEqual($frameset->getFrameFocus(), array(1));
  155. $this->assertEqual($frameset->getRaw(), 'Stuff1');
  156. $this->assertTrue($frameset->setFrameFocusByIndex(2));
  157. $this->assertEqual($frameset->getFrameFocus(), array(2));
  158. $this->assertEqual($frameset->getRaw(), 'Stuff2');
  159. $this->assertFalse($frameset->setFrameFocusByIndex(3));
  160. $this->assertEqual($frameset->getFrameFocus(), array(2));
  161. $frameset->clearFrameFocus();
  162. $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
  163. }
  164. function testCanFocusByName() {
  165. $page = new MockSimplePage();
  166. $frame1 = new MockSimplePage();
  167. $frame1->setReturnValue('getRaw', 'Stuff1');
  168. $frame1->setReturnValue('getFrameFocus', array());
  169. $frame2 = new MockSimplePage();
  170. $frame2->setReturnValue('getRaw', 'Stuff2');
  171. $frame2->setReturnValue('getFrameFocus', array());
  172. $frameset = new SimpleFrameset($page);
  173. $frameset->addFrame($frame1, 'A');
  174. $frameset->addFrame($frame2, 'B');
  175. $this->assertTrue($frameset->setFrameFocus('A'));
  176. $this->assertEqual($frameset->getFrameFocus(), array('A'));
  177. $this->assertEqual($frameset->getRaw(), 'Stuff1');
  178. $this->assertTrue($frameset->setFrameFocusByIndex(2));
  179. $this->assertEqual($frameset->getFrameFocus(), array('B'));
  180. $this->assertEqual($frameset->getRaw(), 'Stuff2');
  181. $this->assertFalse($frameset->setFrameFocus('z'));
  182. $frameset->clearFrameFocus();
  183. $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
  184. }
  185. }
  186. class TestOfFramesetPageInterface extends UnitTestCase {
  187. private $page_interface;
  188. private $frameset_interface;
  189. function __construct() {
  190. parent::__construct();
  191. $this->page_interface = $this->getPageMethods();
  192. $this->frameset_interface = $this->getFramesetMethods();
  193. }
  194. function assertListInAnyOrder($list, $expected) {
  195. sort($list);
  196. sort($expected);
  197. $this->assertEqual($list, $expected);
  198. }
  199. private function getPageMethods() {
  200. $methods = array();
  201. foreach (get_class_methods('SimplePage') as $method) {
  202. if (strtolower($method) == strtolower('SimplePage')) {
  203. continue;
  204. }
  205. if (strtolower($method) == strtolower('getFrameset')) {
  206. continue;
  207. }
  208. if (strncmp($method, '_', 1) == 0) {
  209. continue;
  210. }
  211. if (strncmp($method, 'accept', 6) == 0) {
  212. continue;
  213. }
  214. $methods[] = $method;
  215. }
  216. return $methods;
  217. }
  218. private function getFramesetMethods() {
  219. $methods = array();
  220. foreach (get_class_methods('SimpleFrameset') as $method) {
  221. if (strtolower($method) == strtolower('SimpleFrameset')) {
  222. continue;
  223. }
  224. if (strncmp($method, '_', 1) == 0) {
  225. continue;
  226. }
  227. if (strncmp($method, 'add', 3) == 0) {
  228. continue;
  229. }
  230. $methods[] = $method;
  231. }
  232. return $methods;
  233. }
  234. function testFramsetHasPageInterface() {
  235. $difference = array();
  236. foreach ($this->page_interface as $method) {
  237. if (! in_array($method, $this->frameset_interface)) {
  238. $this->fail("No [$method] in Frameset class");
  239. return;
  240. }
  241. }
  242. $this->pass('Frameset covers Page interface');
  243. }
  244. function testHeadersReadFromFrameIfInFocus() {
  245. $frame = new MockSimplePage();
  246. $frame->setReturnValue('getUrl', new SimpleUrl('http://localhost/stuff'));
  247. $frame->setReturnValue('getRequest', 'POST stuff');
  248. $frame->setReturnValue('getMethod', 'POST');
  249. $frame->setReturnValue('getRequestData', array('a' => 'A'));
  250. $frame->setReturnValue('getHeaders', 'Header: content');
  251. $frame->setReturnValue('getMimeType', 'text/xml');
  252. $frame->setReturnValue('getResponseCode', 401);
  253. $frame->setReturnValue('getTransportError', 'Could not parse headers');
  254. $frame->setReturnValue('getAuthentication', 'Basic');
  255. $frame->setReturnValue('getRealm', 'Safe place');
  256. $frameset = new SimpleFrameset(new MockSimplePage());
  257. $frameset->addFrame($frame);
  258. $frameset->setFrameFocusByIndex(1);
  259. $url = new SimpleUrl('http://localhost/stuff');
  260. $url->setTarget(1);
  261. $this->assertIdentical($frameset->getUrl(), $url);
  262. $this->assertIdentical($frameset->getRequest(), 'POST stuff');
  263. $this->assertIdentical($frameset->getMethod(), 'POST');
  264. $this->assertIdentical($frameset->getRequestData(), array('a' => 'A'));
  265. $this->assertIdentical($frameset->getHeaders(), 'Header: content');
  266. $this->assertIdentical($frameset->getMimeType(), 'text/xml');
  267. $this->assertIdentical($frameset->getResponseCode(), 401);
  268. $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
  269. $this->assertIdentical($frameset->getAuthentication(), 'Basic');
  270. $this->assertIdentical($frameset->getRealm(), 'Safe place');
  271. }
  272. function testUrlsComeFromBothFrames() {
  273. $page = new MockSimplePage();
  274. $page->expectNever('getUrls');
  275. $frame1 = new MockSimplePage();
  276. $frame1->setReturnValue(
  277. 'getUrls',
  278. array('http://www.lastcraft.com/', 'http://myserver/'));
  279. $frame2 = new MockSimplePage();
  280. $frame2->setReturnValue(
  281. 'getUrls',
  282. array('http://www.lastcraft.com/', 'http://test/'));
  283. $frameset = new SimpleFrameset($page);
  284. $frameset->addFrame($frame1);
  285. $frameset->addFrame($frame2);
  286. $this->assertListInAnyOrder(
  287. $frameset->getUrls(),
  288. array('http://www.lastcraft.com/', 'http://myserver/', 'http://test/'));
  289. }
  290. function testLabelledUrlsComeFromBothFrames() {
  291. $frame1 = new MockSimplePage();
  292. $frame1->setReturnValue(
  293. 'getUrlsByLabel',
  294. array(new SimpleUrl('goodbye.php')),
  295. array('a'));
  296. $frame2 = new MockSimplePage();
  297. $frame2->setReturnValue(
  298. 'getUrlsByLabel',
  299. array(new SimpleUrl('hello.php')),
  300. array('a'));
  301. $frameset = new SimpleFrameset(new MockSimplePage());
  302. $frameset->addFrame($frame1);
  303. $frameset->addFrame($frame2, 'Two');
  304. $expected1 = new SimpleUrl('goodbye.php');
  305. $expected1->setTarget(1);
  306. $expected2 = new SimpleUrl('hello.php');
  307. $expected2->setTarget('Two');
  308. $this->assertEqual(
  309. $frameset->getUrlsByLabel('a'),
  310. array($expected1, $expected2));
  311. }
  312. function testUrlByIdComesFromFirstFrameToRespond() {
  313. $frame1 = new MockSimplePage();
  314. $frame1->setReturnValue('getUrlById', new SimpleUrl('four.php'), array(4));
  315. $frame1->setReturnValue('getUrlById', false, array(5));
  316. $frame2 = new MockSimplePage();
  317. $frame2->setReturnValue('getUrlById', false, array(4));
  318. $frame2->setReturnValue('getUrlById', new SimpleUrl('five.php'), array(5));
  319. $frameset = new SimpleFrameset(new MockSimplePage());
  320. $frameset->addFrame($frame1);
  321. $frameset->addFrame($frame2);
  322. $four = new SimpleUrl('four.php');
  323. $four->setTarget(1);
  324. $this->assertEqual($frameset->getUrlById(4), $four);
  325. $five = new SimpleUrl('five.php');
  326. $five->setTarget(2);
  327. $this->assertEqual($frameset->getUrlById(5), $five);
  328. }
  329. function testReadUrlsFromFrameInFocus() {
  330. $frame1 = new MockSimplePage();
  331. $frame1->setReturnValue('getUrls', array('a'));
  332. $frame1->setReturnValue('getUrlsByLabel', array(new SimpleUrl('l')));
  333. $frame1->setReturnValue('getUrlById', new SimpleUrl('i'));
  334. $frame2 = new MockSimplePage();
  335. $frame2->expectNever('getUrls');
  336. $frame2->expectNever('getUrlsByLabel');
  337. $frame2->expectNever('getUrlById');
  338. $frameset = new SimpleFrameset(new MockSimplePage());
  339. $frameset->addFrame($frame1, 'A');
  340. $frameset->addFrame($frame2, 'B');
  341. $frameset->setFrameFocus('A');
  342. $this->assertIdentical($frameset->getUrls(), array('a'));
  343. $expected = new SimpleUrl('l');
  344. $expected->setTarget('A');
  345. $this->assertIdentical($frameset->getUrlsByLabel('label'), array($expected));
  346. $expected = new SimpleUrl('i');
  347. $expected->setTarget('A');
  348. $this->assertIdentical($frameset->getUrlById(99), $expected);
  349. }
  350. function testReadFrameTaggedUrlsFromFrameInFocus() {
  351. $frame = new MockSimplePage();
  352. $by_label = new SimpleUrl('l');
  353. $by_label->setTarget('L');
  354. $frame->setReturnValue('getUrlsByLabel', array($by_label));
  355. $by_id = new SimpleUrl('i');
  356. $by_id->setTarget('I');
  357. $frame->setReturnValue('getUrlById', $by_id);
  358. $frameset = new SimpleFrameset(new MockSimplePage());
  359. $frameset->addFrame($frame, 'A');
  360. $frameset->setFrameFocus('A');
  361. $this->assertIdentical($frameset->getUrlsByLabel('label'), array($by_label));
  362. $this->assertIdentical($frameset->getUrlById(99), $by_id);
  363. }
  364. function testFindingFormsById() {
  365. $frame = new MockSimplePage();
  366. $form = new MockSimpleForm();
  367. $frame->returns('getFormById', $form, array('a'));
  368. $frameset = new SimpleFrameset(new MockSimplePage());
  369. $frameset->addFrame(new MockSimplePage(), 'A');
  370. $frameset->addFrame($frame, 'B');
  371. $this->assertSame($frameset->getFormById('a'), $form);
  372. $frameset->setFrameFocus('A');
  373. $this->assertNull($frameset->getFormById('a'));
  374. $frameset->setFrameFocus('B');
  375. $this->assertSame($frameset->getFormById('a'), $form);
  376. }
  377. function testFindingFormsBySubmit() {
  378. $frame = new MockSimplePage();
  379. $form = new MockSimpleForm();
  380. $frame->returns(
  381. 'getFormBySubmit',
  382. $form,
  383. array(new SimpleByLabel('a')));
  384. $frameset = new SimpleFrameset(new MockSimplePage());
  385. $frameset->addFrame(new MockSimplePage(), 'A');
  386. $frameset->addFrame($frame, 'B');
  387. $this->assertSame($frameset->getFormBySubmit(new SimpleByLabel('a')), $form);
  388. $frameset->setFrameFocus('A');
  389. $this->assertNull($frameset->getFormBySubmit(new SimpleByLabel('a')));
  390. $frameset->setFrameFocus('B');
  391. $this->assertSame($frameset->getFormBySubmit(new SimpleByLabel('a')), $form);
  392. }
  393. function testFindingFormsByImage() {
  394. $frame = new MockSimplePage();
  395. $form = new MockSimpleForm();
  396. $frame->returns(
  397. 'getFormByImage',
  398. $form,
  399. array(new SimpleByLabel('a')));
  400. $frameset = new SimpleFrameset(new MockSimplePage());
  401. $frameset->addFrame(new MockSimplePage(), 'A');
  402. $frameset->addFrame($frame, 'B');
  403. $this->assertSame($frameset->getFormByImage(new SimpleByLabel('a')), $form);
  404. $frameset->setFrameFocus('A');
  405. $this->assertNull($frameset->getFormByImage(new SimpleByLabel('a')));
  406. $frameset->setFrameFocus('B');
  407. $this->assertSame($frameset->getFormByImage(new SimpleByLabel('a')), $form);
  408. }
  409. function testSettingAllFrameFieldsWhenNoFrameFocus() {
  410. $frame1 = new MockSimplePage();
  411. $frame1->expectOnce('setField', array(new SimpleById(22), 'A'));
  412. $frame2 = new MockSimplePage();
  413. $frame2->expectOnce('setField', array(new SimpleById(22), 'A'));
  414. $frameset = new SimpleFrameset(new MockSimplePage());
  415. $frameset->addFrame($frame1, 'A');
  416. $frameset->addFrame($frame2, 'B');
  417. $frameset->setField(new SimpleById(22), 'A');
  418. }
  419. function testOnlySettingFieldFromFocusedFrame() {
  420. $frame1 = new MockSimplePage();
  421. $frame1->expectOnce('setField', array(new SimpleByLabelOrName('a'), 'A'));
  422. $frame2 = new MockSimplePage();
  423. $frame2->expectNever('setField');
  424. $frameset = new SimpleFrameset(new MockSimplePage());
  425. $frameset->addFrame($frame1, 'A');
  426. $frameset->addFrame($frame2, 'B');
  427. $frameset->setFrameFocus('A');
  428. $frameset->setField(new SimpleByLabelOrName('a'), 'A');
  429. }
  430. function testOnlyGettingFieldFromFocusedFrame() {
  431. $frame1 = new MockSimplePage();
  432. $frame1->setReturnValue('getField', 'f', array(new SimpleByName('a')));
  433. $frame2 = new MockSimplePage();
  434. $frame2->expectNever('getField');
  435. $frameset = new SimpleFrameset(new MockSimplePage());
  436. $frameset->addFrame($frame1, 'A');
  437. $frameset->addFrame($frame2, 'B');
  438. $frameset->setFrameFocus('A');
  439. $this->assertIdentical($frameset->getField(new SimpleByName('a')), 'f');
  440. }
  441. }
  442. ?>