View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  package example.messagebox;
16  
17  import org.fourthline.cling.UpnpService;
18  import org.fourthline.cling.mock.MockUpnpService;
19  import org.fourthline.cling.model.action.ActionInvocation;
20  import org.fourthline.cling.model.message.UpnpResponse;
21  import org.fourthline.cling.model.meta.LocalDevice;
22  import org.fourthline.cling.model.meta.LocalService;
23  import org.fourthline.cling.model.types.ServiceId;
24  import org.fourthline.cling.support.messagebox.AddMessage;
25  import org.fourthline.cling.support.messagebox.model.DateTime;
26  import org.fourthline.cling.support.messagebox.model.MessageIncomingCall;
27  import org.fourthline.cling.support.messagebox.model.MessageSMS;
28  import org.fourthline.cling.support.messagebox.model.MessageScheduleReminder;
29  import org.fourthline.cling.support.messagebox.model.NumberName;
30  import org.testng.annotations.Test;
31  
32  import static org.testng.Assert.assertEquals;
33  
34  /**
35   * <p>
36   * There are several message types available. The first is an SMS with a sender and receiver
37   * names and phone numbers, as well as a timestamp and message text:
38   * </p>
39   * <a class="citation" href="javacode://this#createSMS()" style="include: M1"/>
40   * <p>
41   * This message will appear as a "New SMS Received!" notification on your TV, with the
42   * option to reveal all message details. The other message types recognized by the TV
43   * are incoming call notification as well as calendar schedule reminder:
44   * </p>
45   * <a class="citation" href="javacode://this#createIncomingCall()" style="include: M2"/>
46   * <a class="citation" href="javacode://this#createScheduleReminder()" style="include: M3"/>
47   * <p>
48   * This is how you send a message asynchronously:
49   * </p>
50   * <a class="citation" href="javacode://this#sendMessageToTV()" style="include: S1; exclude: EXC1;"/>
51   * <p>
52   * Note that although your TV's service descriptor most likely contains a
53   * <code>RemoveMessage</code> action and Cling Support also ships with a
54   * <code>RemoveMessageCallback</code>, this action doesn't seem to be implemented
55   * by any Samsung TVs. Messages can only be deleted directly on the TV, with the
56   * remote control.
57   * </p>
58   */
59  public class MessageBoxTest {
60  
61      static private final String MESSAGE_SMS =
62              "<Category>SMS</Category>" +
63                      "<DisplayType>Maximum</DisplayType>" +
64                      "<ReceiveTime><Date>2010-06-21</Date><Time>16:34:12</Time></ReceiveTime>" +
65                      "<Receiver><Number>1234</Number><Name>The Receiver</Name></Receiver>" +
66                      "<Sender><Number>5678</Number><Name>The Sender</Name></Sender>" +
67                      "<Body>Hello World!</Body>";
68  
69      static private final String MESSAGE_INCOMING_CALL =
70              "<Category>Incoming Call</Category>" +
71                      "<DisplayType>Maximum</DisplayType>" +
72                      "<CallTime><Date>2010-06-21</Date><Time>16:34:12</Time></CallTime>" +
73                      "<Callee><Number>1234</Number><Name>The Callee</Name></Callee>" +
74                      "<Caller><Number>5678</Number><Name>The Caller</Name></Caller>";
75  
76      static private final String MESSAGE_SCHEDULE_REMINDER =
77              "<Category>Schedule Reminder</Category>" +
78                      "<DisplayType>Maximum</DisplayType>" +
79                      "<StartTime><Date>2010-06-21</Date><Time>16:34:12</Time></StartTime>" +
80                      "<Owner><Number>1234</Number><Name>The Owner</Name></Owner>" +
81                      "<Subject>The Subject</Subject>" +
82                      "<EndTime><Date>2010-06-21</Date><Time>17:34:12</Time></EndTime>" +
83                      "<Location>The Location</Location>" +
84                      "<Body>Hello World!</Body>";
85  
86      @Test
87      public void createSMS() {
88          MessageSMS msg = new MessageSMS(                    // DOC: M1
89                  new DateTime("2010-06-21", "16:34:12"),
90                  new NumberName("1234", "The Receiver"),
91                  new NumberName("5678", "The Sender"),
92                  "Hello World!"
93          );                                                  // DOC: M1
94  
95          String output = msg.toString();
96          assertEquals(output, MESSAGE_SMS);
97  
98      }
99  
100     @Test
101     public void createIncomingCall() {
102         MessageIncomingCall msg = new MessageIncomingCall(  // DOC: M2
103                 new DateTime("2010-06-21", "16:34:12"),
104                 new NumberName("1234", "The Callee"),
105                 new NumberName("5678", "The Caller")
106         );                                                  // DOC: M2
107 
108         String output = msg.toString();
109         assertEquals(output, MESSAGE_INCOMING_CALL);
110 
111     }
112 
113     @Test
114     public void createScheduleReminder() {
115         MessageScheduleReminder msg = new MessageScheduleReminder(  // DOC: M3
116                 new DateTime("2010-06-21", "16:34:12"),
117                 new NumberName("1234", "The Owner"),
118                 "The Subject",
119                 new DateTime("2010-06-21", "17:34:12"),
120                 "The Location",
121                 "Hello World!"
122         );                                                          // DOC: M3
123 
124         String output = msg.toString();
125         assertEquals(output, MESSAGE_SCHEDULE_REMINDER);
126 
127     }
128 
129     @Test
130     public void sendMessageToTV() throws Exception {
131 
132         final boolean[] tests = new boolean[1];
133 
134         UpnpService upnpService = new MockUpnpService();
135 
136         LocalDevice device = MessageBoxSampleData.createDevice(MyTV.class);
137         upnpService.getRegistry().addDevice(device);
138 
139         MessageSMS msg = new MessageSMS(
140                 new DateTime("2010-06-21", "16:34:12"),
141                 new NumberName("1234", "The Receiver"),
142                 new NumberName("5678", "The Sender"),
143                 "Hello World!"
144         );
145 
146         LocalService service = device.findService(new ServiceId("samsung.com", "MessageBoxService"));       // DOC: S1
147 
148         upnpService.getControlPoint().execute(
149             new AddMessage(service, msg) {
150 
151                 @Override
152                 public void success(ActionInvocation invocation) {
153                     // All OK
154                     tests[0] = true;                                                                    // DOC: EXC1
155                 }
156 
157                 @Override
158                 public void failure(ActionInvocation invocation,
159                                     UpnpResponse operation,
160                                     String defaultMsg) {
161                     // Something is wrong
162                 }
163             }
164         );                                                                                                  // DOC: S1
165 
166         for (boolean test : tests) {
167             assert test;
168         }
169         for (boolean test : ((LocalService<MyTV>) service).getManager().getImplementation().tests) {
170             assert test;
171         }
172 
173     }
174 
175     public static class MyTV extends MessageBoxSampleData.MessageBoxService {
176 
177         boolean[] tests = new boolean[1];
178 
179         @Override
180         protected void checkMessage(String id, String type, String messageText) {
181             assert id != null;
182             assertEquals(type, "text/xml;charset=\"utf-8\"");
183             assertEquals(messageText, MESSAGE_SMS);
184             tests[0] = true;
185         }
186     }
187 
188 }