1/*
2Distributed under both the W3C Test Suite License [1] and the W3C
33-clause BSD License [2]. To contribute to a W3C Test Suite, see the
4policies and contribution forms [3].
5
6[1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
7[2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
8[3] http://www.w3.org/2004/10/27-testcases
9*/
10
11//
12// Helper Functions for PageVisibility W3C tests
13//
14var VISIBILITY_STATES =
15{
16 HIDDEN: "hidden",
17 VISIBLE: "visible"
18};
19
20var feature_check = false;
21
22//
23// All test() functions in the WebPerf PageVis test suite should use pv_test() instead.
24//
25// pv_test() validates the document.hidden and document.visibilityState attributes
26// exist prior to running tests and immediately shows a failure if they do not.
27//
28
29function pv_test(func, msg, doc)
30{
31 if (!doc)
32 {
33 doc = document;
34 }
35
36 // only run the feature check once, unless func == null, in which case,
37 // this call is intended as a feature check
38 if (!feature_check)
39 {
40 feature_check = true;
41
42 var hiddenVal = doc.hidden;
43 var visStateVal = doc.visibilityState;
44
45 // show a single error that the Page Visibility feature is undefined
46 test(function()
47 {
48 assert_true(hiddenVal !== undefined && hiddenVal != null,
49 "document.hidden is defined and not null.");},
50 "document.hidden is defined and not null.");
51
52 test(function()
53 {
54 assert_true(visStateVal !== undefined && hiddenVal != null,
55 "document.visibilityState is defined and not null.");},
56 "document.visibilityState is defined and not null.");
57
58 }
59
60 if (func)
61 {
62 test(func, msg);
63 }
64}
65
66
67function test_feature_exists(doc, msg)
68{
69 if (!msg)
70 {
71 msg = "";
72 }
73 var hiddenMsg = "document.hidden is defined" + msg + ".";
74 var stateMsg = "document.visibilityState is defined" + msg + ".";
75 pv_test(function(){assert_true(document.hidden !== undefined, hiddenMsg);}, hiddenMsg, doc);
76 pv_test(function(){assert_true(document.visibilityState !== undefined, stateMsg);}, stateMsg, doc);
77}
78
79//
80// Common helper functions
81//
82
83function test_true(value, msg)
84{
85 pv_test(function() { assert_true(value, msg); }, msg);
86}
87
88function test_equals(value, equals, msg)
89{
90 pv_test(function() { assert_equals(value, equals, msg); }, msg);
91}
92
93//
94// asynchronous test helper functions
95//
96
97function add_async_result(test_obj, pass_state)
98{
99 // add assertion to manual test for the pass state
100 test_obj.step(function() { assert_true(pass_state) });
101
102 // end manual test
103 test_obj.done();
104}
105
106function add_async_result_assert(test_obj, func)
107{
108 // add assertion to manual test for the pass state
109 test_obj.step(func);
110
111 // end manual test
112 test_obj.done();
113}
114
115var open_link;
116function TabSwitch()
117{
118 //var open_link = window.open("http://www.bing.com");
119 open_link = window.open('', '_blank');
120 step_timeout(function() { open_link.close(); }, 2000);
121}