Android:如何在单元测试期间重置/清除应用程序首选项?

发布于 2024-09-26 09:26:01 字数 1409 浏览 0 评论 0原文

我想从一致的测试环境开始,因此我需要重置/清除我的首选项。这是迄今为止我进行的测试设置。它没有报告任何错误,并且我的测试通过了,但首选项没有被清除。

我正在测试“MainMenu”活动,但我暂时切换到 OptionScreen 活动(它扩展了 Android 的 PreferenceActivity 类)。我确实看到测试在运行期间正确打开了 OptionScreen。

 public class MyTest extends ActivityInstrumentationTestCase2<MainMenu> {

...

    @Override
    protected void setUp() throws Exception {
    super.setUp();

    Instrumentation instrumentation = getInstrumentation();
    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(OptionScreen.class.getName(), null, false);

    StartNewActivity(); // See next paragraph for what this does, probably mostly irrelevant.
    activity = getActivity();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
    settings.edit().clear();
    settings.edit().commit(); // I am pretty sure this is not necessary but not harmful either.

StartNewActivity 代码:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(instrumentation.getTargetContext(),
            OptionScreen.class.getName());
    instrumentation.startActivitySync(intent);
    Activity currentActivity = getInstrumentation()
            .waitForMonitorWithTimeout(monitor, 5);
    assertTrue(currentActivity != null);

谢谢!

I want to start with a consistent test environment so I need to reset/clear my preferences. Here's the SetUp for test I have so far. It's not reporting any errors, and my tests pass, but the preferences are not being cleared.

I'm testing the "MainMenu" activity, but I temporarily switch to the OptionScreen activity (which extends Android's PreferenceActivity class.) I do see the test correctly open the OptionScreen during the run.

 public class MyTest extends ActivityInstrumentationTestCase2<MainMenu> {

...

    @Override
    protected void setUp() throws Exception {
    super.setUp();

    Instrumentation instrumentation = getInstrumentation();
    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(OptionScreen.class.getName(), null, false);

    StartNewActivity(); // See next paragraph for what this does, probably mostly irrelevant.
    activity = getActivity();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
    settings.edit().clear();
    settings.edit().commit(); // I am pretty sure this is not necessary but not harmful either.

StartNewActivity Code:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(instrumentation.getTargetContext(),
            OptionScreen.class.getName());
    instrumentation.startActivitySync(intent);
    Activity currentActivity = getInstrumentation()
            .waitForMonitorWithTimeout(monitor, 5);
    assertTrue(currentActivity != null);

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-10-03 09:26:01

问题是您没有从 edit() 调用中保存原始编辑器,并且您获取了编辑器的新实例并在其上调用 commit() ,而没有对该编辑器进行任何更改。试试这个:

Editor editor = settings.edit();
editor.clear();
editor.commit();

The problem is that you aren't saving the original editor from the edit() call, and you fetch a new instance of the editor and call commit() on that without having made any changes to that one. Try this:

Editor editor = settings.edit();
editor.clear();
editor.commit();
独孤求败 2024-10-03 09:26:01

答案就在这里,
android 单元测试:在测试活动之前清除首选项

致电,

this.getInstrumentation().getTargetContext()

Answer is here,
android unit test: clearing prefs before testing activity

Call,

this.getInstrumentation().getTargetContext()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文