udacity android 实践笔记: lesson 4 part b


作者:干货店打杂的 /titer1 /Archimedes

出处:https://code.csdn.net/titer1

联系:1307316一九六八(短信最佳)

声明:本文採用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 。转载请注明作者及出处。

tips:https://code.csdn.net/titer1/pat_aha/blob/master/Markdown/android/


摘要

  • 全然展示content provider加入的过程
  • 动态展示android单元測试的过程

    动图出没喔!o(^▽^)o。动图在最后

overviw 图

还是老图,(create / insert /query)

以下 我们開始 content provider的学习

复习四大 组件

content provider
service
broadcast provider
actvity

本章 overview

4.07 init code for content provider



- 測试打桩函数就占了大半

- 然后传统的函数里面改动了 weatherContrace

- 添加了 weatherProvider

- 改动了 fetchweatherTask函数。代码的来源是

从 forececastFragment代码拆解出来的。

- 除了改动weatherContrace,其它文件都是 新添加的

关键词 uri

weatherContract里面添加的最多就是生成uri的函数



    /* Inner class that defines the table contents of the location table */
public static final class LocationEntry implements BaseColumns {
...
public static Uri buildLocationUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
} ===
    /* Inner class that defines the table contents of the weather table */
public static final class WeatherEntry implements BaseColumns { ...
public static Uri buildWeatherLocation(String locationSetting) {
return null;
} public static Uri buildWeatherLocationWithStartDate(
String locationSetting, long startDate) {
long normalizedDate = normalizeDate(startDate);
return CONTENT_URI.buildUpon().appendPath(locationSetting)
.appendQueryParameter(COLUMN_DATE, Long.toString(normalizedDate)).build();
} public static Uri buildWeatherLocationWithDate(String locationSetting, long date) {
return CONTENT_URI.buildUpon().appendPath(locationSetting)
.appendPath(Long.toString(normalizeDate(date))).build();
} public static String getLocationSettingFromUri(Uri uri) {
return uri.getPathSegments().get(1);
} public static long getDateFromUri(Uri uri) {
return Long.parseLong(uri.getPathSegments().get(2));
} public static long getStartDateFromUri(Uri uri) {
String dateString = uri.getQueryParameter(COLUMN_DATE);
if (null != dateString && dateString.length() > 0)
return Long.parseLong(dateString);
else
return 0;
}
}
}

4.08 uri location with weather

仅2处更新

1/2 weatherContract

        /*
Student: This is the buildWeatherLocation function you filled in.
*/
public static Uri buildWeatherLocation(String locationSetting) {
return CONTENT_URI.buildUpon().appendPath(locationSetting).build();//上一版本号这里返回空值
}

##2/2 test

只打开 这里的凝视

“` java

/*

Students: This is NOT a complete test for the WeatherContract — just for the functions

that we expect you to write.

*/

public class TestWeatherContract extends AndroidTestCase {

// intentionally includes a slash to make sure Uri is getting quoted correctly
private static final String TEST_WEATHER_LOCATION = "/North Pole";
private static final long TEST_WEATHER_DATE = 1419033600L; // December 20th, 2014 /*
Students: Uncomment this out to test your weather location function.
*/
public void testBuildWeatherLocation() {
Uri locationUri = WeatherContract.WeatherEntry.buildWeatherLocation(TEST_WEATHER_LOCATION);
assertNotNull("Error: Null Uri returned. You must fill-in buildWeatherLocation in " +
"WeatherContract.",
locationUri);
assertEquals("Error: Weather location not properly appended to the end of the Uri",
TEST_WEATHER_LOCATION, locationUri.getLastPathSegment());
assertEquals("Error: Weather location Uri doesn't match our expected result",
locationUri.toString(),
"content://com.example.android.sunshine.app/weather/%2FNorth%20Pole");
}

}

“`

4.09 write uri matcher

1/2 weatherProvider

这是框架里面的代码,更新一处

    /*
Students: Here is where you need to create the UriMatcher. This UriMatcher will
match each URI to the WEATHER, WEATHER_WITH_LOCATION, WEATHER_WITH_LOCATION_AND_DATE,
and LOCATION integer constants defined above. You can test this by uncommenting the
testUriMatcher test within TestUriMatcher.
*/
static UriMatcher buildUriMatcher() {
// I know what you're thinking. Why create a UriMatcher when you can use regular
// expressions instead? Because you're not crazy, that's why. // All paths added to the UriMatcher have a corresponding code to return when a match is
// found. The code passed into the constructor represents the code to return for the root
// URI. It's common to use NO_MATCH as the code for this case.
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY; // For each type of URI you want to add, create a corresponding code.
matcher.addURI(authority, WeatherContract.PATH_WEATHER, WEATHER);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*", WEATHER_WITH_LOCATION);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*/#", WEATHER_WITH_LOCATION_AND_DATE); matcher.addURI(authority, WeatherContract.PATH_LOCATION, LOCATION);
return matcher;
}

2/2 test

依旧是已经写好的測试代码框架,这里只打开凝视

public class TestUriMatcher extends AndroidTestCase {
...
/*
Students: This function tests that your UriMatcher returns the correct integer value
for each of the Uri types that our ContentProvider can handle. Uncomment this when you are
ready to test your UriMatcher.
*/
public void testUriMatcher() {
UriMatcher testMatcher = WeatherProvider.buildUriMatcher(); assertEquals("Error: The WEATHER URI was matched incorrectly.",
testMatcher.match(TEST_WEATHER_DIR), WeatherProvider.WEATHER);
assertEquals("Error: The WEATHER WITH LOCATION URI was matched incorrectly.",
testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR), WeatherProvider.WEATHER_WITH_LOCATION);
assertEquals("Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.",
testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR), WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE);
assertEquals("Error: The LOCATION URI was matched incorrectly.",
testMatcher.match(TEST_LOCATION_DIR), WeatherProvider.LOCATION);
}

···

4.10 register content provider

1/2

这里只使能(打开凝视) testProviderRegistry


public class TestProvider extends AndroidTestCase { ...
/*
This test checks to make sure that the content provider is registered correctly.
Students: Uncomment this test to make sure you've correctly registered the WeatherProvider.
*/
public void testProviderRegistry() {
PackageManager pm = mContext.getPackageManager(); // We define the component name based on the package name from the context and the
// WeatherProvider class.
ComponentName componentName = new ComponentName(mContext.getPackageName(),
WeatherProvider.class.getName());
try {
// Fetch the provider info using the component name from the PackageManager
// This throws an exception if the provider isn't registered.
ProviderInfo providerInfo = pm.getProviderInfo(componentName, 0); // Make sure that the registered authority matches the authority from the Contract.
assertEquals("Error: WeatherProvider registered with authority: " + providerInfo.authority +
" instead of authority: " + WeatherContract.CONTENT_AUTHORITY,
providerInfo.authority, WeatherContract.CONTENT_AUTHORITY);
} catch (PackageManager.NameNotFoundException e) {
// I guess the provider isn't registered correctly.
assertTrue("Error: WeatherProvider not registered at " + mContext.getPackageName(),
false);
}
}
}

androidManifest update

        <provider
android:authorities="com.example.android.sunshine.app"
android:name=".data.WeatherProvider" />

小憩

添加provider布凑

- 预计要从 1代版本号代码 和 2代代码 进行比較

聪明的编译器

智能提示

android 在debug模式下,有非常有趣的话题在里面,比方变量值的提示

还有搜索字符时,主动过滤凝视里面字符

test ok 单元測试的方法 && testPractise



而且 我没有选右键触发,而是直接 run ( shift f10)

(4a-28 )里面单元測试的方法。全然能够复现



- 展现执行的配置

- 展示出错情形怎样定位错误

- 展示正常情况

使用的是 sunshine version2 的4.02的代码。code: testPractise
todo 实践后思考
空白的android testproject是怎样的?
4.02章节里面。命名唯独有效的3个testcase,怎么显示有6个testcase ?

testDb之testCreateDb

4.04 (git version) delete the testPractise,

实际执行记录:
我在全部的 assertTrue,assertFalse,assertEqual
位置都下了断点,可是唯独testDb中的testCreateDb 被触发。
数字上。run窗体显示的“6 of 6”ok,
但6个中的4个被触发,
我怀疑这个6是不是显示过时

testdb之testLocationTable

測试全然通过.

  • 该函数完整的调用了db的操作函数

    • insert
    • query

而且有趣的是调用了TestUtilities.validateCurrentRecord。

(这里面还有2个assert,这里面的断言最终被触发了)

这是我观察中

testUtilities类中函数的第一次调用。

上一轮。我在这个类的static函数里面设置断点。

都没有被调用。

还有这个类testUtilities提供了数据库插入的样本数据

todo
实践后思考:
既然单元測试都是利用AndroidTestCase
那么为什么每次都是testDb被调用,
而其它AndroidTestCase的继承类testUtilities 没有被直接调用.

testWeatherTable 和重构 testLocationTable

4.06 lesson,

而且有趣的是前者内部调用调用了

locationTable的内容

居然全部的測试通过了

todo 内容深入分析
// Fifth Step: Validate the location Query
TestUtilities.validateCurrentRecord("testInsertReadDb weatherEntry failed to validate",
weatherCursor, weatherValues);

小憩

github 前6个小节,就是认识单元測试

单元測试的魔力非常大啊。这里

兴许

from 4.06 -4.10 (github)

測试函数有:

- test weatherContract

- test uri matcher

- test Provider

- test providerRegistery

构造一个content provider,每个模块都不能马虎

晚安 小憩

至少方向是光明的

下一天 复习

解释 前面 为什么 不是 全部的uri都被命中

再看 weather provider 和 testProvider的演示

作者是逐一功能的enable,逐一測试。逐渐深入

差点儿每种testProvider的函数(query/getType/insert/updat)

都含有uri matcher的筛选

新近发现 能够 查看 每个測试的内容:

video 4b-24 是个阶段总结

bulkinsert 事实上利用了事务,唯独endtrancsation 才执行操作

小节

小节:整章节 就是 uri 使用

- define uri

- update uri(feflect to number)

- uri matcher (I think ,here addmatch)

- implement (I think, sUriMatcher.match(uri);)

说到。开源非常多 封装了 content provider ?

toco

怎样理解 notification oserver ..here

整合

至此。我们的content provider 还没有和 database 联合起来。

操作不是 test*.java,也不主要是 weatherprovider里面

我们将精力集中到 FetchweatherTask

- addlocation

- getWeatherDataFromJson insert + query part

(网络收报后的处理例程)

看到凡是使用了getContentResolver的地方,都是利用了contentprovider的地方,

最后

为了ui theread 更加流畅,引出Loader

至此,我们的video 来到4b的借书,代码

代码范围是:

todo 附code

小节

android 你好

android 再见

呵呵。本博客android暂停更新。假设有兴趣能够短信联系

udacity android 实践笔记: lesson 4 part b的更多相关文章

  1. udacity android 实践笔记: lesson 4 part a

    udacity android 实践笔记: lesson 4 part a 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  2. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  3. udacity android 学习笔记: lesson 4 part a

    udacity android 学习笔记: lesson 4 part a 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  4. Udacity调试课笔记之断言异常

    Udacity调试课笔记之断言异常 这一单元的内容不是很多,如Zeller教授所说,就是如何写.检查断言,并如何使用工具实现自动推导出断言的条件. 现在,多数的编程语言,尤其是高级编程语言都会有内置的 ...

  5. android 应用笔记

    android 应用笔记 android 应用笔记 小书匠 Android 综合教程 Android常用技巧 安卓系统架构 安卓源码开发 安卓驱动 Linux内核 安卓应用开发 Java 教程 tic ...

  6. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  7. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  8. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  9. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

随机推荐

  1. C#和Java交互相关研究

    之前总觉得C#和Java可以交互应用,但是由于时间以及其他方面的原因,一直没有调研.今天抽空搜了一下,终于折腾出来了. 以下是我自己就C#和Java整合的一些提问和分析,如果有不对的地方,请路过的各位 ...

  2. mysql数据库维护(备份和还原)和性能提高

    为了有效防止数据丢失,并将损失降到最低,应对数据库服务器做维护.数据库维护,包括数据备份,还原,导出和导入操作. 1. MySQL数据库备份 所谓数据库维护,主要包含备份数据,还原数据和数据库迁移,对 ...

  3. vi命令整理

    vi命令整理 u 撤销上一次操作 ctrl+r 恢复上一次操作 : 跳转至第1行 :$ 跳转至最后一行 ctrl+f 向文章末尾翻页 ctrl+b 向文章开始翻页 yy 复制一行 p 粘贴刚刚复制第一 ...

  4. ckeditor 实现图片上传以及预览(亲测有效)

    引用ckeditor <script type="text/javascript" src="static/ckeditor/ckeditor.js"&g ...

  5. 2014年七月最佳jQuery插件荟萃

    本月的jQuery插件荟萃我们将介绍几款非常不错的jQuery插件,涵盖了表单,幻灯,页面设计等等方面,相信大家肯定会喜欢! Select or Die 一款帮助开发者美化并且强化选择框的jQuery ...

  6. java线程同步问题——由腾讯笔试题引发的风波

    刚刚wm问我了一道线程的问题,因为自己一直是coder界里的渣渣.所以就须要恶补一下. 2016年4月2号题目例如以下. import java.util.logging.Handler; /** * ...

  7. 环境变量之执行文件路径的变量PATH

    当我们执行一个命令时,系统会依据PATH的设置去PATH定义的每个目录下查寻该命令的可执行文件,如果在PATH定义的目录中含有多个文件名为我们要执行的命令的可执行文件时,那么先查询到的同名命令先被执行 ...

  8. POI生成EXCEL文件(字体、样式、单元格合并、计算公式)

    创建一个封装类: package com.jason.excel; import java.io.FileNotFoundException; import java.io.FileOutputStr ...

  9. JUnit 3.8 演示递归删除文件目录的 测试类程序 .

    用递归方式来实现删除硬盘的文件或目录(空文件夹) 首先要找到递归的入口及出口,这点很重要,成败在此,呵呵! 代码实现: import java.io.File ; class RecursionDel ...

  10. 〖Android〗把CM(Android)源代码导入eclipse的正确方法(2013-7-3)

    1. 首先应当使CM代码成功编译过一次: cd /path/to/cm . build/envsetup lunch full-eng mka 2. 配置eclipse开发的基本环境 cd /path ...