From 05d01294782115b652a0ef28e5cb35ab3a7ad642 Mon Sep 17 00:00:00 2001 From: Yvonne Wong Date: Thu, 27 Aug 2015 12:19:54 -0700 Subject: Add way to migrate CM specific settings to CMSettingsProvider issue-id: CYNGNOS-829 Change-Id: I08743ebf9ffd3846ae501ed41e396b1556dc41cf --- .../tests/providers/CMSettingsTest.java | 70 ++++++++++++++++++++-- 1 file changed, 64 insertions(+), 6 deletions(-) (limited to 'tests/src') diff --git a/tests/src/org/cyanogenmod/tests/providers/CMSettingsTest.java b/tests/src/org/cyanogenmod/tests/providers/CMSettingsTest.java index d179af8..77299db 100644 --- a/tests/src/org/cyanogenmod/tests/providers/CMSettingsTest.java +++ b/tests/src/org/cyanogenmod/tests/providers/CMSettingsTest.java @@ -17,6 +17,10 @@ package org.cyanogenmod.tests.providers; import android.content.ContentResolver; +import android.database.ContentObserver; +import android.net.Uri; +import android.os.Handler; +import android.os.UserHandle; import android.provider.Settings; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.MediumTest; @@ -24,10 +28,20 @@ import cyanogenmod.providers.CMSettings; public class CMSettingsTest extends AndroidTestCase{ private ContentResolver mContentResolver; + private CMSettingsTestObserver mTestObserver; + + private static boolean sIsOnChangedCalled = false; + private static Uri sExpectedUriChange = null; @Override public void setUp() { mContentResolver = getContext().getContentResolver(); + mTestObserver = new CMSettingsTestObserver(null); + } + + @Override + public void tearDown() { + mContentResolver.unregisterContentObserver(mTestObserver); } @MediumTest @@ -43,6 +57,12 @@ public class CMSettingsTest extends AndroidTestCase{ String actualValue = CMSettings.System.getString(mContentResolver, key); assertEquals(expectedValue, actualValue); + // setup observer + sIsOnChangedCalled = false; + sExpectedUriChange = CMSettings.System.getUriFor(key); + mContentResolver.registerContentObserver(sExpectedUriChange, false, mTestObserver, + UserHandle.USER_ALL); + // replace final String expectedReplaceValue = "systemTestValue2"; isPutSuccessful = CMSettings.System.putString(mContentResolver, key, expectedReplaceValue); @@ -53,9 +73,13 @@ public class CMSettingsTest extends AndroidTestCase{ assertEquals(expectedReplaceValue, actualValue); // delete to clean up - int rowsAffected = mContentResolver.delete(CMSettings.System.CONTENT_URI, Settings.NameValueTable.NAME + " = ?", - new String[]{ key }); + int rowsAffected = mContentResolver.delete(CMSettings.System.CONTENT_URI, + Settings.NameValueTable.NAME + " = ?", new String[]{ key }); assertEquals(1, rowsAffected); + + if (!sIsOnChangedCalled) { + fail("On change was never called or was called with the wrong uri"); + } } @MediumTest @@ -71,6 +95,12 @@ public class CMSettingsTest extends AndroidTestCase{ String actualValue = CMSettings.Secure.getString(mContentResolver, key); assertEquals(expectedValue, actualValue); + // setup observer + sIsOnChangedCalled = false; + sExpectedUriChange = CMSettings.Secure.getUriFor(key); + mContentResolver.registerContentObserver(sExpectedUriChange, false, mTestObserver, + UserHandle.USER_ALL); + // replace final String expectedReplaceValue = "secureTestValue2"; isPutSuccessful = CMSettings.Secure.putString(mContentResolver, key, expectedReplaceValue); @@ -81,9 +111,13 @@ public class CMSettingsTest extends AndroidTestCase{ assertEquals(expectedReplaceValue, actualValue); // delete to clean up - int rowsAffected = mContentResolver.delete(CMSettings.Secure.CONTENT_URI, Settings.NameValueTable.NAME + " = ?", - new String[]{ key }); + int rowsAffected = mContentResolver.delete(CMSettings.Secure.CONTENT_URI, + Settings.NameValueTable.NAME + " = ?", new String[]{ key }); assertEquals(1, rowsAffected); + + if (!sIsOnChangedCalled) { + fail("On change was never called or was called with the wrong uri"); + } } @MediumTest @@ -99,6 +133,12 @@ public class CMSettingsTest extends AndroidTestCase{ String actualValue = CMSettings.Global.getString(mContentResolver, key); assertEquals(expectedValue, actualValue); + // setup observer + sIsOnChangedCalled = false; + sExpectedUriChange = CMSettings.Global.getUriFor(key); + mContentResolver.registerContentObserver(sExpectedUriChange, false, mTestObserver, + UserHandle.USER_OWNER); + // replace final String expectedReplaceValue = "globalTestValue2"; isPutSuccessful = CMSettings.Global.putString(mContentResolver, key, expectedReplaceValue); @@ -109,9 +149,27 @@ public class CMSettingsTest extends AndroidTestCase{ assertEquals(expectedReplaceValue, actualValue); // delete to clean up - int rowsAffected = mContentResolver.delete(CMSettings.Global.CONTENT_URI, Settings.NameValueTable.NAME + " = ?", - new String[]{ key }); + int rowsAffected = mContentResolver.delete(CMSettings.Global.CONTENT_URI, + Settings.NameValueTable.NAME + " = ?", new String[]{ key }); assertEquals(1, rowsAffected); + + if (!sIsOnChangedCalled) { + fail("On change was never called or was called with the wrong uri"); + } + } + + private class CMSettingsTestObserver extends ContentObserver { + + public CMSettingsTestObserver(Handler handler) { + super(handler); + } + + @Override + public void onChange(boolean selfChange, Uri uri) { + if (sExpectedUriChange.equals(uri)) { + sIsOnChangedCalled = true; + } + } } // TODO Add tests for other users -- cgit v1.1