aboutsummaryrefslogtreecommitdiff
path: root/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java
blob: 97a8b277a4090b83fe7a08bf6cbd080a44609cbd (plain)
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
/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.mobly.snippet.example1;

import com.google.android.mobly.snippet.Snippet;
import com.google.android.mobly.snippet.event.EventCache;
import com.google.android.mobly.snippet.event.SnippetEvent;
import com.google.android.mobly.snippet.rpc.AsyncRpc;
import com.google.android.mobly.snippet.rpc.Rpc;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class ExampleSnippet2 implements Snippet {

    private final EventCache mEventQueue = EventCache.getInstance();

    @Rpc(description = "Returns the given string with the prefix \"bar\"")
    public String getBar(String input) {
        return "bar " + input;
    }

    @Rpc(description = "Throws an exception")
    public String throwSomething() throws IOException {
        throw new IOException("Example exception from throwSomething()");
    }

    /**
     * An Rpc method demonstrating the async event mechanism.
     *
     * Expect to see an event on the client side that looks like:
     * {
     *  'name': 'ExampleEvent',
     *  'time': <timestamp>,
     *  'data': {
     *      'exampleData': "Here's a simple event.",
     *      'secret': 42.24,
     *      'isSecretive': True
     *  }
     * }
     *
     * @param eventId
     * @throws JSONException
     */
    @AsyncRpc(description = "This call puts an event in the event queue.")
    public void tryEvent(String eventId) throws JSONException {
        SnippetEvent event = new SnippetEvent(eventId, "ExampleEvent");
        event.addData("exampleData", "Here's a simple event.");
        event.addData("secret", 42.24);
        event.addData("isSecretive", true);
        JSONObject moreData = new JSONObject();
        moreData.put("evenMoreData", "More Data!");
        event.addData("moreData", moreData);
        mEventQueue.postEvent(event);
    }

    @Override
    public void shutdown() {}
}