| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
 | describe('angular.scenario.Application', function() {
  var $window;
  var app, frames;
  function callLoadHandlers(app) {
    var handlers = app.getFrame_().data('events').load;
    expect(handlers).toBeDefined();
    expect(handlers.length).toEqual(1);
    handlers[0].handler();
  }
  beforeEach(function() {
    frames = _jQuery("<div></div>");
    app = new angular.scenario.Application(frames);
    app.checkUrlStatus_ = function(url, callback) {
      callback.call(this);
    };
  });
  it('should return new $window and $document after navigate', function() {
    var called;
    var testWindow, testDocument, counter = 0;
    app.getWindow_ = function() {
      return {x:counter++, document:{x:counter++}};
    };
    app.navigateTo('http://www.google.com/');
    app.executeAction(function($document, $window) {
      testWindow = $window;
      testDocument = $document;
    });
    app.navigateTo('http://www.google.com/');
    app.executeAction(function($window, $document) {
      expect($window).not.toEqual(testWindow);
      expect($document).not.toEqual(testDocument);
      called = true;
    });
    expect(called).toBeTruthy();
  });
  it('should execute callback with correct arguments', function() {
    var called;
    var testWindow = {document: {}};
    app.getWindow_ = function() {
      return testWindow;
    };
    app.executeAction(function($window, $document) {
      expect(this).toEqual(app);
      expect($document).toEqual(_jQuery($window.document));
      expect($window).toEqual(testWindow);
      called = true;
    });
    expect(called).toBeTruthy();
  });
  it('should use a new iframe each time', function() {
    app.navigateTo('http://localhost/');
    var frame = app.getFrame_();
    frame.attr('test', true);
    app.navigateTo('http://localhost/');
    expect(app.getFrame_().attr('test')).toBeFalsy();
  });
  it('should call error handler if document not accessible', function() {
    var called;
    app.getWindow_ = function() {
      return {};
    };
    app.navigateTo('http://localhost/', angular.noop, function(error) {
      expect(error).toMatch(/Sandbox Error/);
      called = true;
    });
    callLoadHandlers(app);
    expect(called).toBeTruthy();
  });
  it('should call error handler if navigating to about:blank', function() {
    var called;
    app.navigateTo('about:blank', angular.noop, function(error) {
      expect(error).toMatch(/Sandbox Error/);
      called = true;
    });
    expect(called).toBeTruthy();
  });
  it('should call error handler if status check fails', function() {
    app.checkUrlStatus_ = function(url, callback) {
      callback.call(this, 'Example Error');
    };
    app.navigateTo('http://localhost/', angular.noop, function(error) {
      expect(error).toEqual('Example Error');
    });
  });
  it('should hide old iframes and navigate to about:blank', function() {
    app.navigateTo('http://localhost/#foo');
    app.navigateTo('http://localhost/#bar');
    var iframes = frames.find('iframe');
    expect(iframes.length).toEqual(2);
    expect(iframes[0].src).toEqual('about:blank');
    expect(iframes[1].src).toEqual('http://localhost/#bar');
    expect(_jQuery(iframes[0]).css('display')).toEqual('none');
  });
  it('should URL update description bar', function() {
    app.navigateTo('http://localhost/');
    var anchor = frames.find('> h2 a');
    expect(anchor.attr('href')).toEqual('http://localhost/');
    expect(anchor.text()).toEqual('http://localhost/');
  });
  it('should call onload handler when frame loads', function() {
    var called;
    app.getWindow_ = function() {
      return {document: {}};
    };
    app.navigateTo('http://localhost/', function($window, $document) {
      called = true;
    });
    callLoadHandlers(app);
    expect(called).toBeTruthy();
  });
  it('should wait for pending requests in executeAction', function() {
    var called, polled;
    var handlers = [];
    var testWindow = {
      document: _jQuery('<div class="test-foo"></div>'),
      angular: {
        service: {}
      }
    };
    testWindow.angular.service.$browser = function() {
      return {
        poll: function() {
          polled = true;
        },
        notifyWhenNoOutstandingRequests: function(fn) {
          handlers.push(fn);
        }
      };
    };
    app.getWindow_ = function() {
      return testWindow;
    };
    app.executeAction(function($window, $document) {
      expect($window).toEqual(testWindow);
      expect($document).toBeDefined();
      expect($document[0].className).toEqual('test-foo');
    });
    expect(polled).toBeTruthy();
    expect(handlers.length).toEqual(1);
    handlers[0]();
  });
  describe('jQuery ajax', function() {
    var options;
    var response;
    var jQueryAjax;
    beforeEach(function() {
      response = {
        status: 200,
        statusText: 'OK'
      };
      jQueryAjax = _jQuery.ajax;
      _jQuery.ajax = function(opts) {
        options = opts;
        opts.complete.call(this, response);
      };
      app.checkUrlStatus_ = angular.scenario.Application.
        prototype.checkUrlStatus_;
    });
    afterEach(function() {
      _jQuery.ajax = jQueryAjax;
    });
    it('should perform a HEAD request to verify file existence', function() {
      app.navigateTo('http://www.google.com/', angular.noop, angular.noop);
      expect(options.type).toEqual('HEAD');
      expect(options.url).toEqual('http://www.google.com/');
    });
    it('should call error handler if status code is less than 200', function() {
      var finished;
      response.status = 199;
      response.statusText = 'Error Message';
      app.navigateTo('http://localhost/', angular.noop, function(error) {
        expect(error).toEqual('199 Error Message');
        finished = true;
      });
      expect(finished).toBeTruthy();
    });
    it('should call error handler if status code is greater than 299', function() {
      var finished;
      response.status = 300;
      response.statusText = 'Error';
      app.navigateTo('http://localhost/', angular.noop, function(error) {
        expect(error).toEqual('300 Error');
        finished = true;
      });
      expect(finished).toBeTruthy();
    });
    it('should call error handler if status code is 0 for sandbox error', function() {
      var finished;
      response.status = 0;
      response.statusText = '';
      app.navigateTo('http://localhost/', angular.noop, function(error) {
        expect(error).toEqual('Sandbox Error: Cannot access http://localhost/');
        finished = true;
      });
      expect(finished).toBeTruthy();
    });
  });
});
 |