geo/src/test/java/net/java/html/geo/OnLocationTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 842 ebb2428bb7e1
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@172
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@172
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@172
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@172
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@172
    42
 */
jaroslav@172
    43
package net.java.html.geo;
jaroslav@172
    44
jaroslav@172
    45
import org.testng.annotations.Test;
jaroslav@173
    46
import static org.testng.Assert.*;
jaroslav@172
    47
jaroslav@173
    48
/** Testing correctness of the generated code.
jaroslav@172
    49
 */
jaroslav@172
    50
public class OnLocationTest {
jaroslav@173
    51
    static int cnt;
jaroslav@172
    52
    static @OnLocation void onLocation(Position p) {
jaroslav@173
    53
        assertNotNull(p, "Position object provided");
jaroslav@173
    54
        cnt++;
jaroslav@172
    55
    }
jaroslav@172
    56
jaroslav@172
    57
    @Test public void createOneTimeQueryStatic() {
jaroslav@175
    58
        net.java.html.geo.Position.Handle h = OnLocationHandle.createQuery();
jaroslav@172
    59
        h.setHighAccuracy(false);
jaroslav@172
    60
        h.setTimeout(1000L);
jaroslav@172
    61
        h.setMaximumAge(1000L);
jaroslav@193
    62
        if (h.isSupported()) h.start();
jaroslav@172
    63
        h.stop();
jaroslav@172
    64
    }
jaroslav@173
    65
    
jaroslav@178
    66
    @Test public void onLocationHandleCallback() throws Throwable {
jaroslav@175
    67
        net.java.html.geo.Position.Handle h = OnLocationHandle.createQuery();
jaroslav@173
    68
        cnt = 0;
jaroslav@181
    69
        h.onLocation(new Position(null));
jaroslav@173
    70
        assertEquals(cnt, 1, "The callback has been made");
jaroslav@173
    71
    }
jaroslav@172
    72
jaroslav@172
    73
    @Test public void createRepeatableWatchStatic() {
jaroslav@175
    74
        net.java.html.geo.Position.Handle h = OnLocationHandle.createQuery();
jaroslav@172
    75
        h.setHighAccuracy(false);
jaroslav@172
    76
        h.setTimeout(1000L);
jaroslav@172
    77
        h.setMaximumAge(1000L);
jaroslav@193
    78
        if (h.isSupported()) h.start();
jaroslav@172
    79
        h.stop();
jaroslav@172
    80
    }
jaroslav@173
    81
jaroslav@173
    82
    int instCnt;
jaroslav@174
    83
    Throwable instT;
jaroslav@178
    84
    @OnLocation(onError = "someError") void instance(Position p) throws Error {
jaroslav@173
    85
        assertNotNull(p, "Some position passed in");
jaroslav@173
    86
        instCnt++;
jaroslav@172
    87
    }
jaroslav@178
    88
    void someError(Throwable t) throws Exception {
jaroslav@174
    89
        instT = t;
jaroslav@174
    90
        instCnt++;
jaroslav@174
    91
    }
jaroslav@172
    92
    
jaroslav@172
    93
    @Test public void createOneTimeQueryInstance() {
jaroslav@172
    94
        OnLocationTest t = new OnLocationTest();
jaroslav@172
    95
        
jaroslav@175
    96
        net.java.html.geo.Position.Handle h = InstanceHandle.createQuery(t);
jaroslav@172
    97
        h.setHighAccuracy(false);
jaroslav@172
    98
        h.setTimeout(1000L);
jaroslav@172
    99
        h.setMaximumAge(1000L);
jaroslav@193
   100
        if (h.isSupported()) h.start();
jaroslav@172
   101
        h.stop();
jaroslav@172
   102
    }
jaroslav@173
   103
    
jaroslav@178
   104
    @Test public void onInstanceCallback() throws Throwable {
jaroslav@173
   105
        OnLocationTest t = new OnLocationTest();
jaroslav@175
   106
        net.java.html.geo.Position.Handle h = InstanceHandle.createWatch(t);
jaroslav@181
   107
        h.onLocation(new Position(null));
jaroslav@173
   108
        assertEquals(t.instCnt, 1, "One callback made");
jaroslav@173
   109
    }
jaroslav@172
   110
jaroslav@178
   111
    @Test public void onInstanceError() throws Throwable {
jaroslav@175
   112
        net.java.html.geo.Position.Handle h = InstanceHandle.createWatch(this);
jaroslav@174
   113
        InterruptedException e = new InterruptedException();
jaroslav@174
   114
        h.onError(e);
jaroslav@174
   115
        assertEquals(instCnt, 1, "One callback made");
jaroslav@174
   116
        assertEquals(instT, e, "The same exception passed in");
jaroslav@174
   117
    }
jaroslav@174
   118
jaroslav@172
   119
    @Test public void createRepeatableWatch() {
jaroslav@172
   120
        OnLocationTest t = new OnLocationTest();
jaroslav@172
   121
        
jaroslav@175
   122
        net.java.html.geo.Position.Handle h = InstanceHandle.createWatch(t);
jaroslav@172
   123
        h.setHighAccuracy(false);
jaroslav@172
   124
        h.setTimeout(1000L);
jaroslav@172
   125
        h.setMaximumAge(1000L);
jaroslav@193
   126
        if (h.isSupported()) h.start();
jaroslav@172
   127
        h.stop();
jaroslav@172
   128
    }
jaroslav@212
   129
    
jaroslav@212
   130
    @OnLocation(onError = "errParam") void withParam(Position pos, int param) {
jaroslav@212
   131
        instCnt = param;
jaroslav@212
   132
    }
jaroslav@212
   133
    
jaroslav@212
   134
    void errParam(Exception ex, int param) {
jaroslav@212
   135
        instCnt = param;
jaroslav@212
   136
    }
jaroslav@172
   137
}