blob: 7e7c434fb91e1514f887ab6f656a2ef5e4e87d9a [file] [log] [blame]
/*
* Copyright (C) 2020 The Android Open Source Project
*
* 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.android.eventlib.events;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import com.android.bedstead.nene.TestApis;
import com.android.eventlib.EventLogs;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public final class CustomEventTest {
// TODO: We need a standard pattern for testing that events log correctly cross-process
// (when within the process serialization never happens)
private static final TestApis sTestApis = new TestApis();
private static final Context sContext = sTestApis.context().instrumentedContext();
private static final String TAG_1 = "TAG_1";
private static final String TAG_2 = "TAG_2";
private static final String DATA_1 = "DATA_1";
private static final String DATA_2 = "DATA_2";
@Before
public void setUp() {
EventLogs.resetLogs();
}
@Test
public void whereTag_works() {
CustomEvent.logger(sContext)
.setTag(TAG_1)
.log();
EventLogs<CustomEvent> eventLogs = CustomEvent.queryPackage(sContext.getPackageName())
.whereTag().isEqualTo(TAG_1);
assertThat(eventLogs.poll().tag()).isEqualTo(TAG_1);
}
@Test
public void whereTag_skipsNonMatching() {
CustomEvent.logger(sContext)
.setTag(TAG_1)
.log();
CustomEvent.logger(sContext)
.setTag(TAG_2)
.log();
EventLogs<CustomEvent> eventLogs = CustomEvent.queryPackage(sContext.getPackageName())
.whereTag().isEqualTo(TAG_2);
assertThat(eventLogs.poll().tag()).isEqualTo(TAG_2);
}
@Test
public void whereData_works() {
CustomEvent.logger(sContext)
.setData(DATA_1)
.log();
EventLogs<CustomEvent> eventLogs = CustomEvent.queryPackage(sContext.getPackageName())
.whereData().isEqualTo(DATA_1);
assertThat(eventLogs.poll().data()).isEqualTo(DATA_1);
}
@Test
public void whereData_skipsNonMatching() {
CustomEvent.logger(sContext)
.setData(DATA_1)
.log();
CustomEvent.logger(sContext)
.setData(DATA_2)
.log();
EventLogs<CustomEvent> eventLogs = CustomEvent.queryPackage(sContext.getPackageName())
.whereData().isEqualTo(DATA_2);
assertThat(eventLogs.poll().data()).isEqualTo(DATA_2);
}
}