OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.instrumentation; |
| 6 |
| 7 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 |
| 10 import '../reflective_tests.dart'; |
| 11 |
| 12 main() { |
| 13 group('instrumentation', () { |
| 14 runReflectiveTests(InstrumentationServiceTest); |
| 15 runReflectiveTests(MulticastInstrumentationServerTest); |
| 16 }); |
| 17 } |
| 18 |
| 19 @reflectiveTest |
| 20 class InstrumentationServiceTest { |
| 21 void assertNormal( |
| 22 TestInstrumentationServer server, String tag, String message) { |
| 23 String sent = server.normalChannel.toString(); |
| 24 if (!sent.endsWith(':$tag:$message\n')) { |
| 25 fail('Expected "...:$tag:$message", found "$sent"'); |
| 26 } |
| 27 } |
| 28 |
| 29 void test_logError_withColon() { |
| 30 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 31 InstrumentationService service = new InstrumentationService(server); |
| 32 service.logError('Error:message'); |
| 33 assertNormal(server, InstrumentationService.TAG_ERROR, 'Error::message'); |
| 34 } |
| 35 |
| 36 void test_logError_withLeadingColon() { |
| 37 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 38 InstrumentationService service = new InstrumentationService(server); |
| 39 service.logError(':a:bb'); |
| 40 assertNormal(server, InstrumentationService.TAG_ERROR, '::a::bb'); |
| 41 } |
| 42 |
| 43 void test_logError_withoutColon() { |
| 44 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 45 InstrumentationService service = new InstrumentationService(server); |
| 46 String message = 'Error message'; |
| 47 service.logError(message); |
| 48 assertNormal(server, InstrumentationService.TAG_ERROR, message); |
| 49 } |
| 50 |
| 51 void test_logException_noTrace() { |
| 52 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 53 InstrumentationService service = new InstrumentationService(server); |
| 54 String message = 'exceptionMessage'; |
| 55 service.logException(message, null); |
| 56 assertNormal(server, InstrumentationService.TAG_EXCEPTION, '$message:null'); |
| 57 } |
| 58 |
| 59 void test_logFileRead() { |
| 60 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 61 InstrumentationService service = new InstrumentationService(server); |
| 62 String path = '/file/path'; |
| 63 int time = 978336000000; |
| 64 String content = 'class C {\n}\n'; |
| 65 service.logFileRead(path, time, content); |
| 66 assertNormal( |
| 67 server, InstrumentationService.TAG_FILE_READ, '$path:$time:$content'); |
| 68 } |
| 69 |
| 70 void test_logLogEntry() { |
| 71 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 72 InstrumentationService service = new InstrumentationService(server); |
| 73 String level = 'level'; |
| 74 DateTime time = new DateTime(2001); |
| 75 String message = 'message'; |
| 76 String exception = 'exception'; |
| 77 String stackTraceText = 'stackTrace'; |
| 78 StackTrace stackTrace = new StackTrace.fromString(stackTraceText); |
| 79 service.logLogEntry(level, time, message, exception, stackTrace); |
| 80 assertNormal(server, InstrumentationService.TAG_LOG_ENTRY, |
| 81 '$level:${time.millisecondsSinceEpoch}:$message:$exception:$stackTraceTe
xt'); |
| 82 } |
| 83 |
| 84 void test_logNotification() { |
| 85 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 86 InstrumentationService service = new InstrumentationService(server); |
| 87 String message = 'notificationText'; |
| 88 service.logNotification(message); |
| 89 assertNormal(server, InstrumentationService.TAG_NOTIFICATION, message); |
| 90 } |
| 91 |
| 92 void test_logRequest() { |
| 93 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 94 InstrumentationService service = new InstrumentationService(server); |
| 95 String message = 'requestText'; |
| 96 service.logRequest(message); |
| 97 assertNormal(server, InstrumentationService.TAG_REQUEST, message); |
| 98 } |
| 99 |
| 100 void test_logResponse() { |
| 101 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 102 InstrumentationService service = new InstrumentationService(server); |
| 103 String message = 'responseText'; |
| 104 service.logResponse(message); |
| 105 assertNormal(server, InstrumentationService.TAG_RESPONSE, message); |
| 106 } |
| 107 |
| 108 void test_logVersion() { |
| 109 TestInstrumentationServer server = new TestInstrumentationServer(); |
| 110 InstrumentationService service = new InstrumentationService(server); |
| 111 service.logVersion('myUuid', 'someClientId', 'someClientVersion', |
| 112 'aServerVersion', 'anSdkVersion'); |
| 113 expect(server.normalChannel.toString(), ''); |
| 114 expect( |
| 115 server.priorityChannel.toString(), |
| 116 endsWith( |
| 117 ':myUuid:someClientId:someClientVersion:aServerVersion:anSdkVersion\
n')); |
| 118 } |
| 119 } |
| 120 |
| 121 @reflectiveTest |
| 122 class MulticastInstrumentationServerTest { |
| 123 TestInstrumentationServer serverA = new TestInstrumentationServer(); |
| 124 TestInstrumentationServer serverB = new TestInstrumentationServer(); |
| 125 MulticastInstrumentationServer server; |
| 126 |
| 127 void setUp() { |
| 128 server = new MulticastInstrumentationServer([serverA, serverB]); |
| 129 } |
| 130 |
| 131 void test_log() { |
| 132 server.log('foo bar'); |
| 133 _assertNormal(serverA, 'foo bar'); |
| 134 _assertNormal(serverB, 'foo bar'); |
| 135 } |
| 136 |
| 137 void test_logWithPriority() { |
| 138 server.logWithPriority('foo bar'); |
| 139 _assertPriority(serverA, 'foo bar'); |
| 140 _assertPriority(serverB, 'foo bar'); |
| 141 } |
| 142 |
| 143 void test_shutdown() { |
| 144 server.shutdown(); |
| 145 } |
| 146 |
| 147 void _assertNormal(TestInstrumentationServer server, String message) { |
| 148 String sent = server.normalChannel.toString(); |
| 149 if (!sent.endsWith('$message\n')) { |
| 150 fail('Expected "...$message", found "$sent"'); |
| 151 } |
| 152 } |
| 153 |
| 154 void _assertPriority(TestInstrumentationServer server, String message) { |
| 155 String sent = server.priorityChannel.toString(); |
| 156 if (!sent.endsWith('$message\n')) { |
| 157 fail('Expected "...$message", found "$sent"'); |
| 158 } |
| 159 } |
| 160 } |
| 161 |
| 162 class TestInstrumentationServer implements InstrumentationServer { |
| 163 StringBuffer normalChannel = new StringBuffer(); |
| 164 StringBuffer priorityChannel = new StringBuffer(); |
| 165 |
| 166 @override |
| 167 void log(String message) { |
| 168 normalChannel.writeln(message); |
| 169 } |
| 170 |
| 171 @override |
| 172 void logWithPriority(String message) { |
| 173 priorityChannel.writeln(message); |
| 174 } |
| 175 |
| 176 @override |
| 177 void shutdown() { |
| 178 // Ignored |
| 179 } |
| 180 } |
OLD | NEW |