1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file 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 package com.android.tradefed.result; 17 18 import com.android.tradefed.build.BuildInfo; 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.invoker.IInvocationContext; 21 import com.android.tradefed.invoker.InvocationContext; 22 import com.android.tradefed.util.IEmail; 23 import com.android.tradefed.util.IEmail.Message; 24 25 import junit.framework.TestCase; 26 27 import org.easymock.EasyMock; 28 29 import java.io.IOException; 30 31 /** 32 * Unit tests for {@link EmailResultReporter}. 33 */ 34 public class EmailResultReporterTest extends TestCase { 35 private IEmail mMockMailer; 36 private EmailResultReporter mEmailReporter; 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 mMockMailer = EasyMock.createMock(IEmail.class); 42 mEmailReporter = new EmailResultReporter(mMockMailer); 43 } 44 45 /** 46 * Test normal success case for {@link EmailResultReporter#invocationEnded(long)}. 47 * @throws IOException 48 */ 49 public void testInvocationEnded() throws IllegalArgumentException, IOException { 50 mMockMailer.send(EasyMock.<Message>anyObject()); 51 EasyMock.replay(mMockMailer); 52 IInvocationContext context = new InvocationContext(); 53 context.addDeviceBuildInfo("fakeDevice", new BuildInfo("888", "mybuild")); 54 context.setTestTag("mytest"); 55 mEmailReporter.invocationStarted(context); 56 mEmailReporter.addDestination("foo"); 57 mEmailReporter.invocationEnded(0); 58 EasyMock.verify(mMockMailer); 59 60 assertEquals("Tradefed result for mytest on BuildInfo{bid=888, target=mybuild}: SUCCESS", 61 mEmailReporter.generateEmailSubject()); 62 } 63 64 /** 65 * Test normal success case for {@link EmailResultReporter#invocationEnded(long)} with multiple 66 * builds. 67 */ 68 public void testInvocationEnded_multiBuild() throws IllegalArgumentException, IOException { 69 mMockMailer.send(EasyMock.<Message>anyObject()); 70 EasyMock.replay(mMockMailer); 71 IInvocationContext context = new InvocationContext(); 72 context.addAllocatedDevice("fakeDevice", EasyMock.createMock(ITestDevice.class)); 73 context.addDeviceBuildInfo("fakeDevice", new BuildInfo("888", "mybuild")); 74 context.addAllocatedDevice("fakeDevice2", EasyMock.createMock(ITestDevice.class)); 75 context.addDeviceBuildInfo("fakeDevice2", new BuildInfo("999", "mybuild2")); 76 context.setTestTag("mytest"); 77 mEmailReporter.invocationStarted(context); 78 mEmailReporter.addDestination("foo"); 79 mEmailReporter.invocationEnded(0); 80 EasyMock.verify(mMockMailer); 81 82 assertEquals("Tradefed result for mytest on BuildInfo{bid=888, target=mybuild}" 83 + "BuildInfo{bid=999, target=mybuild2}: SUCCESS", 84 mEmailReporter.generateEmailSubject()); 85 } 86 87 /** 88 * Make sure that we don't include the string "null" in a generated email subject 89 */ 90 public void testNullFlavorAndBranch() throws Exception { 91 mMockMailer.send(EasyMock.<Message>anyObject()); 92 EasyMock.replay(mMockMailer); 93 IInvocationContext context = new InvocationContext(); 94 context.addDeviceBuildInfo("fakeDevice", new BuildInfo("888", null)); 95 mEmailReporter.invocationStarted(context); 96 mEmailReporter.addDestination("foo"); 97 mEmailReporter.invocationEnded(0); 98 99 EasyMock.verify(mMockMailer); 100 101 assertEquals("Tradefed result for (unknown suite) on BuildInfo{bid=888}: SUCCESS", 102 mEmailReporter.generateEmailSubject()); 103 } 104 } 105