source: Dev/branches/rest-dojo-ui/client/dijit/tests/_testCommon.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 4.1 KB
Line 
1// module:
2//              dijit/tests/_testCommon.js
3// description:
4//              A simple module to be included in dijit test pages to allow
5//              for easy switching between the many many points of the test-matrix.
6//
7//              in your test browser, provides a way to switch between available themes,
8//              and optionally enable RTL (right to left) mode, and/or dijit_a11y (high-
9//              contrast/image off emulation) ... probably not a genuine test for a11y.
10//
11//              usage: on any dijit test_* page, press ctrl-f9 to popup links.
12//
13//              there are currently (3 themes * 4 tests) * (10 variations of supported browsers)
14//              not including testing individual locale-strings
15//
16//              you should NOT be using this in a production environment. include
17//              your css and set your classes manually. for test purposes only ...
18
19require([
20        "require",
21        "dojo/_base/array",
22        "dojo/_base/config",
23        "dojo/dom",
24        "dojo/dom-attr",
25        "dojo/dom-class",
26        "dojo/dom-construct",
27        "dojo/_base/kernel",
28        "dojo/_base/lang",
29        "dojo/query",
30        "dojo/ready",
31        "dojo/_base/window"
32], function(require, array, config, dom, domAttr, domClass, domConstruct, kernel, lang, query, ready, win){
33
34        var dir = "",
35                theme = false,
36                themeModule = "dijit",
37                testMode = null,
38                defTheme = "claro",
39                vars={};
40
41        if(window.location.href.indexOf("?") > -1){
42                var str = window.location.href.substr(window.location.href.indexOf("?")+1).split(/#/);
43                var ary  = str[0].split(/&/);
44                for(var i=0; i<ary.length; i++){
45                        var split = ary[i].split("="),
46                                key = split[0],
47                                value = (split[1]||'').replace(/[^\w]/g, "");   // replace() to prevent XSS attack
48                        switch(key){
49                                case "locale":
50                                        // locale string | null
51                                        kernel.locale = config.locale = locale = value;
52                                        break;
53                                case "dir":
54                                        // rtl | null
55                                        document.getElementsByTagName("html")[0].dir = value;
56                                        dir = value;
57                                        break;
58                                case "theme":
59                                        // tundra | soria | nihilo | claro | null
60                                        theme = value;
61                                        break;
62                                case "a11y":
63                                        if(value){ testMode = "dijit_a11y"; }
64                                        break;
65                                case "themeModule":
66                                        // moduleName | null
67                                        if(value){ themeModule = value; }
68                        }
69                        vars[key] = value;
70                }
71        }
72        kernel._getVar = function(k, def){      // TODO: not sure what this is
73                return vars[k] || def;
74        };
75
76        // BIDI
77        if(dir == "rtl"){
78                ready(0, function(){
79                        // pretend all the labels are in an RTL language, because
80                        // that affects how they lay out relative to inline form widgets
81                        query("label").attr("dir", "rtl");
82                });
83        }
84
85        // a11y
86        if(testMode){
87                ready(0, function(){
88                        var b = win.body();
89                        if(testMode){
90                                domClass.add(b, testMode);
91                        }
92                });
93        }
94
95        // If URL specifies a non-claro theme then pull in those theme CSS files and modify
96        // <body> to point to that new theme instead of claro.
97        //
98        // Also defer parsing and any dojo.ready() calls that the test file makes
99        // until the CSS has finished loading.
100        if(theme){
101                // Wait until JS modules have finished loading so this doesn't confuse
102                // AMD loader.
103                ready(1, function(){
104                        // Reset <body> to point to the specified theme
105                        var b = win.body();
106                        domClass.replace(b, theme, defTheme);
107
108                        // Remove claro CSS
109                        query('link[href$="claro.css"]').orphan();
110                        query('link[href$="claro/document.css"]').orphan();
111
112                        // Load theme CSS.
113                        // Eventually would like to use [something like]
114                        // https://github.com/unscriptable/curl/blob/master/src/curl/plugin/css.js
115                        // to load the CSS and then know exactly when it finishes loading.
116                        var modules = [
117                                require.toUrl(themeModule+"/themes/"+theme+"/"+theme+".css"),
118                                require.toUrl(themeModule+"/themes/"+theme+"/"+theme+"_rtl.css"),
119                                require.toUrl("dojo/resources/dojo.css")
120                        ];
121                        var head = query("head")[0];
122                        array.forEach(modules, function(css){
123                                if(document.createStyleSheet){
124                                        // For IE
125                                        document.createStyleSheet(css);
126                                }else{
127                                        // For other browsers
128                                        domConstruct.place('<link rel="stylesheet" type="text/css" href="'+css+'"/>',
129                                                head);
130                                }
131                        });
132                });
133                ready(2, function(){
134                        // Delay parsing and other dojo.ready() callbacks (except ones in this file)
135                        // until the injected <link>'s above have finished loading.
136                        require(["dijit/tests/delay!320"]);
137                });
138        }
139});
Note: See TracBrowser for help on using the repository browser.