0adbb51101969475aa68cfeba3bc369e313a31d2
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 describe("app.ui.RefreshButton", function() {
17
18         var RefreshButton = window.app.ui.RefreshButton;
19
20         var r, refresh_handler, change_handler;
21
22         function openMenuPanel( button, label ) {
23                 button.el.find("BUTTON").eq(1).click();
24                 $(".uiMenuPanel-label:contains(" + label + ")").click();
25                 test.clock.tick(); // menuPanel -> bind _close_handler
26         }
27
28
29         beforeEach( function() {
30                 test.clock.steal();
31                 refresh_handler = jasmine.createSpy("refresh_handler");
32                 change_handler = jasmine.createSpy("change_handler");
33                 r = new RefreshButton({
34                         onRefresh: refresh_handler,
35                         onChange: change_handler
36                 });
37                 r.attach( document.body );
38         });
39
40         afterEach( function() {
41                 r.remove();
42                 test.clock.restore();
43         });
44
45         it("should have an initial default value", function() {
46                 expect( r.value ).toBe( -1 );
47         });
48
49         it("should fire a refresh event after clicking the refresh button ", function() {
50                 r.el.find("BUTTON").eq(0).click();
51
52                 expect( refresh_handler ).toHaveBeenCalled();
53         });
54
55         it("should change the refresh rate when set it called", function() {
56                 r.set( 100 );
57                 expect( r.value ).toBe( 100 );
58         });
59
60         it("should set an interval when rate is set to a positive value", function() {
61                 r.set( 100 );
62                 test.clock.tick();
63                 expect( refresh_handler.calls.count() ).toBe( 1 );
64         });
65
66         it("should not set an interval when rate is set to a non positive value", function() {
67                 r.set( -1 );
68                 test.clock.tick();
69                 expect( refresh_handler.calls.count() ).toBe( 0 );
70         });
71
72         it("should fire a refresh event on intervals if refresh menu item is set to quickly", function() {
73                 openMenuPanel( r, "quickly" );
74
75                 expect( refresh_handler.calls.count() ).toBe( 0 );
76                 test.clock.tick();
77                 expect( refresh_handler.calls.count() ).toBe( 1 );
78                 test.clock.tick();
79                 expect( refresh_handler.calls.count() ).toBe( 2 );
80         });
81
82         it("should not fire refresh events when user selects Manual", function() {
83
84                 openMenuPanel( r, "quickly" );
85
86                 expect( refresh_handler.calls.count() ).toBe( 0 );
87                 test.clock.tick();
88                 expect( refresh_handler.calls.count() ).toBe( 1 );
89
90                 openMenuPanel( r, "Manual" );
91
92                 test.clock.tick();
93                 expect( refresh_handler.calls.count() ).toBe( 1 );
94                 test.clock.tick();
95                 expect( refresh_handler.calls.count() ).toBe( 1 );
96         });
97
98         it("should fire a change event when a new refresh rate is selected", function() {
99                 openMenuPanel( r, "quickly" );
100                 expect( change_handler.calls.count() ).toBe( 1 );
101                 expect( r.value ).toBe( 100 );
102                 openMenuPanel( r, "Manual" );
103                 expect( change_handler.calls.count() ).toBe( 2 );
104                 expect( r.value ).toBe( -1 );
105         });
106
107 });