Move SharedPreferenceLogger to instrumentation package.

Bug: 30914916
Test: SettingsUnitTests and RunSettingsRoboTests

- Added interface to abstract logger
- Added test for SharePrefLogger
- Moved Existing tests for instrumentation to robotests.

Change-Id: I2b431ea4b0fd09d0f11389d8b9181448f08a52c5
This commit is contained in:
Fan Zhang
2016-08-18 16:04:19 -07:00
parent cbb2f5f498
commit 5f79ae9677
9 changed files with 184 additions and 26 deletions

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2016 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.settings.core.instrumentation;
import com.android.settings.TestConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class MetricsFactoryTest {
@Test
public void factoryShouldReuseCachedInstance() {
MetricsFactory factory1 = MetricsFactory.get();
MetricsFactory factory2 = MetricsFactory.get();
assertTrue(factory1 == factory2);
}
@Test
public void factoryShouldCacheLogger() {
MetricsFactory factory = MetricsFactory.get();
LogWriter logger1 = factory.getLogger();
LogWriter logger2 = factory.getLogger();
assertTrue(logger1 == logger2);
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2016 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.settings.core.instrumentation;
import android.content.Context;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SharedPreferenceLoggerTest {
private static final String TEST_TAG = "tag";
private static final String TEST_KEY = "key";
@Mock
private LogWriter mLogWriter;
private ShadowApplication mApplication;
private SharedPreferencesLogger mSharedPrefLogger;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
mApplication = ShadowApplication.getInstance();
MetricsFactory.get().setLogger(mLogWriter);
mSharedPrefLogger = new SharedPreferencesLogger(
mApplication.getApplicationContext(), TEST_TAG);
}
@Test
public void putInt_shouldLogCount() {
mSharedPrefLogger.edit().putInt(TEST_KEY, 1);
verify(mLogWriter).count(any(Context.class), anyString(), anyInt());
}
@Test
public void putBoolean_shouldLogCount() {
mSharedPrefLogger.edit().putBoolean(TEST_KEY, true);
verify(mLogWriter).count(any(Context.class), anyString(), anyInt());
}
@Test
public void putLong_shouldLogCount() {
mSharedPrefLogger.edit().putLong(TEST_KEY, 1);
verify(mLogWriter).count(any(Context.class), anyString(), anyInt());
}
@Test
public void putFloat_shouldLogCount() {
mSharedPrefLogger.edit().putInt(TEST_KEY, 1);
verify(mLogWriter).count(any(Context.class), anyString(), anyInt());
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2016 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.settings.core.instrumentation;
import android.content.Context;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class VisibilityLoggerMixinTest {
@Mock
private EventLogWriter mLogger;
private VisibilityLoggerMixin mMixin;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
mMixin = new VisibilityLoggerMixin(new TestInstrumentable(), mLogger);
}
@Test
public void shouldLogVisibleOnResume() {
mMixin.onResume(null);
verify(mLogger, times(1))
.visible(any(Context.class), eq(TestInstrumentable.TEST_METRIC));
}
@Test
public void shouldLogHideOnPause() {
mMixin.onPause(null);
verify(mLogger, times(1))
.hidden(any(Context.class), eq(TestInstrumentable.TEST_METRIC));
}
private final class TestInstrumentable implements Instrumentable {
public static final int TEST_METRIC = 12345;
@Override
public int getMetricsCategory() {
return TEST_METRIC;
}
}
}